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.event.Logging;
19 import akka.event.LoggingAdapter;
20 import akka.japi.Creator;
21 import akka.japi.Function;
22 import akka.japi.Procedure;
23 import akka.persistence.RecoveryCompleted;
24 import akka.persistence.RecoveryFailure;
25 import com.google.common.annotations.VisibleForTesting;
26 import com.google.common.base.Preconditions;
27 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
28 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
29 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
30 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
31 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
32 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
33 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
34 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
35 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
36 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
37 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
38 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
39 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
40 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
41 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import scala.concurrent.duration.Duration;
46 import java.io.Serializable;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.List;
56 * The ShardManager has the following jobs,
58 * <li> Create all the local shard replicas that belong on this cluster member
59 * <li> Find the address of the local shard
60 * <li> Find the primary replica for any given shard
61 * <li> Monitor the cluster members and store their addresses
64 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
66 protected final LoggingAdapter LOG =
67 Logging.getLogger(getContext().system(), this);
69 // Stores a mapping between a member name and the address of the member
70 // Member names look like "member-1", "member-2" etc and are as specified
72 private final Map<String, Address> memberNameToAddress = new HashMap<>();
74 // Stores a mapping between a shard name and it's corresponding information
75 // Shard names look like inventory, topology etc and are as specified in
77 private final Map<String, ShardInformation> localShards = new HashMap<>();
79 // The type of a ShardManager reflects the type of the datastore itself
80 // A data store could be of type config/operational
81 private final String type;
83 private final ClusterWrapper cluster;
85 private final Configuration configuration;
87 private ShardManagerInfoMBean mBean;
89 private final DatastoreContext datastoreContext;
91 private final Collection<String> knownModules = new HashSet<>(128);
94 * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
95 * configuration or operational
97 private ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
98 DatastoreContext datastoreContext) {
100 this.type = Preconditions.checkNotNull(type, "type should not be null");
101 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
102 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
103 this.datastoreContext = datastoreContext;
105 // Subscribe this actor to cluster member events
106 cluster.subscribeToMemberEvents(getSelf());
108 //createLocalShards(null);
111 public static Props props(final String type,
112 final ClusterWrapper cluster,
113 final Configuration configuration,
114 final DatastoreContext datastoreContext) {
116 Preconditions.checkNotNull(type, "type should not be null");
117 Preconditions.checkNotNull(cluster, "cluster should not be null");
118 Preconditions.checkNotNull(configuration, "configuration should not be null");
120 return Props.create(new ShardManagerCreator(type, cluster, configuration, datastoreContext));
124 public void handleCommand(Object message) throws Exception {
125 if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
127 FindPrimary.fromSerializable(message));
128 } else if(message instanceof FindLocalShard){
129 findLocalShard((FindLocalShard) message);
130 } else if (message instanceof UpdateSchemaContext) {
131 updateSchemaContext(message);
132 } else if(message instanceof ActorInitialized) {
133 onActorInitialized(message);
134 } else if (message instanceof ClusterEvent.MemberUp){
135 memberUp((ClusterEvent.MemberUp) message);
136 } else if(message instanceof ClusterEvent.MemberRemoved) {
137 memberRemoved((ClusterEvent.MemberRemoved) message);
138 } else if(message instanceof ClusterEvent.UnreachableMember) {
139 ignoreMessage(message);
141 unknownMessage(message);
146 private void onActorInitialized(Object message) {
147 final ActorRef sender = getSender();
149 if (sender == null) {
150 return; //why is a non-actor sending this message? Just ignore.
153 String actorName = sender.path().name();
154 //find shard name from actor name; actor name is stringified shardId
155 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
157 if (shardId.getShardName() == null) {
160 markShardAsInitialized(shardId.getShardName());
163 @VisibleForTesting protected void markShardAsInitialized(String shardName) {
164 LOG.debug("Initializing shard [{}]", shardName);
165 ShardInformation shardInformation = localShards.get(shardName);
166 if (shardInformation != null) {
167 shardInformation.setShardInitialized(true);
171 @Override protected void handleRecover(Object message) throws Exception {
173 if(message instanceof SchemaContextModules){
174 SchemaContextModules msg = (SchemaContextModules) message;
175 knownModules.clear();
176 knownModules.addAll(msg.getModules());
177 } else if(message instanceof RecoveryFailure){
178 RecoveryFailure failure = (RecoveryFailure) message;
179 LOG.error(failure.cause(), "Recovery failed");
180 } else if(message instanceof RecoveryCompleted){
181 LOG.info("Recovery complete : {}", persistenceId());
183 // Delete all the messages from the akka journal except the last one
184 deleteMessages(lastSequenceNr() - 1);
188 private void findLocalShard(FindLocalShard message) {
189 ShardInformation shardInformation = localShards.get(message.getShardName());
191 if(shardInformation == null){
192 getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
196 sendResponse(shardInformation, new LocalShardFound(shardInformation.getActor()));
199 private void sendResponse(ShardInformation shardInformation, Object message) {
200 if (!shardInformation.isShardInitialized()) {
201 getSender().tell(new ActorNotInitialized(), getSelf());
205 getSender().tell(message, getSelf());
208 private void memberRemoved(ClusterEvent.MemberRemoved message) {
209 memberNameToAddress.remove(message.member().roles().head());
212 private void memberUp(ClusterEvent.MemberUp message) {
213 String memberName = message.member().roles().head();
215 memberNameToAddress.put(memberName, message.member().address());
217 for(ShardInformation info : localShards.values()){
218 String shardName = info.getShardName();
219 info.updatePeerAddress(getShardIdentifier(memberName, shardName),
220 getShardActorPath(shardName, memberName));
225 * Notifies all the local shards of a change in the schema context
229 private void updateSchemaContext(final Object message) {
230 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
232 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
233 Set<String> newModules = new HashSet<>(128);
235 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
236 String s = moduleIdentifier.getNamespace().toString();
240 if(newModules.containsAll(knownModules)) {
242 LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
244 knownModules.clear();
245 knownModules.addAll(newModules);
247 persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
249 @Override public void apply(SchemaContextModules param) throws Exception {
250 LOG.info("Sending new SchemaContext to Shards");
251 if (localShards.size() == 0) {
252 createLocalShards(schemaContext);
254 for (ShardInformation info : localShards.values()) {
255 info.getActor().tell(message, getSelf());
262 LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
267 private void findPrimary(FindPrimary message) {
268 final ActorRef sender = getSender();
269 String shardName = message.getShardName();
271 // First see if the there is a local replica for the shard
272 ShardInformation info = localShards.get(shardName);
274 ActorPath shardPath = info.getActorPath();
275 sendResponse(info, new PrimaryFound(shardPath.toString()).toSerializable());
279 List<String> members = configuration.getMembersFromShardName(shardName);
281 if(cluster.getCurrentMemberName() != null) {
282 members.remove(cluster.getCurrentMemberName());
286 * FIXME: Instead of sending remote shard actor path back to sender,
287 * forward FindPrimary message to remote shard manager
289 // There is no way for us to figure out the primary (for now) so assume
290 // that one of the remote nodes is a primary
291 for(String memberName : members) {
292 Address address = memberNameToAddress.get(memberName);
295 getShardActorPath(shardName, memberName);
296 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
300 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
303 private String getShardActorPath(String shardName, String memberName) {
304 Address address = memberNameToAddress.get(memberName);
305 if(address != null) {
306 StringBuilder builder = new StringBuilder();
307 builder.append(address.toString())
309 .append(ShardManagerIdentifier.builder().type(type).build().toString())
311 .append(getShardIdentifier(memberName, shardName));
312 return builder.toString();
318 * Construct the name of the shard actor given the name of the member on
319 * which the shard resides and the name of the shard
325 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
326 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
330 * Create shards that are local to the member on which the ShardManager
334 private void createLocalShards(SchemaContext schemaContext) {
335 String memberName = this.cluster.getCurrentMemberName();
336 List<String> memberShardNames =
337 this.configuration.getMemberShardNames(memberName);
339 List<String> localShardActorNames = new ArrayList<>();
340 for(String shardName : memberShardNames){
341 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
342 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
343 ActorRef actor = getContext()
344 .actorOf(Shard.props(shardId, peerAddresses, datastoreContext, schemaContext),
346 localShardActorNames.add(shardId.toString());
347 localShards.put(shardName, new ShardInformation(shardName, actor, peerAddresses));
350 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
351 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
355 * Given the name of the shard find the addresses of all it's peers
360 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
362 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
364 List<String> members =
365 this.configuration.getMembersFromShardName(shardName);
367 String currentMemberName = this.cluster.getCurrentMemberName();
369 for(String memberName : members){
370 if(!currentMemberName.equals(memberName)){
371 ShardIdentifier shardId = getShardIdentifier(memberName,
374 getShardActorPath(shardName, currentMemberName);
375 peerAddresses.put(shardId, path);
378 return peerAddresses;
382 public SupervisorStrategy supervisorStrategy() {
384 return new OneForOneStrategy(10, Duration.create("1 minute"),
385 new Function<Throwable, SupervisorStrategy.Directive>() {
387 public SupervisorStrategy.Directive apply(Throwable t) {
388 StringBuilder sb = new StringBuilder();
389 for(StackTraceElement element : t.getStackTrace()) {
391 .append(element.toString());
393 LOG.warning("Supervisor Strategy of resume applied {}",sb.toString());
394 return SupervisorStrategy.resume();
401 @Override public String persistenceId() {
402 return "shard-manager-" + type;
405 @VisibleForTesting public Collection<String> getKnownModules() {
409 private class ShardInformation {
410 private final String shardName;
411 private final ActorRef actor;
412 private final ActorPath actorPath;
413 private final Map<ShardIdentifier, String> peerAddresses;
414 private boolean shardInitialized = false; //flag that determines if the actor is ready for business
416 private ShardInformation(String shardName, ActorRef actor,
417 Map<ShardIdentifier, String> peerAddresses) {
418 this.shardName = shardName;
420 this.actorPath = actor.path();
421 this.peerAddresses = peerAddresses;
424 public String getShardName() {
428 public ActorRef getActor(){
432 public ActorPath getActorPath() {
436 public void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
437 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
439 if(peerAddresses.containsKey(peerId)){
440 peerAddresses.put(peerId, peerAddress);
441 if(LOG.isDebugEnabled()) {
443 "Sending PeerAddressResolved for peer {} with address {} to {}",
444 peerId, peerAddress, actor.path());
447 .tell(new PeerAddressResolved(peerId, peerAddress),
453 public boolean isShardInitialized() {
454 return shardInitialized;
457 public void setShardInitialized(boolean shardInitialized) {
458 this.shardInitialized = shardInitialized;
462 private static class ShardManagerCreator implements Creator<ShardManager> {
463 private static final long serialVersionUID = 1L;
466 final ClusterWrapper cluster;
467 final Configuration configuration;
468 final DatastoreContext datastoreContext;
470 ShardManagerCreator(String type, ClusterWrapper cluster,
471 Configuration configuration, DatastoreContext datastoreContext) {
473 this.cluster = cluster;
474 this.configuration = configuration;
475 this.datastoreContext = datastoreContext;
479 public ShardManager create() throws Exception {
480 return new ShardManager(type, cluster, configuration, datastoreContext);
484 static class SchemaContextModules implements Serializable {
485 private final Set<String> modules;
487 SchemaContextModules(Set<String> modules){
488 this.modules = modules;
491 public Set<String> getModules() {