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.debug("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.debug("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.debug("Rejecting schema context update - not a super set of previously known modules:\nUPDATE: {}\nKNOWN: {}",
297 newModules, knownModules);
302 private void findPrimary(FindPrimary message) {
303 String shardName = message.getShardName();
305 // First see if the there is a local replica for the shard
306 final ShardInformation info = localShards.get(shardName);
308 sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
310 public Object get() {
311 return new PrimaryFound(info.getActorPath().toString()).toSerializable();
318 List<String> members = configuration.getMembersFromShardName(shardName);
320 if(cluster.getCurrentMemberName() != null) {
321 members.remove(cluster.getCurrentMemberName());
325 * FIXME: Instead of sending remote shard actor path back to sender,
326 * forward FindPrimary message to remote shard manager
328 // There is no way for us to figure out the primary (for now) so assume
329 // that one of the remote nodes is a primary
330 for(String memberName : members) {
331 Address address = memberNameToAddress.get(memberName);
334 getShardActorPath(shardName, memberName);
335 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
339 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
342 private String getShardActorPath(String shardName, String memberName) {
343 Address address = memberNameToAddress.get(memberName);
344 if(address != null) {
345 StringBuilder builder = new StringBuilder();
346 builder.append(address.toString())
348 .append(ShardManagerIdentifier.builder().type(type).build().toString())
350 .append(getShardIdentifier(memberName, shardName));
351 return builder.toString();
357 * Construct the name of the shard actor given the name of the member on
358 * which the shard resides and the name of the shard
364 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
365 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
369 * Create shards that are local to the member on which the ShardManager
373 private void createLocalShards() {
374 String memberName = this.cluster.getCurrentMemberName();
375 List<String> memberShardNames =
376 this.configuration.getMemberShardNames(memberName);
378 List<String> localShardActorNames = new ArrayList<>();
379 for(String shardName : memberShardNames){
380 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
381 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
382 localShardActorNames.add(shardId.toString());
383 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
386 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
387 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
391 * Given the name of the shard find the addresses of all it's peers
396 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
398 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
400 List<String> members =
401 this.configuration.getMembersFromShardName(shardName);
403 String currentMemberName = this.cluster.getCurrentMemberName();
405 for(String memberName : members){
406 if(!currentMemberName.equals(memberName)){
407 ShardIdentifier shardId = getShardIdentifier(memberName,
410 getShardActorPath(shardName, currentMemberName);
411 peerAddresses.put(shardId, path);
414 return peerAddresses;
418 public SupervisorStrategy supervisorStrategy() {
420 return new OneForOneStrategy(10, Duration.create("1 minute"),
421 new Function<Throwable, SupervisorStrategy.Directive>() {
423 public SupervisorStrategy.Directive apply(Throwable t) {
424 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
425 return SupervisorStrategy.resume();
433 public String persistenceId() {
434 return "shard-manager-" + type;
438 Collection<String> getKnownModules() {
443 DataPersistenceProvider getDataPersistenceProvider() {
444 return dataPersistenceProvider;
447 private class ShardInformation {
448 private final ShardIdentifier shardId;
449 private final String shardName;
450 private ActorRef actor;
451 private ActorPath actorPath;
452 private final Map<ShardIdentifier, String> peerAddresses;
454 // flag that determines if the actor is ready for business
455 private boolean actorInitialized = false;
457 private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
459 private ShardInformation(String shardName, ShardIdentifier shardId,
460 Map<ShardIdentifier, String> peerAddresses) {
461 this.shardName = shardName;
462 this.shardId = shardId;
463 this.peerAddresses = peerAddresses;
466 String getShardName() {
474 ActorPath getActorPath() {
478 void setActor(ActorRef actor) {
480 this.actorPath = actor.path();
483 ShardIdentifier getShardId() {
487 Map<ShardIdentifier, String> getPeerAddresses() {
488 return peerAddresses;
491 void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
492 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
494 if(peerAddresses.containsKey(peerId)){
495 peerAddresses.put(peerId, peerAddress);
498 if(LOG.isDebugEnabled()) {
499 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
500 peerId, peerAddress, actor.path());
503 actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
508 boolean isShardInitialized() {
509 return getActor() != null && actorInitialized;
512 void setActorInitialized() {
513 this.actorInitialized = true;
515 for(Runnable runnable: runnablesOnInitialized) {
519 runnablesOnInitialized.clear();
522 void addRunnableOnInitialized(Runnable runnable) {
523 runnablesOnInitialized.add(runnable);
527 private static class ShardManagerCreator implements Creator<ShardManager> {
528 private static final long serialVersionUID = 1L;
530 final ClusterWrapper cluster;
531 final Configuration configuration;
532 final DatastoreContext datastoreContext;
534 ShardManagerCreator(ClusterWrapper cluster,
535 Configuration configuration, DatastoreContext datastoreContext) {
536 this.cluster = cluster;
537 this.configuration = configuration;
538 this.datastoreContext = datastoreContext;
542 public ShardManager create() throws Exception {
543 return new ShardManager(cluster, configuration, datastoreContext);
547 static class SchemaContextModules implements Serializable {
548 private static final long serialVersionUID = -8884620101025936590L;
550 private final Set<String> modules;
552 SchemaContextModules(Set<String> modules){
553 this.modules = modules;
556 public Set<String> getModules() {