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.OneForOneStrategy;
15 import akka.actor.Props;
16 import akka.actor.SupervisorStrategy;
17 import akka.cluster.ClusterEvent;
18 import akka.japi.Creator;
19 import akka.japi.Function;
20 import akka.japi.Procedure;
21 import akka.persistence.RecoveryCompleted;
22 import akka.persistence.RecoveryFailure;
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.base.Preconditions;
25 import com.google.common.base.Strings;
26 import com.google.common.base.Supplier;
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.Lists;
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
38 import java.util.concurrent.CountDownLatch;
39 import org.opendaylight.controller.cluster.DataPersistenceProvider;
40 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
41 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
42 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
43 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
44 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
45 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
46 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
47 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
48 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
49 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
50 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
51 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
52 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
53 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
54 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
55 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
56 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
57 import org.opendaylight.controller.cluster.raft.RaftState;
58 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
59 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import scala.concurrent.duration.Duration;
65 * The ShardManager has the following jobs,
67 * <li> Create all the local shard replicas that belong on this cluster member
68 * <li> Find the address of the local shard
69 * <li> Find the primary replica for any given shard
70 * <li> Monitor the cluster members and store their addresses
73 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
75 private final Logger LOG = LoggerFactory.getLogger(getClass());
77 // Stores a mapping between a member name and the address of the member
78 // Member names look like "member-1", "member-2" etc and are as specified
80 private final Map<String, Address> memberNameToAddress = new HashMap<>();
82 // Stores a mapping between a shard name and it's corresponding information
83 // Shard names look like inventory, topology etc and are as specified in
85 private final Map<String, ShardInformation> localShards = new HashMap<>();
87 // The type of a ShardManager reflects the type of the datastore itself
88 // A data store could be of type config/operational
89 private final String type;
91 private final ClusterWrapper cluster;
93 private final Configuration configuration;
95 private final String shardDispatcherPath;
97 private ShardManagerInfo mBean;
99 private DatastoreContext datastoreContext;
101 private Collection<String> knownModules = Collections.emptySet();
103 private final DataPersistenceProvider dataPersistenceProvider;
105 private final CountDownLatch waitTillReadyCountdownLatch;
109 protected ShardManager(ClusterWrapper cluster, Configuration configuration,
110 DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
112 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
113 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
114 this.datastoreContext = datastoreContext;
115 this.dataPersistenceProvider = createDataPersistenceProvider(datastoreContext.isPersistent());
116 this.type = datastoreContext.getDataStoreType();
117 this.shardDispatcherPath =
118 new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
119 this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
121 // Subscribe this actor to cluster member events
122 cluster.subscribeToMemberEvents(getSelf());
127 protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
128 return (persistent) ? new PersistentDataProvider() : new NonPersistentDataProvider();
131 public static Props props(
132 final ClusterWrapper cluster,
133 final Configuration configuration,
134 final DatastoreContext datastoreContext,
135 final CountDownLatch waitTillReadyCountdownLatch) {
137 Preconditions.checkNotNull(cluster, "cluster should not be null");
138 Preconditions.checkNotNull(configuration, "configuration should not be null");
139 Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
141 return Props.create(new ShardManagerCreator(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch));
145 public void postStop() {
146 LOG.info("Stopping ShardManager");
148 mBean.unregisterMBean();
152 public void handleCommand(Object message) throws Exception {
153 if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
154 findPrimary(FindPrimary.fromSerializable(message));
155 } else if(message instanceof FindLocalShard){
156 findLocalShard((FindLocalShard) message);
157 } else if (message instanceof UpdateSchemaContext) {
158 updateSchemaContext(message);
159 } else if(message instanceof ActorInitialized) {
160 onActorInitialized(message);
161 } else if (message instanceof ClusterEvent.MemberUp){
162 memberUp((ClusterEvent.MemberUp) message);
163 } else if(message instanceof ClusterEvent.MemberRemoved) {
164 memberRemoved((ClusterEvent.MemberRemoved) message);
165 } else if(message instanceof ClusterEvent.UnreachableMember) {
166 ignoreMessage(message);
167 } else if(message instanceof DatastoreContext) {
168 onDatastoreContext((DatastoreContext)message);
169 } else if(message instanceof RoleChangeNotification){
170 onRoleChangeNotification((RoleChangeNotification) message);
172 unknownMessage(message);
177 private void onRoleChangeNotification(RoleChangeNotification message) {
178 RoleChangeNotification roleChanged = message;
179 LOG.info("Received role changed for {} from {} to {}", roleChanged.getMemberId(),
180 roleChanged.getOldRole(), roleChanged.getNewRole());
182 ShardInformation shardInformation = findShardInformation(roleChanged.getMemberId());
183 if(shardInformation != null) {
184 shardInformation.setRole(roleChanged.getNewRole());
187 LOG.info("All Shards are ready - data store {} is ready, available count is {}", type,
188 waitTillReadyCountdownLatch.getCount());
190 waitTillReadyCountdownLatch.countDown();
196 private ShardInformation findShardInformation(String memberId) {
197 for(ShardInformation info : localShards.values()){
198 if(info.getShardId().toString().equals(memberId)){
206 private boolean isReady() {
207 boolean isReady = true;
208 for (ShardInformation info : localShards.values()) {
209 if(RaftState.Candidate.name().equals(info.getRole()) || Strings.isNullOrEmpty(info.getRole())){
217 private void onActorInitialized(Object message) {
218 final ActorRef sender = getSender();
220 if (sender == null) {
221 return; //why is a non-actor sending this message? Just ignore.
224 String actorName = sender.path().name();
225 //find shard name from actor name; actor name is stringified shardId
226 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
228 if (shardId.getShardName() == null) {
231 markShardAsInitialized(shardId.getShardName());
234 private void markShardAsInitialized(String shardName) {
235 LOG.debug("Initializing shard [{}]", shardName);
236 ShardInformation shardInformation = localShards.get(shardName);
237 if (shardInformation != null) {
238 shardInformation.setActorInitialized();
243 protected void handleRecover(Object message) throws Exception {
244 if(dataPersistenceProvider.isRecoveryApplicable()) {
245 if (message instanceof SchemaContextModules) {
246 SchemaContextModules msg = (SchemaContextModules) message;
247 knownModules = ImmutableSet.copyOf(msg.getModules());
248 } else if (message instanceof RecoveryFailure) {
249 RecoveryFailure failure = (RecoveryFailure) message;
250 LOG.error("Recovery failed", failure.cause());
251 } else if (message instanceof RecoveryCompleted) {
252 LOG.info("Recovery complete : {}", persistenceId());
254 // Delete all the messages from the akka journal except the last one
255 deleteMessages(lastSequenceNr() - 1);
258 if (message instanceof RecoveryCompleted) {
259 LOG.info("Recovery complete : {}", persistenceId());
261 // Delete all the messages from the akka journal
262 deleteMessages(lastSequenceNr());
267 private void findLocalShard(FindLocalShard message) {
268 final ShardInformation shardInformation = localShards.get(message.getShardName());
270 if(shardInformation == null){
271 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
275 sendResponse(shardInformation, message.isWaitUntilInitialized(), new Supplier<Object>() {
277 public Object get() {
278 return new LocalShardFound(shardInformation.getActor());
283 private void sendResponse(ShardInformation shardInformation, boolean waitUntilInitialized,
284 final Supplier<Object> messageSupplier) {
285 if (!shardInformation.isShardInitialized()) {
286 if(waitUntilInitialized) {
287 final ActorRef sender = getSender();
288 final ActorRef self = self();
289 shardInformation.addRunnableOnInitialized(new Runnable() {
292 sender.tell(messageSupplier.get(), self);
296 getSender().tell(new ActorNotInitialized(), getSelf());
302 getSender().tell(messageSupplier.get(), getSelf());
305 private void memberRemoved(ClusterEvent.MemberRemoved message) {
306 memberNameToAddress.remove(message.member().roles().head());
309 private void memberUp(ClusterEvent.MemberUp message) {
310 String memberName = message.member().roles().head();
312 memberNameToAddress.put(memberName, message.member().address());
314 for(ShardInformation info : localShards.values()){
315 String shardName = info.getShardName();
316 info.updatePeerAddress(getShardIdentifier(memberName, shardName),
317 getShardActorPath(shardName, memberName));
321 private void onDatastoreContext(DatastoreContext context) {
322 datastoreContext = context;
323 for (ShardInformation info : localShards.values()) {
324 if (info.getActor() != null) {
325 info.getActor().tell(datastoreContext, getSelf());
331 * Notifies all the local shards of a change in the schema context
335 private void updateSchemaContext(final Object message) {
336 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
338 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
339 Set<String> newModules = new HashSet<>(128);
341 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
342 String s = moduleIdentifier.getNamespace().toString();
346 if(newModules.containsAll(knownModules)) {
348 LOG.debug("New SchemaContext has a super set of current knownModules - persisting info");
350 knownModules = ImmutableSet.copyOf(newModules);
352 dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
355 public void apply(SchemaContextModules param) throws Exception {
356 LOG.debug("Sending new SchemaContext to Shards");
357 for (ShardInformation info : localShards.values()) {
358 if (info.getActor() == null) {
359 info.setActor(getContext().actorOf(Shard.props(info.getShardId(),
360 info.getPeerAddresses(), datastoreContext, schemaContext)
361 .withDispatcher(shardDispatcherPath), info.getShardId().toString()));
363 info.getActor().tell(message, getSelf());
365 info.getActor().tell(new RegisterRoleChangeListener(), self());
371 LOG.debug("Rejecting schema context update - not a super set of previously known modules:\nUPDATE: {}\nKNOWN: {}",
372 newModules, knownModules);
377 private void findPrimary(FindPrimary message) {
378 String shardName = message.getShardName();
380 // First see if the there is a local replica for the shard
381 final ShardInformation info = localShards.get(shardName);
383 sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
385 public Object get() {
386 return new PrimaryFound(info.getActorPath().toString()).toSerializable();
393 List<String> members = configuration.getMembersFromShardName(shardName);
395 if(cluster.getCurrentMemberName() != null) {
396 members.remove(cluster.getCurrentMemberName());
400 * FIXME: Instead of sending remote shard actor path back to sender,
401 * forward FindPrimary message to remote shard manager
403 // There is no way for us to figure out the primary (for now) so assume
404 // that one of the remote nodes is a primary
405 for(String memberName : members) {
406 Address address = memberNameToAddress.get(memberName);
409 getShardActorPath(shardName, memberName);
410 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
414 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
417 private String getShardActorPath(String shardName, String memberName) {
418 Address address = memberNameToAddress.get(memberName);
419 if(address != null) {
420 StringBuilder builder = new StringBuilder();
421 builder.append(address.toString())
423 .append(ShardManagerIdentifier.builder().type(type).build().toString())
425 .append(getShardIdentifier(memberName, shardName));
426 return builder.toString();
432 * Construct the name of the shard actor given the name of the member on
433 * which the shard resides and the name of the shard
439 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
440 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
444 * Create shards that are local to the member on which the ShardManager
448 private void createLocalShards() {
449 String memberName = this.cluster.getCurrentMemberName();
450 List<String> memberShardNames =
451 this.configuration.getMemberShardNames(memberName);
453 List<String> localShardActorNames = new ArrayList<>();
454 for(String shardName : memberShardNames){
455 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
456 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
457 localShardActorNames.add(shardId.toString());
458 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
461 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
462 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
466 * Given the name of the shard find the addresses of all it's peers
471 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
473 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
475 List<String> members =
476 this.configuration.getMembersFromShardName(shardName);
478 String currentMemberName = this.cluster.getCurrentMemberName();
480 for(String memberName : members){
481 if(!currentMemberName.equals(memberName)){
482 ShardIdentifier shardId = getShardIdentifier(memberName,
485 getShardActorPath(shardName, currentMemberName);
486 peerAddresses.put(shardId, path);
489 return peerAddresses;
493 public SupervisorStrategy supervisorStrategy() {
495 return new OneForOneStrategy(10, Duration.create("1 minute"),
496 new Function<Throwable, SupervisorStrategy.Directive>() {
498 public SupervisorStrategy.Directive apply(Throwable t) {
499 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
500 return SupervisorStrategy.resume();
508 public String persistenceId() {
509 return "shard-manager-" + type;
513 Collection<String> getKnownModules() {
518 DataPersistenceProvider getDataPersistenceProvider() {
519 return dataPersistenceProvider;
522 private class ShardInformation {
523 private final ShardIdentifier shardId;
524 private final String shardName;
525 private ActorRef actor;
526 private ActorPath actorPath;
527 private final Map<ShardIdentifier, String> peerAddresses;
529 // flag that determines if the actor is ready for business
530 private boolean actorInitialized = false;
532 private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
533 private String role ;
535 private ShardInformation(String shardName, ShardIdentifier shardId,
536 Map<ShardIdentifier, String> peerAddresses) {
537 this.shardName = shardName;
538 this.shardId = shardId;
539 this.peerAddresses = peerAddresses;
542 String getShardName() {
550 ActorPath getActorPath() {
554 void setActor(ActorRef actor) {
556 this.actorPath = actor.path();
559 ShardIdentifier getShardId() {
563 Map<ShardIdentifier, String> getPeerAddresses() {
564 return peerAddresses;
567 void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
568 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
570 if(peerAddresses.containsKey(peerId)){
571 peerAddresses.put(peerId, peerAddress);
574 if(LOG.isDebugEnabled()) {
575 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
576 peerId, peerAddress, actor.path());
579 actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
584 boolean isShardInitialized() {
585 return getActor() != null && actorInitialized;
588 void setActorInitialized() {
589 this.actorInitialized = true;
591 for(Runnable runnable: runnablesOnInitialized) {
595 runnablesOnInitialized.clear();
598 void addRunnableOnInitialized(Runnable runnable) {
599 runnablesOnInitialized.add(runnable);
602 public void setRole(String newRole) {
606 public String getRole(){
612 private static class ShardManagerCreator implements Creator<ShardManager> {
613 private static final long serialVersionUID = 1L;
615 final ClusterWrapper cluster;
616 final Configuration configuration;
617 final DatastoreContext datastoreContext;
618 private final CountDownLatch waitTillReadyCountdownLatch;
620 ShardManagerCreator(ClusterWrapper cluster,
621 Configuration configuration, DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
622 this.cluster = cluster;
623 this.configuration = configuration;
624 this.datastoreContext = datastoreContext;
625 this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
629 public ShardManager create() throws Exception {
630 return new ShardManager(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch);
634 static class SchemaContextModules implements Serializable {
635 private static final long serialVersionUID = -8884620101025936590L;
637 private final Set<String> modules;
639 SchemaContextModules(Set<String> modules){
640 this.modules = modules;
643 public Set<String> getModules() {