2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.Cancellable;
14 import akka.actor.Props;
15 import akka.japi.Creator;
16 import akka.persistence.RecoveryFailure;
17 import akka.serialization.Serialization;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.io.IOException;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.controller.cluster.DataPersistenceProvider;
34 import org.opendaylight.controller.cluster.common.actor.CommonConfig;
35 import org.opendaylight.controller.cluster.common.actor.MeteringBehavior;
36 import org.opendaylight.controller.cluster.datastore.ShardCommitCoordinator.CohortEntry;
37 import org.opendaylight.controller.cluster.datastore.compat.BackwardsCompatibleThreePhaseCommitCohort;
38 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
39 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
40 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
41 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardMBeanFactory;
42 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
43 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
44 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
45 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
46 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
47 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
48 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
49 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
50 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
51 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
52 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
53 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
54 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
55 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
56 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
57 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
58 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
59 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
60 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
61 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
62 import org.opendaylight.controller.cluster.datastore.modification.Modification;
63 import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
64 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
65 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
66 import org.opendaylight.controller.cluster.datastore.utils.MessageTracker;
67 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
68 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
69 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
70 import org.opendaylight.controller.cluster.raft.RaftActor;
71 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
72 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
73 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
74 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
75 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
76 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
77 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
78 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
79 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
80 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
81 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
82 import org.opendaylight.yangtools.concepts.ListenerRegistration;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
84 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
85 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
86 import scala.concurrent.duration.Duration;
87 import scala.concurrent.duration.FiniteDuration;
90 * A Shard represents a portion of the logical data tree <br/>
92 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
95 public class Shard extends RaftActor {
97 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
99 private static final Object TX_COMMIT_TIMEOUT_CHECK_MESSAGE = "txCommitTimeoutCheck";
102 static final String DEFAULT_NAME = "default";
104 // The state of this Shard
105 private final InMemoryDOMDataStore store;
107 /// The name of this shard
108 private final String name;
110 private final ShardStats shardMBean;
112 private final List<ActorSelection> dataChangeListeners = Lists.newArrayList();
114 private final List<DelayedListenerRegistration> delayedListenerRegistrations =
115 Lists.newArrayList();
117 private DatastoreContext datastoreContext;
119 private DataPersistenceProvider dataPersistenceProvider;
121 private SchemaContext schemaContext;
123 private int createSnapshotTransactionCounter;
125 private final ShardCommitCoordinator commitCoordinator;
127 private long transactionCommitTimeout;
129 private Cancellable txCommitTimeoutCheckSchedule;
131 private final Optional<ActorRef> roleChangeNotifier;
133 private final MessageTracker appendEntriesReplyTracker;
135 private final ReadyTransactionReply READY_TRANSACTION_REPLY = new ReadyTransactionReply(
136 Serialization.serializedActorPath(getSelf()));
140 * Coordinates persistence recovery on startup.
142 private ShardRecoveryCoordinator recoveryCoordinator;
143 private List<Object> currentLogRecoveryBatch;
145 private final DOMTransactionFactory transactionFactory;
147 private final String txnDispatcherPath;
149 protected Shard(final ShardIdentifier name, final Map<ShardIdentifier, String> peerAddresses,
150 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
151 super(name.toString(), mapPeerAddresses(peerAddresses),
152 Optional.of(datastoreContext.getShardRaftConfig()));
154 this.name = name.toString();
155 this.datastoreContext = datastoreContext;
156 this.schemaContext = schemaContext;
157 this.dataPersistenceProvider = (datastoreContext.isPersistent())
158 ? new PersistentDataProvider() : new NonPersistentRaftDataProvider();
159 this.txnDispatcherPath = new Dispatchers(context().system().dispatchers())
160 .getDispatcherPath(Dispatchers.DispatcherType.Transaction);
163 LOG.info("Shard created : {}, persistent : {}", name, datastoreContext.isPersistent());
165 store = InMemoryDOMDataStoreFactory.create(name.toString(), null,
166 datastoreContext.getDataStoreProperties());
168 if(schemaContext != null) {
169 store.onGlobalContextUpdated(schemaContext);
172 shardMBean = ShardMBeanFactory.getShardStatsMBean(name.toString(),
173 datastoreContext.getDataStoreMXBeanType());
174 shardMBean.setNotificationManager(store.getDataChangeListenerNotificationManager());
175 shardMBean.setShardActor(getSelf());
177 if (isMetricsCaptureEnabled()) {
178 getContext().become(new MeteringBehavior(this));
181 transactionFactory = new DOMTransactionFactory(store, shardMBean, LOG, this.name);
183 commitCoordinator = new ShardCommitCoordinator(transactionFactory,
184 TimeUnit.SECONDS.convert(5, TimeUnit.MINUTES),
185 datastoreContext.getShardTransactionCommitQueueCapacity(), self(), LOG, this.name);
187 setTransactionCommitTimeout();
189 // create a notifier actor for each cluster member
190 roleChangeNotifier = createRoleChangeNotifier(name.toString());
192 appendEntriesReplyTracker = new MessageTracker(AppendEntriesReply.class,
193 getRaftActorContext().getConfigParams().getIsolatedCheckIntervalInMillis());
196 private void setTransactionCommitTimeout() {
197 transactionCommitTimeout = TimeUnit.MILLISECONDS.convert(
198 datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS);
201 private static Map<String, String> mapPeerAddresses(
202 final Map<ShardIdentifier, String> peerAddresses) {
203 Map<String, String> map = new HashMap<>();
205 for (Map.Entry<ShardIdentifier, String> entry : peerAddresses
207 map.put(entry.getKey().toString(), entry.getValue());
213 public static Props props(final ShardIdentifier name,
214 final Map<ShardIdentifier, String> peerAddresses,
215 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
216 Preconditions.checkNotNull(name, "name should not be null");
217 Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
218 Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null");
219 Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
221 return Props.create(new ShardCreator(name, peerAddresses, datastoreContext, schemaContext));
224 private Optional<ActorRef> createRoleChangeNotifier(String shardId) {
225 ActorRef shardRoleChangeNotifier = this.getContext().actorOf(
226 RoleChangeNotifier.getProps(shardId), shardId + "-notifier");
227 return Optional.of(shardRoleChangeNotifier);
231 public void postStop() {
232 LOG.info("Stopping Shard {}", persistenceId());
236 if(txCommitTimeoutCheckSchedule != null) {
237 txCommitTimeoutCheckSchedule.cancel();
240 shardMBean.unregisterMBean();
244 public void onReceiveRecover(final Object message) throws Exception {
245 if(LOG.isDebugEnabled()) {
246 LOG.debug("{}: onReceiveRecover: Received message {} from {}", persistenceId(),
247 message.getClass().toString(), getSender());
250 if (message instanceof RecoveryFailure){
251 LOG.error("{}: Recovery failed because of this cause",
252 persistenceId(), ((RecoveryFailure) message).cause());
254 // Even though recovery failed, we still need to finish our recovery, eg send the
255 // ActorInitialized message and start the txCommitTimeoutCheckSchedule.
256 onRecoveryComplete();
258 super.onReceiveRecover(message);
259 if(LOG.isTraceEnabled()) {
260 appendEntriesReplyTracker.begin();
266 public void onReceiveCommand(final Object message) throws Exception {
268 MessageTracker.Context context = appendEntriesReplyTracker.received(message);
270 if(context.error().isPresent()){
271 LOG.trace("{} : AppendEntriesReply failed to arrive at the expected interval {}", persistenceId(),
276 if (CreateTransaction.SERIALIZABLE_CLASS.isInstance(message)) {
277 handleCreateTransaction(message);
278 } else if (BatchedModifications.class.isInstance(message)) {
279 handleBatchedModifications((BatchedModifications)message);
280 } else if (message instanceof ForwardedReadyTransaction) {
281 handleForwardedReadyTransaction((ForwardedReadyTransaction) message);
282 } else if (CanCommitTransaction.SERIALIZABLE_CLASS.isInstance(message)) {
283 handleCanCommitTransaction(CanCommitTransaction.fromSerializable(message));
284 } else if (CommitTransaction.SERIALIZABLE_CLASS.isInstance(message)) {
285 handleCommitTransaction(CommitTransaction.fromSerializable(message));
286 } else if (AbortTransaction.SERIALIZABLE_CLASS.isInstance(message)) {
287 handleAbortTransaction(AbortTransaction.fromSerializable(message));
288 } else if (CloseTransactionChain.SERIALIZABLE_CLASS.isInstance(message)) {
289 closeTransactionChain(CloseTransactionChain.fromSerializable(message));
290 } else if (message instanceof RegisterChangeListener) {
291 registerChangeListener((RegisterChangeListener) message);
292 } else if (message instanceof UpdateSchemaContext) {
293 updateSchemaContext((UpdateSchemaContext) message);
294 } else if (message instanceof PeerAddressResolved) {
295 PeerAddressResolved resolved = (PeerAddressResolved) message;
296 setPeerAddress(resolved.getPeerId().toString(),
297 resolved.getPeerAddress());
298 } else if (message.equals(TX_COMMIT_TIMEOUT_CHECK_MESSAGE)) {
299 handleTransactionCommitTimeoutCheck();
300 } else if(message instanceof DatastoreContext) {
301 onDatastoreContext((DatastoreContext)message);
302 } else if(message instanceof RegisterRoleChangeListener){
303 roleChangeNotifier.get().forward(message, context());
304 } else if (message instanceof FollowerInitialSyncUpStatus){
305 shardMBean.setFollowerInitialSyncStatus(((FollowerInitialSyncUpStatus) message).isInitialSyncDone());
306 context().parent().tell(message, self());
308 super.onReceiveCommand(message);
316 protected Optional<ActorRef> getRoleChangeNotifier() {
317 return roleChangeNotifier;
320 private void onDatastoreContext(DatastoreContext context) {
321 datastoreContext = context;
323 commitCoordinator.setQueueCapacity(datastoreContext.getShardTransactionCommitQueueCapacity());
325 setTransactionCommitTimeout();
327 if(datastoreContext.isPersistent() &&
328 dataPersistenceProvider instanceof NonPersistentRaftDataProvider) {
329 dataPersistenceProvider = new PersistentDataProvider();
330 } else if(!datastoreContext.isPersistent() &&
331 dataPersistenceProvider instanceof PersistentDataProvider) {
332 dataPersistenceProvider = new NonPersistentRaftDataProvider();
335 updateConfigParams(datastoreContext.getShardRaftConfig());
338 private void handleTransactionCommitTimeoutCheck() {
339 CohortEntry cohortEntry = commitCoordinator.getCurrentCohortEntry();
340 if(cohortEntry != null) {
341 long elapsed = System.currentTimeMillis() - cohortEntry.getLastAccessTime();
342 if(elapsed > transactionCommitTimeout) {
343 LOG.warn("{}: Current transaction {} has timed out after {} ms - aborting",
344 persistenceId(), cohortEntry.getTransactionID(), transactionCommitTimeout);
346 doAbortTransaction(cohortEntry.getTransactionID(), null);
351 private void handleCommitTransaction(final CommitTransaction commit) {
352 final String transactionID = commit.getTransactionID();
354 LOG.debug("{}: Committing transaction {}", persistenceId(), transactionID);
356 // Get the current in-progress cohort entry in the commitCoordinator if it corresponds to
358 final CohortEntry cohortEntry = commitCoordinator.getCohortEntryIfCurrent(transactionID);
359 if(cohortEntry == null) {
360 // We're not the current Tx - the Tx was likely expired b/c it took too long in
361 // between the canCommit and commit messages.
362 IllegalStateException ex = new IllegalStateException(
363 String.format("%s: Cannot commit transaction %s - it is not the current transaction",
364 persistenceId(), transactionID));
365 LOG.error(ex.getMessage());
366 shardMBean.incrementFailedTransactionsCount();
367 getSender().tell(new akka.actor.Status.Failure(ex), getSelf());
371 // We perform the preCommit phase here atomically with the commit phase. This is an
372 // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
373 // coordination of preCommit across shards in case of failure but preCommit should not
374 // normally fail since we ensure only one concurrent 3-phase commit.
377 // We block on the future here so we don't have to worry about possibly accessing our
378 // state on a different thread outside of our dispatcher. Also, the data store
379 // currently uses a same thread executor anyway.
380 cohortEntry.getCohort().preCommit().get();
382 // If we do not have any followers and we are not using persistence
383 // or if cohortEntry has no modifications
384 // we can apply modification to the state immediately
385 if((!hasFollowers() && !persistence().isRecoveryApplicable()) || (!cohortEntry.hasModifications())){
386 applyModificationToState(getSender(), transactionID, cohortEntry.getModification());
388 Shard.this.persistData(getSender(), transactionID,
389 new ModificationPayload(cohortEntry.getModification()));
391 } catch (Exception e) {
392 LOG.error("{} An exception occurred while preCommitting transaction {}",
393 persistenceId(), cohortEntry.getTransactionID(), e);
394 shardMBean.incrementFailedTransactionsCount();
395 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
398 cohortEntry.updateLastAccessTime();
401 private void finishCommit(@Nonnull final ActorRef sender, final @Nonnull String transactionID) {
402 // With persistence enabled, this method is called via applyState by the leader strategy
403 // after the commit has been replicated to a majority of the followers.
405 CohortEntry cohortEntry = commitCoordinator.getCohortEntryIfCurrent(transactionID);
406 if(cohortEntry == null) {
407 // The transaction is no longer the current commit. This can happen if the transaction
408 // was aborted prior, most likely due to timeout in the front-end. We need to finish
409 // committing the transaction though since it was successfully persisted and replicated
410 // however we can't use the original cohort b/c it was already preCommitted and may
411 // conflict with the current commit or may have been aborted so we commit with a new
413 cohortEntry = commitCoordinator.getAndRemoveCohortEntry(transactionID);
414 if(cohortEntry != null) {
415 commitWithNewTransaction(cohortEntry.getModification());
416 sender.tell(CommitTransactionReply.INSTANCE.toSerializable(), getSelf());
418 // This really shouldn't happen - it likely means that persistence or replication
419 // took so long to complete such that the cohort entry was expired from the cache.
420 IllegalStateException ex = new IllegalStateException(
421 String.format("%s: Could not finish committing transaction %s - no CohortEntry found",
422 persistenceId(), transactionID));
423 LOG.error(ex.getMessage());
424 sender.tell(new akka.actor.Status.Failure(ex), getSelf());
430 LOG.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionID());
433 // We block on the future here so we don't have to worry about possibly accessing our
434 // state on a different thread outside of our dispatcher. Also, the data store
435 // currently uses a same thread executor anyway.
436 cohortEntry.getCohort().commit().get();
438 sender.tell(CommitTransactionReply.INSTANCE.toSerializable(), getSelf());
440 shardMBean.incrementCommittedTransactionCount();
441 shardMBean.setLastCommittedTransactionTime(System.currentTimeMillis());
443 } catch (Exception e) {
444 sender.tell(new akka.actor.Status.Failure(e), getSelf());
446 LOG.error("{}, An exception occurred while committing transaction {}", persistenceId(),
448 shardMBean.incrementFailedTransactionsCount();
450 commitCoordinator.currentTransactionComplete(transactionID, true);
454 private void handleCanCommitTransaction(final CanCommitTransaction canCommit) {
455 LOG.debug("{}: Can committing transaction {}", persistenceId(), canCommit.getTransactionID());
456 commitCoordinator.handleCanCommit(canCommit, getSender(), self());
459 private void handleBatchedModifications(BatchedModifications batched) {
460 // This message is sent to prepare the modificationsa transaction directly on the Shard as an
461 // optimization to avoid the extra overhead of a separate ShardTransaction actor. On the last
462 // BatchedModifications message, the caller sets the ready flag in the message indicating
463 // modifications are complete. The reply contains the cohort actor path (this actor) for the caller
464 // to initiate the 3-phase commit. This also avoids the overhead of sending an additional
465 // ReadyTransaction message.
467 // If we're not the leader then forward to the leader. This is a safety measure - we shouldn't
468 // normally get here if we're not the leader as the front-end (TransactionProxy) should determine
469 // the primary/leader shard. However with timing and caching on the front-end, there's a small
470 // window where it could have a stale leader during leadership transitions.
474 BatchedModificationsReply reply = commitCoordinator.handleTransactionModifications(batched);
475 sender().tell(reply, self());
476 } catch (Exception e) {
477 LOG.error("{}: Error handling BatchedModifications for Tx {}", persistenceId(),
478 batched.getTransactionID(), e);
479 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
482 ActorSelection leader = getLeader();
484 // TODO: what if this is not the first batch and leadership changed in between batched messages?
485 // We could check if the commitCoordinator already has a cached entry and forward all the previous
486 // batched modifications.
487 LOG.debug("{}: Forwarding BatchedModifications to leader {}", persistenceId(), leader);
488 leader.forward(batched, getContext());
490 // TODO: rather than throwing an immediate exception, we could schedule a timer to try again to make
491 // it more resilient in case we're in the process of electing a new leader.
492 getSender().tell(new akka.actor.Status.Failure(new NoShardLeaderException(String.format(
493 "Could not find the leader for shard %s. This typically happens" +
494 " when the system is coming up or recovering and a leader is being elected. Try again" +
495 " later.", persistenceId()))), getSelf());
500 private void handleForwardedReadyTransaction(ForwardedReadyTransaction ready) {
501 LOG.debug("{}: Readying transaction {}, client version {}", persistenceId(),
502 ready.getTransactionID(), ready.getTxnClientVersion());
504 // This message is forwarded by the ShardTransaction on ready. We cache the cohort in the
505 // commitCoordinator in preparation for the subsequent three phase commit initiated by
507 commitCoordinator.transactionReady(ready.getTransactionID(), ready.getCohort(),
508 (MutableCompositeModification) ready.getModification());
510 // Return our actor path as we'll handle the three phase commit, except if the Tx client
511 // version < 1 (Helium-1 version). This means the Tx was initiated by a base Helium version
512 // node. In that case, the subsequent 3-phase commit messages won't contain the
513 // transactionId so to maintain backwards compatibility, we create a separate cohort actor
514 // to provide the compatible behavior.
515 if(ready.getTxnClientVersion() < DataStoreVersions.HELIUM_1_VERSION) {
516 LOG.debug("{}: Creating BackwardsCompatibleThreePhaseCommitCohort", persistenceId());
517 ActorRef replyActorPath = getContext().actorOf(BackwardsCompatibleThreePhaseCommitCohort.props(
518 ready.getTransactionID()));
520 ReadyTransactionReply readyTransactionReply =
521 new ReadyTransactionReply(Serialization.serializedActorPath(replyActorPath));
522 getSender().tell(ready.isReturnSerialized() ? readyTransactionReply.toSerializable() :
523 readyTransactionReply, getSelf());
527 getSender().tell(ready.isReturnSerialized() ? READY_TRANSACTION_REPLY.toSerializable() :
528 READY_TRANSACTION_REPLY, getSelf());
532 private void handleAbortTransaction(final AbortTransaction abort) {
533 doAbortTransaction(abort.getTransactionID(), getSender());
536 void doAbortTransaction(final String transactionID, final ActorRef sender) {
537 final CohortEntry cohortEntry = commitCoordinator.getCohortEntryIfCurrent(transactionID);
538 if(cohortEntry != null) {
539 LOG.debug("{}: Aborting transaction {}", persistenceId(), transactionID);
541 // We don't remove the cached cohort entry here (ie pass false) in case the Tx was
542 // aborted during replication in which case we may still commit locally if replication
544 commitCoordinator.currentTransactionComplete(transactionID, false);
546 final ListenableFuture<Void> future = cohortEntry.getCohort().abort();
547 final ActorRef self = getSelf();
549 Futures.addCallback(future, new FutureCallback<Void>() {
551 public void onSuccess(final Void v) {
552 shardMBean.incrementAbortTransactionsCount();
555 sender.tell(AbortTransactionReply.INSTANCE.toSerializable(), self);
560 public void onFailure(final Throwable t) {
561 LOG.error("{}: An exception happened during abort", persistenceId(), t);
564 sender.tell(new akka.actor.Status.Failure(t), self);
571 private void handleCreateTransaction(final Object message) {
573 createTransaction(CreateTransaction.fromSerializable(message));
574 } else if (getLeader() != null) {
575 getLeader().forward(message, getContext());
577 getSender().tell(new akka.actor.Status.Failure(new NoShardLeaderException(String.format(
578 "Could not find leader for shard %s so transaction cannot be created. This typically happens" +
579 " when the system is coming up or recovering and a leader is being elected. Try again" +
580 " later.", persistenceId()))), getSelf());
584 private void closeTransactionChain(final CloseTransactionChain closeTransactionChain) {
585 transactionFactory.closeTransactionChain(closeTransactionChain.getTransactionChainId());
588 private ActorRef createTypedTransactionActor(int transactionType,
589 ShardTransactionIdentifier transactionId, String transactionChainId,
590 short clientVersion ) {
592 DOMStoreTransaction transaction = transactionFactory.newTransaction(
593 TransactionProxy.TransactionType.fromInt(transactionType), transactionId.toString(),
596 return createShardTransaction(transaction, transactionId, clientVersion);
599 private ActorRef createShardTransaction(DOMStoreTransaction transaction, ShardTransactionIdentifier transactionId,
600 short clientVersion){
601 return getContext().actorOf(
602 ShardTransaction.props(transaction, getSelf(),
603 schemaContext, datastoreContext, shardMBean,
604 transactionId.getRemoteTransactionId(), clientVersion)
605 .withDispatcher(txnDispatcherPath),
606 transactionId.toString());
610 private void createTransaction(CreateTransaction createTransaction) {
612 ActorRef transactionActor = createTransaction(createTransaction.getTransactionType(),
613 createTransaction.getTransactionId(), createTransaction.getTransactionChainId(),
614 createTransaction.getVersion());
616 getSender().tell(new CreateTransactionReply(Serialization.serializedActorPath(transactionActor),
617 createTransaction.getTransactionId()).toSerializable(), getSelf());
618 } catch (Exception e) {
619 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
623 private ActorRef createTransaction(int transactionType, String remoteTransactionId,
624 String transactionChainId, short clientVersion) {
627 ShardTransactionIdentifier transactionId = new ShardTransactionIdentifier(remoteTransactionId);
629 if(LOG.isDebugEnabled()) {
630 LOG.debug("{}: Creating transaction : {} ", persistenceId(), transactionId);
633 ActorRef transactionActor = createTypedTransactionActor(transactionType, transactionId,
634 transactionChainId, clientVersion);
636 return transactionActor;
639 private void syncCommitTransaction(final DOMStoreWriteTransaction transaction)
640 throws ExecutionException, InterruptedException {
641 DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
642 commitCohort.preCommit().get();
643 commitCohort.commit().get();
646 private void commitWithNewTransaction(final Modification modification) {
647 DOMStoreWriteTransaction tx = store.newWriteOnlyTransaction();
648 modification.apply(tx);
650 syncCommitTransaction(tx);
651 shardMBean.incrementCommittedTransactionCount();
652 shardMBean.setLastCommittedTransactionTime(System.currentTimeMillis());
653 } catch (InterruptedException | ExecutionException e) {
654 shardMBean.incrementFailedTransactionsCount();
655 LOG.error("{}: Failed to commit", persistenceId(), e);
659 private void updateSchemaContext(final UpdateSchemaContext message) {
660 this.schemaContext = message.getSchemaContext();
661 updateSchemaContext(message.getSchemaContext());
662 store.onGlobalContextUpdated(message.getSchemaContext());
666 void updateSchemaContext(final SchemaContext schemaContext) {
667 store.onGlobalContextUpdated(schemaContext);
670 private void registerChangeListener(final RegisterChangeListener registerChangeListener) {
672 LOG.debug("{}: registerDataChangeListener for {}", persistenceId(), registerChangeListener.getPath());
674 ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
675 NormalizedNode<?, ?>>> registration;
677 registration = doChangeListenerRegistration(registerChangeListener);
679 LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
681 DelayedListenerRegistration delayedReg =
682 new DelayedListenerRegistration(registerChangeListener);
683 delayedListenerRegistrations.add(delayedReg);
684 registration = delayedReg;
687 ActorRef listenerRegistration = getContext().actorOf(
688 DataChangeListenerRegistration.props(registration));
690 LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
691 persistenceId(), listenerRegistration.path());
693 getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
696 private ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
697 NormalizedNode<?, ?>>> doChangeListenerRegistration(
698 final RegisterChangeListener registerChangeListener) {
700 ActorSelection dataChangeListenerPath = getContext().system().actorSelection(
701 registerChangeListener.getDataChangeListenerPath());
703 // Notify the listener if notifications should be enabled or not
704 // If this shard is the leader then it will enable notifications else
706 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
708 // Now store a reference to the data change listener so it can be notified
709 // at a later point if notifications should be enabled or disabled
710 dataChangeListeners.add(dataChangeListenerPath);
712 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
713 new DataChangeListenerProxy(dataChangeListenerPath);
715 LOG.debug("{}: Registering for path {}", persistenceId(), registerChangeListener.getPath());
717 return store.registerChangeListener(registerChangeListener.getPath(), listener,
718 registerChangeListener.getScope());
721 private boolean isMetricsCaptureEnabled(){
722 CommonConfig config = new CommonConfig(getContext().system().settings().config());
723 return config.isMetricCaptureEnabled();
728 void startLogRecoveryBatch(final int maxBatchSize) {
729 currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
731 if(LOG.isDebugEnabled()) {
732 LOG.debug("{}: starting log recovery batch with max size {}", persistenceId(), maxBatchSize);
737 protected void appendRecoveredLogEntry(final Payload data) {
738 if(data instanceof ModificationPayload) {
740 currentLogRecoveryBatch.add(((ModificationPayload) data).getModification());
741 } catch (ClassNotFoundException | IOException e) {
742 LOG.error("{}: Error extracting ModificationPayload", persistenceId(), e);
744 } else if (data instanceof CompositeModificationPayload) {
745 currentLogRecoveryBatch.add(((CompositeModificationPayload) data).getModification());
746 } else if (data instanceof CompositeModificationByteStringPayload) {
747 currentLogRecoveryBatch.add(((CompositeModificationByteStringPayload) data).getModification());
749 LOG.error("{}: Unknown state received {} during recovery", persistenceId(), data);
754 protected void applyRecoverySnapshot(final byte[] snapshotBytes) {
755 if(recoveryCoordinator == null) {
756 recoveryCoordinator = new ShardRecoveryCoordinator(persistenceId(), schemaContext,
757 LOG, name.toString());
760 recoveryCoordinator.submit(snapshotBytes, store.newWriteOnlyTransaction());
762 if(LOG.isDebugEnabled()) {
763 LOG.debug("{}: submitted recovery sbapshot", persistenceId());
768 protected void applyCurrentLogRecoveryBatch() {
769 if(recoveryCoordinator == null) {
770 recoveryCoordinator = new ShardRecoveryCoordinator(persistenceId(), schemaContext,
771 LOG, name.toString());
774 recoveryCoordinator.submit(currentLogRecoveryBatch, store.newWriteOnlyTransaction());
776 if(LOG.isDebugEnabled()) {
777 LOG.debug("{}: submitted log recovery batch with size {}", persistenceId(),
778 currentLogRecoveryBatch.size());
783 protected void onRecoveryComplete() {
784 if(recoveryCoordinator != null) {
785 Collection<DOMStoreWriteTransaction> txList = recoveryCoordinator.getTransactions();
787 if(LOG.isDebugEnabled()) {
788 LOG.debug("{}: recovery complete - committing {} Tx's", persistenceId(), txList.size());
791 for(DOMStoreWriteTransaction tx: txList) {
793 syncCommitTransaction(tx);
794 shardMBean.incrementCommittedTransactionCount();
795 } catch (InterruptedException | ExecutionException e) {
796 shardMBean.incrementFailedTransactionsCount();
797 LOG.error("{}: Failed to commit", persistenceId(), e);
802 recoveryCoordinator = null;
803 currentLogRecoveryBatch = null;
805 //notify shard manager
806 getContext().parent().tell(new ActorInitialized(), getSelf());
808 // Being paranoid here - this method should only be called once but just in case...
809 if(txCommitTimeoutCheckSchedule == null) {
810 // Schedule a message to be periodically sent to check if the current in-progress
811 // transaction should be expired and aborted.
812 FiniteDuration period = Duration.create(transactionCommitTimeout / 3, TimeUnit.MILLISECONDS);
813 txCommitTimeoutCheckSchedule = getContext().system().scheduler().schedule(
814 period, period, getSelf(),
815 TX_COMMIT_TIMEOUT_CHECK_MESSAGE, getContext().dispatcher(), ActorRef.noSender());
820 protected void applyState(final ActorRef clientActor, final String identifier, final Object data) {
822 if(data instanceof ModificationPayload) {
824 applyModificationToState(clientActor, identifier, ((ModificationPayload) data).getModification());
825 } catch (ClassNotFoundException | IOException e) {
826 LOG.error("{}: Error extracting ModificationPayload", persistenceId(), e);
829 else if (data instanceof CompositeModificationPayload) {
830 Object modification = ((CompositeModificationPayload) data).getModification();
832 applyModificationToState(clientActor, identifier, modification);
833 } else if(data instanceof CompositeModificationByteStringPayload ){
834 Object modification = ((CompositeModificationByteStringPayload) data).getModification();
836 applyModificationToState(clientActor, identifier, modification);
838 LOG.error("{}: Unknown state received {} Class loader = {} CompositeNodeMod.ClassLoader = {}",
839 persistenceId(), data, data.getClass().getClassLoader(),
840 CompositeModificationPayload.class.getClassLoader());
844 private void applyModificationToState(ActorRef clientActor, String identifier, Object modification) {
845 if(modification == null) {
847 "{}: modification is null - this is very unexpected, clientActor = {}, identifier = {}",
848 persistenceId(), identifier, clientActor != null ? clientActor.path().toString() : null);
849 } else if(clientActor == null) {
850 // There's no clientActor to which to send a commit reply so we must be applying
851 // replicated state from the leader.
852 commitWithNewTransaction(MutableCompositeModification.fromSerializable(modification));
854 // This must be the OK to commit after replication consensus.
855 finishCommit(clientActor, identifier);
860 protected void createSnapshot() {
861 // Create a transaction actor. We are really going to treat the transaction as a worker
862 // so that this actor does not get block building the snapshot. THe transaction actor will
863 // after processing the CreateSnapshot message.
865 ActorRef createSnapshotTransaction = createTransaction(
866 TransactionProxy.TransactionType.READ_ONLY.ordinal(),
867 "createSnapshot" + ++createSnapshotTransactionCounter, "",
868 DataStoreVersions.CURRENT_VERSION);
870 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, self());
875 protected void applySnapshot(final byte[] snapshotBytes) {
876 // Since this will be done only on Recovery or when this actor is a Follower
877 // we can safely commit everything in here. We not need to worry about event notifications
878 // as they would have already been disabled on the follower
880 LOG.info("{}: Applying snapshot", persistenceId());
882 DOMStoreWriteTransaction transaction = store.newWriteOnlyTransaction();
884 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
886 // delete everything first
887 transaction.delete(DATASTORE_ROOT);
889 // Add everything from the remote node back
890 transaction.write(DATASTORE_ROOT, node);
891 syncCommitTransaction(transaction);
892 } catch (InterruptedException | ExecutionException e) {
893 LOG.error("{}: An exception occurred when applying snapshot", persistenceId(), e);
895 LOG.info("{}: Done applying snapshot", persistenceId());
900 protected void onStateChanged() {
901 boolean isLeader = isLeader();
902 for (ActorSelection dataChangeListener : dataChangeListeners) {
903 dataChangeListener.tell(new EnableNotification(isLeader), getSelf());
907 for(DelayedListenerRegistration reg: delayedListenerRegistrations) {
908 if(!reg.isClosed()) {
909 reg.setDelegate(doChangeListenerRegistration(reg.getRegisterChangeListener()));
913 delayedListenerRegistrations.clear();
916 // If this actor is no longer the leader close all the transaction chains
918 if(LOG.isDebugEnabled()) {
920 "{}: onStateChanged: Closing all transaction chains because shard {} is no longer the leader",
921 persistenceId(), getId());
924 transactionFactory.closeAllTransactionChains();
929 protected DataPersistenceProvider persistence() {
930 return dataPersistenceProvider;
933 @Override public String persistenceId() {
938 DataPersistenceProvider getDataPersistenceProvider() {
939 return dataPersistenceProvider;
943 ShardCommitCoordinator getCommitCoordinator() {
944 return commitCoordinator;
948 private static class ShardCreator implements Creator<Shard> {
950 private static final long serialVersionUID = 1L;
952 final ShardIdentifier name;
953 final Map<ShardIdentifier, String> peerAddresses;
954 final DatastoreContext datastoreContext;
955 final SchemaContext schemaContext;
957 ShardCreator(final ShardIdentifier name, final Map<ShardIdentifier, String> peerAddresses,
958 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
960 this.peerAddresses = peerAddresses;
961 this.datastoreContext = datastoreContext;
962 this.schemaContext = schemaContext;
966 public Shard create() throws Exception {
967 return new Shard(name, peerAddresses, datastoreContext, schemaContext);
972 public InMemoryDOMDataStore getDataStore() {
977 ShardStats getShardMBean() {
981 private static class DelayedListenerRegistration implements
982 ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> {
984 private volatile boolean closed;
986 private final RegisterChangeListener registerChangeListener;
988 private volatile ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
989 NormalizedNode<?, ?>>> delegate;
991 DelayedListenerRegistration(final RegisterChangeListener registerChangeListener) {
992 this.registerChangeListener = registerChangeListener;
995 void setDelegate( final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
996 NormalizedNode<?, ?>>> registration) {
997 this.delegate = registration;
1000 boolean isClosed() {
1004 RegisterChangeListener getRegisterChangeListener() {
1005 return registerChangeListener;
1009 public AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> getInstance() {
1010 return delegate != null ? delegate.getInstance() : null;
1014 public void close() {
1016 if(delegate != null) {