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.AbstractUntypedPersistentActor;
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.FindLocalShard;
33 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
34 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
35 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
36 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
37 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
38 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
39 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import scala.concurrent.duration.Duration;
44 import java.io.Serializable;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
54 * The ShardManager has the following jobs,
56 * <li> Create all the local shard replicas that belong on this cluster member
57 * <li> Find the address of the local shard
58 * <li> Find the primary replica for any given shard
59 * <li> Monitor the cluster members and store their addresses
62 public class ShardManager extends AbstractUntypedPersistentActor {
64 protected final LoggingAdapter LOG =
65 Logging.getLogger(getContext().system(), this);
67 // Stores a mapping between a member name and the address of the member
68 // Member names look like "member-1", "member-2" etc and are as specified
70 private final Map<String, Address> memberNameToAddress = new HashMap<>();
72 // Stores a mapping between a shard name and it's corresponding information
73 // Shard names look like inventory, topology etc and are as specified in
75 private final Map<String, ShardInformation> localShards = new HashMap<>();
77 // The type of a ShardManager reflects the type of the datastore itself
78 // A data store could be of type config/operational
79 private final String type;
81 private final ClusterWrapper cluster;
83 private final Configuration configuration;
85 private ShardManagerInfoMBean mBean;
87 private final DatastoreContext datastoreContext;
89 private final Collection<String> knownModules = new HashSet<>(128);
92 * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
93 * configuration or operational
95 private ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
96 DatastoreContext datastoreContext) {
98 this.type = Preconditions.checkNotNull(type, "type should not be null");
99 this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
100 this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
101 this.datastoreContext = datastoreContext;
103 // Subscribe this actor to cluster member events
104 cluster.subscribeToMemberEvents(getSelf());
106 //createLocalShards(null);
109 public static Props props(final String type,
110 final ClusterWrapper cluster,
111 final Configuration configuration,
112 final DatastoreContext datastoreContext) {
114 Preconditions.checkNotNull(type, "type should not be null");
115 Preconditions.checkNotNull(cluster, "cluster should not be null");
116 Preconditions.checkNotNull(configuration, "configuration should not be null");
118 return Props.create(new ShardManagerCreator(type, cluster, configuration, datastoreContext));
122 public void handleCommand(Object message) throws Exception {
123 if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
125 FindPrimary.fromSerializable(message));
126 } else if(message instanceof FindLocalShard){
127 findLocalShard((FindLocalShard) message);
128 } else if (message instanceof UpdateSchemaContext) {
129 updateSchemaContext(message);
130 } else if (message instanceof ClusterEvent.MemberUp){
131 memberUp((ClusterEvent.MemberUp) message);
132 } else if(message instanceof ClusterEvent.MemberRemoved) {
133 memberRemoved((ClusterEvent.MemberRemoved) message);
134 } else if(message instanceof ClusterEvent.UnreachableMember) {
135 ignoreMessage(message);
137 unknownMessage(message);
142 @Override protected void handleRecover(Object message) throws Exception {
144 if(message instanceof SchemaContextModules){
145 SchemaContextModules msg = (SchemaContextModules) message;
146 knownModules.clear();
147 knownModules.addAll(msg.getModules());
148 } else if(message instanceof RecoveryFailure){
149 RecoveryFailure failure = (RecoveryFailure) message;
150 LOG.error(failure.cause(), "Recovery failed");
151 } else if(message instanceof RecoveryCompleted){
152 LOG.info("Recovery complete : {}", persistenceId());
154 // Delete all the messages from the akka journal except the last one
155 deleteMessages(lastSequenceNr() - 1);
159 private void findLocalShard(FindLocalShard message) {
160 ShardInformation shardInformation =
161 localShards.get(message.getShardName());
163 if(shardInformation != null){
164 getSender().tell(new LocalShardFound(shardInformation.getActor()), getSelf());
168 getSender().tell(new LocalShardNotFound(message.getShardName()),
172 private void memberRemoved(ClusterEvent.MemberRemoved message) {
173 memberNameToAddress.remove(message.member().roles().head());
176 private void memberUp(ClusterEvent.MemberUp message) {
177 String memberName = message.member().roles().head();
179 memberNameToAddress.put(memberName , message.member().address());
181 for(ShardInformation info : localShards.values()){
182 String shardName = info.getShardName();
183 info.updatePeerAddress(getShardIdentifier(memberName, shardName),
184 getShardActorPath(shardName, memberName));
189 * Notifies all the local shards of a change in the schema context
193 private void updateSchemaContext(final Object message) {
194 final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
196 Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
197 Set<String> newModules = new HashSet<>(128);
199 for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
200 String s = moduleIdentifier.getNamespace().toString();
204 if(newModules.containsAll(knownModules)) {
206 LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
208 knownModules.clear();
209 knownModules.addAll(newModules);
211 persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
213 @Override public void apply(SchemaContextModules param) throws Exception {
214 LOG.info("Sending new SchemaContext to Shards");
215 if (localShards.size() == 0) {
216 createLocalShards(schemaContext);
218 for (ShardInformation info : localShards.values()) {
219 info.getActor().tell(message, getSelf());
226 LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
231 private void findPrimary(FindPrimary message) {
232 String shardName = message.getShardName();
234 // First see if the there is a local replica for the shard
235 ShardInformation info = localShards.get(shardName);
237 ActorPath shardPath = info.getActorPath();
238 if (shardPath != null) {
241 new PrimaryFound(shardPath.toString()).toSerializable(),
247 List<String> members =
248 configuration.getMembersFromShardName(shardName);
250 if(cluster.getCurrentMemberName() != null) {
251 members.remove(cluster.getCurrentMemberName());
254 // There is no way for us to figure out the primary (for now) so assume
255 // that one of the remote nodes is a primary
256 for(String memberName : members) {
257 Address address = memberNameToAddress.get(memberName);
260 getShardActorPath(shardName, memberName);
261 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
265 getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
268 private String getShardActorPath(String shardName, String memberName) {
269 Address address = memberNameToAddress.get(memberName);
270 if(address != null) {
271 StringBuilder builder = new StringBuilder();
272 builder.append(address.toString())
274 .append(ShardManagerIdentifier.builder().type(type).build().toString())
276 .append(getShardIdentifier(memberName, shardName));
277 return builder.toString();
283 * Construct the name of the shard actor given the name of the member on
284 * which the shard resides and the name of the shard
290 private ShardIdentifier getShardIdentifier(String memberName, String shardName){
291 return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
295 * Create shards that are local to the member on which the ShardManager
299 private void createLocalShards(SchemaContext schemaContext) {
300 String memberName = this.cluster.getCurrentMemberName();
301 List<String> memberShardNames =
302 this.configuration.getMemberShardNames(memberName);
304 List<String> localShardActorNames = new ArrayList<>();
305 for(String shardName : memberShardNames){
306 ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
307 Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
308 ActorRef actor = getContext()
309 .actorOf(Shard.props(shardId, peerAddresses, datastoreContext, schemaContext),
311 localShardActorNames.add(shardId.toString());
312 localShards.put(shardName, new ShardInformation(shardName, actor, peerAddresses));
315 mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
316 datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
320 * Given the name of the shard find the addresses of all it's peers
325 private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
327 Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
329 List<String> members =
330 this.configuration.getMembersFromShardName(shardName);
332 String currentMemberName = this.cluster.getCurrentMemberName();
334 for(String memberName : members){
335 if(!currentMemberName.equals(memberName)){
336 ShardIdentifier shardId = getShardIdentifier(memberName,
339 getShardActorPath(shardName, currentMemberName);
340 peerAddresses.put(shardId, path);
343 return peerAddresses;
347 public SupervisorStrategy supervisorStrategy() {
349 return new OneForOneStrategy(10, Duration.create("1 minute"),
350 new Function<Throwable, SupervisorStrategy.Directive>() {
352 public SupervisorStrategy.Directive apply(Throwable t) {
353 StringBuilder sb = new StringBuilder();
354 for(StackTraceElement element : t.getStackTrace()) {
356 .append(element.toString());
358 LOG.warning("Supervisor Strategy of resume applied {}",sb.toString());
359 return SupervisorStrategy.resume();
366 @Override public String persistenceId() {
367 return "shard-manager-" + type;
370 @VisibleForTesting public Collection<String> getKnownModules() {
374 private class ShardInformation {
375 private final String shardName;
376 private final ActorRef actor;
377 private final ActorPath actorPath;
378 private final Map<ShardIdentifier, String> peerAddresses;
380 private ShardInformation(String shardName, ActorRef actor,
381 Map<ShardIdentifier, String> peerAddresses) {
382 this.shardName = shardName;
384 this.actorPath = actor.path();
385 this.peerAddresses = peerAddresses;
388 public String getShardName() {
392 public ActorRef getActor(){
396 public ActorPath getActorPath() {
400 public void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
401 LOG.info("updatePeerAddress for peer {} with address {}", peerId,
403 if(peerAddresses.containsKey(peerId)){
404 peerAddresses.put(peerId, peerAddress);
405 if(LOG.isDebugEnabled()) {
407 "Sending PeerAddressResolved for peer {} with address {} to {}",
408 peerId, peerAddress, actor.path());
411 .tell(new PeerAddressResolved(peerId, peerAddress),
418 private static class ShardManagerCreator implements Creator<ShardManager> {
419 private static final long serialVersionUID = 1L;
422 final ClusterWrapper cluster;
423 final Configuration configuration;
424 final DatastoreContext datastoreContext;
426 ShardManagerCreator(String type, ClusterWrapper cluster,
427 Configuration configuration, DatastoreContext datastoreContext) {
429 this.cluster = cluster;
430 this.configuration = configuration;
431 this.datastoreContext = datastoreContext;
435 public ShardManager create() throws Exception {
436 return new ShardManager(type, cluster, configuration, datastoreContext);
440 static class SchemaContextModules implements Serializable {
441 private final Set<String> modules;
443 SchemaContextModules(Set<String> modules){
444 this.modules = modules;
447 public Set<String> getModules() {