Merge "BUG-2573: add NormalizedNode-based RPC API"
[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.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;
35 import java.util.Map;
36 import java.util.Set;
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;
58
59 /**
60  * The ShardManager has the following jobs,
61  * <ul>
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
66  * <ul>
67  */
68 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
69
70     private final Logger LOG = LoggerFactory.getLogger(getClass());
71
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
74     // in configuration
75     private final Map<String, Address> memberNameToAddress = new HashMap<>();
76
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
79     // configuration
80     private final Map<String, ShardInformation> localShards = new HashMap<>();
81
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;
85
86     private final ClusterWrapper cluster;
87
88     private final Configuration configuration;
89
90     private ShardManagerInfoMBean mBean;
91
92     private final DatastoreContext datastoreContext;
93
94     private Collection<String> knownModules = Collections.emptySet();
95
96     private final DataPersistenceProvider dataPersistenceProvider;
97
98     /**
99      */
100     protected ShardManager(ClusterWrapper cluster, Configuration configuration,
101             DatastoreContext datastoreContext) {
102
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();
108
109         // Subscribe this actor to cluster member events
110         cluster.subscribeToMemberEvents(getSelf());
111
112         createLocalShards();
113     }
114
115     protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
116         return (persistent) ? new PersistentDataProvider() : new NonPersistentDataProvider();
117     }
118
119     public static Props props(
120         final ClusterWrapper cluster,
121         final Configuration configuration,
122         final DatastoreContext datastoreContext) {
123
124         Preconditions.checkNotNull(cluster, "cluster should not be null");
125         Preconditions.checkNotNull(configuration, "configuration should not be null");
126
127         return Props.create(new ShardManagerCreator(cluster, configuration, datastoreContext));
128     }
129
130     @Override
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);
146         } else{
147             unknownMessage(message);
148         }
149
150     }
151
152     private void onActorInitialized(Object message) {
153         final ActorRef sender = getSender();
154
155         if (sender == null) {
156             return; //why is a non-actor sending this message? Just ignore.
157         }
158
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();
162
163         if (shardId.getShardName() == null) {
164             return;
165         }
166         markShardAsInitialized(shardId.getShardName());
167     }
168
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();
174         }
175     }
176
177     @Override
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());
188
189                 // Delete all the messages from the akka journal except the last one
190                 deleteMessages(lastSequenceNr() - 1);
191             }
192         } else {
193             if (message instanceof RecoveryCompleted) {
194                 LOG.info("Recovery complete : {}", persistenceId());
195
196                 // Delete all the messages from the akka journal
197                 deleteMessages(lastSequenceNr());
198             }
199         }
200     }
201
202     private void findLocalShard(FindLocalShard message) {
203         final ShardInformation shardInformation = localShards.get(message.getShardName());
204
205         if(shardInformation == null){
206             getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
207             return;
208         }
209
210         sendResponse(shardInformation, message.isWaitUntilInitialized(), new Supplier<Object>() {
211             @Override
212             public Object get() {
213                 return new LocalShardFound(shardInformation.getActor());
214             }
215         });
216     }
217
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() {
225                     @Override
226                     public void run() {
227                         sender.tell(messageSupplier.get(), self);
228                     }
229                 });
230             } else {
231                 getSender().tell(new ActorNotInitialized(), getSelf());
232             }
233
234             return;
235         }
236
237         getSender().tell(messageSupplier.get(), getSelf());
238     }
239
240     private void memberRemoved(ClusterEvent.MemberRemoved message) {
241         memberNameToAddress.remove(message.member().roles().head());
242     }
243
244     private void memberUp(ClusterEvent.MemberUp message) {
245         String memberName = message.member().roles().head();
246
247         memberNameToAddress.put(memberName, message.member().address());
248
249         for(ShardInformation info : localShards.values()){
250             String shardName = info.getShardName();
251             info.updatePeerAddress(getShardIdentifier(memberName, shardName),
252                 getShardActorPath(shardName, memberName));
253         }
254     }
255
256     /**
257      * Notifies all the local shards of a change in the schema context
258      *
259      * @param message
260      */
261     private void updateSchemaContext(final Object message) {
262         final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
263
264         Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
265         Set<String> newModules = new HashSet<>(128);
266
267         for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
268             String s = moduleIdentifier.getNamespace().toString();
269             newModules.add(s);
270         }
271
272         if(newModules.containsAll(knownModules)) {
273
274             LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
275
276             knownModules = ImmutableSet.copyOf(newModules);
277
278             dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
279
280                 @Override
281                 public void apply(SchemaContextModules param) throws Exception {
282                     LOG.info("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()));
288                         } else {
289                             info.getActor().tell(message, getSelf());
290                         }
291                     }
292                 }
293
294             });
295         } else {
296             LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
297         }
298
299     }
300
301     private void findPrimary(FindPrimary message) {
302         String shardName = message.getShardName();
303
304         // First see if the there is a local replica for the shard
305         final ShardInformation info = localShards.get(shardName);
306         if (info != null) {
307             sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
308                 @Override
309                 public Object get() {
310                     return new PrimaryFound(info.getActorPath().toString()).toSerializable();
311                 }
312             });
313
314             return;
315         }
316
317         List<String> members = configuration.getMembersFromShardName(shardName);
318
319         if(cluster.getCurrentMemberName() != null) {
320             members.remove(cluster.getCurrentMemberName());
321         }
322
323         /**
324          * FIXME: Instead of sending remote shard actor path back to sender,
325          * forward FindPrimary message to remote shard manager
326          */
327         // There is no way for us to figure out the primary (for now) so assume
328         // that one of the remote nodes is a primary
329         for(String memberName : members) {
330             Address address = memberNameToAddress.get(memberName);
331             if(address != null){
332                 String path =
333                     getShardActorPath(shardName, memberName);
334                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
335                 return;
336             }
337         }
338         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
339     }
340
341     private String getShardActorPath(String shardName, String memberName) {
342         Address address = memberNameToAddress.get(memberName);
343         if(address != null) {
344             StringBuilder builder = new StringBuilder();
345             builder.append(address.toString())
346                 .append("/user/")
347                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
348                 .append("/")
349                 .append(getShardIdentifier(memberName, shardName));
350             return builder.toString();
351         }
352         return null;
353     }
354
355     /**
356      * Construct the name of the shard actor given the name of the member on
357      * which the shard resides and the name of the shard
358      *
359      * @param memberName
360      * @param shardName
361      * @return
362      */
363     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
364         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
365     }
366
367     /**
368      * Create shards that are local to the member on which the ShardManager
369      * runs
370      *
371      */
372     private void createLocalShards() {
373         String memberName = this.cluster.getCurrentMemberName();
374         List<String> memberShardNames =
375             this.configuration.getMemberShardNames(memberName);
376
377         List<String> localShardActorNames = new ArrayList<>();
378         for(String shardName : memberShardNames){
379             ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
380             Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
381             localShardActorNames.add(shardId.toString());
382             localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
383         }
384
385         mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
386                     datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
387     }
388
389     /**
390      * Given the name of the shard find the addresses of all it's peers
391      *
392      * @param shardName
393      * @return
394      */
395     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
396
397         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
398
399         List<String> members =
400             this.configuration.getMembersFromShardName(shardName);
401
402         String currentMemberName = this.cluster.getCurrentMemberName();
403
404         for(String memberName : members){
405             if(!currentMemberName.equals(memberName)){
406                 ShardIdentifier shardId = getShardIdentifier(memberName,
407                     shardName);
408                 String path =
409                     getShardActorPath(shardName, currentMemberName);
410                 peerAddresses.put(shardId, path);
411             }
412         }
413         return peerAddresses;
414     }
415
416     @Override
417     public SupervisorStrategy supervisorStrategy() {
418
419         return new OneForOneStrategy(10, Duration.create("1 minute"),
420             new Function<Throwable, SupervisorStrategy.Directive>() {
421                 @Override
422                 public SupervisorStrategy.Directive apply(Throwable t) {
423                     LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
424                     return SupervisorStrategy.resume();
425                 }
426             }
427         );
428
429     }
430
431     @Override
432     public String persistenceId() {
433         return "shard-manager-" + type;
434     }
435
436     @VisibleForTesting
437     Collection<String> getKnownModules() {
438         return knownModules;
439     }
440
441     @VisibleForTesting
442     DataPersistenceProvider getDataPersistenceProvider() {
443         return dataPersistenceProvider;
444     }
445
446     private class ShardInformation {
447         private final ShardIdentifier shardId;
448         private final String shardName;
449         private ActorRef actor;
450         private ActorPath actorPath;
451         private final Map<ShardIdentifier, String> peerAddresses;
452
453         // flag that determines if the actor is ready for business
454         private boolean actorInitialized = false;
455
456         private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
457
458         private ShardInformation(String shardName, ShardIdentifier shardId,
459                 Map<ShardIdentifier, String> peerAddresses) {
460             this.shardName = shardName;
461             this.shardId = shardId;
462             this.peerAddresses = peerAddresses;
463         }
464
465         String getShardName() {
466             return shardName;
467         }
468
469         ActorRef getActor(){
470             return actor;
471         }
472
473         ActorPath getActorPath() {
474             return actorPath;
475         }
476
477         void setActor(ActorRef actor) {
478             this.actor = actor;
479             this.actorPath = actor.path();
480         }
481
482         ShardIdentifier getShardId() {
483             return shardId;
484         }
485
486         Map<ShardIdentifier, String> getPeerAddresses() {
487             return peerAddresses;
488         }
489
490         void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
491             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
492                 peerAddress);
493             if(peerAddresses.containsKey(peerId)){
494                 peerAddresses.put(peerId, peerAddress);
495
496                 if(actor != null) {
497                     if(LOG.isDebugEnabled()) {
498                         LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
499                                 peerId, peerAddress, actor.path());
500                     }
501
502                     actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
503                 }
504             }
505         }
506
507         boolean isShardInitialized() {
508             return getActor() != null && actorInitialized;
509         }
510
511         void setActorInitialized() {
512             this.actorInitialized = true;
513
514             for(Runnable runnable: runnablesOnInitialized) {
515                 runnable.run();
516             }
517
518             runnablesOnInitialized.clear();
519         }
520
521         void addRunnableOnInitialized(Runnable runnable) {
522             runnablesOnInitialized.add(runnable);
523         }
524     }
525
526     private static class ShardManagerCreator implements Creator<ShardManager> {
527         private static final long serialVersionUID = 1L;
528
529         final ClusterWrapper cluster;
530         final Configuration configuration;
531         final DatastoreContext datastoreContext;
532
533         ShardManagerCreator(ClusterWrapper cluster,
534                 Configuration configuration, DatastoreContext datastoreContext) {
535             this.cluster = cluster;
536             this.configuration = configuration;
537             this.datastoreContext = datastoreContext;
538         }
539
540         @Override
541         public ShardManager create() throws Exception {
542             return new ShardManager(cluster, configuration, datastoreContext);
543         }
544     }
545
546     static class SchemaContextModules implements Serializable {
547         private static final long serialVersionUID = -8884620101025936590L;
548
549         private final Set<String> modules;
550
551         SchemaContextModules(Set<String> modules){
552             this.modules = modules;
553         }
554
555         public Set<String> getModules() {
556             return modules;
557         }
558     }
559 }
560
561
562