Merge "BUG-1866: sal-compatibility does not register some services"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardManager.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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;
43
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;
50 import java.util.Map;
51 import java.util.Set;
52
53 /**
54  * The ShardManager has the following jobs,
55  * <ul>
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
60  * <ul>
61  */
62 public class ShardManager extends AbstractUntypedPersistentActor {
63
64     protected final LoggingAdapter LOG =
65         Logging.getLogger(getContext().system(), this);
66
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
69     // in configuration
70     private final Map<String, Address> memberNameToAddress = new HashMap<>();
71
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
74     // configuration
75     private final Map<String, ShardInformation> localShards = new HashMap<>();
76
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;
80
81     private final ClusterWrapper cluster;
82
83     private final Configuration configuration;
84
85     private ShardManagerInfoMBean mBean;
86
87     private final DatastoreContext datastoreContext;
88
89     private final Collection<String> knownModules = new HashSet<>(128);
90
91     /**
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
94      */
95     private ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
96             DatastoreContext datastoreContext) {
97
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;
102
103         // Subscribe this actor to cluster member events
104         cluster.subscribeToMemberEvents(getSelf());
105
106         //createLocalShards(null);
107     }
108
109     public static Props props(final String type,
110         final ClusterWrapper cluster,
111         final Configuration configuration,
112         final DatastoreContext datastoreContext) {
113
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");
117
118         return Props.create(new ShardManagerCreator(type, cluster, configuration, datastoreContext));
119     }
120
121     @Override
122     public void handleCommand(Object message) throws Exception {
123         if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
124             findPrimary(
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);
136         } else{
137             unknownMessage(message);
138         }
139
140     }
141
142     @Override protected void handleRecover(Object message) throws Exception {
143
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());
153
154             // Delete all the messages from the akka journal except the last one
155             deleteMessages(lastSequenceNr() - 1);
156         }
157     }
158
159     private void findLocalShard(FindLocalShard message) {
160         ShardInformation shardInformation =
161             localShards.get(message.getShardName());
162
163         if(shardInformation != null){
164             getSender().tell(new LocalShardFound(shardInformation.getActor()), getSelf());
165             return;
166         }
167
168         getSender().tell(new LocalShardNotFound(message.getShardName()),
169             getSelf());
170     }
171
172     private void memberRemoved(ClusterEvent.MemberRemoved message) {
173         memberNameToAddress.remove(message.member().roles().head());
174     }
175
176     private void memberUp(ClusterEvent.MemberUp message) {
177         String memberName = message.member().roles().head();
178
179         memberNameToAddress.put(memberName , message.member().address());
180
181         for(ShardInformation info : localShards.values()){
182             String shardName = info.getShardName();
183             info.updatePeerAddress(getShardIdentifier(memberName, shardName),
184                 getShardActorPath(shardName, memberName));
185         }
186     }
187
188     /**
189      * Notifies all the local shards of a change in the schema context
190      *
191      * @param message
192      */
193     private void updateSchemaContext(final Object message) {
194         final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
195
196         Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
197         Set<String> newModules = new HashSet<>(128);
198
199         for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
200             String s = moduleIdentifier.getNamespace().toString();
201             newModules.add(s);
202         }
203
204         if(newModules.containsAll(knownModules)) {
205
206             LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
207
208             knownModules.clear();
209             knownModules.addAll(newModules);
210
211             persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
212
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);
217                     } else {
218                         for (ShardInformation info : localShards.values()) {
219                             info.getActor().tell(message, getSelf());
220                         }
221                     }
222                 }
223
224             });
225         } else {
226             LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
227         }
228
229     }
230
231     private void findPrimary(FindPrimary message) {
232         String shardName = message.getShardName();
233
234         // First see if the there is a local replica for the shard
235         ShardInformation info = localShards.get(shardName);
236         if(info != null) {
237             ActorPath shardPath = info.getActorPath();
238             if (shardPath != null) {
239                 getSender()
240                     .tell(
241                         new PrimaryFound(shardPath.toString()).toSerializable(),
242                         getSelf());
243                 return;
244             }
245         }
246
247         List<String> members =
248             configuration.getMembersFromShardName(shardName);
249
250         if(cluster.getCurrentMemberName() != null) {
251             members.remove(cluster.getCurrentMemberName());
252         }
253
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);
258             if(address != null){
259                 String path =
260                     getShardActorPath(shardName, memberName);
261                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
262                 return;
263             }
264         }
265         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
266     }
267
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())
273                 .append("/user/")
274                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
275                 .append("/")
276                 .append(getShardIdentifier(memberName, shardName));
277             return builder.toString();
278         }
279         return null;
280     }
281
282     /**
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
285      *
286      * @param memberName
287      * @param shardName
288      * @return
289      */
290     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
291         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
292     }
293
294     /**
295      * Create shards that are local to the member on which the ShardManager
296      * runs
297      *
298      */
299     private void createLocalShards(SchemaContext schemaContext) {
300         String memberName = this.cluster.getCurrentMemberName();
301         List<String> memberShardNames =
302             this.configuration.getMemberShardNames(memberName);
303
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),
310                     shardId.toString());
311             localShardActorNames.add(shardId.toString());
312             localShards.put(shardName, new ShardInformation(shardName, actor, peerAddresses));
313         }
314
315         mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
316                     datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
317     }
318
319     /**
320      * Given the name of the shard find the addresses of all it's peers
321      *
322      * @param shardName
323      * @return
324      */
325     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
326
327         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
328
329         List<String> members =
330             this.configuration.getMembersFromShardName(shardName);
331
332         String currentMemberName = this.cluster.getCurrentMemberName();
333
334         for(String memberName : members){
335             if(!currentMemberName.equals(memberName)){
336                 ShardIdentifier shardId = getShardIdentifier(memberName,
337                     shardName);
338                 String path =
339                     getShardActorPath(shardName, currentMemberName);
340                 peerAddresses.put(shardId, path);
341             }
342         }
343         return peerAddresses;
344     }
345
346     @Override
347     public SupervisorStrategy supervisorStrategy() {
348
349         return new OneForOneStrategy(10, Duration.create("1 minute"),
350             new Function<Throwable, SupervisorStrategy.Directive>() {
351                 @Override
352                 public SupervisorStrategy.Directive apply(Throwable t) {
353                     StringBuilder sb = new StringBuilder();
354                     for(StackTraceElement element : t.getStackTrace()) {
355                        sb.append("\n\tat ")
356                          .append(element.toString());
357                     }
358                     LOG.warning("Supervisor Strategy of resume applied {}",sb.toString());
359                     return SupervisorStrategy.resume();
360                 }
361             }
362         );
363
364     }
365
366     @Override public String persistenceId() {
367         return "shard-manager-" + type;
368     }
369
370     @VisibleForTesting public Collection<String> getKnownModules() {
371         return knownModules;
372     }
373
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;
379
380         private ShardInformation(String shardName, ActorRef actor,
381             Map<ShardIdentifier, String> peerAddresses) {
382             this.shardName = shardName;
383             this.actor = actor;
384             this.actorPath = actor.path();
385             this.peerAddresses = peerAddresses;
386         }
387
388         public String getShardName() {
389             return shardName;
390         }
391
392         public ActorRef getActor(){
393             return actor;
394         }
395
396         public ActorPath getActorPath() {
397             return actorPath;
398         }
399
400         public void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
401             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
402                 peerAddress);
403             if(peerAddresses.containsKey(peerId)){
404                 peerAddresses.put(peerId, peerAddress);
405                 if(LOG.isDebugEnabled()) {
406                     LOG.debug(
407                         "Sending PeerAddressResolved for peer {} with address {} to {}",
408                         peerId, peerAddress, actor.path());
409                 }
410                 actor
411                     .tell(new PeerAddressResolved(peerId, peerAddress),
412                         getSelf());
413
414             }
415         }
416     }
417
418     private static class ShardManagerCreator implements Creator<ShardManager> {
419         private static final long serialVersionUID = 1L;
420
421         final String type;
422         final ClusterWrapper cluster;
423         final Configuration configuration;
424         final DatastoreContext datastoreContext;
425
426         ShardManagerCreator(String type, ClusterWrapper cluster,
427                 Configuration configuration, DatastoreContext datastoreContext) {
428             this.type = type;
429             this.cluster = cluster;
430             this.configuration = configuration;
431             this.datastoreContext = datastoreContext;
432         }
433
434         @Override
435         public ShardManager create() throws Exception {
436             return new ShardManager(type, cluster, configuration, datastoreContext);
437         }
438     }
439
440     static class SchemaContextModules implements Serializable {
441         private final Set<String> modules;
442
443         SchemaContextModules(Set<String> modules){
444             this.modules = modules;
445         }
446
447         public Set<String> getModules() {
448             return modules;
449         }
450     }
451 }
452
453
454