2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static akka.pattern.Patterns.ask;
12 import akka.actor.ActorPath;
13 import akka.actor.ActorRef;
14 import akka.actor.Address;
15 import akka.actor.Cancellable;
16 import akka.actor.OneForOneStrategy;
17 import akka.actor.PoisonPill;
18 import akka.actor.Props;
19 import akka.actor.SupervisorStrategy;
20 import akka.cluster.ClusterEvent;
21 import akka.dispatch.OnComplete;
22 import akka.japi.Function;
23 import akka.persistence.RecoveryCompleted;
24 import akka.persistence.SaveSnapshotFailure;
25 import akka.persistence.SaveSnapshotSuccess;
26 import akka.persistence.SnapshotOffer;
27 import akka.serialization.Serialization;
28 import akka.util.Timeout;
29 import com.google.common.annotations.VisibleForTesting;
30 import com.google.common.base.Objects;
31 import com.google.common.base.Optional;
32 import com.google.common.base.Preconditions;
33 import com.google.common.base.Strings;
34 import com.google.common.base.Supplier;
35 import com.google.common.collect.Sets;
36 import java.io.Serializable;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
46 import java.util.concurrent.CountDownLatch;
47 import java.util.concurrent.TimeUnit;
48 import java.util.concurrent.TimeoutException;
49 import javax.annotation.Nullable;
50 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
51 import org.opendaylight.controller.cluster.datastore.config.Configuration;
52 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
53 import org.opendaylight.controller.cluster.datastore.exceptions.AlreadyExistsException;
54 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
55 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
56 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
57 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
58 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
59 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
60 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
61 import org.opendaylight.controller.cluster.datastore.messages.AddShardReplica;
62 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
63 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
64 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
65 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
66 import org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound;
67 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
68 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
69 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
70 import org.opendaylight.controller.cluster.datastore.messages.PeerDown;
71 import org.opendaylight.controller.cluster.datastore.messages.PeerUp;
72 import org.opendaylight.controller.cluster.datastore.messages.RemoteFindPrimary;
73 import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound;
74 import org.opendaylight.controller.cluster.datastore.messages.RemoveShardReplica;
75 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
76 import org.opendaylight.controller.cluster.datastore.messages.SwitchShardBehavior;
77 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
78 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
79 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
80 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
81 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
82 import org.opendaylight.controller.cluster.raft.RaftState;
83 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
84 import org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior;
85 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot;
86 import org.opendaylight.controller.cluster.raft.messages.AddServer;
87 import org.opendaylight.controller.cluster.raft.messages.AddServerReply;
88 import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus;
89 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
90 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
91 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
92 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
93 import org.slf4j.Logger;
94 import org.slf4j.LoggerFactory;
95 import scala.concurrent.Future;
96 import scala.concurrent.duration.Duration;
97 import scala.concurrent.duration.FiniteDuration;
100 * The ShardManager has the following jobs,
102 * <li> Create all the local shard replicas that belong on this cluster member
103 * <li> Find the address of the local shard
104 * <li> Find the primary replica for any given shard
105 * <li> Monitor the cluster members and store their addresses
108 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
110 private static final Logger LOG = LoggerFactory.getLogger(ShardManager.class);
112 // Stores a mapping between a shard name and it's corresponding information
113 // Shard names look like inventory, topology etc and are as specified in
115 private final Map<String, ShardInformation> localShards = new HashMap<>();
117 // The type of a ShardManager reflects the type of the datastore itself
118 // A data store could be of type config/operational
119 private final String type;
121 private final ClusterWrapper cluster;
123 private final Configuration configuration;
125 private final String shardDispatcherPath;
127 private final ShardManagerInfo mBean;
129 private DatastoreContextFactory datastoreContextFactory;
131 private final CountDownLatch waitTillReadyCountdownLatch;
133 private final PrimaryShardInfoFutureCache primaryShardInfoCache;
135 private final ShardPeerAddressResolver peerAddressResolver;
137 private SchemaContext schemaContext;
139 private DatastoreSnapshot restoreFromSnapshot;
141 private final Set<String> shardReplicaOperationsInProgress = new HashSet<>();
143 private final String persistenceId;
147 protected ShardManager(Builder builder) {
149 this.cluster = builder.cluster;
150 this.configuration = builder.configuration;
151 this.datastoreContextFactory = builder.datastoreContextFactory;
152 this.type = builder.datastoreContextFactory.getBaseDatastoreContext().getDataStoreType();
153 this.shardDispatcherPath =
154 new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
155 this.waitTillReadyCountdownLatch = builder.waitTillReadyCountdownLatch;
156 this.primaryShardInfoCache = builder.primaryShardInfoCache;
157 this.restoreFromSnapshot = builder.restoreFromSnapshot;
159 String possiblePersistenceId = datastoreContextFactory.getBaseDatastoreContext().getShardManagerPersistenceId();
160 persistenceId = possiblePersistenceId != null ? possiblePersistenceId : "shard-manager-" + type;
162 peerAddressResolver = new ShardPeerAddressResolver(type, cluster.getCurrentMemberName());
164 // Subscribe this actor to cluster member events
165 cluster.subscribeToMemberEvents(getSelf());
167 List<String> localShardActorNames = new ArrayList<>();
168 mBean = ShardManagerInfo.createShardManagerMBean(cluster.getCurrentMemberName(),
169 "shard-manager-" + this.type,
170 datastoreContextFactory.getBaseDatastoreContext().getDataStoreMXBeanType(),
171 localShardActorNames);
172 mBean.setShardManager(this);
176 public void postStop() {
177 LOG.info("Stopping ShardManager");
179 mBean.unregisterMBean();
183 public void handleCommand(Object message) throws Exception {
184 if (message instanceof FindPrimary) {
185 findPrimary((FindPrimary)message);
186 } else if(message instanceof FindLocalShard){
187 findLocalShard((FindLocalShard) message);
188 } else if (message instanceof UpdateSchemaContext) {
189 updateSchemaContext(message);
190 } else if(message instanceof ActorInitialized) {
191 onActorInitialized(message);
192 } else if (message instanceof ClusterEvent.MemberUp){
193 memberUp((ClusterEvent.MemberUp) message);
194 } else if (message instanceof ClusterEvent.MemberExited){
195 memberExited((ClusterEvent.MemberExited) message);
196 } else if(message instanceof ClusterEvent.MemberRemoved) {
197 memberRemoved((ClusterEvent.MemberRemoved) message);
198 } else if(message instanceof ClusterEvent.UnreachableMember) {
199 memberUnreachable((ClusterEvent.UnreachableMember)message);
200 } else if(message instanceof ClusterEvent.ReachableMember) {
201 memberReachable((ClusterEvent.ReachableMember) message);
202 } else if(message instanceof DatastoreContextFactory) {
203 onDatastoreContextFactory((DatastoreContextFactory)message);
204 } else if(message instanceof RoleChangeNotification) {
205 onRoleChangeNotification((RoleChangeNotification) message);
206 } else if(message instanceof FollowerInitialSyncUpStatus){
207 onFollowerInitialSyncStatus((FollowerInitialSyncUpStatus) message);
208 } else if(message instanceof ShardNotInitializedTimeout) {
209 onShardNotInitializedTimeout((ShardNotInitializedTimeout)message);
210 } else if(message instanceof ShardLeaderStateChanged) {
211 onLeaderStateChanged((ShardLeaderStateChanged) message);
212 } else if(message instanceof SwitchShardBehavior){
213 onSwitchShardBehavior((SwitchShardBehavior) message);
214 } else if(message instanceof CreateShard) {
215 onCreateShard((CreateShard)message);
216 } else if(message instanceof AddShardReplica){
217 onAddShardReplica((AddShardReplica)message);
218 } else if(message instanceof ForwardedAddServerReply) {
219 ForwardedAddServerReply msg = (ForwardedAddServerReply)message;
220 onAddServerReply(msg.shardInfo, msg.addServerReply, getSender(), msg.leaderPath,
221 msg.removeShardOnFailure);
222 } else if(message instanceof ForwardedAddServerFailure) {
223 ForwardedAddServerFailure msg = (ForwardedAddServerFailure)message;
224 onAddServerFailure(msg.shardName, msg.failureMessage, msg.failure, getSender(), msg.removeShardOnFailure);
225 } else if(message instanceof ForwardedAddServerPrimaryShardFound) {
226 ForwardedAddServerPrimaryShardFound msg = (ForwardedAddServerPrimaryShardFound)message;
227 addShard(msg.shardName, msg.primaryFound, getSender());
228 } else if(message instanceof RemoveShardReplica){
229 onRemoveShardReplica((RemoveShardReplica)message);
230 } else if(message instanceof GetSnapshot) {
232 } else if(message instanceof ServerRemoved){
233 onShardReplicaRemoved((ServerRemoved) message);
234 } else if (message instanceof SaveSnapshotSuccess) {
235 LOG.debug("{} saved ShardManager snapshot successfully", persistenceId());
236 } else if (message instanceof SaveSnapshotFailure) {
237 LOG.error ("{}: SaveSnapshotFailure received for saving snapshot of shards",
238 persistenceId(), ((SaveSnapshotFailure)message).cause());
240 unknownMessage(message);
244 private void onShardReplicaRemoved(ServerRemoved message) {
245 final ShardIdentifier shardId = new ShardIdentifier.Builder().fromShardIdString(message.getServerId()).build();
246 final ShardInformation shardInformation = localShards.remove(shardId.getShardName());
247 if(shardInformation == null) {
248 LOG.debug("{} : Shard replica {} is not present in list", persistenceId(), shardId.toString());
250 } else if(shardInformation.getActor() != null) {
251 LOG.debug("{} : Sending PoisonPill to Shard actor {}", persistenceId(), shardInformation.getActor());
252 shardInformation.getActor().tell(PoisonPill.getInstance(), self());
254 LOG.debug("{} : Local Shard replica for shard {} has been removed", persistenceId(), shardId.getShardName());
258 private void onGetSnapshot() {
259 LOG.debug("{}: onGetSnapshot", persistenceId());
261 List<String> notInitialized = null;
262 for(ShardInformation shardInfo: localShards.values()) {
263 if(!shardInfo.isShardInitialized()) {
264 if(notInitialized == null) {
265 notInitialized = new ArrayList<>();
268 notInitialized.add(shardInfo.getShardName());
272 if(notInitialized != null) {
273 getSender().tell(new akka.actor.Status.Failure(new IllegalStateException(String.format(
274 "%d shard(s) %s are not initialized", notInitialized.size(), notInitialized))), getSelf());
278 byte[] shardManagerSnapshot = null;
279 ActorRef replyActor = getContext().actorOf(ShardManagerGetSnapshotReplyActor.props(
280 new ArrayList<>(localShards.keySet()), type, shardManagerSnapshot , getSender(), persistenceId(),
281 datastoreContextFactory.getBaseDatastoreContext().getShardInitializationTimeout().duration()));
283 for(ShardInformation shardInfo: localShards.values()) {
284 shardInfo.getActor().tell(GetSnapshot.INSTANCE, replyActor);
288 private void onCreateShard(CreateShard createShard) {
291 String shardName = createShard.getModuleShardConfig().getShardName();
292 if(localShards.containsKey(shardName)) {
293 reply = new akka.actor.Status.Success(String.format("Shard with name %s already exists", shardName));
295 doCreateShard(createShard);
296 reply = new akka.actor.Status.Success(null);
298 } catch (Exception e) {
299 LOG.error("onCreateShard failed", e);
300 reply = new akka.actor.Status.Failure(e);
303 if(getSender() != null && !getContext().system().deadLetters().equals(getSender())) {
304 getSender().tell(reply, getSelf());
308 private void doCreateShard(CreateShard createShard) {
309 ModuleShardConfiguration moduleShardConfig = createShard.getModuleShardConfig();
310 String shardName = moduleShardConfig.getShardName();
312 configuration.addModuleShardConfiguration(moduleShardConfig);
314 DatastoreContext shardDatastoreContext = createShard.getDatastoreContext();
315 if(shardDatastoreContext == null) {
316 shardDatastoreContext = newShardDatastoreContext(shardName);
318 shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).shardPeerAddressResolver(
319 peerAddressResolver).build();
322 ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
324 Map<String, String> peerAddresses;
325 boolean isActiveMember;
326 if(configuration.getMembersFromShardName(shardName).contains(cluster.getCurrentMemberName())) {
327 peerAddresses = getPeerAddresses(shardName);
328 isActiveMember = true;
330 // The local member is not in the given shard member configuration. In this case we'll create
331 // the shard with no peers and with elections disabled so it stays as follower. A
332 // subsequent AddServer request will be needed to make it an active member.
333 isActiveMember = false;
334 peerAddresses = Collections.emptyMap();
335 shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).
336 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).build();
339 LOG.debug("onCreateShard: shardId: {}, memberNames: {}. peerAddresses: {}", shardId,
340 moduleShardConfig.getShardMemberNames(), peerAddresses);
342 ShardInformation info = new ShardInformation(shardName, shardId, peerAddresses,
343 shardDatastoreContext, createShard.getShardBuilder(), peerAddressResolver);
344 info.setActiveMember(isActiveMember);
345 localShards.put(info.getShardName(), info);
347 mBean.addLocalShard(shardId.toString());
349 if(schemaContext != null) {
350 info.setActor(newShardActor(schemaContext, info));
354 private DatastoreContext.Builder newShardDatastoreContextBuilder(String shardName) {
355 return DatastoreContext.newBuilderFrom(datastoreContextFactory.getShardDatastoreContext(shardName)).
356 shardPeerAddressResolver(peerAddressResolver);
359 private DatastoreContext newShardDatastoreContext(String shardName) {
360 return newShardDatastoreContextBuilder(shardName).build();
363 private void checkReady(){
364 if (isReadyWithLeaderId()) {
365 LOG.info("{}: All Shards are ready - data store {} is ready, available count is {}",
366 persistenceId(), type, waitTillReadyCountdownLatch.getCount());
368 waitTillReadyCountdownLatch.countDown();
372 private void onLeaderStateChanged(ShardLeaderStateChanged leaderStateChanged) {
373 LOG.info("{}: Received LeaderStateChanged message: {}", persistenceId(), leaderStateChanged);
375 ShardInformation shardInformation = findShardInformation(leaderStateChanged.getMemberId());
376 if(shardInformation != null) {
377 shardInformation.setLocalDataTree(leaderStateChanged.getLocalShardDataTree());
378 shardInformation.setLeaderVersion(leaderStateChanged.getLeaderPayloadVersion());
379 if(shardInformation.setLeaderId(leaderStateChanged.getLeaderId())) {
380 primaryShardInfoCache.remove(shardInformation.getShardName());
385 LOG.debug("No shard found with member Id {}", leaderStateChanged.getMemberId());
389 private void onShardNotInitializedTimeout(ShardNotInitializedTimeout message) {
390 ShardInformation shardInfo = message.getShardInfo();
392 LOG.debug("{}: Received ShardNotInitializedTimeout message for shard {}", persistenceId(),
393 shardInfo.getShardName());
395 shardInfo.removeOnShardInitialized(message.getOnShardInitialized());
397 if(!shardInfo.isShardInitialized()) {
398 LOG.debug("{}: Returning NotInitializedException for shard {}", persistenceId(), shardInfo.getShardName());
399 message.getSender().tell(createNotInitializedException(shardInfo.shardId), getSelf());
401 LOG.debug("{}: Returning NoShardLeaderException for shard {}", persistenceId(), shardInfo.getShardName());
402 message.getSender().tell(createNoShardLeaderException(shardInfo.shardId), getSelf());
406 private void onFollowerInitialSyncStatus(FollowerInitialSyncUpStatus status) {
407 LOG.info("{} Received follower initial sync status for {} status sync done {}", persistenceId(),
408 status.getName(), status.isInitialSyncDone());
410 ShardInformation shardInformation = findShardInformation(status.getName());
412 if(shardInformation != null) {
413 shardInformation.setFollowerSyncStatus(status.isInitialSyncDone());
415 mBean.setSyncStatus(isInSync());
420 private void onRoleChangeNotification(RoleChangeNotification roleChanged) {
421 LOG.info("{}: Received role changed for {} from {} to {}", persistenceId(), roleChanged.getMemberId(),
422 roleChanged.getOldRole(), roleChanged.getNewRole());
424 ShardInformation shardInformation = findShardInformation(roleChanged.getMemberId());
425 if(shardInformation != null) {
426 shardInformation.setRole(roleChanged.getNewRole());
428 mBean.setSyncStatus(isInSync());
433 private ShardInformation findShardInformation(String memberId) {
434 for(ShardInformation info : localShards.values()){
435 if(info.getShardId().toString().equals(memberId)){
443 private boolean isReadyWithLeaderId() {
444 boolean isReady = true;
445 for (ShardInformation info : localShards.values()) {
446 if(!info.isShardReadyWithLeaderId()){
454 private boolean isInSync(){
455 for (ShardInformation info : localShards.values()) {
456 if(!info.isInSync()){
463 private void onActorInitialized(Object message) {
464 final ActorRef sender = getSender();
466 if (sender == null) {
467 return; //why is a non-actor sending this message? Just ignore.
470 String actorName = sender.path().name();
471 //find shard name from actor name; actor name is stringified shardId
472 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
474 if (shardId.getShardName() == null) {
478 markShardAsInitialized(shardId.getShardName());
481 private void markShardAsInitialized(String shardName) {
482 LOG.debug("{}: Initializing shard [{}]", persistenceId(), shardName);
484 ShardInformation shardInformation = localShards.get(shardName);
485 if (shardInformation != null) {
486 shardInformation.setActorInitialized();
488 shardInformation.getActor().tell(new RegisterRoleChangeListener(), self());
493 protected void handleRecover(Object message) throws Exception {
494 if (message instanceof RecoveryCompleted) {
495 LOG.info("Recovery complete : {}", persistenceId());
497 // We no longer persist SchemaContext modules so delete all the prior messages from the akka
498 // journal on upgrade from Helium.
499 deleteMessages(lastSequenceNr());
501 } else if (message instanceof SnapshotOffer) {
502 handleShardRecovery((SnapshotOffer) message);
506 private void findLocalShard(FindLocalShard message) {
507 final ShardInformation shardInformation = localShards.get(message.getShardName());
509 if(shardInformation == null){
510 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
514 sendResponse(shardInformation, message.isWaitUntilInitialized(), false, new Supplier<Object>() {
516 public Object get() {
517 return new LocalShardFound(shardInformation.getActor());
522 private void sendResponse(ShardInformation shardInformation, boolean doWait,
523 boolean wantShardReady, final Supplier<Object> messageSupplier) {
524 if (!shardInformation.isShardInitialized() || (wantShardReady && !shardInformation.isShardReadyWithLeaderId())) {
526 final ActorRef sender = getSender();
527 final ActorRef self = self();
529 Runnable replyRunnable = new Runnable() {
532 sender.tell(messageSupplier.get(), self);
536 OnShardInitialized onShardInitialized = wantShardReady ? new OnShardReady(replyRunnable) :
537 new OnShardInitialized(replyRunnable);
539 shardInformation.addOnShardInitialized(onShardInitialized);
541 FiniteDuration timeout = shardInformation.getDatastoreContext().getShardInitializationTimeout().duration();
542 if(shardInformation.isShardInitialized()) {
543 // If the shard is already initialized then we'll wait enough time for the shard to
544 // elect a leader, ie 2 times the election timeout.
545 timeout = FiniteDuration.create(shardInformation.getDatastoreContext().getShardRaftConfig()
546 .getElectionTimeOutInterval().toMillis() * 2, TimeUnit.MILLISECONDS);
549 LOG.debug("{}: Scheduling {} ms timer to wait for shard {}", persistenceId(), timeout.toMillis(),
550 shardInformation.getShardName());
552 Cancellable timeoutSchedule = getContext().system().scheduler().scheduleOnce(
554 new ShardNotInitializedTimeout(shardInformation, onShardInitialized, sender),
555 getContext().dispatcher(), getSelf());
557 onShardInitialized.setTimeoutSchedule(timeoutSchedule);
559 } else if (!shardInformation.isShardInitialized()) {
560 LOG.debug("{}: Returning NotInitializedException for shard {}", persistenceId(),
561 shardInformation.getShardName());
562 getSender().tell(createNotInitializedException(shardInformation.shardId), getSelf());
564 LOG.debug("{}: Returning NoShardLeaderException for shard {}", persistenceId(),
565 shardInformation.getShardName());
566 getSender().tell(createNoShardLeaderException(shardInformation.shardId), getSelf());
572 getSender().tell(messageSupplier.get(), getSelf());
575 private static NoShardLeaderException createNoShardLeaderException(ShardIdentifier shardId) {
576 return new NoShardLeaderException(null, shardId.toString());
579 private static NotInitializedException createNotInitializedException(ShardIdentifier shardId) {
580 return new NotInitializedException(String.format(
581 "Found primary shard %s but it's not initialized yet. Please try again later", shardId));
584 private void memberRemoved(ClusterEvent.MemberRemoved message) {
585 String memberName = message.member().roles().head();
587 LOG.debug("{}: Received MemberRemoved: memberName: {}, address: {}", persistenceId(), memberName,
588 message.member().address());
590 peerAddressResolver.removePeerAddress(memberName);
592 for(ShardInformation info : localShards.values()){
593 info.peerDown(memberName, getShardIdentifier(memberName, info.getShardName()).toString(), getSelf());
597 private void memberExited(ClusterEvent.MemberExited message) {
598 String memberName = message.member().roles().head();
600 LOG.debug("{}: Received MemberExited: memberName: {}, address: {}", persistenceId(), memberName,
601 message.member().address());
603 peerAddressResolver.removePeerAddress(memberName);
605 for(ShardInformation info : localShards.values()){
606 info.peerDown(memberName, getShardIdentifier(memberName, info.getShardName()).toString(), getSelf());
610 private void memberUp(ClusterEvent.MemberUp message) {
611 String memberName = message.member().roles().head();
613 LOG.debug("{}: Received MemberUp: memberName: {}, address: {}", persistenceId(), memberName,
614 message.member().address());
616 addPeerAddress(memberName, message.member().address());
621 private void addPeerAddress(String memberName, Address address) {
622 peerAddressResolver.addPeerAddress(memberName, address);
624 for(ShardInformation info : localShards.values()){
625 String shardName = info.getShardName();
626 String peerId = getShardIdentifier(memberName, shardName).toString();
627 info.updatePeerAddress(peerId, peerAddressResolver.getShardActorAddress(shardName, memberName), getSelf());
629 info.peerUp(memberName, peerId, getSelf());
633 private void memberReachable(ClusterEvent.ReachableMember message) {
634 String memberName = message.member().roles().head();
635 LOG.debug("Received ReachableMember: memberName {}, address: {}", memberName, message.member().address());
637 addPeerAddress(memberName, message.member().address());
639 markMemberAvailable(memberName);
642 private void memberUnreachable(ClusterEvent.UnreachableMember message) {
643 String memberName = message.member().roles().head();
644 LOG.debug("Received UnreachableMember: memberName {}, address: {}", memberName, message.member().address());
646 markMemberUnavailable(memberName);
649 private void markMemberUnavailable(final String memberName) {
650 for(ShardInformation info : localShards.values()){
651 String leaderId = info.getLeaderId();
652 if(leaderId != null && leaderId.contains(memberName)) {
653 LOG.debug("Marking Leader {} as unavailable.", leaderId);
654 info.setLeaderAvailable(false);
656 primaryShardInfoCache.remove(info.getShardName());
659 info.peerDown(memberName, getShardIdentifier(memberName, info.getShardName()).toString(), getSelf());
663 private void markMemberAvailable(final String memberName) {
664 for(ShardInformation info : localShards.values()){
665 String leaderId = info.getLeaderId();
666 if(leaderId != null && leaderId.contains(memberName)) {
667 LOG.debug("Marking Leader {} as available.", leaderId);
668 info.setLeaderAvailable(true);
671 info.peerUp(memberName, getShardIdentifier(memberName, info.getShardName()).toString(), getSelf());
675 private void onDatastoreContextFactory(DatastoreContextFactory factory) {
676 datastoreContextFactory = factory;
677 for (ShardInformation info : localShards.values()) {
678 info.setDatastoreContext(newShardDatastoreContext(info.getShardName()), getSelf());
682 private void onSwitchShardBehavior(SwitchShardBehavior message) {
683 ShardIdentifier identifier = ShardIdentifier.builder().fromShardIdString(message.getShardName()).build();
685 ShardInformation shardInformation = localShards.get(identifier.getShardName());
687 if(shardInformation != null && shardInformation.getActor() != null) {
688 shardInformation.getActor().tell(
689 new SwitchBehavior(RaftState.valueOf(message.getNewState()), message.getTerm()), getSelf());
691 LOG.warn("Could not switch the behavior of shard {} to {} - shard is not yet available",
692 message.getShardName(), message.getNewState());
697 * Notifies all the local shards of a change in the schema context
701 private void updateSchemaContext(final Object message) {
702 schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
704 LOG.debug("Got updated SchemaContext: # of modules {}", schemaContext.getAllModuleIdentifiers().size());
706 for (ShardInformation info : localShards.values()) {
707 if (info.getActor() == null) {
708 LOG.debug("Creating Shard {}", info.getShardId());
709 info.setActor(newShardActor(schemaContext, info));
711 info.getActor().tell(message, getSelf());
717 protected ClusterWrapper getCluster() {
722 protected ActorRef newShardActor(final SchemaContext schemaContext, ShardInformation info) {
723 return getContext().actorOf(info.newProps(schemaContext)
724 .withDispatcher(shardDispatcherPath), info.getShardId().toString());
727 private void findPrimary(FindPrimary message) {
728 LOG.debug("{}: In findPrimary: {}", persistenceId(), message);
730 final String shardName = message.getShardName();
731 final boolean canReturnLocalShardState = !(message instanceof RemoteFindPrimary);
733 // First see if the there is a local replica for the shard
734 final ShardInformation info = localShards.get(shardName);
735 if (info != null && info.isActiveMember()) {
736 sendResponse(info, message.isWaitUntilReady(), true, new Supplier<Object>() {
738 public Object get() {
739 String primaryPath = info.getSerializedLeaderActor();
740 Object found = canReturnLocalShardState && info.isLeader() ?
741 new LocalPrimaryShardFound(primaryPath, info.getLocalShardDataTree().get()) :
742 new RemotePrimaryShardFound(primaryPath, info.getLeaderVersion());
744 if(LOG.isDebugEnabled()) {
745 LOG.debug("{}: Found primary for {}: {}", persistenceId(), shardName, found);
755 for(String address: peerAddressResolver.getShardManagerPeerActorAddresses()) {
756 LOG.debug("{}: findPrimary for {} forwarding to remote ShardManager {}", persistenceId(),
759 getContext().actorSelection(address).forward(new RemoteFindPrimary(shardName,
760 message.isWaitUntilReady()), getContext());
764 LOG.debug("{}: No shard found for {}", persistenceId(), shardName);
766 getSender().tell(new PrimaryNotFoundException(
767 String.format("No primary shard found for %s.", shardName)), getSelf());
771 * Construct the name of the shard actor given the name of the member on
772 * which the shard resides and the name of the shard
778 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
779 return peerAddressResolver.getShardIdentifier(memberName, shardName);
783 * Create shards that are local to the member on which the ShardManager
787 private void createLocalShards() {
788 String memberName = this.cluster.getCurrentMemberName();
789 Collection<String> memberShardNames = this.configuration.getMemberShardNames(memberName);
791 Map<String, DatastoreSnapshot.ShardSnapshot> shardSnapshots = new HashMap<>();
792 if(restoreFromSnapshot != null)
794 for(DatastoreSnapshot.ShardSnapshot snapshot: restoreFromSnapshot.getShardSnapshots()) {
795 shardSnapshots.put(snapshot.getName(), snapshot);
799 restoreFromSnapshot = null; // null out to GC
801 for(String shardName : memberShardNames){
802 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
803 Map<String, String> peerAddresses = getPeerAddresses(shardName);
804 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses,
805 newShardDatastoreContext(shardName), Shard.builder().restoreFromSnapshot(
806 shardSnapshots.get(shardName)), peerAddressResolver));
807 mBean.addLocalShard(shardId.toString());
812 * Given the name of the shard find the addresses of all it's peers
816 private Map<String, String> getPeerAddresses(String shardName) {
817 Collection<String> members = configuration.getMembersFromShardName(shardName);
818 Map<String, String> peerAddresses = new HashMap<>();
820 String currentMemberName = this.cluster.getCurrentMemberName();
822 for(String memberName : members) {
823 if(!currentMemberName.equals(memberName)) {
824 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
825 String address = peerAddressResolver.getShardActorAddress(shardName, memberName);
826 peerAddresses.put(shardId.toString(), address);
829 return peerAddresses;
833 public SupervisorStrategy supervisorStrategy() {
835 return new OneForOneStrategy(10, Duration.create("1 minute"),
836 new Function<Throwable, SupervisorStrategy.Directive>() {
838 public SupervisorStrategy.Directive apply(Throwable t) {
839 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
840 return SupervisorStrategy.resume();
848 public String persistenceId() {
849 return persistenceId;
853 ShardManagerInfoMBean getMBean(){
857 private boolean isShardReplicaOperationInProgress(final String shardName, final ActorRef sender) {
858 if (shardReplicaOperationsInProgress.contains(shardName)) {
859 String msg = String.format("A shard replica operation for %s is already in progress", shardName);
860 LOG.debug ("{}: {}", persistenceId(), msg);
861 sender.tell(new akka.actor.Status.Failure(new IllegalStateException(msg)), getSelf());
868 private void onAddShardReplica (AddShardReplica shardReplicaMsg) {
869 final String shardName = shardReplicaMsg.getShardName();
871 LOG.debug("{}: onAddShardReplica: {}", persistenceId(), shardReplicaMsg);
873 // verify the shard with the specified name is present in the cluster configuration
874 if (!(this.configuration.isShardConfigured(shardName))) {
875 String msg = String.format("No module configuration exists for shard %s", shardName);
876 LOG.debug ("{}: {}", persistenceId(), msg);
877 getSender().tell(new akka.actor.Status.Failure(new IllegalArgumentException(msg)), getSelf());
881 // Create the localShard
882 if (schemaContext == null) {
883 String msg = String.format(
884 "No SchemaContext is available in order to create a local shard instance for %s", shardName);
885 LOG.debug ("{}: {}", persistenceId(), msg);
886 getSender().tell(new akka.actor.Status.Failure(new IllegalStateException(msg)), getSelf());
890 Timeout findPrimaryTimeout = new Timeout(datastoreContextFactory.getBaseDatastoreContext().
891 getShardInitializationTimeout().duration().$times(2));
893 final ActorRef sender = getSender();
894 Future<Object> futureObj = ask(getSelf(), new FindPrimary(shardName, true), findPrimaryTimeout);
895 futureObj.onComplete(new OnComplete<Object>() {
897 public void onComplete(Throwable failure, Object response) {
898 if (failure != null) {
899 LOG.debug ("{}: Received failure from FindPrimary for shard {}", persistenceId(), shardName, failure);
900 sender.tell(new akka.actor.Status.Failure(new RuntimeException(
901 String.format("Failed to find leader for shard %s", shardName), failure)), getSelf());
903 if(response instanceof RemotePrimaryShardFound) {
904 self().tell(new ForwardedAddServerPrimaryShardFound(shardName,
905 (RemotePrimaryShardFound)response), sender);
906 } else if(response instanceof LocalPrimaryShardFound) {
907 sendLocalReplicaAlreadyExistsReply(shardName, sender);
909 String msg = String.format("Failed to find leader for shard %s: received response: %s",
910 shardName, response);
911 LOG.debug ("{}: {}", persistenceId(), msg);
912 sender.tell(new akka.actor.Status.Failure(response instanceof Throwable ? (Throwable)response :
913 new RuntimeException(msg)), getSelf());
917 }, new Dispatchers(context().system().dispatchers()).getDispatcher(Dispatchers.DispatcherType.Client));
920 private void sendLocalReplicaAlreadyExistsReply(String shardName, ActorRef sender) {
921 String msg = String.format("Local shard %s already exists", shardName);
922 LOG.debug ("{}: {}", persistenceId(), msg);
923 sender.tell(new akka.actor.Status.Failure(new AlreadyExistsException(msg)), getSelf());
926 private void addShard(final String shardName, final RemotePrimaryShardFound response, final ActorRef sender) {
927 if(isShardReplicaOperationInProgress(shardName, sender)) {
931 shardReplicaOperationsInProgress.add(shardName);
933 final ShardInformation shardInfo;
934 final boolean removeShardOnFailure;
935 ShardInformation existingShardInfo = localShards.get(shardName);
936 if(existingShardInfo == null) {
937 removeShardOnFailure = true;
938 ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
940 DatastoreContext datastoreContext = newShardDatastoreContextBuilder(shardName).customRaftPolicyImplementation(
941 DisableElectionsRaftPolicy.class.getName()).build();
943 shardInfo = new ShardInformation(shardName, shardId, getPeerAddresses(shardName), datastoreContext,
944 Shard.builder(), peerAddressResolver);
945 shardInfo.setActiveMember(false);
946 localShards.put(shardName, shardInfo);
947 shardInfo.setActor(newShardActor(schemaContext, shardInfo));
949 removeShardOnFailure = false;
950 shardInfo = existingShardInfo;
953 String localShardAddress = peerAddressResolver.getShardActorAddress(shardName, cluster.getCurrentMemberName());
955 //inform ShardLeader to add this shard as a replica by sending an AddServer message
956 LOG.debug ("{}: Sending AddServer message to peer {} for shard {}", persistenceId(),
957 response.getPrimaryPath(), shardInfo.getShardId());
959 Timeout addServerTimeout = new Timeout(shardInfo.getDatastoreContext().getShardLeaderElectionTimeout().
961 Future<Object> futureObj = ask(getContext().actorSelection(response.getPrimaryPath()),
962 new AddServer(shardInfo.getShardId().toString(), localShardAddress, true), addServerTimeout);
964 futureObj.onComplete(new OnComplete<Object>() {
966 public void onComplete(Throwable failure, Object addServerResponse) {
967 if (failure != null) {
968 LOG.debug ("{}: AddServer request to {} for {} failed", persistenceId(),
969 response.getPrimaryPath(), shardName, failure);
971 String msg = String.format("AddServer request to leader %s for shard %s failed",
972 response.getPrimaryPath(), shardName);
973 self().tell(new ForwardedAddServerFailure(shardName, msg, failure, removeShardOnFailure), sender);
975 self().tell(new ForwardedAddServerReply(shardInfo, (AddServerReply)addServerResponse,
976 response.getPrimaryPath(), removeShardOnFailure), sender);
979 }, new Dispatchers(context().system().dispatchers()).getDispatcher(Dispatchers.DispatcherType.Client));
982 private void onAddServerFailure(String shardName, String message, Throwable failure, ActorRef sender,
983 boolean removeShardOnFailure) {
984 shardReplicaOperationsInProgress.remove(shardName);
986 if(removeShardOnFailure) {
987 ShardInformation shardInfo = localShards.remove(shardName);
988 if (shardInfo.getActor() != null) {
989 shardInfo.getActor().tell(PoisonPill.getInstance(), getSelf());
993 sender.tell(new akka.actor.Status.Failure(message == null ? failure :
994 new RuntimeException(message, failure)), getSelf());
997 private void onAddServerReply(ShardInformation shardInfo, AddServerReply replyMsg, ActorRef sender,
998 String leaderPath, boolean removeShardOnFailure) {
999 String shardName = shardInfo.getShardName();
1000 shardReplicaOperationsInProgress.remove(shardName);
1002 LOG.debug ("{}: Received {} for shard {} from leader {}", persistenceId(), replyMsg, shardName, leaderPath);
1004 if (replyMsg.getStatus() == ServerChangeStatus.OK) {
1005 LOG.debug ("{}: Leader shard successfully added the replica shard {}", persistenceId(), shardName);
1007 // Make the local shard voting capable
1008 shardInfo.setDatastoreContext(newShardDatastoreContext(shardName), getSelf());
1009 shardInfo.setActiveMember(true);
1012 mBean.addLocalShard(shardInfo.getShardId().toString());
1013 sender.tell(new akka.actor.Status.Success(null), getSelf());
1014 } else if(replyMsg.getStatus() == ServerChangeStatus.ALREADY_EXISTS) {
1015 sendLocalReplicaAlreadyExistsReply(shardName, sender);
1017 LOG.warn ("{}: Leader failed to add shard replica {} with status {}",
1018 persistenceId(), shardName, replyMsg.getStatus());
1021 switch (replyMsg.getStatus()) {
1023 failure = new TimeoutException(String.format(
1024 "The shard leader %s timed out trying to replicate the initial data to the new shard %s." +
1025 "Possible causes - there was a problem replicating the data or shard leadership changed while replicating the shard data",
1026 leaderPath, shardName));
1029 failure = createNoShardLeaderException(shardInfo.getShardId());
1032 failure = new RuntimeException(String.format(
1033 "AddServer request to leader %s for shard %s failed with status %s",
1034 leaderPath, shardName, replyMsg.getStatus()));
1037 onAddServerFailure(shardName, null, failure, sender, removeShardOnFailure);
1041 private void onRemoveShardReplica (RemoveShardReplica shardReplicaMsg) {
1042 String shardName = shardReplicaMsg.getShardName();
1044 // verify the local shard replica is available in the controller node
1045 if (!localShards.containsKey(shardName)) {
1046 String msg = String.format("Local shard %s does not", shardName);
1047 LOG.debug ("{}: {}", persistenceId(), msg);
1048 getSender().tell(new akka.actor.Status.Failure(new IllegalArgumentException(msg)), getSelf());
1051 // call RemoveShard for the shardName
1052 getSender().tell(new akka.actor.Status.Success(true), getSelf());
1056 private void persistShardList() {
1057 List<String> shardList = new ArrayList<>(localShards.keySet());
1058 for (ShardInformation shardInfo : localShards.values()) {
1059 if (!shardInfo.isActiveMember()) {
1060 shardList.remove(shardInfo.getShardName());
1063 LOG.debug ("{}: persisting the shard list {}", persistenceId(), shardList);
1064 saveSnapshot(new ShardManagerSnapshot(shardList));
1067 private void handleShardRecovery(SnapshotOffer offer) {
1068 LOG.debug ("{}: in handleShardRecovery", persistenceId());
1069 ShardManagerSnapshot snapshot = (ShardManagerSnapshot)offer.snapshot();
1070 String currentMember = cluster.getCurrentMemberName();
1071 Set<String> configuredShardList =
1072 new HashSet<>(configuration.getMemberShardNames(currentMember));
1073 for (String shard : snapshot.getShardList()) {
1074 if (!configuredShardList.contains(shard)) {
1075 // add the current member as a replica for the shard
1076 LOG.debug ("{}: adding shard {}", persistenceId(), shard);
1077 configuration.addMemberReplicaForShard(shard, currentMember);
1079 configuredShardList.remove(shard);
1082 for (String shard : configuredShardList) {
1083 // remove the member as a replica for the shard
1084 LOG.debug ("{}: removing shard {}", persistenceId(), shard);
1085 configuration.removeMemberReplicaForShard(shard, currentMember);
1089 private static class ForwardedAddServerPrimaryShardFound {
1091 RemotePrimaryShardFound primaryFound;
1093 ForwardedAddServerPrimaryShardFound(String shardName, RemotePrimaryShardFound primaryFound) {
1094 this.shardName = shardName;
1095 this.primaryFound = primaryFound;
1099 private static class ForwardedAddServerReply {
1100 ShardInformation shardInfo;
1101 AddServerReply addServerReply;
1103 boolean removeShardOnFailure;
1105 ForwardedAddServerReply(ShardInformation shardInfo, AddServerReply addServerReply, String leaderPath,
1106 boolean removeShardOnFailure) {
1107 this.shardInfo = shardInfo;
1108 this.addServerReply = addServerReply;
1109 this.leaderPath = leaderPath;
1110 this.removeShardOnFailure = removeShardOnFailure;
1114 private static class ForwardedAddServerFailure {
1116 String failureMessage;
1118 boolean removeShardOnFailure;
1120 ForwardedAddServerFailure(String shardName, String failureMessage, Throwable failure,
1121 boolean removeShardOnFailure) {
1122 this.shardName = shardName;
1123 this.failureMessage = failureMessage;
1124 this.failure = failure;
1125 this.removeShardOnFailure = removeShardOnFailure;
1130 protected static class ShardInformation {
1131 private final ShardIdentifier shardId;
1132 private final String shardName;
1133 private ActorRef actor;
1134 private ActorPath actorPath;
1135 private final Map<String, String> initialPeerAddresses;
1136 private Optional<DataTree> localShardDataTree;
1137 private boolean leaderAvailable = false;
1139 // flag that determines if the actor is ready for business
1140 private boolean actorInitialized = false;
1142 private boolean followerSyncStatus = false;
1144 private final Set<OnShardInitialized> onShardInitializedSet = Sets.newHashSet();
1145 private String role ;
1146 private String leaderId;
1147 private short leaderVersion;
1149 private DatastoreContext datastoreContext;
1150 private Shard.AbstractBuilder<?, ?> builder;
1151 private final ShardPeerAddressResolver addressResolver;
1152 private boolean isActiveMember = true;
1154 private ShardInformation(String shardName, ShardIdentifier shardId,
1155 Map<String, String> initialPeerAddresses, DatastoreContext datastoreContext,
1156 Shard.AbstractBuilder<?, ?> builder, ShardPeerAddressResolver addressResolver) {
1157 this.shardName = shardName;
1158 this.shardId = shardId;
1159 this.initialPeerAddresses = initialPeerAddresses;
1160 this.datastoreContext = datastoreContext;
1161 this.builder = builder;
1162 this.addressResolver = addressResolver;
1165 Props newProps(SchemaContext schemaContext) {
1166 Preconditions.checkNotNull(builder);
1167 Props props = builder.id(shardId).peerAddresses(initialPeerAddresses).datastoreContext(datastoreContext).
1168 schemaContext(schemaContext).props();
1173 String getShardName() {
1178 ActorRef getActor(){
1182 ActorPath getActorPath() {
1186 void setActor(ActorRef actor) {
1188 this.actorPath = actor.path();
1191 ShardIdentifier getShardId() {
1195 void setLocalDataTree(Optional<DataTree> localShardDataTree) {
1196 this.localShardDataTree = localShardDataTree;
1199 Optional<DataTree> getLocalShardDataTree() {
1200 return localShardDataTree;
1203 DatastoreContext getDatastoreContext() {
1204 return datastoreContext;
1207 void setDatastoreContext(DatastoreContext datastoreContext, ActorRef sender) {
1208 this.datastoreContext = datastoreContext;
1209 if (actor != null) {
1210 LOG.debug ("Sending new DatastoreContext to {}", shardId);
1211 actor.tell(this.datastoreContext, sender);
1215 void updatePeerAddress(String peerId, String peerAddress, ActorRef sender){
1216 LOG.info("updatePeerAddress for peer {} with address {}", peerId, peerAddress);
1219 if(LOG.isDebugEnabled()) {
1220 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
1221 peerId, peerAddress, actor.path());
1224 actor.tell(new PeerAddressResolved(peerId, peerAddress), sender);
1227 notifyOnShardInitializedCallbacks();
1230 void peerDown(String memberName, String peerId, ActorRef sender) {
1232 actor.tell(new PeerDown(memberName, peerId), sender);
1236 void peerUp(String memberName, String peerId, ActorRef sender) {
1238 actor.tell(new PeerUp(memberName, peerId), sender);
1242 boolean isShardReady() {
1243 return !RaftState.Candidate.name().equals(role) && !Strings.isNullOrEmpty(role);
1246 boolean isShardReadyWithLeaderId() {
1247 return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role) &&
1248 (isLeader() || addressResolver.resolve(leaderId) != null);
1251 boolean isShardInitialized() {
1252 return getActor() != null && actorInitialized;
1255 boolean isLeader() {
1256 return Objects.equal(leaderId, shardId.toString());
1259 String getSerializedLeaderActor() {
1261 return Serialization.serializedActorPath(getActor());
1263 return addressResolver.resolve(leaderId);
1267 void setActorInitialized() {
1268 LOG.debug("Shard {} is initialized", shardId);
1270 this.actorInitialized = true;
1272 notifyOnShardInitializedCallbacks();
1275 private void notifyOnShardInitializedCallbacks() {
1276 if(onShardInitializedSet.isEmpty()) {
1280 boolean ready = isShardReadyWithLeaderId();
1282 if(LOG.isDebugEnabled()) {
1283 LOG.debug("Shard {} is {} - notifying {} OnShardInitialized callbacks", shardId,
1284 ready ? "ready" : "initialized", onShardInitializedSet.size());
1287 Iterator<OnShardInitialized> iter = onShardInitializedSet.iterator();
1288 while(iter.hasNext()) {
1289 OnShardInitialized onShardInitialized = iter.next();
1290 if(!(onShardInitialized instanceof OnShardReady) || ready) {
1292 onShardInitialized.getTimeoutSchedule().cancel();
1293 onShardInitialized.getReplyRunnable().run();
1298 void addOnShardInitialized(OnShardInitialized onShardInitialized) {
1299 onShardInitializedSet.add(onShardInitialized);
1302 void removeOnShardInitialized(OnShardInitialized onShardInitialized) {
1303 onShardInitializedSet.remove(onShardInitialized);
1306 void setRole(String newRole) {
1307 this.role = newRole;
1309 notifyOnShardInitializedCallbacks();
1312 void setFollowerSyncStatus(boolean syncStatus){
1313 this.followerSyncStatus = syncStatus;
1317 if(RaftState.Follower.name().equals(this.role)){
1318 return followerSyncStatus;
1319 } else if(RaftState.Leader.name().equals(this.role)){
1326 boolean setLeaderId(String leaderId) {
1327 boolean changed = !Objects.equal(this.leaderId, leaderId);
1328 this.leaderId = leaderId;
1329 if(leaderId != null) {
1330 this.leaderAvailable = true;
1332 notifyOnShardInitializedCallbacks();
1337 String getLeaderId() {
1341 void setLeaderAvailable(boolean leaderAvailable) {
1342 this.leaderAvailable = leaderAvailable;
1345 short getLeaderVersion() {
1346 return leaderVersion;
1349 void setLeaderVersion(short leaderVersion) {
1350 this.leaderVersion = leaderVersion;
1353 boolean isActiveMember() {
1354 return isActiveMember;
1357 void setActiveMember(boolean isActiveMember) {
1358 this.isActiveMember = isActiveMember;
1362 private static class OnShardInitialized {
1363 private final Runnable replyRunnable;
1364 private Cancellable timeoutSchedule;
1366 OnShardInitialized(Runnable replyRunnable) {
1367 this.replyRunnable = replyRunnable;
1370 Runnable getReplyRunnable() {
1371 return replyRunnable;
1374 Cancellable getTimeoutSchedule() {
1375 return timeoutSchedule;
1378 void setTimeoutSchedule(Cancellable timeoutSchedule) {
1379 this.timeoutSchedule = timeoutSchedule;
1383 private static class OnShardReady extends OnShardInitialized {
1384 OnShardReady(Runnable replyRunnable) {
1385 super(replyRunnable);
1389 private static class ShardNotInitializedTimeout {
1390 private final ActorRef sender;
1391 private final ShardInformation shardInfo;
1392 private final OnShardInitialized onShardInitialized;
1394 ShardNotInitializedTimeout(ShardInformation shardInfo, OnShardInitialized onShardInitialized, ActorRef sender) {
1395 this.sender = sender;
1396 this.shardInfo = shardInfo;
1397 this.onShardInitialized = onShardInitialized;
1400 ActorRef getSender() {
1404 ShardInformation getShardInfo() {
1408 OnShardInitialized getOnShardInitialized() {
1409 return onShardInitialized;
1414 * We no longer persist SchemaContextModules but keep this class around for now for backwards
1415 * compatibility so we don't get de-serialization failures on upgrade from Helium.
1418 static class SchemaContextModules implements Serializable {
1419 private static final long serialVersionUID = -8884620101025936590L;
1421 private final Set<String> modules;
1423 SchemaContextModules(Set<String> modules){
1424 this.modules = modules;
1427 public Set<String> getModules() {
1432 public static Builder builder() {
1433 return new Builder();
1436 public static class Builder {
1437 private ClusterWrapper cluster;
1438 private Configuration configuration;
1439 private DatastoreContextFactory datastoreContextFactory;
1440 private CountDownLatch waitTillReadyCountdownLatch;
1441 private PrimaryShardInfoFutureCache primaryShardInfoCache;
1442 private DatastoreSnapshot restoreFromSnapshot;
1443 private volatile boolean sealed;
1445 protected void checkSealed() {
1446 Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
1449 public Builder cluster(ClusterWrapper cluster) {
1451 this.cluster = cluster;
1455 public Builder configuration(Configuration configuration) {
1457 this.configuration = configuration;
1461 public Builder datastoreContextFactory(DatastoreContextFactory datastoreContextFactory) {
1463 this.datastoreContextFactory = datastoreContextFactory;
1467 public Builder waitTillReadyCountdownLatch(CountDownLatch waitTillReadyCountdownLatch) {
1469 this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
1473 public Builder primaryShardInfoCache(PrimaryShardInfoFutureCache primaryShardInfoCache) {
1475 this.primaryShardInfoCache = primaryShardInfoCache;
1479 public Builder restoreFromSnapshot(DatastoreSnapshot restoreFromSnapshot) {
1481 this.restoreFromSnapshot = restoreFromSnapshot;
1485 protected void verify() {
1487 Preconditions.checkNotNull(cluster, "cluster should not be null");
1488 Preconditions.checkNotNull(configuration, "configuration should not be null");
1489 Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
1490 Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
1491 Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null");
1494 public Props props() {
1496 return Props.create(ShardManager.class, this);