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.Supplier;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.Lists;
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
37 import org.opendaylight.controller.cluster.DataPersistenceProvider;
38 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
39 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
40 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
41 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
42 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
43 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
44 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
45 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
46 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
47 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
48 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
49 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
50 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
51 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
52 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
53 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import scala.concurrent.duration.Duration;
60 * The ShardManager has the following jobs,
62 * <li> Create all the local shard replicas that belong on this cluster member
63 * <li> Find the address of the local shard
64 * <li> Find the primary replica for any given shard
65 * <li> Monitor the cluster members and store their addresses
68 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
70 private final Logger LOG = LoggerFactory.getLogger(getClass());
72 // Stores a mapping between a member name and the address of the member
73 // Member names look like "member-1", "member-2" etc and are as specified
75 private final Map<String, Address> memberNameToAddress = new HashMap<>();
77 // Stores a mapping between a shard name and it's corresponding information
78 // Shard names look like inventory, topology etc and are as specified in
80 private final Map<String, ShardInformation> localShards = new HashMap<>();
82 // The type of a ShardManager reflects the type of the datastore itself
83 // A data store could be of type config/operational
84 private final String type;
86 private final ClusterWrapper cluster;
88 private final Configuration configuration;
90 private ShardManagerInfoMBean mBean;
92 private final DatastoreContext datastoreContext;
94 private Collection<String> knownModules = Collections.emptySet();
96 private final DataPersistenceProvider dataPersistenceProvider;
100 protected ShardManager(ClusterWrapper cluster, Configuration configuration,
101 DatastoreContext datastoreContext) {
103 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
104 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
105 this.datastoreContext = datastoreContext;
106 this.dataPersistenceProvider = createDataPersistenceProvider(datastoreContext.isPersistent());
107 this.type = datastoreContext.getDataStoreType();
109 // Subscribe this actor to cluster member events
110 cluster.subscribeToMemberEvents(getSelf());
115 protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
116 return (persistent) ? new PersistentDataProvider() : new NonPersistentDataProvider();
119 public static Props props(
120 final ClusterWrapper cluster,
121 final Configuration configuration,
122 final DatastoreContext datastoreContext) {
124 Preconditions.checkNotNull(cluster, "cluster should not be null");
125 Preconditions.checkNotNull(configuration, "configuration should not be null");
127 return Props.create(new ShardManagerCreator(cluster, configuration, datastoreContext));
131 public void handleCommand(Object message) throws Exception {
132 if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
133 findPrimary(FindPrimary.fromSerializable(message));
134 } else if(message instanceof FindLocalShard){
135 findLocalShard((FindLocalShard) message);
136 } else if (message instanceof UpdateSchemaContext) {
137 updateSchemaContext(message);
138 } else if(message instanceof ActorInitialized) {
139 onActorInitialized(message);
140 } else if (message instanceof ClusterEvent.MemberUp){
141 memberUp((ClusterEvent.MemberUp) message);
142 } else if(message instanceof ClusterEvent.MemberRemoved) {
143 memberRemoved((ClusterEvent.MemberRemoved) message);
144 } else if(message instanceof ClusterEvent.UnreachableMember) {
145 ignoreMessage(message);
147 unknownMessage(message);
152 private void onActorInitialized(Object message) {
153 final ActorRef sender = getSender();
155 if (sender == null) {
156 return; //why is a non-actor sending this message? Just ignore.
159 String actorName = sender.path().name();
160 //find shard name from actor name; actor name is stringified shardId
161 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
163 if (shardId.getShardName() == null) {
166 markShardAsInitialized(shardId.getShardName());
169 private void markShardAsInitialized(String shardName) {
170 LOG.debug("Initializing shard [{}]", shardName);
171 ShardInformation shardInformation = localShards.get(shardName);
172 if (shardInformation != null) {
173 shardInformation.setActorInitialized();
178 protected void handleRecover(Object message) throws Exception {
179 if(dataPersistenceProvider.isRecoveryApplicable()) {
180 if (message instanceof SchemaContextModules) {
181 SchemaContextModules msg = (SchemaContextModules) message;
182 knownModules = ImmutableSet.copyOf(msg.getModules());
183 } else if (message instanceof RecoveryFailure) {
184 RecoveryFailure failure = (RecoveryFailure) message;
185 LOG.error("Recovery failed", failure.cause());
186 } else if (message instanceof RecoveryCompleted) {
187 LOG.info("Recovery complete : {}", persistenceId());
189 // Delete all the messages from the akka journal except the last one
190 deleteMessages(lastSequenceNr() - 1);
193 if (message instanceof RecoveryCompleted) {
194 LOG.info("Recovery complete : {}", persistenceId());
196 // Delete all the messages from the akka journal
197 deleteMessages(lastSequenceNr());
202 private void findLocalShard(FindLocalShard message) {
203 final ShardInformation shardInformation = localShards.get(message.getShardName());
205 if(shardInformation == null){
206 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
210 sendResponse(shardInformation, message.isWaitUntilInitialized(), new Supplier<Object>() {
212 public Object get() {
213 return new LocalShardFound(shardInformation.getActor());
218 private void sendResponse(ShardInformation shardInformation, boolean waitUntilInitialized,
219 final Supplier<Object> messageSupplier) {
220 if (!shardInformation.isShardInitialized()) {
221 if(waitUntilInitialized) {
222 final ActorRef sender = getSender();
223 final ActorRef self = self();
224 shardInformation.addRunnableOnInitialized(new Runnable() {
227 sender.tell(messageSupplier.get(), self);
231 getSender().tell(new ActorNotInitialized(), getSelf());
237 getSender().tell(messageSupplier.get(), getSelf());
240 private void memberRemoved(ClusterEvent.MemberRemoved message) {
241 memberNameToAddress.remove(message.member().roles().head());
244 private void memberUp(ClusterEvent.MemberUp message) {
245 String memberName = message.member().roles().head();
247 memberNameToAddress.put(memberName, message.member().address());
249 for(ShardInformation info : localShards.values()){
250 String shardName = info.getShardName();
251 info.updatePeerAddress(getShardIdentifier(memberName, shardName),
252 getShardActorPath(shardName, memberName));
257 * Notifies all the local shards of a change in the schema context
261 private void updateSchemaContext(final Object message) {
262 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
264 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
265 Set<String> newModules = new HashSet<>(128);
267 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
268 String s = moduleIdentifier.getNamespace().toString();
272 if(newModules.containsAll(knownModules)) {
274 LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
276 knownModules = ImmutableSet.copyOf(newModules);
278 dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
281 public void apply(SchemaContextModules param) throws Exception {
282 LOG.info("Sending new SchemaContext to Shards");
283 for (ShardInformation info : localShards.values()) {
284 if (info.getActor() == null) {
285 info.setActor(getContext().actorOf(Shard.props(info.getShardId(),
286 info.getPeerAddresses(), datastoreContext, schemaContext),
287 info.getShardId().toString()));
289 info.getActor().tell(message, getSelf());
296 LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
301 private void findPrimary(FindPrimary message) {
302 String shardName = message.getShardName();
304 // First see if the there is a local replica for the shard
305 final ShardInformation info = localShards.get(shardName);
307 sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
309 public Object get() {
310 return new PrimaryFound(info.getActorPath().toString()).toSerializable();
317 List<String> members = configuration.getMembersFromShardName(shardName);
319 if(cluster.getCurrentMemberName() != null) {
320 members.remove(cluster.getCurrentMemberName());
324 * FIXME: Instead of sending remote shard actor path back to sender,
325 * forward FindPrimary message to remote shard manager
327 // There is no way for us to figure out the primary (for now) so assume
328 // that one of the remote nodes is a primary
329 for(String memberName : members) {
330 Address address = memberNameToAddress.get(memberName);
333 getShardActorPath(shardName, memberName);
334 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
338 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
341 private String getShardActorPath(String shardName, String memberName) {
342 Address address = memberNameToAddress.get(memberName);
343 if(address != null) {
344 StringBuilder builder = new StringBuilder();
345 builder.append(address.toString())
347 .append(ShardManagerIdentifier.builder().type(type).build().toString())
349 .append(getShardIdentifier(memberName, shardName));
350 return builder.toString();
356 * Construct the name of the shard actor given the name of the member on
357 * which the shard resides and the name of the shard
363 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
364 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
368 * Create shards that are local to the member on which the ShardManager
372 private void createLocalShards() {
373 String memberName = this.cluster.getCurrentMemberName();
374 List<String> memberShardNames =
375 this.configuration.getMemberShardNames(memberName);
377 List<String> localShardActorNames = new ArrayList<>();
378 for(String shardName : memberShardNames){
379 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
380 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
381 localShardActorNames.add(shardId.toString());
382 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
385 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
386 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
390 * Given the name of the shard find the addresses of all it's peers
395 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
397 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
399 List<String> members =
400 this.configuration.getMembersFromShardName(shardName);
402 String currentMemberName = this.cluster.getCurrentMemberName();
404 for(String memberName : members){
405 if(!currentMemberName.equals(memberName)){
406 ShardIdentifier shardId = getShardIdentifier(memberName,
409 getShardActorPath(shardName, currentMemberName);
410 peerAddresses.put(shardId, path);
413 return peerAddresses;
417 public SupervisorStrategy supervisorStrategy() {
419 return new OneForOneStrategy(10, Duration.create("1 minute"),
420 new Function<Throwable, SupervisorStrategy.Directive>() {
422 public SupervisorStrategy.Directive apply(Throwable t) {
423 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
424 return SupervisorStrategy.resume();
432 public String persistenceId() {
433 return "shard-manager-" + type;
437 Collection<String> getKnownModules() {
442 DataPersistenceProvider getDataPersistenceProvider() {
443 return dataPersistenceProvider;
446 private class ShardInformation {
447 private final ShardIdentifier shardId;
448 private final String shardName;
449 private ActorRef actor;
450 private ActorPath actorPath;
451 private final Map<ShardIdentifier, String> peerAddresses;
453 // flag that determines if the actor is ready for business
454 private boolean actorInitialized = false;
456 private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
458 private ShardInformation(String shardName, ShardIdentifier shardId,
459 Map<ShardIdentifier, String> peerAddresses) {
460 this.shardName = shardName;
461 this.shardId = shardId;
462 this.peerAddresses = peerAddresses;
465 String getShardName() {
473 ActorPath getActorPath() {
477 void setActor(ActorRef actor) {
479 this.actorPath = actor.path();
482 ShardIdentifier getShardId() {
486 Map<ShardIdentifier, String> getPeerAddresses() {
487 return peerAddresses;
490 void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
491 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
493 if(peerAddresses.containsKey(peerId)){
494 peerAddresses.put(peerId, peerAddress);
497 if(LOG.isDebugEnabled()) {
498 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
499 peerId, peerAddress, actor.path());
502 actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
507 boolean isShardInitialized() {
508 return getActor() != null && actorInitialized;
511 void setActorInitialized() {
512 this.actorInitialized = true;
514 for(Runnable runnable: runnablesOnInitialized) {
518 runnablesOnInitialized.clear();
521 void addRunnableOnInitialized(Runnable runnable) {
522 runnablesOnInitialized.add(runnable);
526 private static class ShardManagerCreator implements Creator<ShardManager> {
527 private static final long serialVersionUID = 1L;
529 final ClusterWrapper cluster;
530 final Configuration configuration;
531 final DatastoreContext datastoreContext;
533 ShardManagerCreator(ClusterWrapper cluster,
534 Configuration configuration, DatastoreContext datastoreContext) {
535 this.cluster = cluster;
536 this.configuration = configuration;
537 this.datastoreContext = datastoreContext;
541 public ShardManager create() throws Exception {
542 return new ShardManager(cluster, configuration, datastoreContext);
546 static class SchemaContextModules implements Serializable {
547 private static final long serialVersionUID = -8884620101025936590L;
549 private final Set<String> modules;
551 SchemaContextModules(Set<String> modules){
552 this.modules = modules;
555 public Set<String> getModules() {