Optimize TransactionProxy for write-only transactions
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / Shard.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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;
29 import java.util.Map;
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;
88
89 /**
90  * A Shard represents a portion of the logical data tree <br/>
91  * <p>
92  * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
93  * </p>
94  */
95 public class Shard extends RaftActor {
96
97     private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
98
99     private static final Object TX_COMMIT_TIMEOUT_CHECK_MESSAGE = "txCommitTimeoutCheck";
100
101     @VisibleForTesting
102     static final String DEFAULT_NAME = "default";
103
104     // The state of this Shard
105     private final InMemoryDOMDataStore store;
106
107     /// The name of this shard
108     private final String name;
109
110     private final ShardStats shardMBean;
111
112     private final List<ActorSelection> dataChangeListeners =  Lists.newArrayList();
113
114     private final List<DelayedListenerRegistration> delayedListenerRegistrations =
115                                                                        Lists.newArrayList();
116
117     private DatastoreContext datastoreContext;
118
119     private DataPersistenceProvider dataPersistenceProvider;
120
121     private SchemaContext schemaContext;
122
123     private int createSnapshotTransactionCounter;
124
125     private final ShardCommitCoordinator commitCoordinator;
126
127     private long transactionCommitTimeout;
128
129     private Cancellable txCommitTimeoutCheckSchedule;
130
131     private final Optional<ActorRef> roleChangeNotifier;
132
133     private final MessageTracker appendEntriesReplyTracker;
134
135     private final ReadyTransactionReply READY_TRANSACTION_REPLY = new ReadyTransactionReply(
136             Serialization.serializedActorPath(getSelf()));
137
138
139     /**
140      * Coordinates persistence recovery on startup.
141      */
142     private ShardRecoveryCoordinator recoveryCoordinator;
143     private List<Object> currentLogRecoveryBatch;
144
145     private final DOMTransactionFactory transactionFactory;
146
147     private final String txnDispatcherPath;
148
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()));
153
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);
161
162
163         LOG.info("Shard created : {}, persistent : {}", name, datastoreContext.isPersistent());
164
165         store = InMemoryDOMDataStoreFactory.create(name.toString(), null,
166                 datastoreContext.getDataStoreProperties());
167
168         if(schemaContext != null) {
169             store.onGlobalContextUpdated(schemaContext);
170         }
171
172         shardMBean = ShardMBeanFactory.getShardStatsMBean(name.toString(),
173                 datastoreContext.getDataStoreMXBeanType());
174         shardMBean.setNotificationManager(store.getDataChangeListenerNotificationManager());
175         shardMBean.setShardActor(getSelf());
176
177         if (isMetricsCaptureEnabled()) {
178             getContext().become(new MeteringBehavior(this));
179         }
180
181         transactionFactory = new DOMTransactionFactory(store, shardMBean, LOG, this.name);
182
183         commitCoordinator = new ShardCommitCoordinator(transactionFactory,
184                 TimeUnit.SECONDS.convert(5, TimeUnit.MINUTES),
185                 datastoreContext.getShardTransactionCommitQueueCapacity(), self(), LOG, this.name);
186
187         setTransactionCommitTimeout();
188
189         // create a notifier actor for each cluster member
190         roleChangeNotifier = createRoleChangeNotifier(name.toString());
191
192         appendEntriesReplyTracker = new MessageTracker(AppendEntriesReply.class,
193                 getRaftActorContext().getConfigParams().getIsolatedCheckIntervalInMillis());
194     }
195
196     private void setTransactionCommitTimeout() {
197         transactionCommitTimeout = TimeUnit.MILLISECONDS.convert(
198                 datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS);
199     }
200
201     private static Map<String, String> mapPeerAddresses(
202         final Map<ShardIdentifier, String> peerAddresses) {
203         Map<String, String> map = new HashMap<>();
204
205         for (Map.Entry<ShardIdentifier, String> entry : peerAddresses
206             .entrySet()) {
207             map.put(entry.getKey().toString(), entry.getValue());
208         }
209
210         return map;
211     }
212
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");
220
221         return Props.create(new ShardCreator(name, peerAddresses, datastoreContext, schemaContext));
222     }
223
224     private Optional<ActorRef> createRoleChangeNotifier(String shardId) {
225         ActorRef shardRoleChangeNotifier = this.getContext().actorOf(
226             RoleChangeNotifier.getProps(shardId), shardId + "-notifier");
227         return Optional.of(shardRoleChangeNotifier);
228     }
229
230     @Override
231     public void postStop() {
232         LOG.info("Stopping Shard {}", persistenceId());
233
234         super.postStop();
235
236         if(txCommitTimeoutCheckSchedule != null) {
237             txCommitTimeoutCheckSchedule.cancel();
238         }
239
240         shardMBean.unregisterMBean();
241     }
242
243     @Override
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());
248         }
249
250         if (message instanceof RecoveryFailure){
251             LOG.error("{}: Recovery failed because of this cause",
252                     persistenceId(), ((RecoveryFailure) message).cause());
253
254             // Even though recovery failed, we still need to finish our recovery, eg send the
255             // ActorInitialized message and start the txCommitTimeoutCheckSchedule.
256             onRecoveryComplete();
257         } else {
258             super.onReceiveRecover(message);
259             if(LOG.isTraceEnabled()) {
260                 appendEntriesReplyTracker.begin();
261             }
262         }
263     }
264
265     @Override
266     public void onReceiveCommand(final Object message) throws Exception {
267
268         MessageTracker.Context context = appendEntriesReplyTracker.received(message);
269
270         if(context.error().isPresent()){
271             LOG.trace("{} : AppendEntriesReply failed to arrive at the expected interval {}", persistenceId(),
272                     context.error());
273         }
274
275         try {
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());
307             } else {
308                 super.onReceiveCommand(message);
309             }
310         } finally {
311             context.done();
312         }
313     }
314
315     @Override
316     protected Optional<ActorRef> getRoleChangeNotifier() {
317         return roleChangeNotifier;
318     }
319
320     private void onDatastoreContext(DatastoreContext context) {
321         datastoreContext = context;
322
323         commitCoordinator.setQueueCapacity(datastoreContext.getShardTransactionCommitQueueCapacity());
324
325         setTransactionCommitTimeout();
326
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();
333         }
334
335         updateConfigParams(datastoreContext.getShardRaftConfig());
336     }
337
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);
345
346                 doAbortTransaction(cohortEntry.getTransactionID(), null);
347             }
348         }
349     }
350
351     private void handleCommitTransaction(final CommitTransaction commit) {
352         final String transactionID = commit.getTransactionID();
353
354         LOG.debug("{}: Committing transaction {}", persistenceId(), transactionID);
355
356         // Get the current in-progress cohort entry in the commitCoordinator if it corresponds to
357         // this transaction.
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());
368             return;
369         }
370
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.
375
376         try {
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();
381
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());
387             } else {
388                 Shard.this.persistData(getSender(), transactionID,
389                         new ModificationPayload(cohortEntry.getModification()));
390             }
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());
396         }
397
398         cohortEntry.updateLastAccessTime();
399     }
400
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.
404
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
412             // transaction.
413             cohortEntry = commitCoordinator.getAndRemoveCohortEntry(transactionID);
414             if(cohortEntry != null) {
415                 commitWithNewTransaction(cohortEntry.getModification());
416                 sender.tell(CommitTransactionReply.INSTANCE.toSerializable(), getSelf());
417             } else {
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());
425             }
426
427             return;
428         }
429
430         LOG.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionID());
431
432         try {
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();
437
438             sender.tell(CommitTransactionReply.INSTANCE.toSerializable(), getSelf());
439
440             shardMBean.incrementCommittedTransactionCount();
441             shardMBean.setLastCommittedTransactionTime(System.currentTimeMillis());
442
443         } catch (Exception e) {
444             sender.tell(new akka.actor.Status.Failure(e), getSelf());
445
446             LOG.error("{}, An exception occurred while committing transaction {}", persistenceId(),
447                     transactionID, e);
448             shardMBean.incrementFailedTransactionsCount();
449         } finally {
450             commitCoordinator.currentTransactionComplete(transactionID, true);
451         }
452     }
453
454     private void handleCanCommitTransaction(final CanCommitTransaction canCommit) {
455         LOG.debug("{}: Can committing transaction {}", persistenceId(), canCommit.getTransactionID());
456         commitCoordinator.handleCanCommit(canCommit, getSender(), self());
457     }
458
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.
466
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.
471         //
472         if(isLeader()) {
473             try {
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());
480             }
481         } else {
482             ActorSelection leader = getLeader();
483             if(leader != null) {
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());
489             } else {
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());
496             }
497         }
498     }
499
500     private void handleForwardedReadyTransaction(ForwardedReadyTransaction ready) {
501         LOG.debug("{}: Readying transaction {}, client version {}", persistenceId(),
502                 ready.getTransactionID(), ready.getTxnClientVersion());
503
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
506         // the front-end.
507         commitCoordinator.transactionReady(ready.getTransactionID(), ready.getCohort(),
508                 (MutableCompositeModification) ready.getModification());
509
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()));
519
520             ReadyTransactionReply readyTransactionReply =
521                     new ReadyTransactionReply(Serialization.serializedActorPath(replyActorPath));
522             getSender().tell(ready.isReturnSerialized() ? readyTransactionReply.toSerializable() :
523                     readyTransactionReply, getSelf());
524
525         } else {
526
527             getSender().tell(ready.isReturnSerialized() ? READY_TRANSACTION_REPLY.toSerializable() :
528                     READY_TRANSACTION_REPLY, getSelf());
529         }
530     }
531
532     private void handleAbortTransaction(final AbortTransaction abort) {
533         doAbortTransaction(abort.getTransactionID(), getSender());
534     }
535
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);
540
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
543             // succeeds.
544             commitCoordinator.currentTransactionComplete(transactionID, false);
545
546             final ListenableFuture<Void> future = cohortEntry.getCohort().abort();
547             final ActorRef self = getSelf();
548
549             Futures.addCallback(future, new FutureCallback<Void>() {
550                 @Override
551                 public void onSuccess(final Void v) {
552                     shardMBean.incrementAbortTransactionsCount();
553
554                     if(sender != null) {
555                         sender.tell(AbortTransactionReply.INSTANCE.toSerializable(), self);
556                     }
557                 }
558
559                 @Override
560                 public void onFailure(final Throwable t) {
561                     LOG.error("{}: An exception happened during abort", persistenceId(), t);
562
563                     if(sender != null) {
564                         sender.tell(new akka.actor.Status.Failure(t), self);
565                     }
566                 }
567             });
568         }
569     }
570
571     private void handleCreateTransaction(final Object message) {
572         if (isLeader()) {
573             createTransaction(CreateTransaction.fromSerializable(message));
574         } else if (getLeader() != null) {
575             getLeader().forward(message, getContext());
576         } else {
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());
581         }
582     }
583
584     private void closeTransactionChain(final CloseTransactionChain closeTransactionChain) {
585         transactionFactory.closeTransactionChain(closeTransactionChain.getTransactionChainId());
586     }
587
588     private ActorRef createTypedTransactionActor(int transactionType,
589             ShardTransactionIdentifier transactionId, String transactionChainId,
590             short clientVersion ) {
591
592         DOMStoreTransaction transaction = transactionFactory.newTransaction(
593                 TransactionProxy.TransactionType.fromInt(transactionType), transactionId.toString(),
594                 transactionChainId);
595
596         return createShardTransaction(transaction, transactionId, clientVersion);
597     }
598
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());
607
608     }
609
610     private void createTransaction(CreateTransaction createTransaction) {
611         try {
612             ActorRef transactionActor = createTransaction(createTransaction.getTransactionType(),
613                 createTransaction.getTransactionId(), createTransaction.getTransactionChainId(),
614                 createTransaction.getVersion());
615
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());
620         }
621     }
622
623     private ActorRef createTransaction(int transactionType, String remoteTransactionId,
624             String transactionChainId, short clientVersion) {
625
626
627         ShardTransactionIdentifier transactionId = new ShardTransactionIdentifier(remoteTransactionId);
628
629         if(LOG.isDebugEnabled()) {
630             LOG.debug("{}: Creating transaction : {} ", persistenceId(), transactionId);
631         }
632
633         ActorRef transactionActor = createTypedTransactionActor(transactionType, transactionId,
634                 transactionChainId, clientVersion);
635
636         return transactionActor;
637     }
638
639     private void syncCommitTransaction(final DOMStoreWriteTransaction transaction)
640         throws ExecutionException, InterruptedException {
641         DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
642         commitCohort.preCommit().get();
643         commitCohort.commit().get();
644     }
645
646     private void commitWithNewTransaction(final Modification modification) {
647         DOMStoreWriteTransaction tx = store.newWriteOnlyTransaction();
648         modification.apply(tx);
649         try {
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);
656         }
657     }
658
659     private void updateSchemaContext(final UpdateSchemaContext message) {
660         this.schemaContext = message.getSchemaContext();
661         updateSchemaContext(message.getSchemaContext());
662         store.onGlobalContextUpdated(message.getSchemaContext());
663     }
664
665     @VisibleForTesting
666     void updateSchemaContext(final SchemaContext schemaContext) {
667         store.onGlobalContextUpdated(schemaContext);
668     }
669
670     private void registerChangeListener(final RegisterChangeListener registerChangeListener) {
671
672         LOG.debug("{}: registerDataChangeListener for {}", persistenceId(), registerChangeListener.getPath());
673
674         ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
675                                                      NormalizedNode<?, ?>>> registration;
676         if(isLeader()) {
677             registration = doChangeListenerRegistration(registerChangeListener);
678         } else {
679             LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
680
681             DelayedListenerRegistration delayedReg =
682                     new DelayedListenerRegistration(registerChangeListener);
683             delayedListenerRegistrations.add(delayedReg);
684             registration = delayedReg;
685         }
686
687         ActorRef listenerRegistration = getContext().actorOf(
688                 DataChangeListenerRegistration.props(registration));
689
690         LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
691                 persistenceId(), listenerRegistration.path());
692
693         getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
694     }
695
696     private ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
697                                                NormalizedNode<?, ?>>> doChangeListenerRegistration(
698             final RegisterChangeListener registerChangeListener) {
699
700         ActorSelection dataChangeListenerPath = getContext().system().actorSelection(
701                 registerChangeListener.getDataChangeListenerPath());
702
703         // Notify the listener if notifications should be enabled or not
704         // If this shard is the leader then it will enable notifications else
705         // it will not
706         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
707
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);
711
712         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
713                 new DataChangeListenerProxy(dataChangeListenerPath);
714
715         LOG.debug("{}: Registering for path {}", persistenceId(), registerChangeListener.getPath());
716
717         return store.registerChangeListener(registerChangeListener.getPath(), listener,
718                 registerChangeListener.getScope());
719     }
720
721     private boolean isMetricsCaptureEnabled(){
722         CommonConfig config = new CommonConfig(getContext().system().settings().config());
723         return config.isMetricCaptureEnabled();
724     }
725
726     @Override
727     protected
728     void startLogRecoveryBatch(final int maxBatchSize) {
729         currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
730
731         if(LOG.isDebugEnabled()) {
732             LOG.debug("{}: starting log recovery batch with max size {}", persistenceId(), maxBatchSize);
733         }
734     }
735
736     @Override
737     protected void appendRecoveredLogEntry(final Payload data) {
738         if(data instanceof ModificationPayload) {
739             try {
740                 currentLogRecoveryBatch.add(((ModificationPayload) data).getModification());
741             } catch (ClassNotFoundException | IOException e) {
742                 LOG.error("{}: Error extracting ModificationPayload", persistenceId(), e);
743             }
744         } else if (data instanceof CompositeModificationPayload) {
745             currentLogRecoveryBatch.add(((CompositeModificationPayload) data).getModification());
746         } else if (data instanceof CompositeModificationByteStringPayload) {
747             currentLogRecoveryBatch.add(((CompositeModificationByteStringPayload) data).getModification());
748         } else {
749             LOG.error("{}: Unknown state received {} during recovery", persistenceId(), data);
750         }
751     }
752
753     @Override
754     protected void applyRecoverySnapshot(final byte[] snapshotBytes) {
755         if(recoveryCoordinator == null) {
756             recoveryCoordinator = new ShardRecoveryCoordinator(persistenceId(), schemaContext,
757                     LOG, name.toString());
758         }
759
760         recoveryCoordinator.submit(snapshotBytes, store.newWriteOnlyTransaction());
761
762         if(LOG.isDebugEnabled()) {
763             LOG.debug("{}: submitted recovery sbapshot", persistenceId());
764         }
765     }
766
767     @Override
768     protected void applyCurrentLogRecoveryBatch() {
769         if(recoveryCoordinator == null) {
770             recoveryCoordinator = new ShardRecoveryCoordinator(persistenceId(), schemaContext,
771                     LOG, name.toString());
772         }
773
774         recoveryCoordinator.submit(currentLogRecoveryBatch, store.newWriteOnlyTransaction());
775
776         if(LOG.isDebugEnabled()) {
777             LOG.debug("{}: submitted log recovery batch with size {}", persistenceId(),
778                     currentLogRecoveryBatch.size());
779         }
780     }
781
782     @Override
783     protected void onRecoveryComplete() {
784         if(recoveryCoordinator != null) {
785             Collection<DOMStoreWriteTransaction> txList = recoveryCoordinator.getTransactions();
786
787             if(LOG.isDebugEnabled()) {
788                 LOG.debug("{}: recovery complete - committing {} Tx's", persistenceId(), txList.size());
789             }
790
791             for(DOMStoreWriteTransaction tx: txList) {
792                 try {
793                     syncCommitTransaction(tx);
794                     shardMBean.incrementCommittedTransactionCount();
795                 } catch (InterruptedException | ExecutionException e) {
796                     shardMBean.incrementFailedTransactionsCount();
797                     LOG.error("{}: Failed to commit", persistenceId(), e);
798                 }
799             }
800         }
801
802         recoveryCoordinator = null;
803         currentLogRecoveryBatch = null;
804
805         //notify shard manager
806         getContext().parent().tell(new ActorInitialized(), getSelf());
807
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());
816         }
817     }
818
819     @Override
820     protected void applyState(final ActorRef clientActor, final String identifier, final Object data) {
821
822         if(data instanceof ModificationPayload) {
823             try {
824                 applyModificationToState(clientActor, identifier, ((ModificationPayload) data).getModification());
825             } catch (ClassNotFoundException | IOException e) {
826                 LOG.error("{}: Error extracting ModificationPayload", persistenceId(), e);
827             }
828         }
829         else if (data instanceof CompositeModificationPayload) {
830             Object modification = ((CompositeModificationPayload) data).getModification();
831
832             applyModificationToState(clientActor, identifier, modification);
833         } else if(data instanceof CompositeModificationByteStringPayload ){
834             Object modification = ((CompositeModificationByteStringPayload) data).getModification();
835
836             applyModificationToState(clientActor, identifier, modification);
837         } else {
838             LOG.error("{}: Unknown state received {} Class loader = {} CompositeNodeMod.ClassLoader = {}",
839                     persistenceId(), data, data.getClass().getClassLoader(),
840                     CompositeModificationPayload.class.getClassLoader());
841         }
842     }
843
844     private void applyModificationToState(ActorRef clientActor, String identifier, Object modification) {
845         if(modification == null) {
846             LOG.error(
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));
853         } else {
854             // This must be the OK to commit after replication consensus.
855             finishCommit(clientActor, identifier);
856         }
857     }
858
859     @Override
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.
864
865         ActorRef createSnapshotTransaction = createTransaction(
866                 TransactionProxy.TransactionType.READ_ONLY.ordinal(),
867                 "createSnapshot" + ++createSnapshotTransactionCounter, "",
868                 DataStoreVersions.CURRENT_VERSION);
869
870         createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, self());
871     }
872
873     @VisibleForTesting
874     @Override
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
879
880         LOG.info("{}: Applying snapshot", persistenceId());
881         try {
882             DOMStoreWriteTransaction transaction = store.newWriteOnlyTransaction();
883
884             NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
885
886             // delete everything first
887             transaction.delete(DATASTORE_ROOT);
888
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);
894         } finally {
895             LOG.info("{}: Done applying snapshot", persistenceId());
896         }
897     }
898
899     @Override
900     protected void onStateChanged() {
901         boolean isLeader = isLeader();
902         for (ActorSelection dataChangeListener : dataChangeListeners) {
903             dataChangeListener.tell(new EnableNotification(isLeader), getSelf());
904         }
905
906         if(isLeader) {
907             for(DelayedListenerRegistration reg: delayedListenerRegistrations) {
908                 if(!reg.isClosed()) {
909                     reg.setDelegate(doChangeListenerRegistration(reg.getRegisterChangeListener()));
910                 }
911             }
912
913             delayedListenerRegistrations.clear();
914         }
915
916         // If this actor is no longer the leader close all the transaction chains
917         if(!isLeader) {
918             if(LOG.isDebugEnabled()) {
919                 LOG.debug(
920                     "{}: onStateChanged: Closing all transaction chains because shard {} is no longer the leader",
921                     persistenceId(), getId());
922             }
923
924             transactionFactory.closeAllTransactionChains();
925         }
926     }
927
928     @Override
929     protected DataPersistenceProvider persistence() {
930         return dataPersistenceProvider;
931     }
932
933     @Override public String persistenceId() {
934         return this.name;
935     }
936
937     @VisibleForTesting
938     DataPersistenceProvider getDataPersistenceProvider() {
939         return dataPersistenceProvider;
940     }
941
942     @VisibleForTesting
943     ShardCommitCoordinator getCommitCoordinator() {
944         return commitCoordinator;
945     }
946
947
948     private static class ShardCreator implements Creator<Shard> {
949
950         private static final long serialVersionUID = 1L;
951
952         final ShardIdentifier name;
953         final Map<ShardIdentifier, String> peerAddresses;
954         final DatastoreContext datastoreContext;
955         final SchemaContext schemaContext;
956
957         ShardCreator(final ShardIdentifier name, final Map<ShardIdentifier, String> peerAddresses,
958                 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
959             this.name = name;
960             this.peerAddresses = peerAddresses;
961             this.datastoreContext = datastoreContext;
962             this.schemaContext = schemaContext;
963         }
964
965         @Override
966         public Shard create() throws Exception {
967             return new Shard(name, peerAddresses, datastoreContext, schemaContext);
968         }
969     }
970
971     @VisibleForTesting
972     public InMemoryDOMDataStore getDataStore() {
973         return store;
974     }
975
976     @VisibleForTesting
977     ShardStats getShardMBean() {
978         return shardMBean;
979     }
980
981     private static class DelayedListenerRegistration implements
982         ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> {
983
984         private volatile boolean closed;
985
986         private final RegisterChangeListener registerChangeListener;
987
988         private volatile ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
989                                                              NormalizedNode<?, ?>>> delegate;
990
991         DelayedListenerRegistration(final RegisterChangeListener registerChangeListener) {
992             this.registerChangeListener = registerChangeListener;
993         }
994
995         void setDelegate( final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
996                                             NormalizedNode<?, ?>>> registration) {
997             this.delegate = registration;
998         }
999
1000         boolean isClosed() {
1001             return closed;
1002         }
1003
1004         RegisterChangeListener getRegisterChangeListener() {
1005             return registerChangeListener;
1006         }
1007
1008         @Override
1009         public AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> getInstance() {
1010             return delegate != null ? delegate.getInstance() : null;
1011         }
1012
1013         @Override
1014         public void close() {
1015             closed = true;
1016             if(delegate != null) {
1017                 delegate.close();
1018             }
1019         }
1020     }
1021 }