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;
99 * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
100 * configuration or operational
102 protected ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
103 DatastoreContext datastoreContext) {
105 this.type = Preconditions.checkNotNull(type, "type should not be null");
106 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
107 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
108 this.datastoreContext = datastoreContext;
109 this.dataPersistenceProvider = createDataPersistenceProvider(datastoreContext.isPersistent());
111 // Subscribe this actor to cluster member events
112 cluster.subscribeToMemberEvents(getSelf());
117 protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
118 return (persistent) ? new PersistentDataProvider() : new NonPersistentDataProvider();
121 public static Props props(final String type,
122 final ClusterWrapper cluster,
123 final Configuration configuration,
124 final DatastoreContext datastoreContext) {
126 Preconditions.checkNotNull(type, "type should not be null");
127 Preconditions.checkNotNull(cluster, "cluster should not be null");
128 Preconditions.checkNotNull(configuration, "configuration should not be null");
130 return Props.create(new ShardManagerCreator(type, cluster, configuration, datastoreContext));
134 public void handleCommand(Object message) throws Exception {
135 if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
136 findPrimary(FindPrimary.fromSerializable(message));
137 } else if(message instanceof FindLocalShard){
138 findLocalShard((FindLocalShard) message);
139 } else if (message instanceof UpdateSchemaContext) {
140 updateSchemaContext(message);
141 } else if(message instanceof ActorInitialized) {
142 onActorInitialized(message);
143 } else if (message instanceof ClusterEvent.MemberUp){
144 memberUp((ClusterEvent.MemberUp) message);
145 } else if(message instanceof ClusterEvent.MemberRemoved) {
146 memberRemoved((ClusterEvent.MemberRemoved) message);
147 } else if(message instanceof ClusterEvent.UnreachableMember) {
148 ignoreMessage(message);
150 unknownMessage(message);
155 private void onActorInitialized(Object message) {
156 final ActorRef sender = getSender();
158 if (sender == null) {
159 return; //why is a non-actor sending this message? Just ignore.
162 String actorName = sender.path().name();
163 //find shard name from actor name; actor name is stringified shardId
164 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
166 if (shardId.getShardName() == null) {
169 markShardAsInitialized(shardId.getShardName());
172 private void markShardAsInitialized(String shardName) {
173 LOG.debug("Initializing shard [{}]", shardName);
174 ShardInformation shardInformation = localShards.get(shardName);
175 if (shardInformation != null) {
176 shardInformation.setActorInitialized();
181 protected void handleRecover(Object message) throws Exception {
182 if(dataPersistenceProvider.isRecoveryApplicable()) {
183 if (message instanceof SchemaContextModules) {
184 SchemaContextModules msg = (SchemaContextModules) message;
185 knownModules = ImmutableSet.copyOf(msg.getModules());
186 } else if (message instanceof RecoveryFailure) {
187 RecoveryFailure failure = (RecoveryFailure) message;
188 LOG.error("Recovery failed", failure.cause());
189 } else if (message instanceof RecoveryCompleted) {
190 LOG.info("Recovery complete : {}", persistenceId());
192 // Delete all the messages from the akka journal except the last one
193 deleteMessages(lastSequenceNr() - 1);
196 if (message instanceof RecoveryCompleted) {
197 LOG.info("Recovery complete : {}", persistenceId());
199 // Delete all the messages from the akka journal
200 deleteMessages(lastSequenceNr());
205 private void findLocalShard(FindLocalShard message) {
206 final ShardInformation shardInformation = localShards.get(message.getShardName());
208 if(shardInformation == null){
209 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
213 sendResponse(shardInformation, message.isWaitUntilInitialized(), new Supplier<Object>() {
215 public Object get() {
216 return new LocalShardFound(shardInformation.getActor());
221 private void sendResponse(ShardInformation shardInformation, boolean waitUntilInitialized,
222 final Supplier<Object> messageSupplier) {
223 if (!shardInformation.isShardInitialized()) {
224 if(waitUntilInitialized) {
225 final ActorRef sender = getSender();
226 final ActorRef self = self();
227 shardInformation.addRunnableOnInitialized(new Runnable() {
230 sender.tell(messageSupplier.get(), self);
234 getSender().tell(new ActorNotInitialized(), getSelf());
240 getSender().tell(messageSupplier.get(), getSelf());
243 private void memberRemoved(ClusterEvent.MemberRemoved message) {
244 memberNameToAddress.remove(message.member().roles().head());
247 private void memberUp(ClusterEvent.MemberUp message) {
248 String memberName = message.member().roles().head();
250 memberNameToAddress.put(memberName, message.member().address());
252 for(ShardInformation info : localShards.values()){
253 String shardName = info.getShardName();
254 info.updatePeerAddress(getShardIdentifier(memberName, shardName),
255 getShardActorPath(shardName, memberName));
260 * Notifies all the local shards of a change in the schema context
264 private void updateSchemaContext(final Object message) {
265 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
267 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
268 Set<String> newModules = new HashSet<>(128);
270 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
271 String s = moduleIdentifier.getNamespace().toString();
275 if(newModules.containsAll(knownModules)) {
277 LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
279 knownModules = ImmutableSet.copyOf(newModules);
281 dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
284 public void apply(SchemaContextModules param) throws Exception {
285 LOG.info("Sending new SchemaContext to Shards");
286 for (ShardInformation info : localShards.values()) {
287 if (info.getActor() == null) {
288 info.setActor(getContext().actorOf(Shard.props(info.getShardId(),
289 info.getPeerAddresses(), datastoreContext, schemaContext),
290 info.getShardId().toString()));
292 info.getActor().tell(message, getSelf());
299 LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
304 private void findPrimary(FindPrimary message) {
305 String shardName = message.getShardName();
307 // First see if the there is a local replica for the shard
308 final ShardInformation info = localShards.get(shardName);
310 sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
312 public Object get() {
313 return new PrimaryFound(info.getActorPath().toString()).toSerializable();
320 List<String> members = configuration.getMembersFromShardName(shardName);
322 if(cluster.getCurrentMemberName() != null) {
323 members.remove(cluster.getCurrentMemberName());
327 * FIXME: Instead of sending remote shard actor path back to sender,
328 * forward FindPrimary message to remote shard manager
330 // There is no way for us to figure out the primary (for now) so assume
331 // that one of the remote nodes is a primary
332 for(String memberName : members) {
333 Address address = memberNameToAddress.get(memberName);
336 getShardActorPath(shardName, memberName);
337 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
341 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
344 private String getShardActorPath(String shardName, String memberName) {
345 Address address = memberNameToAddress.get(memberName);
346 if(address != null) {
347 StringBuilder builder = new StringBuilder();
348 builder.append(address.toString())
350 .append(ShardManagerIdentifier.builder().type(type).build().toString())
352 .append(getShardIdentifier(memberName, shardName));
353 return builder.toString();
359 * Construct the name of the shard actor given the name of the member on
360 * which the shard resides and the name of the shard
366 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
367 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
371 * Create shards that are local to the member on which the ShardManager
375 private void createLocalShards() {
376 String memberName = this.cluster.getCurrentMemberName();
377 List<String> memberShardNames =
378 this.configuration.getMemberShardNames(memberName);
380 List<String> localShardActorNames = new ArrayList<>();
381 for(String shardName : memberShardNames){
382 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
383 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
384 localShardActorNames.add(shardId.toString());
385 localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
388 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
389 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
393 * Given the name of the shard find the addresses of all it's peers
398 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
400 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
402 List<String> members =
403 this.configuration.getMembersFromShardName(shardName);
405 String currentMemberName = this.cluster.getCurrentMemberName();
407 for(String memberName : members){
408 if(!currentMemberName.equals(memberName)){
409 ShardIdentifier shardId = getShardIdentifier(memberName,
412 getShardActorPath(shardName, currentMemberName);
413 peerAddresses.put(shardId, path);
416 return peerAddresses;
420 public SupervisorStrategy supervisorStrategy() {
422 return new OneForOneStrategy(10, Duration.create("1 minute"),
423 new Function<Throwable, SupervisorStrategy.Directive>() {
425 public SupervisorStrategy.Directive apply(Throwable t) {
426 LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
427 return SupervisorStrategy.resume();
435 public String persistenceId() {
436 return "shard-manager-" + type;
440 Collection<String> getKnownModules() {
445 DataPersistenceProvider getDataPersistenceProvider() {
446 return dataPersistenceProvider;
449 private class ShardInformation {
450 private final ShardIdentifier shardId;
451 private final String shardName;
452 private ActorRef actor;
453 private ActorPath actorPath;
454 private final Map<ShardIdentifier, String> peerAddresses;
456 // flag that determines if the actor is ready for business
457 private boolean actorInitialized = false;
459 private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
461 private ShardInformation(String shardName, ShardIdentifier shardId,
462 Map<ShardIdentifier, String> peerAddresses) {
463 this.shardName = shardName;
464 this.shardId = shardId;
465 this.peerAddresses = peerAddresses;
468 String getShardName() {
476 ActorPath getActorPath() {
480 void setActor(ActorRef actor) {
482 this.actorPath = actor.path();
485 ShardIdentifier getShardId() {
489 Map<ShardIdentifier, String> getPeerAddresses() {
490 return peerAddresses;
493 void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
494 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
496 if(peerAddresses.containsKey(peerId)){
497 peerAddresses.put(peerId, peerAddress);
500 if(LOG.isDebugEnabled()) {
501 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
502 peerId, peerAddress, actor.path());
505 actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
510 boolean isShardInitialized() {
511 return getActor() != null && actorInitialized;
514 void setActorInitialized() {
515 this.actorInitialized = true;
517 for(Runnable runnable: runnablesOnInitialized) {
521 runnablesOnInitialized.clear();
524 void addRunnableOnInitialized(Runnable runnable) {
525 runnablesOnInitialized.add(runnable);
529 private static class ShardManagerCreator implements Creator<ShardManager> {
530 private static final long serialVersionUID = 1L;
533 final ClusterWrapper cluster;
534 final Configuration configuration;
535 final DatastoreContext datastoreContext;
537 ShardManagerCreator(String type, ClusterWrapper cluster,
538 Configuration configuration, DatastoreContext datastoreContext) {
540 this.cluster = cluster;
541 this.configuration = configuration;
542 this.datastoreContext = datastoreContext;
546 public ShardManager create() throws Exception {
547 return new ShardManager(type, cluster, configuration, datastoreContext);
551 static class SchemaContextModules implements Serializable {
552 private static final long serialVersionUID = -8884620101025936590L;
554 private final Set<String> modules;
556 SchemaContextModules(Set<String> modules){
557 this.modules = modules;
560 public Set<String> getModules() {