Merge "Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to...
[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.Strings;
26 import com.google.common.base.Supplier;
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.Lists;
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.concurrent.CountDownLatch;
39 import org.opendaylight.controller.cluster.DataPersistenceProvider;
40 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedPersistentActorWithMetering;
41 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
42 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
43 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
44 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
45 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
46 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
47 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
48 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
49 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
50 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
51 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
52 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
53 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
54 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
55 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
56 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
57 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
58 import org.opendaylight.controller.cluster.raft.RaftState;
59 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
60 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 import scala.concurrent.duration.Duration;
65
66 /**
67  * The ShardManager has the following jobs,
68  * <ul>
69  * <li> Create all the local shard replicas that belong on this cluster member
70  * <li> Find the address of the local shard
71  * <li> Find the primary replica for any given shard
72  * <li> Monitor the cluster members and store their addresses
73  * <ul>
74  */
75 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
76
77     private final Logger LOG = LoggerFactory.getLogger(getClass());
78
79     // Stores a mapping between a member name and the address of the member
80     // Member names look like "member-1", "member-2" etc and are as specified
81     // in configuration
82     private final Map<String, Address> memberNameToAddress = new HashMap<>();
83
84     // Stores a mapping between a shard name and it's corresponding information
85     // Shard names look like inventory, topology etc and are as specified in
86     // configuration
87     private final Map<String, ShardInformation> localShards = new HashMap<>();
88
89     // The type of a ShardManager reflects the type of the datastore itself
90     // A data store could be of type config/operational
91     private final String type;
92
93     private final ClusterWrapper cluster;
94
95     private final Configuration configuration;
96
97     private final String shardDispatcherPath;
98
99     private ShardManagerInfo mBean;
100
101     private DatastoreContext datastoreContext;
102
103     private Collection<String> knownModules = Collections.emptySet();
104
105     private final DataPersistenceProvider dataPersistenceProvider;
106
107     private final CountDownLatch waitTillReadyCountdownLatch;
108
109     /**
110      */
111     protected ShardManager(ClusterWrapper cluster, Configuration configuration,
112             DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
113
114         this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
115         this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
116         this.datastoreContext = datastoreContext;
117         this.dataPersistenceProvider = createDataPersistenceProvider(datastoreContext.isPersistent());
118         this.type = datastoreContext.getDataStoreType();
119         this.shardDispatcherPath =
120                 new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
121         this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
122
123         // Subscribe this actor to cluster member events
124         cluster.subscribeToMemberEvents(getSelf());
125
126         createLocalShards();
127     }
128
129     protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
130         return (persistent) ? new PersistentDataProvider() : new NonPersistentDataProvider();
131     }
132
133     public static Props props(
134         final ClusterWrapper cluster,
135         final Configuration configuration,
136         final DatastoreContext datastoreContext,
137         final CountDownLatch waitTillReadyCountdownLatch) {
138
139         Preconditions.checkNotNull(cluster, "cluster should not be null");
140         Preconditions.checkNotNull(configuration, "configuration should not be null");
141         Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
142
143         return Props.create(new ShardManagerCreator(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch));
144     }
145
146     @Override
147     public void postStop() {
148         LOG.info("Stopping ShardManager");
149
150         mBean.unregisterMBean();
151     }
152
153     @Override
154     public void handleCommand(Object message) throws Exception {
155         if (FindPrimary.SERIALIZABLE_CLASS.isInstance(message)) {
156             findPrimary(FindPrimary.fromSerializable(message));
157         } else if(message instanceof FindLocalShard){
158             findLocalShard((FindLocalShard) message);
159         } else if (message instanceof UpdateSchemaContext) {
160             updateSchemaContext(message);
161         } else if(message instanceof ActorInitialized) {
162             onActorInitialized(message);
163         } else if (message instanceof ClusterEvent.MemberUp){
164             memberUp((ClusterEvent.MemberUp) message);
165         } else if(message instanceof ClusterEvent.MemberRemoved) {
166             memberRemoved((ClusterEvent.MemberRemoved) message);
167         } else if(message instanceof ClusterEvent.UnreachableMember) {
168             ignoreMessage(message);
169         } else if(message instanceof DatastoreContext) {
170             onDatastoreContext((DatastoreContext)message);
171         } else if(message instanceof RoleChangeNotification) {
172             onRoleChangeNotification((RoleChangeNotification) message);
173         } else if(message instanceof FollowerInitialSyncUpStatus){
174             onFollowerInitialSyncStatus((FollowerInitialSyncUpStatus) message);
175         } else{
176             unknownMessage(message);
177         }
178
179     }
180
181     private void onFollowerInitialSyncStatus(FollowerInitialSyncUpStatus status) {
182         LOG.info("Received follower initial sync status for {} status sync done {}", status.getName(),
183                 status.isInitialSyncDone());
184
185         ShardInformation shardInformation = findShardInformation(status.getName());
186
187         if(shardInformation != null) {
188             shardInformation.setFollowerSyncStatus(status.isInitialSyncDone());
189
190             mBean.setSyncStatus(isInSync());
191         }
192
193     }
194
195     private void onRoleChangeNotification(RoleChangeNotification roleChanged) {
196         LOG.info("Received role changed for {} from {} to {}", roleChanged.getMemberId(),
197                 roleChanged.getOldRole(), roleChanged.getNewRole());
198
199         ShardInformation shardInformation = findShardInformation(roleChanged.getMemberId());
200         if(shardInformation != null) {
201             shardInformation.setRole(roleChanged.getNewRole());
202
203             if (isReady()) {
204                 LOG.info("All Shards are ready - data store {} is ready, available count is {}", type,
205                         waitTillReadyCountdownLatch.getCount());
206
207                 waitTillReadyCountdownLatch.countDown();
208             }
209
210             mBean.setSyncStatus(isInSync());
211         }
212     }
213
214
215     private ShardInformation findShardInformation(String memberId) {
216         for(ShardInformation info : localShards.values()){
217             if(info.getShardId().toString().equals(memberId)){
218                 return info;
219             }
220         }
221
222         return null;
223     }
224
225     private boolean isReady() {
226         boolean isReady = true;
227         for (ShardInformation info : localShards.values()) {
228             if(RaftState.Candidate.name().equals(info.getRole()) || Strings.isNullOrEmpty(info.getRole())){
229                 isReady = false;
230                 break;
231             }
232         }
233         return isReady;
234     }
235
236     private boolean isInSync(){
237         for (ShardInformation info : localShards.values()) {
238             if(!info.isInSync()){
239                 return false;
240             }
241         }
242         return true;
243     }
244
245     private void onActorInitialized(Object message) {
246         final ActorRef sender = getSender();
247
248         if (sender == null) {
249             return; //why is a non-actor sending this message? Just ignore.
250         }
251
252         String actorName = sender.path().name();
253         //find shard name from actor name; actor name is stringified shardId
254         ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
255
256         if (shardId.getShardName() == null) {
257             return;
258         }
259         markShardAsInitialized(shardId.getShardName());
260     }
261
262     private void markShardAsInitialized(String shardName) {
263         LOG.debug("Initializing shard [{}]", shardName);
264         ShardInformation shardInformation = localShards.get(shardName);
265         if (shardInformation != null) {
266             shardInformation.setActorInitialized();
267         }
268     }
269
270     @Override
271     protected void handleRecover(Object message) throws Exception {
272         if(dataPersistenceProvider.isRecoveryApplicable()) {
273             if (message instanceof SchemaContextModules) {
274                 SchemaContextModules msg = (SchemaContextModules) message;
275                 knownModules = ImmutableSet.copyOf(msg.getModules());
276             } else if (message instanceof RecoveryFailure) {
277                 RecoveryFailure failure = (RecoveryFailure) message;
278                 LOG.error("Recovery failed", failure.cause());
279             } else if (message instanceof RecoveryCompleted) {
280                 LOG.info("Recovery complete : {}", persistenceId());
281
282                 // Delete all the messages from the akka journal except the last one
283                 deleteMessages(lastSequenceNr() - 1);
284             }
285         } else {
286             if (message instanceof RecoveryCompleted) {
287                 LOG.info("Recovery complete : {}", persistenceId());
288
289                 // Delete all the messages from the akka journal
290                 deleteMessages(lastSequenceNr());
291             }
292         }
293     }
294
295     private void findLocalShard(FindLocalShard message) {
296         final ShardInformation shardInformation = localShards.get(message.getShardName());
297
298         if(shardInformation == null){
299             getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
300             return;
301         }
302
303         sendResponse(shardInformation, message.isWaitUntilInitialized(), new Supplier<Object>() {
304             @Override
305             public Object get() {
306                 return new LocalShardFound(shardInformation.getActor());
307             }
308         });
309     }
310
311     private void sendResponse(ShardInformation shardInformation, boolean waitUntilInitialized,
312             final Supplier<Object> messageSupplier) {
313         if (!shardInformation.isShardInitialized()) {
314             if(waitUntilInitialized) {
315                 final ActorRef sender = getSender();
316                 final ActorRef self = self();
317                 shardInformation.addRunnableOnInitialized(new Runnable() {
318                     @Override
319                     public void run() {
320                         sender.tell(messageSupplier.get(), self);
321                     }
322                 });
323             } else {
324                 getSender().tell(new ActorNotInitialized(), getSelf());
325             }
326
327             return;
328         }
329
330         getSender().tell(messageSupplier.get(), getSelf());
331     }
332
333     private void memberRemoved(ClusterEvent.MemberRemoved message) {
334         memberNameToAddress.remove(message.member().roles().head());
335     }
336
337     private void memberUp(ClusterEvent.MemberUp message) {
338         String memberName = message.member().roles().head();
339
340         memberNameToAddress.put(memberName, message.member().address());
341
342         for(ShardInformation info : localShards.values()){
343             String shardName = info.getShardName();
344             info.updatePeerAddress(getShardIdentifier(memberName, shardName),
345                 getShardActorPath(shardName, memberName));
346         }
347     }
348
349     private void onDatastoreContext(DatastoreContext context) {
350         datastoreContext = context;
351         for (ShardInformation info : localShards.values()) {
352             if (info.getActor() != null) {
353                 info.getActor().tell(datastoreContext, getSelf());
354             }
355         }
356     }
357
358     /**
359      * Notifies all the local shards of a change in the schema context
360      *
361      * @param message
362      */
363     private void updateSchemaContext(final Object message) {
364         final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
365
366         Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
367         Set<String> newModules = new HashSet<>(128);
368
369         for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
370             String s = moduleIdentifier.getNamespace().toString();
371             newModules.add(s);
372         }
373
374         if(newModules.containsAll(knownModules)) {
375
376             LOG.debug("New SchemaContext has a super set of current knownModules - persisting info");
377
378             knownModules = ImmutableSet.copyOf(newModules);
379
380             dataPersistenceProvider.persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
381
382                 @Override
383                 public void apply(SchemaContextModules param) throws Exception {
384                     LOG.debug("Sending new SchemaContext to Shards");
385                     for (ShardInformation info : localShards.values()) {
386                         if (info.getActor() == null) {
387                             info.setActor(getContext().actorOf(Shard.props(info.getShardId(),
388                                     info.getPeerAddresses(), datastoreContext, schemaContext)
389                                             .withDispatcher(shardDispatcherPath), info.getShardId().toString()));
390                         } else {
391                             info.getActor().tell(message, getSelf());
392                         }
393                         info.getActor().tell(new RegisterRoleChangeListener(), self());
394                     }
395                 }
396
397             });
398         } else {
399             LOG.debug("Rejecting schema context update - not a super set of previously known modules:\nUPDATE: {}\nKNOWN: {}",
400                     newModules, knownModules);
401         }
402
403     }
404
405     private void findPrimary(FindPrimary message) {
406         String shardName = message.getShardName();
407
408         // First see if the there is a local replica for the shard
409         final ShardInformation info = localShards.get(shardName);
410         if (info != null) {
411             sendResponse(info, message.isWaitUntilInitialized(), new Supplier<Object>() {
412                 @Override
413                 public Object get() {
414                     return new PrimaryFound(info.getActorPath().toString()).toSerializable();
415                 }
416             });
417
418             return;
419         }
420
421         List<String> members = configuration.getMembersFromShardName(shardName);
422
423         if(cluster.getCurrentMemberName() != null) {
424             members.remove(cluster.getCurrentMemberName());
425         }
426
427         /**
428          * FIXME: Instead of sending remote shard actor path back to sender,
429          * forward FindPrimary message to remote shard manager
430          */
431         // There is no way for us to figure out the primary (for now) so assume
432         // that one of the remote nodes is a primary
433         for(String memberName : members) {
434             Address address = memberNameToAddress.get(memberName);
435             if(address != null){
436                 String path =
437                     getShardActorPath(shardName, memberName);
438                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
439                 return;
440             }
441         }
442         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
443     }
444
445     private String getShardActorPath(String shardName, String memberName) {
446         Address address = memberNameToAddress.get(memberName);
447         if(address != null) {
448             StringBuilder builder = new StringBuilder();
449             builder.append(address.toString())
450                 .append("/user/")
451                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
452                 .append("/")
453                 .append(getShardIdentifier(memberName, shardName));
454             return builder.toString();
455         }
456         return null;
457     }
458
459     /**
460      * Construct the name of the shard actor given the name of the member on
461      * which the shard resides and the name of the shard
462      *
463      * @param memberName
464      * @param shardName
465      * @return
466      */
467     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
468         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
469     }
470
471     /**
472      * Create shards that are local to the member on which the ShardManager
473      * runs
474      *
475      */
476     private void createLocalShards() {
477         String memberName = this.cluster.getCurrentMemberName();
478         List<String> memberShardNames =
479             this.configuration.getMemberShardNames(memberName);
480
481         List<String> localShardActorNames = new ArrayList<>();
482         for(String shardName : memberShardNames){
483             ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
484             Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
485             localShardActorNames.add(shardId.toString());
486             localShards.put(shardName, new ShardInformation(shardName, shardId, peerAddresses));
487         }
488
489         mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
490                     datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
491     }
492
493     /**
494      * Given the name of the shard find the addresses of all it's peers
495      *
496      * @param shardName
497      * @return
498      */
499     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
500
501         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
502
503         List<String> members =
504             this.configuration.getMembersFromShardName(shardName);
505
506         String currentMemberName = this.cluster.getCurrentMemberName();
507
508         for(String memberName : members){
509             if(!currentMemberName.equals(memberName)){
510                 ShardIdentifier shardId = getShardIdentifier(memberName,
511                     shardName);
512                 String path =
513                     getShardActorPath(shardName, currentMemberName);
514                 peerAddresses.put(shardId, path);
515             }
516         }
517         return peerAddresses;
518     }
519
520     @Override
521     public SupervisorStrategy supervisorStrategy() {
522
523         return new OneForOneStrategy(10, Duration.create("1 minute"),
524             new Function<Throwable, SupervisorStrategy.Directive>() {
525                 @Override
526                 public SupervisorStrategy.Directive apply(Throwable t) {
527                     LOG.warn("Supervisor Strategy caught unexpected exception - resuming", t);
528                     return SupervisorStrategy.resume();
529                 }
530             }
531         );
532
533     }
534
535     @Override
536     public String persistenceId() {
537         return "shard-manager-" + type;
538     }
539
540     @VisibleForTesting
541     Collection<String> getKnownModules() {
542         return knownModules;
543     }
544
545     @VisibleForTesting
546     DataPersistenceProvider getDataPersistenceProvider() {
547         return dataPersistenceProvider;
548     }
549
550     @VisibleForTesting
551     ShardManagerInfoMBean getMBean(){
552         return mBean;
553     }
554
555     private class ShardInformation {
556         private final ShardIdentifier shardId;
557         private final String shardName;
558         private ActorRef actor;
559         private ActorPath actorPath;
560         private final Map<ShardIdentifier, String> peerAddresses;
561
562         // flag that determines if the actor is ready for business
563         private boolean actorInitialized = false;
564
565         private boolean followerSyncStatus = false;
566
567         private final List<Runnable> runnablesOnInitialized = Lists.newArrayList();
568         private String role ;
569
570         private ShardInformation(String shardName, ShardIdentifier shardId,
571                 Map<ShardIdentifier, String> peerAddresses) {
572             this.shardName = shardName;
573             this.shardId = shardId;
574             this.peerAddresses = peerAddresses;
575         }
576
577         String getShardName() {
578             return shardName;
579         }
580
581         ActorRef getActor(){
582             return actor;
583         }
584
585         ActorPath getActorPath() {
586             return actorPath;
587         }
588
589         void setActor(ActorRef actor) {
590             this.actor = actor;
591             this.actorPath = actor.path();
592         }
593
594         ShardIdentifier getShardId() {
595             return shardId;
596         }
597
598         Map<ShardIdentifier, String> getPeerAddresses() {
599             return peerAddresses;
600         }
601
602         void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
603             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
604                 peerAddress);
605             if(peerAddresses.containsKey(peerId)){
606                 peerAddresses.put(peerId, peerAddress);
607
608                 if(actor != null) {
609                     if(LOG.isDebugEnabled()) {
610                         LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
611                                 peerId, peerAddress, actor.path());
612                     }
613
614                     actor.tell(new PeerAddressResolved(peerId, peerAddress), getSelf());
615                 }
616             }
617         }
618
619         boolean isShardInitialized() {
620             return getActor() != null && actorInitialized;
621         }
622
623         void setActorInitialized() {
624             this.actorInitialized = true;
625
626             for(Runnable runnable: runnablesOnInitialized) {
627                 runnable.run();
628             }
629
630             runnablesOnInitialized.clear();
631         }
632
633         void addRunnableOnInitialized(Runnable runnable) {
634             runnablesOnInitialized.add(runnable);
635         }
636
637         public void setRole(String newRole) {
638             this.role = newRole;
639         }
640
641         public String getRole(){
642             return this.role;
643         }
644
645         public void setFollowerSyncStatus(boolean syncStatus){
646             this.followerSyncStatus = syncStatus;
647         }
648
649         public boolean isInSync(){
650             if(RaftState.Follower.name().equals(this.role)){
651                 return followerSyncStatus;
652             } else if(RaftState.Leader.name().equals(this.role)){
653                 return true;
654             }
655
656             return false;
657         }
658
659     }
660
661     private static class ShardManagerCreator implements Creator<ShardManager> {
662         private static final long serialVersionUID = 1L;
663
664         final ClusterWrapper cluster;
665         final Configuration configuration;
666         final DatastoreContext datastoreContext;
667         private final CountDownLatch waitTillReadyCountdownLatch;
668
669         ShardManagerCreator(ClusterWrapper cluster,
670                             Configuration configuration, DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch) {
671             this.cluster = cluster;
672             this.configuration = configuration;
673             this.datastoreContext = datastoreContext;
674             this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
675         }
676
677         @Override
678         public ShardManager create() throws Exception {
679             return new ShardManager(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch);
680         }
681     }
682
683     static class SchemaContextModules implements Serializable {
684         private static final long serialVersionUID = -8884620101025936590L;
685
686         private final Set<String> modules;
687
688         SchemaContextModules(Set<String> modules){
689             this.modules = modules;
690         }
691
692         public Set<String> getModules() {
693             return modules;
694         }
695     }
696 }
697
698
699