d52965e055262434c3ff34227fdd4ebca898bc5e
[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.debug("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.debug("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.debug("Rejecting schema context update - not a super set of previously known modules:\nUPDATE: {}\nKNOWN: {}",
297                     newModules, knownModules);
298         }
299
300     }
301
302     private void findPrimary(FindPrimary message) {
303         String shardName = message.getShardName();
304
305         // First see if the there is a local replica for the shard
306         final ShardInformation info = localShards.get(shardName);
307         if (info != null) {
308             sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
309                 @Override
310                 public Object get() {
311                     return new PrimaryFound(info.getActorPath().toString()).toSerializable();
312                 }
313             });
314
315             return;
316         }
317
318         List<String> members = configuration.getMembersFromShardName(shardName);
319
320         if(cluster.getCurrentMemberName() != null) {
321             members.remove(cluster.getCurrentMemberName());
322         }
323
324         /**
325          * FIXME: Instead of sending remote shard actor path back to sender,
326          * forward FindPrimary message to remote shard manager
327          */
328         // There is no way for us to figure out the primary (for now) so assume
329         // that one of the remote nodes is a primary
330         for(String memberName : members) {
331             Address address = memberNameToAddress.get(memberName);
332             if(address != null){
333                 String path =
334                     getShardActorPath(shardName, memberName);
335                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
336                 return;
337             }
338         }
339         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
340     }
341
342     private String getShardActorPath(String shardName, String memberName) {
343         Address address = memberNameToAddress.get(memberName);
344         if(address != null) {
345             StringBuilder builder = new StringBuilder();
346             builder.append(address.toString())
347                 .append("/user/")
348                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
349                 .append("/")
350                 .append(getShardIdentifier(memberName, shardName));
351             return builder.toString();
352         }
353         return null;
354     }
355
356     /**
357      * Construct the name of the shard actor given the name of the member on
358      * which the shard resides and the name of the shard
359      *
360      * @param memberName
361      * @param shardName
362      * @return
363      */
364     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
365         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
366     }
367
368     /**
369      * Create shards that are local to the member on which the ShardManager
370      * runs
371      *
372      */
373     private void createLocalShards() {
374         String memberName = this.cluster.getCurrentMemberName();
375         List<String> memberShardNames =
376             this.configuration.getMemberShardNames(memberName);
377
378         List<String> localShardActorNames = new ArrayList<>();
379         for(String shardName : memberShardNames){
380             ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
381             Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
382             localShardActorNames.add(shardId.toString());
383             localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
384         }
385
386         mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
387                     datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
388     }
389
390     /**
391      * Given the name of the shard find the addresses of all it's peers
392      *
393      * @param shardName
394      * @return
395      */
396     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
397
398         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
399
400         List<String> members =
401             this.configuration.getMembersFromShardName(shardName);
402
403         String currentMemberName = this.cluster.getCurrentMemberName();
404
405         for(String memberName : members){
406             if(!currentMemberName.equals(memberName)){
407                 ShardIdentifier shardId = getShardIdentifier(memberName,
408                     shardName);
409                 String path =
410                     getShardActorPath(shardName, currentMemberName);
411                 peerAddresses.put(shardId, path);
412             }
413         }
414         return peerAddresses;
415     }
416
417     @Override
418     public SupervisorStrategy supervisorStrategy() {
419
420         return new OneForOneStrategy(10, Duration.create("1 minute"),
421             new Function<Throwable, SupervisorStrategy.Directive>() {
422                 @Override
423                 public SupervisorStrategy.Directive apply(Throwable t) {
424                     LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
425                     return SupervisorStrategy.resume();
426                 }
427             }
428         );
429
430     }
431
432     @Override
433     public String persistenceId() {
434         return "shard-manager-" + type;
435     }
436
437     @VisibleForTesting
438     Collection<String> getKnownModules() {
439         return knownModules;
440     }
441
442     @VisibleForTesting
443     DataPersistenceProvider getDataPersistenceProvider() {
444         return dataPersistenceProvider;
445     }
446
447     private class ShardInformation {
448         private final ShardIdentifier shardId;
449         private final String shardName;
450         private ActorRef actor;
451         private ActorPath actorPath;
452         private final Map<ShardIdentifier, String> peerAddresses;
453
454         // flag that determines if the actor is ready for business
455         private boolean actorInitialized = false;
456
457         private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
458
459         private ShardInformation(String shardName, ShardIdentifier shardId,
460                 Map<ShardIdentifier, String> peerAddresses) {
461             this.shardName = shardName;
462             this.shardId = shardId;
463             this.peerAddresses = peerAddresses;
464         }
465
466         String getShardName() {
467             return shardName;
468         }
469
470         ActorRef getActor(){
471             return actor;
472         }
473
474         ActorPath getActorPath() {
475             return actorPath;
476         }
477
478         void setActor(ActorRef actor) {
479             this.actor = actor;
480             this.actorPath = actor.path();
481         }
482
483         ShardIdentifier getShardId() {
484             return shardId;
485         }
486
487         Map<ShardIdentifier, String> getPeerAddresses() {
488             return peerAddresses;
489         }
490
491         void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
492             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
493                 peerAddress);
494             if(peerAddresses.containsKey(peerId)){
495                 peerAddresses.put(peerId, peerAddress);
496
497                 if(actor != null) {
498                     if(LOG.isDebugEnabled()) {
499                         LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
500                                 peerId, peerAddress, actor.path());
501                     }
502
503                     actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
504                 }
505             }
506         }
507
508         boolean isShardInitialized() {
509             return getActor() != null && actorInitialized;
510         }
511
512         void setActorInitialized() {
513             this.actorInitialized = true;
514
515             for(Runnable runnable: runnablesOnInitialized) {
516                 runnable.run();
517             }
518
519             runnablesOnInitialized.clear();
520         }
521
522         void addRunnableOnInitialized(Runnable runnable) {
523             runnablesOnInitialized.add(runnable);
524         }
525     }
526
527     private static class ShardManagerCreator implements Creator<ShardManager> {
528         private static final long serialVersionUID = 1L;
529
530         final ClusterWrapper cluster;
531         final Configuration configuration;
532         final DatastoreContext datastoreContext;
533
534         ShardManagerCreator(ClusterWrapper cluster,
535                 Configuration configuration, DatastoreContext datastoreContext) {
536             this.cluster = cluster;
537             this.configuration = configuration;
538             this.datastoreContext = datastoreContext;
539         }
540
541         @Override
542         public ShardManager create() throws Exception {
543             return new ShardManager(cluster, configuration, datastoreContext);
544         }
545     }
546
547     static class SchemaContextModules implements Serializable {
548         private static final long serialVersionUID = -8884620101025936590L;
549
550         private final Set<String> modules;
551
552         SchemaContextModules(Set<String> modules){
553             this.modules = modules;
554         }
555
556         public Set<String> getModules() {
557             return modules;
558         }
559     }
560 }
561
562
563