Disable state tracking on ReadyLocalTransaction
[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 package org.opendaylight.controller.cluster.datastore;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
14
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSelection;
17 import akka.actor.Cancellable;
18 import akka.actor.ExtendedActorSystem;
19 import akka.actor.PoisonPill;
20 import akka.actor.Props;
21 import akka.actor.Status;
22 import akka.actor.Status.Failure;
23 import akka.persistence.RecoveryCompleted;
24 import akka.persistence.SnapshotOffer;
25 import akka.serialization.JavaSerializer;
26 import akka.serialization.Serialization;
27 import com.google.common.annotations.VisibleForTesting;
28 import com.google.common.base.Ticker;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.ImmutableMap;
31 import com.google.common.collect.ImmutableSet;
32 import com.google.common.collect.Range;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.Optional;
40 import java.util.OptionalLong;
41 import java.util.concurrent.TimeUnit;
42 import org.eclipse.jdt.annotation.NonNull;
43 import org.eclipse.jdt.annotation.Nullable;
44 import org.opendaylight.controller.cluster.access.ABIVersion;
45 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
46 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
47 import org.opendaylight.controller.cluster.access.commands.LocalHistoryRequest;
48 import org.opendaylight.controller.cluster.access.commands.NotLeaderException;
49 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
50 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
51 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
52 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
53 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
54 import org.opendaylight.controller.cluster.access.concepts.Request;
55 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
56 import org.opendaylight.controller.cluster.access.concepts.RequestException;
57 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
58 import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
59 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
60 import org.opendaylight.controller.cluster.access.concepts.SliceableMessage;
61 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
62 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
63 import org.opendaylight.controller.cluster.common.actor.CommonConfig;
64 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
65 import org.opendaylight.controller.cluster.common.actor.Dispatchers.DispatcherType;
66 import org.opendaylight.controller.cluster.common.actor.MessageTracker;
67 import org.opendaylight.controller.cluster.common.actor.MessageTracker.Error;
68 import org.opendaylight.controller.cluster.common.actor.MeteringBehavior;
69 import org.opendaylight.controller.cluster.datastore.actors.JsonExportActor;
70 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
71 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
72 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
73 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
74 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
75 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
76 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
77 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
78 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
79 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
80 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
81 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
82 import org.opendaylight.controller.cluster.datastore.messages.GetKnownClients;
83 import org.opendaylight.controller.cluster.datastore.messages.GetKnownClientsReply;
84 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
85 import org.opendaylight.controller.cluster.datastore.messages.MakeLeaderLocal;
86 import org.opendaylight.controller.cluster.datastore.messages.OnDemandShardState;
87 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
88 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
89 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
90 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
91 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
92 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
93 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
94 import org.opendaylight.controller.cluster.datastore.persisted.DisableTrackingPayload;
95 import org.opendaylight.controller.cluster.messaging.MessageAssembler;
96 import org.opendaylight.controller.cluster.messaging.MessageSlicer;
97 import org.opendaylight.controller.cluster.messaging.SliceOptions;
98 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
99 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
100 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
101 import org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException;
102 import org.opendaylight.controller.cluster.raft.RaftActor;
103 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
104 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
105 import org.opendaylight.controller.cluster.raft.RaftState;
106 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
107 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
108 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
109 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
110 import org.opendaylight.controller.cluster.raft.messages.RequestLeadership;
111 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
112 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStoreProperties.ExportOnRecovery;
114 import org.opendaylight.yangtools.concepts.Identifier;
115 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
116 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
117 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
118 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
119 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
120 import scala.concurrent.duration.FiniteDuration;
121
122 /**
123  * A Shard represents a portion of the logical data tree.
124  *
125  * <p>
126  * Our Shard uses InMemoryDataTree as it's internal representation and delegates all requests it
127  */
128 // FIXME: non-final for testing?
129 public class Shard extends RaftActor {
130
131     @VisibleForTesting
132     static final Object TX_COMMIT_TIMEOUT_CHECK_MESSAGE = new Object() {
133         @Override
134         public String toString() {
135             return "txCommitTimeoutCheck";
136         }
137     };
138
139     @VisibleForTesting
140     static final Object GET_SHARD_MBEAN_MESSAGE = new Object() {
141         @Override
142         public String toString() {
143             return "getShardMBeanMessage";
144         }
145     };
146
147     static final Object RESUME_NEXT_PENDING_TRANSACTION = new Object() {
148         @Override
149         public String toString() {
150             return "resumeNextPendingTransaction";
151         }
152     };
153
154     // FIXME: shard names should be encapsulated in their own class and this should be exposed as a constant.
155     public static final String DEFAULT_NAME = "default";
156
157     private static final Collection<ABIVersion> SUPPORTED_ABIVERSIONS;
158
159     // Make sure to keep this in sync with the journal configuration in factory-akka.conf
160     public static final String NON_PERSISTENT_JOURNAL_ID = "akka.persistence.non-persistent.journal";
161
162     static {
163         final ABIVersion[] values = ABIVersion.values();
164         final ABIVersion[] real = Arrays.copyOfRange(values, 1, values.length - 1);
165         SUPPORTED_ABIVERSIONS = ImmutableList.copyOf(real).reverse();
166     }
167
168     // FIXME: make this a dynamic property based on mailbox size and maximum number of clients
169     private static final int CLIENT_MAX_MESSAGES = 1000;
170
171     // The state of this Shard
172     private final ShardDataTree store;
173
174     /// The name of this shard
175     private final String name;
176
177     private final String shardName;
178
179     private final ShardStats shardMBean;
180
181     private final ShardDataTreeListenerInfoMXBeanImpl listenerInfoMXBean;
182
183     private DatastoreContext datastoreContext;
184
185     private final ShardCommitCoordinator commitCoordinator;
186
187     private long transactionCommitTimeout;
188
189     private Cancellable txCommitTimeoutCheckSchedule;
190
191     private final Optional<ActorRef> roleChangeNotifier;
192
193     private final MessageTracker appendEntriesReplyTracker;
194
195     private final ShardTransactionActorFactory transactionActorFactory;
196
197     private final ShardSnapshotCohort snapshotCohort;
198
199     private final DataTreeChangeListenerSupport treeChangeSupport = new DataTreeChangeListenerSupport(this);
200
201     private ShardSnapshot restoreFromSnapshot;
202
203     private final ShardTransactionMessageRetrySupport messageRetrySupport;
204
205     @VisibleForTesting
206     final FrontendMetadata frontendMetadata;
207
208     private Map<FrontendIdentifier, LeaderFrontendState> knownFrontends = ImmutableMap.of();
209     private boolean paused;
210
211     private final MessageSlicer responseMessageSlicer;
212     private final Dispatchers dispatchers;
213
214     private final MessageAssembler requestMessageAssembler;
215
216     private final ExportOnRecovery exportOnRecovery;
217
218     private final ActorRef exportActor;
219
220     Shard(final AbstractBuilder<?, ?> builder) {
221         super(builder.getId().toString(), builder.getPeerAddresses(),
222                 Optional.of(builder.getDatastoreContext().getShardRaftConfig()), DataStoreVersions.CURRENT_VERSION);
223
224         name = builder.getId().toString();
225         shardName = builder.getId().getShardName();
226         datastoreContext = builder.getDatastoreContext();
227         restoreFromSnapshot = builder.getRestoreFromSnapshot();
228         frontendMetadata = new FrontendMetadata(name);
229         exportOnRecovery = datastoreContext.getExportOnRecovery();
230
231         switch (exportOnRecovery) {
232             case Json:
233                 exportActor = getContext().actorOf(JsonExportActor.props(builder.getSchemaContext(),
234                         datastoreContext.getRecoveryExportBaseDir()));
235                 break;
236             case Off:
237             default:
238                 exportActor = null;
239                 break;
240         }
241
242         setPersistence(datastoreContext.isPersistent());
243
244         LOG.info("Shard created : {}, persistent : {}", name, datastoreContext.isPersistent());
245
246         ShardDataTreeChangeListenerPublisherActorProxy treeChangeListenerPublisher =
247                 new ShardDataTreeChangeListenerPublisherActorProxy(getContext(), name + "-DTCL-publisher", name);
248         if (builder.getDataTree() != null) {
249             store = new ShardDataTree(this, builder.getSchemaContext(), builder.getDataTree(),
250                     treeChangeListenerPublisher, name,
251                     frontendMetadata);
252         } else {
253             store = new ShardDataTree(this, builder.getSchemaContext(), builder.getTreeType(),
254                     builder.getDatastoreContext().getStoreRoot(), treeChangeListenerPublisher, name,
255                     frontendMetadata);
256         }
257
258         shardMBean = ShardStats.create(name, datastoreContext.getDataStoreMXBeanType(), this);
259
260         if (isMetricsCaptureEnabled()) {
261             getContext().become(new MeteringBehavior(this));
262         }
263
264         commitCoordinator = new ShardCommitCoordinator(store, LOG, name);
265
266         setTransactionCommitTimeout();
267
268         // create a notifier actor for each cluster member
269         roleChangeNotifier = createRoleChangeNotifier(name);
270
271         appendEntriesReplyTracker = new MessageTracker(AppendEntriesReply.class,
272                 getRaftActorContext().getConfigParams().getIsolatedCheckIntervalInMillis());
273
274         dispatchers = new Dispatchers(context().system().dispatchers());
275         transactionActorFactory = new ShardTransactionActorFactory(store, datastoreContext,
276             dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Transaction),
277                 self(), getContext(), shardMBean, builder.getId().getShardName());
278
279         snapshotCohort = ShardSnapshotCohort.create(getContext(), builder.getId().getMemberName(), store, LOG,
280             name, datastoreContext);
281
282         messageRetrySupport = new ShardTransactionMessageRetrySupport(this);
283
284         responseMessageSlicer = MessageSlicer.builder().logContext(name)
285                 .messageSliceSize(datastoreContext.getMaximumMessageSliceSize())
286                 .fileBackedStreamFactory(getRaftActorContext().getFileBackedOutputStreamFactory())
287                 .expireStateAfterInactivity(2, TimeUnit.MINUTES).build();
288
289         requestMessageAssembler = MessageAssembler.builder().logContext(name)
290                 .fileBackedStreamFactory(getRaftActorContext().getFileBackedOutputStreamFactory())
291                 .assembledMessageCallback((message, sender) -> self().tell(message, sender))
292                 .expireStateAfterInactivity(datastoreContext.getRequestTimeout(), TimeUnit.NANOSECONDS).build();
293
294         listenerInfoMXBean = new ShardDataTreeListenerInfoMXBeanImpl(name, datastoreContext.getDataStoreMXBeanType(),
295                 self());
296         listenerInfoMXBean.register();
297     }
298
299     private void setTransactionCommitTimeout() {
300         transactionCommitTimeout = TimeUnit.MILLISECONDS.convert(
301                 datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS) / 2;
302     }
303
304     private Optional<ActorRef> createRoleChangeNotifier(final String shardId) {
305         ActorRef shardRoleChangeNotifier = this.getContext().actorOf(
306             RoleChangeNotifier.getProps(shardId), shardId + "-notifier");
307         return Optional.of(shardRoleChangeNotifier);
308     }
309
310     @Override
311     public final void postStop() throws Exception {
312         LOG.info("Stopping Shard {}", persistenceId());
313
314         super.postStop();
315
316         messageRetrySupport.close();
317
318         if (txCommitTimeoutCheckSchedule != null) {
319             txCommitTimeoutCheckSchedule.cancel();
320         }
321
322         commitCoordinator.abortPendingTransactions("Transaction aborted due to shutdown.", this);
323
324         shardMBean.unregisterMBean();
325         listenerInfoMXBean.unregister();
326     }
327
328     @Override
329     protected final void handleRecover(final Object message) {
330         LOG.debug("{}: onReceiveRecover: Received message {} from {}", persistenceId(), message.getClass(),
331             getSender());
332
333         super.handleRecover(message);
334
335         switch (exportOnRecovery) {
336             case Json:
337                 if (message instanceof SnapshotOffer) {
338                     exportActor.tell(new JsonExportActor.ExportSnapshot(store.readCurrentData().get(), name),
339                             ActorRef.noSender());
340                 } else if (message instanceof ReplicatedLogEntry) {
341                     exportActor.tell(new JsonExportActor.ExportJournal((ReplicatedLogEntry) message),
342                             ActorRef.noSender());
343                 } else if (message instanceof RecoveryCompleted) {
344                     exportActor.tell(new JsonExportActor.FinishExport(name), ActorRef.noSender());
345                     exportActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
346                 }
347                 break;
348             case Off:
349             default:
350                 break;
351         }
352
353         if (LOG.isTraceEnabled()) {
354             appendEntriesReplyTracker.begin();
355         }
356     }
357
358     @Override
359     // non-final for TestShard
360     protected void handleNonRaftCommand(final Object message) {
361         try (MessageTracker.Context context = appendEntriesReplyTracker.received(message)) {
362             final Optional<Error> maybeError = context.error();
363             if (maybeError.isPresent()) {
364                 LOG.trace("{} : AppendEntriesReply failed to arrive at the expected interval {}", persistenceId(),
365                     maybeError.get());
366             }
367
368             store.resetTransactionBatch();
369
370             if (message instanceof RequestEnvelope) {
371                 handleRequestEnvelope((RequestEnvelope)message);
372             } else if (MessageAssembler.isHandledMessage(message)) {
373                 handleRequestAssemblerMessage(message);
374             } else if (message instanceof ConnectClientRequest) {
375                 handleConnectClient((ConnectClientRequest)message);
376             } else if (CreateTransaction.isSerializedType(message)) {
377                 handleCreateTransaction(message);
378             } else if (message instanceof BatchedModifications) {
379                 handleBatchedModifications((BatchedModifications)message);
380             } else if (message instanceof ForwardedReadyTransaction) {
381                 handleForwardedReadyTransaction((ForwardedReadyTransaction) message);
382             } else if (message instanceof ReadyLocalTransaction) {
383                 handleReadyLocalTransaction((ReadyLocalTransaction)message);
384             } else if (CanCommitTransaction.isSerializedType(message)) {
385                 handleCanCommitTransaction(CanCommitTransaction.fromSerializable(message));
386             } else if (CommitTransaction.isSerializedType(message)) {
387                 handleCommitTransaction(CommitTransaction.fromSerializable(message));
388             } else if (AbortTransaction.isSerializedType(message)) {
389                 handleAbortTransaction(AbortTransaction.fromSerializable(message));
390             } else if (CloseTransactionChain.isSerializedType(message)) {
391                 closeTransactionChain(CloseTransactionChain.fromSerializable(message));
392             } else if (message instanceof DataTreeChangedReply) {
393                 // Ignore reply
394             } else if (message instanceof RegisterDataTreeChangeListener) {
395                 treeChangeSupport.onMessage((RegisterDataTreeChangeListener) message, isLeader(), hasLeader());
396             } else if (message instanceof UpdateSchemaContext) {
397                 updateSchemaContext((UpdateSchemaContext) message);
398             } else if (message instanceof PeerAddressResolved) {
399                 PeerAddressResolved resolved = (PeerAddressResolved) message;
400                 setPeerAddress(resolved.getPeerId(), resolved.getPeerAddress());
401             } else if (TX_COMMIT_TIMEOUT_CHECK_MESSAGE.equals(message)) {
402                 commitTimeoutCheck();
403             } else if (message instanceof DatastoreContext) {
404                 onDatastoreContext((DatastoreContext)message);
405             } else if (message instanceof RegisterRoleChangeListener) {
406                 roleChangeNotifier.get().forward(message, context());
407             } else if (message instanceof FollowerInitialSyncUpStatus) {
408                 shardMBean.setFollowerInitialSyncStatus(((FollowerInitialSyncUpStatus) message).isInitialSyncDone());
409                 context().parent().tell(message, self());
410             } else if (GET_SHARD_MBEAN_MESSAGE.equals(message)) {
411                 sender().tell(getShardMBean(), self());
412             } else if (message instanceof GetShardDataTree) {
413                 sender().tell(store.getDataTree(), self());
414             } else if (message instanceof ServerRemoved) {
415                 context().parent().forward(message, context());
416             } else if (ShardTransactionMessageRetrySupport.TIMER_MESSAGE_CLASS.isInstance(message)) {
417                 messageRetrySupport.onTimerMessage(message);
418             } else if (message instanceof DataTreeCohortActorRegistry.CohortRegistryCommand) {
419                 store.processCohortRegistryCommand(getSender(),
420                         (DataTreeCohortActorRegistry.CohortRegistryCommand) message);
421             } else if (message instanceof MakeLeaderLocal) {
422                 onMakeLeaderLocal();
423             } else if (RESUME_NEXT_PENDING_TRANSACTION.equals(message)) {
424                 store.resumeNextPendingTransaction();
425             } else if (GetKnownClients.INSTANCE.equals(message)) {
426                 handleGetKnownClients();
427             } else if (!responseMessageSlicer.handleMessage(message)) {
428                 super.handleNonRaftCommand(message);
429             }
430         }
431     }
432
433     private void handleRequestAssemblerMessage(final Object message) {
434         dispatchers.getDispatcher(DispatcherType.Serialization).execute(() -> {
435             JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) context().system());
436             requestMessageAssembler.handleMessage(message, self());
437         });
438     }
439
440     @SuppressWarnings("checkstyle:IllegalCatch")
441     private void handleRequestEnvelope(final RequestEnvelope envelope) {
442         final long now = ticker().read();
443         try {
444             final RequestSuccess<?, ?> success = handleRequest(envelope, now);
445             if (success != null) {
446                 final long executionTimeNanos = ticker().read() - now;
447                 if (success instanceof SliceableMessage) {
448                     dispatchers.getDispatcher(DispatcherType.Serialization).execute(() ->
449                         responseMessageSlicer.slice(SliceOptions.builder().identifier(success.getTarget())
450                             .message(envelope.newSuccessEnvelope(success, executionTimeNanos))
451                             .sendTo(envelope.getMessage().getReplyTo()).replyTo(self())
452                             .onFailureCallback(t -> LOG.warn("Error slicing response {}", success, t)).build()));
453                 } else {
454                     envelope.sendSuccess(success, executionTimeNanos);
455                 }
456             }
457         } catch (RequestException e) {
458             LOG.debug("{}: request {} failed", persistenceId(), envelope, e);
459             envelope.sendFailure(e, ticker().read() - now);
460         } catch (Exception e) {
461             LOG.debug("{}: request {} caused failure", persistenceId(), envelope, e);
462             envelope.sendFailure(new RuntimeRequestException("Request failed to process", e),
463                 ticker().read() - now);
464         }
465     }
466
467     private void commitTimeoutCheck() {
468         store.checkForExpiredTransactions(transactionCommitTimeout, this::updateAccess);
469         commitCoordinator.checkForExpiredTransactions(transactionCommitTimeout, this);
470         requestMessageAssembler.checkExpiredAssembledMessageState();
471     }
472
473     private OptionalLong updateAccess(final SimpleShardDataTreeCohort cohort) {
474         final FrontendIdentifier frontend = cohort.getIdentifier().getHistoryId().getClientId().getFrontendId();
475         final LeaderFrontendState state = knownFrontends.get(frontend);
476         if (state == null) {
477             // Not tell-based protocol, do nothing
478             return OptionalLong.empty();
479         }
480
481         if (isIsolatedLeader()) {
482             // We are isolated and no new request can come through until we emerge from it. We are still updating
483             // liveness of frontend when we see it attempting to communicate. Use the last access timer.
484             return OptionalLong.of(state.getLastSeenTicks());
485         }
486
487         // If this frontend has freshly connected, give it some time to catch up before killing its transactions.
488         return OptionalLong.of(state.getLastConnectTicks());
489     }
490
491     private void disableTracking(final DisableTrackingPayload payload) {
492         final ClientIdentifier clientId = payload.getIdentifier();
493         LOG.debug("{}: disabling tracking of {}", persistenceId(), clientId);
494         frontendMetadata.disableTracking(clientId);
495
496         if (isLeader()) {
497             final FrontendIdentifier frontendId = clientId.getFrontendId();
498             final LeaderFrontendState frontend = knownFrontends.get(frontendId);
499             if (frontend != null) {
500                 if (clientId.equals(frontend.getIdentifier())) {
501                     if (!(frontend instanceof LeaderFrontendState.Disabled)) {
502                         verify(knownFrontends.replace(frontendId, frontend,
503                             new LeaderFrontendState.Disabled(persistenceId(), clientId, store)));
504                         LOG.debug("{}: leader state for {} disabled", persistenceId(), clientId);
505                     } else {
506                         LOG.debug("{}: leader state {} is already disabled", persistenceId(), frontend);
507                     }
508                 } else {
509                     LOG.debug("{}: leader state {} does not match {}", persistenceId(), frontend, clientId);
510                 }
511             } else {
512                 LOG.debug("{}: leader state for {} not found", persistenceId(), clientId);
513                 knownFrontends.put(frontendId, new LeaderFrontendState.Disabled(persistenceId(), clientId,
514                     getDataStore()));
515             }
516         }
517     }
518
519     private void onMakeLeaderLocal() {
520         LOG.debug("{}: onMakeLeaderLocal received", persistenceId());
521         if (isLeader()) {
522             getSender().tell(new Status.Success(null), getSelf());
523             return;
524         }
525
526         final ActorSelection leader = getLeader();
527
528         if (leader == null) {
529             // Leader is not present. The cluster is most likely trying to
530             // elect a leader and we should let that run its normal course
531
532             // TODO we can wait for the election to complete and retry the
533             // request. We can also let the caller retry by sending a flag
534             // in the response indicating the request is "reTryable".
535             getSender().tell(new Failure(
536                     new LeadershipTransferFailedException("We cannot initiate leadership transfer to local node. "
537                             + "Currently there is no leader for " + persistenceId())),
538                     getSelf());
539             return;
540         }
541
542         leader.tell(new RequestLeadership(getId(), getSender()), getSelf());
543     }
544
545     // Acquire our frontend tracking handle and verify generation matches
546     private @Nullable LeaderFrontendState findFrontend(final ClientIdentifier clientId) throws RequestException {
547         final LeaderFrontendState existing = knownFrontends.get(clientId.getFrontendId());
548         if (existing != null) {
549             final int cmp = Long.compareUnsigned(existing.getIdentifier().getGeneration(), clientId.getGeneration());
550             if (cmp == 0) {
551                 existing.touch();
552                 return existing;
553             }
554             if (cmp > 0) {
555                 LOG.debug("{}: rejecting request from outdated client {}", persistenceId(), clientId);
556                 throw new RetiredGenerationException(clientId.getGeneration(),
557                     existing.getIdentifier().getGeneration());
558             }
559
560             LOG.info("{}: retiring state {}, outdated by request from client {}", persistenceId(), existing, clientId);
561             existing.retire();
562             knownFrontends.remove(clientId.getFrontendId());
563         } else {
564             LOG.debug("{}: client {} is not yet known", persistenceId(), clientId);
565         }
566
567         return null;
568     }
569
570     private LeaderFrontendState getFrontend(final ClientIdentifier clientId) throws RequestException {
571         final LeaderFrontendState ret = findFrontend(clientId);
572         if (ret != null) {
573             return ret;
574         }
575
576         // TODO: a dedicated exception would be better, but this is technically true, too
577         throw new OutOfSequenceEnvelopeException(0);
578     }
579
580     private static @NonNull ABIVersion selectVersion(final ConnectClientRequest message) {
581         final Range<ABIVersion> clientRange = Range.closed(message.getMinVersion(), message.getMaxVersion());
582         for (ABIVersion v : SUPPORTED_ABIVERSIONS) {
583             if (clientRange.contains(v)) {
584                 return v;
585             }
586         }
587
588         throw new IllegalArgumentException(String.format(
589             "No common version between backend versions %s and client versions %s", SUPPORTED_ABIVERSIONS,
590             clientRange));
591     }
592
593     @SuppressWarnings("checkstyle:IllegalCatch")
594     private void handleConnectClient(final ConnectClientRequest message) {
595         try {
596             final ClientIdentifier clientId = message.getTarget();
597             final LeaderFrontendState existing = findFrontend(clientId);
598             if (existing != null) {
599                 existing.touch();
600             }
601
602             if (!isLeader() || !isLeaderActive()) {
603                 LOG.info("{}: not currently leader, rejecting request {}. isLeader: {}, isLeaderActive: {},"
604                                 + "isLeadershipTransferInProgress: {}.",
605                         persistenceId(), message, isLeader(), isLeaderActive(), isLeadershipTransferInProgress());
606                 throw new NotLeaderException(getSelf());
607             }
608
609             final ABIVersion selectedVersion = selectVersion(message);
610             final LeaderFrontendState frontend;
611             if (existing == null) {
612                 frontend = new LeaderFrontendState.Enabled(persistenceId(), clientId, store);
613                 knownFrontends.put(clientId.getFrontendId(), frontend);
614                 LOG.debug("{}: created state {} for client {}", persistenceId(), frontend, clientId);
615             } else {
616                 frontend = existing;
617             }
618
619             frontend.reconnect();
620             message.getReplyTo().tell(new ConnectClientSuccess(message.getTarget(), message.getSequence(), getSelf(),
621                 ImmutableList.of(), store.getDataTree(), CLIENT_MAX_MESSAGES).toVersion(selectedVersion),
622                 ActorRef.noSender());
623         } catch (RequestException | RuntimeException e) {
624             message.getReplyTo().tell(new Failure(e), ActorRef.noSender());
625         }
626     }
627
628     private @Nullable RequestSuccess<?, ?> handleRequest(final RequestEnvelope envelope, final long now)
629             throws RequestException {
630         // We are not the leader, hence we want to fail-fast.
631         if (!isLeader() || paused || !isLeaderActive()) {
632             LOG.debug("{}: not currently active leader, rejecting request {}. isLeader: {}, isLeaderActive: {},"
633                             + "isLeadershipTransferInProgress: {}, paused: {}",
634                     persistenceId(), envelope, isLeader(), isLeaderActive(), isLeadershipTransferInProgress(), paused);
635             throw new NotLeaderException(getSelf());
636         }
637
638         final Request<?, ?> request = envelope.getMessage();
639         if (request instanceof TransactionRequest) {
640             final TransactionRequest<?> txReq = (TransactionRequest<?>)request;
641             final ClientIdentifier clientId = txReq.getTarget().getHistoryId().getClientId();
642             return getFrontend(clientId).handleTransactionRequest(txReq, envelope, now);
643         } else if (request instanceof LocalHistoryRequest) {
644             final LocalHistoryRequest<?> lhReq = (LocalHistoryRequest<?>)request;
645             final ClientIdentifier clientId = lhReq.getTarget().getClientId();
646             return getFrontend(clientId).handleLocalHistoryRequest(lhReq, envelope, now);
647         } else {
648             LOG.warn("{}: rejecting unsupported request {}", persistenceId(), request);
649             throw new UnsupportedRequestException(request);
650         }
651     }
652
653     private void handleGetKnownClients() {
654         final ImmutableSet<ClientIdentifier> clients;
655         if (isLeader()) {
656             clients = knownFrontends.values().stream()
657                     .map(LeaderFrontendState::getIdentifier)
658                     .collect(ImmutableSet.toImmutableSet());
659         } else {
660             clients = frontendMetadata.getClients();
661         }
662         sender().tell(new GetKnownClientsReply(clients), self());
663     }
664
665     private boolean hasLeader() {
666         return getLeaderId() != null;
667     }
668
669     final int getPendingTxCommitQueueSize() {
670         return store.getQueueSize();
671     }
672
673     final int getCohortCacheSize() {
674         return commitCoordinator.getCohortCacheSize();
675     }
676
677     @Override
678     protected final Optional<ActorRef> getRoleChangeNotifier() {
679         return roleChangeNotifier;
680     }
681
682     final String getShardName() {
683         return shardName;
684     }
685
686     @Override
687     protected final LeaderStateChanged newLeaderStateChanged(final String memberId, final String leaderId,
688             final short leaderPayloadVersion) {
689         return isLeader() ? new ShardLeaderStateChanged(memberId, leaderId, store.getDataTree(), leaderPayloadVersion)
690                 : new ShardLeaderStateChanged(memberId, leaderId, leaderPayloadVersion);
691     }
692
693     private void onDatastoreContext(final DatastoreContext context) {
694         datastoreContext = verifyNotNull(context);
695
696         setTransactionCommitTimeout();
697
698         setPersistence(datastoreContext.isPersistent());
699
700         updateConfigParams(datastoreContext.getShardRaftConfig());
701     }
702
703     // applyState() will be invoked once consensus is reached on the payload
704     // non-final for mocking
705     void persistPayload(final Identifier id, final Payload payload, final boolean batchHint) {
706         final boolean canSkipPayload = !hasFollowers() && !persistence().isRecoveryApplicable();
707         if (canSkipPayload) {
708             applyState(self(), id, payload);
709         } else {
710             // We are faking the sender
711             persistData(self(), id, payload, batchHint);
712         }
713     }
714
715     private void handleCommitTransaction(final CommitTransaction commit) {
716         final TransactionIdentifier txId = commit.getTransactionId();
717         if (isLeader()) {
718             askProtocolEncountered(txId);
719             commitCoordinator.handleCommit(txId, getSender(), this);
720         } else {
721             ActorSelection leader = getLeader();
722             if (leader == null) {
723                 messageRetrySupport.addMessageToRetry(commit, getSender(), "Could not commit transaction " + txId);
724             } else {
725                 LOG.debug("{}: Forwarding CommitTransaction to leader {}", persistenceId(), leader);
726                 leader.forward(commit, getContext());
727             }
728         }
729     }
730
731     private void handleCanCommitTransaction(final CanCommitTransaction canCommit) {
732         final TransactionIdentifier txId = canCommit.getTransactionId();
733         LOG.debug("{}: Can committing transaction {}", persistenceId(), txId);
734
735         if (isLeader()) {
736             askProtocolEncountered(txId);
737             commitCoordinator.handleCanCommit(txId, getSender(), this);
738         } else {
739             ActorSelection leader = getLeader();
740             if (leader == null) {
741                 messageRetrySupport.addMessageToRetry(canCommit, getSender(),
742                         "Could not canCommit transaction " + txId);
743             } else {
744                 LOG.debug("{}: Forwarding CanCommitTransaction to leader {}", persistenceId(), leader);
745                 leader.forward(canCommit, getContext());
746             }
747         }
748     }
749
750     @SuppressWarnings("checkstyle:IllegalCatch")
751     private void handleBatchedModificationsLocal(final BatchedModifications batched, final ActorRef sender) {
752         askProtocolEncountered(batched.getTransactionId());
753
754         try {
755             commitCoordinator.handleBatchedModifications(batched, sender, this);
756         } catch (Exception e) {
757             LOG.error("{}: Error handling BatchedModifications for Tx {}", persistenceId(),
758                     batched.getTransactionId(), e);
759             sender.tell(new Failure(e), getSelf());
760         }
761     }
762
763     private void handleBatchedModifications(final BatchedModifications batched) {
764         // This message is sent to prepare the modifications transaction directly on the Shard as an
765         // optimization to avoid the extra overhead of a separate ShardTransaction actor. On the last
766         // BatchedModifications message, the caller sets the ready flag in the message indicating
767         // modifications are complete. The reply contains the cohort actor path (this actor) for the caller
768         // to initiate the 3-phase commit. This also avoids the overhead of sending an additional
769         // ReadyTransaction message.
770
771         // If we're not the leader then forward to the leader. This is a safety measure - we shouldn't
772         // normally get here if we're not the leader as the front-end (TransactionProxy) should determine
773         // the primary/leader shard. However with timing and caching on the front-end, there's a small
774         // window where it could have a stale leader during leadership transitions.
775         //
776         boolean isLeaderActive = isLeaderActive();
777         if (isLeader() && isLeaderActive) {
778             handleBatchedModificationsLocal(batched, getSender());
779         } else {
780             ActorSelection leader = getLeader();
781             if (!isLeaderActive || leader == null) {
782                 messageRetrySupport.addMessageToRetry(batched, getSender(),
783                         "Could not process BatchedModifications " + batched.getTransactionId());
784             } else {
785                 // If this is not the first batch and leadership changed in between batched messages,
786                 // we need to reconstruct previous BatchedModifications from the transaction
787                 // DataTreeModification, honoring the max batched modification count, and forward all the
788                 // previous BatchedModifications to the new leader.
789                 Collection<BatchedModifications> newModifications = commitCoordinator
790                         .createForwardedBatchedModifications(batched,
791                                 datastoreContext.getShardBatchedModificationCount());
792
793                 LOG.debug("{}: Forwarding {} BatchedModifications to leader {}", persistenceId(),
794                         newModifications.size(), leader);
795
796                 for (BatchedModifications bm : newModifications) {
797                     leader.forward(bm, getContext());
798                 }
799             }
800         }
801     }
802
803     private boolean failIfIsolatedLeader(final ActorRef sender) {
804         if (isIsolatedLeader()) {
805             sender.tell(new Failure(new NoShardLeaderException(String.format(
806                     "Shard %s was the leader but has lost contact with all of its followers. Either all"
807                     + " other follower nodes are down or this node is isolated by a network partition.",
808                     persistenceId()))), getSelf());
809             return true;
810         }
811
812         return false;
813     }
814
815     protected boolean isIsolatedLeader() {
816         return getRaftState() == RaftState.IsolatedLeader;
817     }
818
819     @SuppressWarnings("checkstyle:IllegalCatch")
820     private void handleReadyLocalTransaction(final ReadyLocalTransaction message) {
821         final TransactionIdentifier txId = message.getTransactionId();
822         LOG.debug("{}: handleReadyLocalTransaction for {}", persistenceId(), txId);
823
824         boolean isLeaderActive = isLeaderActive();
825         if (isLeader() && isLeaderActive) {
826             askProtocolEncountered(txId);
827             try {
828                 commitCoordinator.handleReadyLocalTransaction(message, getSender(), this);
829             } catch (Exception e) {
830                 LOG.error("{}: Error handling ReadyLocalTransaction for Tx {}", persistenceId(), txId, e);
831                 getSender().tell(new Failure(e), getSelf());
832             }
833         } else {
834             ActorSelection leader = getLeader();
835             if (!isLeaderActive || leader == null) {
836                 messageRetrySupport.addMessageToRetry(message, getSender(),
837                         "Could not process ready local transaction " + txId);
838             } else {
839                 LOG.debug("{}: Forwarding ReadyLocalTransaction to leader {}", persistenceId(), leader);
840                 message.setRemoteVersion(getCurrentBehavior().getLeaderPayloadVersion());
841                 leader.forward(message, getContext());
842             }
843         }
844     }
845
846     private void handleForwardedReadyTransaction(final ForwardedReadyTransaction forwardedReady) {
847         LOG.debug("{}: handleForwardedReadyTransaction for {}", persistenceId(), forwardedReady.getTransactionId());
848
849         boolean isLeaderActive = isLeaderActive();
850         if (isLeader() && isLeaderActive) {
851             askProtocolEncountered(forwardedReady.getTransactionId());
852             commitCoordinator.handleForwardedReadyTransaction(forwardedReady, getSender(), this);
853         } else {
854             ActorSelection leader = getLeader();
855             if (!isLeaderActive || leader == null) {
856                 messageRetrySupport.addMessageToRetry(forwardedReady, getSender(),
857                         "Could not process forwarded ready transaction " + forwardedReady.getTransactionId());
858             } else {
859                 LOG.debug("{}: Forwarding ForwardedReadyTransaction to leader {}", persistenceId(), leader);
860
861                 ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(forwardedReady.getTransactionId(),
862                         forwardedReady.getTransaction().getSnapshot(), forwardedReady.isDoImmediateCommit(),
863                         forwardedReady.getParticipatingShardNames());
864                 readyLocal.setRemoteVersion(getCurrentBehavior().getLeaderPayloadVersion());
865                 leader.forward(readyLocal, getContext());
866             }
867         }
868     }
869
870     private void handleAbortTransaction(final AbortTransaction abort) {
871         final TransactionIdentifier transactionId = abort.getTransactionId();
872         askProtocolEncountered(transactionId);
873         doAbortTransaction(transactionId, getSender());
874     }
875
876     final void doAbortTransaction(final Identifier transactionID, final ActorRef sender) {
877         commitCoordinator.handleAbort(transactionID, sender, this);
878     }
879
880     private void handleCreateTransaction(final Object message) {
881         if (isLeader()) {
882             createTransaction(CreateTransaction.fromSerializable(message));
883         } else if (getLeader() != null) {
884             getLeader().forward(message, getContext());
885         } else {
886             getSender().tell(new Failure(new NoShardLeaderException(
887                     "Could not create a shard transaction", persistenceId())), getSelf());
888         }
889     }
890
891     private void closeTransactionChain(final CloseTransactionChain closeTransactionChain) {
892         if (isLeader()) {
893             final LocalHistoryIdentifier id = closeTransactionChain.getIdentifier();
894             askProtocolEncountered(id.getClientId());
895             store.closeTransactionChain(id);
896         } else if (getLeader() != null) {
897             getLeader().forward(closeTransactionChain, getContext());
898         } else {
899             LOG.warn("{}: Could not close transaction {}", persistenceId(), closeTransactionChain.getIdentifier());
900         }
901     }
902
903     @SuppressWarnings("checkstyle:IllegalCatch")
904     private void createTransaction(final CreateTransaction createTransaction) {
905         askProtocolEncountered(createTransaction.getTransactionId());
906
907         try {
908             if (TransactionType.fromInt(createTransaction.getTransactionType()) != TransactionType.READ_ONLY
909                     && failIfIsolatedLeader(getSender())) {
910                 return;
911             }
912
913             ActorRef transactionActor = createTransaction(createTransaction.getTransactionType(),
914                 createTransaction.getTransactionId());
915
916             getSender().tell(new CreateTransactionReply(Serialization.serializedActorPath(transactionActor),
917                     createTransaction.getTransactionId(), createTransaction.getVersion()).toSerializable(), getSelf());
918         } catch (Exception e) {
919             getSender().tell(new Failure(e), getSelf());
920         }
921     }
922
923     private ActorRef createTransaction(final int transactionType, final TransactionIdentifier transactionId) {
924         LOG.debug("{}: Creating transaction : {} ", persistenceId(), transactionId);
925         return transactionActorFactory.newShardTransaction(TransactionType.fromInt(transactionType),
926             transactionId);
927     }
928
929     // Called on leader only
930     private void askProtocolEncountered(final TransactionIdentifier transactionId) {
931         askProtocolEncountered(transactionId.getHistoryId().getClientId());
932     }
933
934     // Called on leader only
935     private void askProtocolEncountered(final ClientIdentifier clientId) {
936         final FrontendIdentifier frontend = clientId.getFrontendId();
937         final LeaderFrontendState state = knownFrontends.get(frontend);
938         if (!(state instanceof LeaderFrontendState.Disabled)) {
939             LOG.debug("{}: encountered ask-based client {}, disabling transaction tracking", persistenceId(), clientId);
940             if (knownFrontends.isEmpty()) {
941                 knownFrontends = new HashMap<>();
942             }
943             knownFrontends.put(frontend, new LeaderFrontendState.Disabled(persistenceId(), clientId, getDataStore()));
944
945             persistPayload(clientId, DisableTrackingPayload.create(clientId,
946                 datastoreContext.getInitialPayloadSerializedBufferCapacity()), false);
947         }
948     }
949
950     private void updateSchemaContext(final UpdateSchemaContext message) {
951         updateSchemaContext(message.getEffectiveModelContext());
952     }
953
954     @VisibleForTesting
955     void updateSchemaContext(final @NonNull EffectiveModelContext schemaContext) {
956         store.updateSchemaContext(schemaContext);
957     }
958
959     private boolean isMetricsCaptureEnabled() {
960         CommonConfig config = new CommonConfig(getContext().system().settings().config());
961         return config.isMetricCaptureEnabled();
962     }
963
964     @Override
965     protected final RaftActorSnapshotCohort getRaftActorSnapshotCohort() {
966         return snapshotCohort;
967     }
968
969     @Override
970     protected final RaftActorRecoveryCohort getRaftActorRecoveryCohort() {
971         if (restoreFromSnapshot == null) {
972             return ShardRecoveryCoordinator.create(store, persistenceId(), LOG);
973         }
974
975         return ShardRecoveryCoordinator.forSnapshot(store, persistenceId(), LOG, restoreFromSnapshot.getSnapshot());
976     }
977
978     @Override
979     // non-final for testing
980     protected void onRecoveryComplete() {
981         restoreFromSnapshot = null;
982
983         //notify shard manager
984         getContext().parent().tell(new ActorInitialized(), getSelf());
985
986         // Being paranoid here - this method should only be called once but just in case...
987         if (txCommitTimeoutCheckSchedule == null) {
988             // Schedule a message to be periodically sent to check if the current in-progress
989             // transaction should be expired and aborted.
990             FiniteDuration period = FiniteDuration.create(transactionCommitTimeout / 3, TimeUnit.MILLISECONDS);
991             txCommitTimeoutCheckSchedule = getContext().system().scheduler().schedule(
992                     period, period, getSelf(),
993                     TX_COMMIT_TIMEOUT_CHECK_MESSAGE, getContext().dispatcher(), ActorRef.noSender());
994         }
995     }
996
997     @Override
998     protected final void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) {
999         if (data instanceof Payload) {
1000             if (data instanceof DisableTrackingPayload) {
1001                 disableTracking((DisableTrackingPayload) data);
1002                 return;
1003             }
1004
1005             try {
1006                 store.applyReplicatedPayload(identifier, (Payload)data);
1007             } catch (DataValidationFailedException | IOException e) {
1008                 LOG.error("{}: Error applying replica {}", persistenceId(), identifier, e);
1009             }
1010         } else {
1011             LOG.error("{}: Unknown state for {} received {}", persistenceId(), identifier, data);
1012         }
1013     }
1014
1015     @Override
1016     protected final void onStateChanged() {
1017         boolean isLeader = isLeader();
1018         boolean hasLeader = hasLeader();
1019         treeChangeSupport.onLeadershipChange(isLeader, hasLeader);
1020
1021         // If this actor is no longer the leader close all the transaction chains
1022         if (!isLeader) {
1023             if (LOG.isDebugEnabled()) {
1024                 LOG.debug(
1025                     "{}: onStateChanged: Closing all transaction chains because shard {} is no longer the leader",
1026                     persistenceId(), getId());
1027             }
1028
1029             paused = false;
1030             store.purgeLeaderState();
1031         }
1032
1033         if (hasLeader && !isIsolatedLeader()) {
1034             messageRetrySupport.retryMessages();
1035         }
1036     }
1037
1038     @Override
1039     protected final void onLeaderChanged(final String oldLeader, final String newLeader) {
1040         shardMBean.incrementLeadershipChangeCount();
1041         paused = false;
1042
1043         if (!isLeader()) {
1044             if (!knownFrontends.isEmpty()) {
1045                 LOG.debug("{}: removing frontend state for {}", persistenceId(), knownFrontends.keySet());
1046                 knownFrontends = ImmutableMap.of();
1047             }
1048
1049             requestMessageAssembler.close();
1050
1051             if (!hasLeader()) {
1052                 // No leader anywhere, nothing else to do
1053                 return;
1054             }
1055
1056             // Another leader was elected. If we were the previous leader and had pending transactions, convert
1057             // them to transaction messages and send to the new leader.
1058             ActorSelection leader = getLeader();
1059             if (leader != null) {
1060                 // Clears all pending transactions and converts them to messages to be forwarded to a new leader.
1061                 Collection<?> messagesToForward = commitCoordinator.convertPendingTransactionsToMessages(
1062                     datastoreContext.getShardBatchedModificationCount());
1063
1064                 if (!messagesToForward.isEmpty()) {
1065                     LOG.debug("{}: Forwarding {} pending transaction messages to leader {}", persistenceId(),
1066                             messagesToForward.size(), leader);
1067
1068                     for (Object message : messagesToForward) {
1069                         LOG.debug("{}: Forwarding pending transaction message {}", persistenceId(), message);
1070
1071                         leader.tell(message, self());
1072                     }
1073                 }
1074             } else {
1075                 commitCoordinator.abortPendingTransactions("The transacton was aborted due to inflight leadership "
1076                         + "change and the leader address isn't available.", this);
1077             }
1078         } else {
1079             // We have become the leader, we need to reconstruct frontend state
1080             knownFrontends = verifyNotNull(frontendMetadata.toLeaderState(this));
1081             LOG.debug("{}: became leader with frontend state for {}", persistenceId(), knownFrontends.keySet());
1082         }
1083
1084         if (!isIsolatedLeader()) {
1085             messageRetrySupport.retryMessages();
1086         }
1087     }
1088
1089     @Override
1090     protected final void pauseLeader(final Runnable operation) {
1091         LOG.debug("{}: In pauseLeader, operation: {}", persistenceId(), operation);
1092         paused = true;
1093
1094         // Tell-based protocol can replay transaction state, so it is safe to blow it up when we are paused.
1095         if (datastoreContext.isUseTellBasedProtocol()) {
1096             knownFrontends.values().forEach(LeaderFrontendState::retire);
1097             knownFrontends = ImmutableMap.of();
1098         }
1099
1100         store.setRunOnPendingTransactionsComplete(operation);
1101     }
1102
1103     @Override
1104     protected final void unpauseLeader() {
1105         LOG.debug("{}: In unpauseLeader", persistenceId());
1106         paused = false;
1107
1108         store.setRunOnPendingTransactionsComplete(null);
1109
1110         // Restore tell-based protocol state as if we were becoming the leader
1111         knownFrontends = verifyNotNull(frontendMetadata.toLeaderState(this));
1112     }
1113
1114     @Override
1115     protected final OnDemandRaftState.AbstractBuilder<?, ?> newOnDemandRaftStateBuilder() {
1116         return OnDemandShardState.newBuilder()
1117             .treeChangeListenerActors(treeChangeSupport.getListenerActors())
1118             .commitCohortActors(store.getCohortActors());
1119     }
1120
1121     @Override
1122     public final String persistenceId() {
1123         return name;
1124     }
1125
1126     @Override
1127     public final String journalPluginId() {
1128         // This method may be invoked from super constructor (wonderful), hence we also need to handle the case of
1129         // the field being uninitialized because our constructor is not finished.
1130         if (datastoreContext != null && !datastoreContext.isPersistent()) {
1131             return NON_PERSISTENT_JOURNAL_ID;
1132         }
1133         return super.journalPluginId();
1134     }
1135
1136     @VisibleForTesting
1137     final ShardCommitCoordinator getCommitCoordinator() {
1138         return commitCoordinator;
1139     }
1140
1141     // non-final for mocking
1142     DatastoreContext getDatastoreContext() {
1143         return datastoreContext;
1144     }
1145
1146     @VisibleForTesting
1147     final ShardDataTree getDataStore() {
1148         return store;
1149     }
1150
1151     @VisibleForTesting
1152     // non-final for mocking
1153     ShardStats getShardMBean() {
1154         return shardMBean;
1155     }
1156
1157     public static Builder builder() {
1158         return new Builder();
1159     }
1160
1161     public abstract static class AbstractBuilder<T extends AbstractBuilder<T, S>, S extends Shard> {
1162         private final Class<? extends S> shardClass;
1163         private ShardIdentifier id;
1164         private Map<String, String> peerAddresses = Collections.emptyMap();
1165         private DatastoreContext datastoreContext;
1166         private EffectiveModelContextProvider schemaContextProvider;
1167         private DatastoreSnapshot.ShardSnapshot restoreFromSnapshot;
1168         private DataTree dataTree;
1169
1170         private volatile boolean sealed;
1171
1172         AbstractBuilder(final Class<? extends S> shardClass) {
1173             this.shardClass = shardClass;
1174         }
1175
1176         final void checkSealed() {
1177             checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
1178         }
1179
1180         @SuppressWarnings("unchecked")
1181         private T self() {
1182             return (T) this;
1183         }
1184
1185         public T id(final ShardIdentifier newId) {
1186             checkSealed();
1187             this.id = newId;
1188             return self();
1189         }
1190
1191         public T peerAddresses(final Map<String, String> newPeerAddresses) {
1192             checkSealed();
1193             this.peerAddresses = newPeerAddresses;
1194             return self();
1195         }
1196
1197         public T datastoreContext(final DatastoreContext newDatastoreContext) {
1198             checkSealed();
1199             this.datastoreContext = newDatastoreContext;
1200             return self();
1201         }
1202
1203         public T schemaContextProvider(final EffectiveModelContextProvider newSchemaContextProvider) {
1204             checkSealed();
1205             this.schemaContextProvider = requireNonNull(newSchemaContextProvider);
1206             return self();
1207         }
1208
1209         public T restoreFromSnapshot(final DatastoreSnapshot.ShardSnapshot newRestoreFromSnapshot) {
1210             checkSealed();
1211             this.restoreFromSnapshot = newRestoreFromSnapshot;
1212             return self();
1213         }
1214
1215         public T dataTree(final DataTree newDataTree) {
1216             checkSealed();
1217             this.dataTree = newDataTree;
1218             return self();
1219         }
1220
1221         public ShardIdentifier getId() {
1222             return id;
1223         }
1224
1225         public Map<String, String> getPeerAddresses() {
1226             return peerAddresses;
1227         }
1228
1229         public DatastoreContext getDatastoreContext() {
1230             return datastoreContext;
1231         }
1232
1233         public EffectiveModelContext getSchemaContext() {
1234             return verifyNotNull(schemaContextProvider.getEffectiveModelContext());
1235         }
1236
1237         public DatastoreSnapshot.ShardSnapshot getRestoreFromSnapshot() {
1238             return restoreFromSnapshot;
1239         }
1240
1241         public DataTree getDataTree() {
1242             return dataTree;
1243         }
1244
1245         public TreeType getTreeType() {
1246             switch (datastoreContext.getLogicalStoreType()) {
1247                 case CONFIGURATION:
1248                     return TreeType.CONFIGURATION;
1249                 case OPERATIONAL:
1250                     return TreeType.OPERATIONAL;
1251                 default:
1252                     throw new IllegalStateException("Unhandled logical store type "
1253                             + datastoreContext.getLogicalStoreType());
1254             }
1255         }
1256
1257         protected void verify() {
1258             requireNonNull(id, "id should not be null");
1259             requireNonNull(peerAddresses, "peerAddresses should not be null");
1260             requireNonNull(datastoreContext, "dataStoreContext should not be null");
1261             requireNonNull(schemaContextProvider, "schemaContextProvider should not be null");
1262         }
1263
1264         public Props props() {
1265             sealed = true;
1266             verify();
1267             return Props.create(shardClass, this);
1268         }
1269     }
1270
1271     public static class Builder extends AbstractBuilder<Builder, Shard> {
1272         Builder() {
1273             this(Shard.class);
1274         }
1275
1276         Builder(final Class<? extends Shard> shardClass) {
1277             super(shardClass);
1278         }
1279     }
1280
1281     Ticker ticker() {
1282         return Ticker.systemTicker();
1283     }
1284
1285     void scheduleNextPendingTransaction() {
1286         self().tell(RESUME_NEXT_PENDING_TRANSACTION, ActorRef.noSender());
1287     }
1288 }