2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.Address;
14 import akka.actor.Cancellable;
15 import akka.actor.OneForOneStrategy;
16 import akka.actor.Props;
17 import akka.actor.SupervisorStrategy;
18 import akka.cluster.ClusterEvent;
19 import akka.japi.Creator;
20 import akka.japi.Function;
21 import akka.japi.Procedure;
22 import akka.persistence.RecoveryCompleted;
23 import akka.persistence.RecoveryFailure;
24 import akka.serialization.Serialization;
25 import com.google.common.annotations.VisibleForTesting;
26 import com.google.common.base.Objects;
27 import com.google.common.base.Preconditions;
28 import com.google.common.base.Strings;
29 import com.google.common.base.Supplier;
30 import com.google.common.collect.ImmutableSet;
31 import com.google.common.collect.Sets;
32 import java.io.Serializable;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
39 import java.util.List;
42 import java.util.concurrent.CountDownLatch;
43 import org.opendaylight.controller.cluster.DataPersistenceProvider;
44 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
45 import org.opendaylight.controller.cluster.PersistentDataProvider;
46 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
47 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
48 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
49 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
50 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
51 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
52 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
53 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
54 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
55 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
56 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
57 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
58 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
59 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
60 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
61 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
62 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
63 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
64 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
65 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
66 import org.opendaylight.controller.cluster.raft.RaftState;
67 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
68 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72 import scala.concurrent.duration.Duration;
75 * The ShardManager has the following jobs,
77 * <li> Create all the local shard replicas that belong on this cluster member
78 * <li> Find the address of the local shard
79 * <li> Find the primary replica for any given shard
80 * <li> Monitor the cluster members and store their addresses
83 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
85 private static final Logger LOG = LoggerFactory.getLogger(ShardManager.class);
87 // Stores a mapping between a member name and the address of the member
88 // Member names look like "member-1", "member-2" etc and are as specified
90 private final Map<String, Address> memberNameToAddress = new HashMap<>();
92 // Stores a mapping between a shard name and it's corresponding information
93 // Shard names look like inventory, topology etc and are as specified in
95 private final Map<String, ShardInformation> localShards = new HashMap<>();
97 // The type of a ShardManager reflects the type of the datastore itself
98 // A data store could be of type config/operational
99 private final String type;
101 private final String shardManagerIdentifierString;
103 private final ClusterWrapper cluster;
105 private final Configuration configuration;
107 private final String shardDispatcherPath;
109 private ShardManagerInfo mBean;
111 private DatastoreContext datastoreContext;
113 private Collection<String> knownModules = Collections.emptySet();
115 private final DataPersistenceProvider dataPersistenceProvider;
117 private final CountDownLatch waitTillReadyCountdownLatch;
121 protected ShardManager(ClusterWrapper cluster, Configuration configuration,
122 DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
124 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
125 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
126 this.datastoreContext = datastoreContext;
127 this.dataPersistenceProvider = createDataPersistenceProvider(datastoreContext.isPersistent());
128 this.type = datastoreContext.getDataStoreType();
129 this.shardManagerIdentifierString = ShardManagerIdentifier.builder().type(type).build().toString();
130 this.shardDispatcherPath =
131 new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
132 this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
134 // Subscribe this actor to cluster member events
135 cluster.subscribeToMemberEvents(getSelf());
140 protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
141 return (persistent) ? new PersistentDataProvider(this) : new NonPersistentDataProvider();
144 public static Props props(
145 final ClusterWrapper cluster,
146 final Configuration configuration,
147 final DatastoreContext datastoreContext,
148 final CountDownLatch waitTillReadyCountdownLatch) {
150 Preconditions.checkNotNull(cluster, "cluster should not be null");
151 Preconditions.checkNotNull(configuration, "configuration should not be null");
152 Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
154 return Props.create(new ShardManagerCreator(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch));
158 public void postStop() {
159 LOG.info("Stopping ShardManager");
161 mBean.unregisterMBean();
165 public void handleCommand(Object message) throws Exception {
166 if (message instanceof FindPrimary) {
167 findPrimary((FindPrimary)message);
168 } else if(message instanceof FindLocalShard){
169 findLocalShard((FindLocalShard) message);
170 } else if (message instanceof UpdateSchemaContext) {
171 updateSchemaContext(message);
172 } else if(message instanceof ActorInitialized) {
173 onActorInitialized(message);
174 } else if (message instanceof ClusterEvent.MemberUp){
175 memberUp((ClusterEvent.MemberUp) message);
176 } else if(message instanceof ClusterEvent.MemberRemoved) {
177 memberRemoved((ClusterEvent.MemberRemoved) message);
178 } else if(message instanceof ClusterEvent.UnreachableMember) {
179 ignoreMessage(message);
180 } else if(message instanceof DatastoreContext) {
181 onDatastoreContext((DatastoreContext)message);
182 } else if(message instanceof RoleChangeNotification) {
183 onRoleChangeNotification((RoleChangeNotification) message);
184 } else if(message instanceof FollowerInitialSyncUpStatus){
185 onFollowerInitialSyncStatus((FollowerInitialSyncUpStatus) message);
186 } else if(message instanceof ShardNotInitializedTimeout) {
187 onShardNotInitializedTimeout((ShardNotInitializedTimeout)message);
188 } else if(message instanceof LeaderStateChanged) {
189 onLeaderStateChanged((LeaderStateChanged)message);
191 unknownMessage(message);
196 private void checkReady(){
197 if (isReadyWithLeaderId()) {
198 LOG.info("{}: All Shards are ready - data store {} is ready, available count is {}",
199 persistenceId(), type, waitTillReadyCountdownLatch.getCount());
201 waitTillReadyCountdownLatch.countDown();
205 private void onLeaderStateChanged(LeaderStateChanged leaderStateChanged) {
206 LOG.info("{}: Received LeaderStateChanged message: {}", persistenceId(), leaderStateChanged);
208 ShardInformation shardInformation = findShardInformation(leaderStateChanged.getMemberId());
209 if(shardInformation != null) {
210 shardInformation.setLeaderId(leaderStateChanged.getLeaderId());
213 LOG.debug("No shard found with member Id {}", leaderStateChanged.getMemberId());
217 private void onShardNotInitializedTimeout(ShardNotInitializedTimeout message) {
218 ShardInformation shardInfo = message.getShardInfo();
220 LOG.debug("{}: Received ShardNotInitializedTimeout message for shard {}", persistenceId(),
221 shardInfo.getShardName());
223 shardInfo.removeOnShardInitialized(message.getOnShardInitialized());
225 if(!shardInfo.isShardInitialized()) {
226 LOG.debug("{}: Returning NotInitializedException for shard {}", persistenceId(), shardInfo.getShardName());
227 message.getSender().tell(createNotInitializedException(shardInfo.shardId), getSelf());
229 LOG.debug("{}: Returning NoShardLeaderException for shard {}", persistenceId(), shardInfo.getShardName());
230 message.getSender().tell(createNoShardLeaderException(shardInfo.shardId), getSelf());
234 private void onFollowerInitialSyncStatus(FollowerInitialSyncUpStatus status) {
235 LOG.info("{} Received follower initial sync status for {} status sync done {}", persistenceId(),
236 status.getName(), status.isInitialSyncDone());
238 ShardInformation shardInformation = findShardInformation(status.getName());
240 if(shardInformation != null) {
241 shardInformation.setFollowerSyncStatus(status.isInitialSyncDone());
243 mBean.setSyncStatus(isInSync());
248 private void onRoleChangeNotification(RoleChangeNotification roleChanged) {
249 LOG.info("{}: Received role changed for {} from {} to {}", persistenceId(), roleChanged.getMemberId(),
250 roleChanged.getOldRole(), roleChanged.getNewRole());
252 ShardInformation shardInformation = findShardInformation(roleChanged.getMemberId());
253 if(shardInformation != null) {
254 shardInformation.setRole(roleChanged.getNewRole());
256 mBean.setSyncStatus(isInSync());
261 private ShardInformation findShardInformation(String memberId) {
262 for(ShardInformation info : localShards.values()){
263 if(info.getShardId().toString().equals(memberId)){
271 private boolean isReadyWithLeaderId() {
272 boolean isReady = true;
273 for (ShardInformation info : localShards.values()) {
274 if(!info.isShardReadyWithLeaderId()){
282 private boolean isInSync(){
283 for (ShardInformation info : localShards.values()) {
284 if(!info.isInSync()){
291 private void onActorInitialized(Object message) {
292 final ActorRef sender = getSender();
294 if (sender == null) {
295 return; //why is a non-actor sending this message? Just ignore.
298 String actorName = sender.path().name();
299 //find shard name from actor name; actor name is stringified shardId
300 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
302 if (shardId.getShardName() == null) {
306 markShardAsInitialized(shardId.getShardName());
309 private void markShardAsInitialized(String shardName) {
310 LOG.debug("{}: Initializing shard [{}]", persistenceId(), shardName);
312 ShardInformation shardInformation = localShards.get(shardName);
313 if (shardInformation != null) {
314 shardInformation.setActorInitialized();
316 shardInformation.getActor().tell(new RegisterRoleChangeListener(), self());
321 protected void handleRecover(Object message) throws Exception {
322 if(dataPersistenceProvider.isRecoveryApplicable()) {
323 if (message instanceof SchemaContextModules) {
324 SchemaContextModules msg = (SchemaContextModules) message;
325 knownModules = ImmutableSet.copyOf(msg.getModules());
326 } else if (message instanceof RecoveryFailure) {
327 RecoveryFailure failure = (RecoveryFailure) message;
328 LOG.error("Recovery failed", failure.cause());
329 } else if (message instanceof RecoveryCompleted) {
330 LOG.info("Recovery complete : {}", persistenceId());
332 // Delete all the messages from the akka journal except the last one
333 deleteMessages(lastSequenceNr() - 1);
336 if (message instanceof RecoveryCompleted) {
337 LOG.info("Recovery complete : {}", persistenceId());
339 // Delete all the messages from the akka journal
340 deleteMessages(lastSequenceNr());
345 private void findLocalShard(FindLocalShard message) {
346 final ShardInformation shardInformation = localShards.get(message.getShardName());
348 if(shardInformation == null){
349 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
353 sendResponse(shardInformation, message.isWaitUntilInitialized(), false, new Supplier<Object>() {
355 public Object get() {
356 return new LocalShardFound(shardInformation.getActor());
361 private void sendResponse(ShardInformation shardInformation, boolean doWait,
362 boolean wantShardReady, final Supplier<Object> messageSupplier) {
363 if (!shardInformation.isShardInitialized() || (wantShardReady && !shardInformation.isShardReadyWithLeaderId())) {
365 final ActorRef sender = getSender();
366 final ActorRef self = self();
368 Runnable replyRunnable = new Runnable() {
371 sender.tell(messageSupplier.get(), self);
375 OnShardInitialized onShardInitialized = wantShardReady ? new OnShardReady(replyRunnable) :
376 new OnShardInitialized(replyRunnable);
378 shardInformation.addOnShardInitialized(onShardInitialized);
380 LOG.debug("{}: Scheduling timer to wait for shard {}", persistenceId(), shardInformation.getShardName());
382 Cancellable timeoutSchedule = getContext().system().scheduler().scheduleOnce(
383 datastoreContext.getShardInitializationTimeout().duration(), getSelf(),
384 new ShardNotInitializedTimeout(shardInformation, onShardInitialized, sender),
385 getContext().dispatcher(), getSelf());
387 onShardInitialized.setTimeoutSchedule(timeoutSchedule);
389 } else if (!shardInformation.isShardInitialized()) {
390 LOG.debug("{}: Returning NotInitializedException for shard {}", persistenceId(),
391 shardInformation.getShardName());
392 getSender().tell(createNotInitializedException(shardInformation.shardId), getSelf());
394 LOG.debug("{}: Returning NoShardLeaderException for shard {}", persistenceId(),
395 shardInformation.getShardName());
396 getSender().tell(createNoShardLeaderException(shardInformation.shardId), getSelf());
402 getSender().tell(messageSupplier.get(), getSelf());
405 private NoShardLeaderException createNoShardLeaderException(ShardIdentifier shardId) {
406 return new NoShardLeaderException(String.format(
407 "Could not find a leader for shard %s. This typically happens when the system is coming up or " +
408 "recovering and a leader is being elected. Try again later.", shardId));
411 private NotInitializedException createNotInitializedException(ShardIdentifier shardId) {
412 return new NotInitializedException(String.format(
413 "Found primary shard %s but it's not initialized yet. Please try again later", shardId));
416 private void memberRemoved(ClusterEvent.MemberRemoved message) {
417 String memberName = message.member().roles().head();
419 LOG.debug("{}: Received MemberRemoved: memberName: {}, address: {}", persistenceId(), memberName,
420 message.member().address());
422 memberNameToAddress.remove(message.member().roles().head());
425 private void memberUp(ClusterEvent.MemberUp message) {
426 String memberName = message.member().roles().head();
428 LOG.debug("{}: Received MemberUp: memberName: {}, address: {}", persistenceId(), memberName,
429 message.member().address());
431 memberNameToAddress.put(memberName, message.member().address());
433 for(ShardInformation info : localShards.values()){
434 String shardName = info.getShardName();
435 info.updatePeerAddress(getShardIdentifier(memberName, shardName).toString(),
436 getShardActorPath(shardName, memberName), getSelf());
440 private void onDatastoreContext(DatastoreContext context) {
441 datastoreContext = context;
442 for (ShardInformation info : localShards.values()) {
443 if (info.getActor() != null) {
444 info.getActor().tell(datastoreContext, getSelf());
450 * Notifies all the local shards of a change in the schema context
454 private void updateSchemaContext(final Object message) {
455 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
457 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
458 Set<String> newModules = new HashSet<>(128);
460 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
461 String s = moduleIdentifier.getNamespace().toString();
465 if(newModules.containsAll(knownModules)) {
467 LOG.debug("New SchemaContext has a super set of current knownModules - persisting info");
469 knownModules = ImmutableSet.copyOf(newModules);
471 dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
474 public void apply(SchemaContextModules param) throws Exception {
475 LOG.debug("Sending new SchemaContext to Shards");
476 for (ShardInformation info : localShards.values()) {
477 if (info.getActor() == null) {
478 info.setActor(newShardActor(schemaContext, info));
480 info.getActor().tell(message, getSelf());
487 LOG.debug("Rejecting schema context update - not a super set of previously known modules:\nUPDATE: {}\nKNOWN: {}",
488 newModules, knownModules);
494 protected ClusterWrapper getCluster() {
499 protected ActorRef newShardActor(final SchemaContext schemaContext, ShardInformation info) {
500 return getContext().actorOf(Shard.props(info.getShardId(),
501 info.getPeerAddresses(), datastoreContext, schemaContext)
502 .withDispatcher(shardDispatcherPath), info.getShardId().toString());
505 private void findPrimary(FindPrimary message) {
506 LOG.debug("{}: In findPrimary: {}", persistenceId(), message);
508 final String shardName = message.getShardName();
510 // First see if the there is a local replica for the shard
511 final ShardInformation info = localShards.get(shardName);
513 sendResponse(info, message.isWaitUntilReady(), true, new Supplier<Object>() {
515 public Object get() {
516 Object found = new PrimaryFound(info.getSerializedLeaderActor());
518 if(LOG.isDebugEnabled()) {
519 LOG.debug("{}: Found primary for {}: {}", persistenceId(), shardName, found);
529 for(Map.Entry<String, Address> entry: memberNameToAddress.entrySet()) {
530 if(!cluster.getCurrentMemberName().equals(entry.getKey())) {
531 String path = getShardManagerActorPathBuilder(entry.getValue()).toString();
533 LOG.debug("{}: findPrimary for {} forwarding to remote ShardManager {}", persistenceId(),
536 getContext().actorSelection(path).forward(message, getContext());
541 LOG.debug("{}: No shard found for {}", persistenceId(), shardName);
543 getSender().tell(new PrimaryNotFoundException(
544 String.format("No primary shard found for %s.", shardName)), getSelf());
547 private StringBuilder getShardManagerActorPathBuilder(Address address) {
548 StringBuilder builder = new StringBuilder();
549 builder.append(address.toString()).append("/user/").append(shardManagerIdentifierString);
553 private String getShardActorPath(String shardName, String memberName) {
554 Address address = memberNameToAddress.get(memberName);
555 if(address != null) {
556 StringBuilder builder = getShardManagerActorPathBuilder(address);
558 .append(getShardIdentifier(memberName, shardName));
559 return builder.toString();
565 * Construct the name of the shard actor given the name of the member on
566 * which the shard resides and the name of the shard
572 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
573 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
577 * Create shards that are local to the member on which the ShardManager
581 private void createLocalShards() {
582 String memberName = this.cluster.getCurrentMemberName();
583 List<String> memberShardNames =
584 this.configuration.getMemberShardNames(memberName);
586 List<String> localShardActorNames = new ArrayList<>();
587 for(String shardName : memberShardNames){
588 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
589 Map<String, String> peerAddresses = getPeerAddresses(shardName);
590 localShardActorNames.add(shardId.toString());
591 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
594 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
595 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
599 * Given the name of the shard find the addresses of all it's peers
604 private Map<String, String> getPeerAddresses(String shardName){
606 Map<String, String> peerAddresses = new HashMap<>();
608 List<String> members = this.configuration.getMembersFromShardName(shardName);
610 String currentMemberName = this.cluster.getCurrentMemberName();
612 for(String memberName : members){
613 if(!currentMemberName.equals(memberName)){
614 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
615 String path = getShardActorPath(shardName, currentMemberName);
616 peerAddresses.put(shardId.toString(), path);
619 return peerAddresses;
623 public SupervisorStrategy supervisorStrategy() {
625 return new OneForOneStrategy(10, Duration.create("1 minute"),
626 new Function<Throwable, SupervisorStrategy.Directive>() {
628 public SupervisorStrategy.Directive apply(Throwable t) {
629 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
630 return SupervisorStrategy.resume();
638 public String persistenceId() {
639 return "shard-manager-" + type;
643 Collection<String> getKnownModules() {
648 DataPersistenceProvider getDataPersistenceProvider() {
649 return dataPersistenceProvider;
653 ShardManagerInfoMBean getMBean(){
658 protected static class ShardInformation {
659 private final ShardIdentifier shardId;
660 private final String shardName;
661 private ActorRef actor;
662 private ActorPath actorPath;
663 private final Map<String, String> peerAddresses;
665 // flag that determines if the actor is ready for business
666 private boolean actorInitialized = false;
668 private boolean followerSyncStatus = false;
670 private final Set<OnShardInitialized> onShardInitializedSet = Sets.newHashSet();
671 private String role ;
672 private String leaderId;
674 private ShardInformation(String shardName, ShardIdentifier shardId,
675 Map<String, String> peerAddresses) {
676 this.shardName = shardName;
677 this.shardId = shardId;
678 this.peerAddresses = peerAddresses;
681 String getShardName() {
689 ActorPath getActorPath() {
693 void setActor(ActorRef actor) {
695 this.actorPath = actor.path();
698 ShardIdentifier getShardId() {
702 Map<String, String> getPeerAddresses() {
703 return peerAddresses;
706 void updatePeerAddress(String peerId, String peerAddress, ActorRef sender){
707 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
709 if(peerAddresses.containsKey(peerId)){
710 peerAddresses.put(peerId, peerAddress);
713 if(LOG.isDebugEnabled()) {
714 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
715 peerId, peerAddress, actor.path());
718 actor.tell(new PeerAddressResolved(peerId.toString(), peerAddress), sender);
721 notifyOnShardInitializedCallbacks();
725 boolean isShardReady() {
726 return !RaftState.Candidate.name().equals(role) && !Strings.isNullOrEmpty(role);
729 boolean isShardReadyWithLeaderId() {
730 return isShardReady() && (isLeader() || peerAddresses.containsKey(leaderId));
733 boolean isShardInitialized() {
734 return getActor() != null && actorInitialized;
738 return Objects.equal(leaderId, shardId.toString());
741 String getSerializedLeaderActor() {
743 return Serialization.serializedActorPath(getActor());
745 return peerAddresses.get(leaderId);
749 void setActorInitialized() {
750 LOG.debug("Shard {} is initialized", shardId);
752 this.actorInitialized = true;
754 notifyOnShardInitializedCallbacks();
757 private void notifyOnShardInitializedCallbacks() {
758 if(onShardInitializedSet.isEmpty()) {
762 boolean ready = isShardReadyWithLeaderId();
764 if(LOG.isDebugEnabled()) {
765 LOG.debug("Shard {} is {} - notifying {} OnShardInitialized callbacks", shardId,
766 ready ? "ready" : "initialized", onShardInitializedSet.size());
769 Iterator<OnShardInitialized> iter = onShardInitializedSet.iterator();
770 while(iter.hasNext()) {
771 OnShardInitialized onShardInitialized = iter.next();
772 if(!(onShardInitialized instanceof OnShardReady) || ready) {
774 onShardInitialized.getTimeoutSchedule().cancel();
775 onShardInitialized.getReplyRunnable().run();
780 void addOnShardInitialized(OnShardInitialized onShardInitialized) {
781 onShardInitializedSet.add(onShardInitialized);
784 void removeOnShardInitialized(OnShardInitialized onShardInitialized) {
785 onShardInitializedSet.remove(onShardInitialized);
788 void setRole(String newRole) {
791 notifyOnShardInitializedCallbacks();
794 void setFollowerSyncStatus(boolean syncStatus){
795 this.followerSyncStatus = syncStatus;
799 if(RaftState.Follower.name().equals(this.role)){
800 return followerSyncStatus;
801 } else if(RaftState.Leader.name().equals(this.role)){
808 void setLeaderId(String leaderId) {
809 this.leaderId = leaderId;
811 notifyOnShardInitializedCallbacks();
815 private static class ShardManagerCreator implements Creator<ShardManager> {
816 private static final long serialVersionUID = 1L;
818 final ClusterWrapper cluster;
819 final Configuration configuration;
820 final DatastoreContext datastoreContext;
821 private final CountDownLatch waitTillReadyCountdownLatch;
823 ShardManagerCreator(ClusterWrapper cluster,
824 Configuration configuration, DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
825 this.cluster = cluster;
826 this.configuration = configuration;
827 this.datastoreContext = datastoreContext;
828 this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
832 public ShardManager create() throws Exception {
833 return new ShardManager(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch);
837 private static class OnShardInitialized {
838 private final Runnable replyRunnable;
839 private Cancellable timeoutSchedule;
841 OnShardInitialized(Runnable replyRunnable) {
842 this.replyRunnable = replyRunnable;
845 Runnable getReplyRunnable() {
846 return replyRunnable;
849 Cancellable getTimeoutSchedule() {
850 return timeoutSchedule;
853 void setTimeoutSchedule(Cancellable timeoutSchedule) {
854 this.timeoutSchedule = timeoutSchedule;
858 private static class OnShardReady extends OnShardInitialized {
859 OnShardReady(Runnable replyRunnable) {
860 super(replyRunnable);
864 private static class ShardNotInitializedTimeout {
865 private final ActorRef sender;
866 private final ShardInformation shardInfo;
867 private final OnShardInitialized onShardInitialized;
869 ShardNotInitializedTimeout(ShardInformation shardInfo, OnShardInitialized onShardInitialized, ActorRef sender) {
870 this.sender = sender;
871 this.shardInfo = shardInfo;
872 this.onShardInitialized = onShardInitialized;
875 ActorRef getSender() {
879 ShardInformation getShardInfo() {
883 OnShardInitialized getOnShardInitialized() {
884 return onShardInitialized;
888 static class SchemaContextModules implements Serializable {
889 private static final long serialVersionUID = -8884620101025936590L;
891 private final Set<String> modules;
893 SchemaContextModules(Set<String> modules){
894 this.modules = modules;
897 public Set<String> getModules() {