Merge "BUG 2047 - HTTP GET - no returning error message"
[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.AbstractUntypedPersistentActorWithMetering;
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.ActorInitialized;
33 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
34 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
35 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
36 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
37 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
38 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
39 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
40 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
41 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import scala.concurrent.duration.Duration;
45
46 import java.io.Serializable;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Set;
54
55 /**
56  * The ShardManager has the following jobs,
57  * <ul>
58  * <li> Create all the local shard replicas that belong on this cluster member
59  * <li> Find the address of the local shard
60  * <li> Find the primary replica for any given shard
61  * <li> Monitor the cluster members and store their addresses
62  * <ul>
63  */
64 public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
65
66     protected final LoggingAdapter LOG =
67         Logging.getLogger(getContext().system(), this);
68
69     // Stores a mapping between a member name and the address of the member
70     // Member names look like "member-1", "member-2" etc and are as specified
71     // in configuration
72     private final Map<String, Address> memberNameToAddress = new HashMap<>();
73
74     // Stores a mapping between a shard name and it's corresponding information
75     // Shard names look like inventory, topology etc and are as specified in
76     // configuration
77     private final Map<String, ShardInformation> localShards = new HashMap<>();
78
79     // The type of a ShardManager reflects the type of the datastore itself
80     // A data store could be of type config/operational
81     private final String type;
82
83     private final ClusterWrapper cluster;
84
85     private final Configuration configuration;
86
87     private ShardManagerInfoMBean mBean;
88
89     private final DatastoreContext datastoreContext;
90
91     private final Collection<String> knownModules = new HashSet<>(128);
92
93     /**
94      * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
95      *             configuration or operational
96      */
97     private ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
98             DatastoreContext datastoreContext) {
99
100         this.type = Preconditions.checkNotNull(type, "type should not be null");
101         this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
102         this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
103         this.datastoreContext = datastoreContext;
104
105         // Subscribe this actor to cluster member events
106         cluster.subscribeToMemberEvents(getSelf());
107
108         //createLocalShards(null);
109     }
110
111     public static Props props(final String type,
112         final ClusterWrapper cluster,
113         final Configuration configuration,
114         final DatastoreContext datastoreContext) {
115
116         Preconditions.checkNotNull(type, "type should not be null");
117         Preconditions.checkNotNull(cluster, "cluster should not be null");
118         Preconditions.checkNotNull(configuration, "configuration should not be null");
119
120         return Props.create(new ShardManagerCreator(type, cluster, configuration, datastoreContext));
121     }
122
123     @Override
124     public void handleCommand(Object message) throws Exception {
125         if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
126             findPrimary(
127                 FindPrimary.fromSerializable(message));
128         } else if(message instanceof FindLocalShard){
129             findLocalShard((FindLocalShard) message);
130         } else if (message instanceof UpdateSchemaContext) {
131             updateSchemaContext(message);
132         } else if(message instanceof ActorInitialized) {
133             onActorInitialized(message);
134         } else if (message instanceof ClusterEvent.MemberUp){
135             memberUp((ClusterEvent.MemberUp) message);
136         } else if(message instanceof ClusterEvent.MemberRemoved) {
137             memberRemoved((ClusterEvent.MemberRemoved) message);
138         } else if(message instanceof ClusterEvent.UnreachableMember) {
139             ignoreMessage(message);
140         } else{
141             unknownMessage(message);
142         }
143
144     }
145
146     private void onActorInitialized(Object message) {
147         final ActorRef sender = getSender();
148
149         if (sender == null) {
150             return; //why is a non-actor sending this message? Just ignore.
151         }
152
153         String actorName = sender.path().name();
154         //find shard name from actor name; actor name is stringified shardId
155         ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(actorName).build();
156
157         if (shardId.getShardName() == null) {
158             return;
159         }
160         markShardAsInitialized(shardId.getShardName());
161     }
162
163     @VisibleForTesting protected void markShardAsInitialized(String shardName) {
164         LOG.debug("Initializing shard [{}]", shardName);
165         ShardInformation shardInformation = localShards.get(shardName);
166         if (shardInformation != null) {
167             shardInformation.setShardInitialized(true);
168         }
169     }
170
171     @Override protected void handleRecover(Object message) throws Exception {
172
173         if(message instanceof SchemaContextModules){
174             SchemaContextModules msg = (SchemaContextModules) message;
175             knownModules.clear();
176             knownModules.addAll(msg.getModules());
177         } else if(message instanceof RecoveryFailure){
178             RecoveryFailure failure = (RecoveryFailure) message;
179             LOG.error(failure.cause(), "Recovery failed");
180         } else if(message instanceof RecoveryCompleted){
181             LOG.info("Recovery complete : {}", persistenceId());
182
183             // Delete all the messages from the akka journal except the last one
184             deleteMessages(lastSequenceNr() - 1);
185         }
186     }
187
188     private void findLocalShard(FindLocalShard message) {
189         ShardInformation shardInformation = localShards.get(message.getShardName());
190
191         if(shardInformation == null){
192             getSender().tell(new LocalShardNotFound(message.getShardName()), getSelf());
193             return;
194         }
195
196         sendResponse(shardInformation, new LocalShardFound(shardInformation.getActor()));
197     }
198
199     private void sendResponse(ShardInformation shardInformation, Object message) {
200         if (!shardInformation.isShardInitialized()) {
201             getSender().tell(new ActorNotInitialized(), getSelf());
202             return;
203         }
204
205         getSender().tell(message, getSelf());
206     }
207
208     private void memberRemoved(ClusterEvent.MemberRemoved message) {
209         memberNameToAddress.remove(message.member().roles().head());
210     }
211
212     private void memberUp(ClusterEvent.MemberUp message) {
213         String memberName = message.member().roles().head();
214
215         memberNameToAddress.put(memberName, message.member().address());
216
217         for(ShardInformation info : localShards.values()){
218             String shardName = info.getShardName();
219             info.updatePeerAddress(getShardIdentifier(memberName, shardName),
220                 getShardActorPath(shardName, memberName));
221         }
222     }
223
224     /**
225      * Notifies all the local shards of a change in the schema context
226      *
227      * @param message
228      */
229     private void updateSchemaContext(final Object message) {
230         final SchemaContext schemaContext = ((UpdateSchemaContext) message).getSchemaContext();
231
232         Set<ModuleIdentifier> allModuleIdentifiers = schemaContext.getAllModuleIdentifiers();
233         Set<String> newModules = new HashSet<>(128);
234
235         for(ModuleIdentifier moduleIdentifier : allModuleIdentifiers){
236             String s = moduleIdentifier.getNamespace().toString();
237             newModules.add(s);
238         }
239
240         if(newModules.containsAll(knownModules)) {
241
242             LOG.info("New SchemaContext has a super set of current knownModules - persisting info");
243
244             knownModules.clear();
245             knownModules.addAll(newModules);
246
247             persist(new SchemaContextModules(newModules), new Procedure<SchemaContextModules>() {
248
249                 @Override public void apply(SchemaContextModules param) throws Exception {
250                     LOG.info("Sending new SchemaContext to Shards");
251                     if (localShards.size() == 0) {
252                         createLocalShards(schemaContext);
253                     } else {
254                         for (ShardInformation info : localShards.values()) {
255                             info.getActor().tell(message, getSelf());
256                         }
257                     }
258                 }
259
260             });
261         } else {
262             LOG.info("Rejecting schema context update because it is not a super set of previously known modules");
263         }
264
265     }
266
267     private void findPrimary(FindPrimary message) {
268         final ActorRef sender = getSender();
269         String shardName = message.getShardName();
270
271         // First see if the there is a local replica for the shard
272         ShardInformation info = localShards.get(shardName);
273         if (info != null) {
274             ActorPath shardPath = info.getActorPath();
275             sendResponse(info, new PrimaryFound(shardPath.toString()).toSerializable());
276             return;
277         }
278
279         List<String> members = configuration.getMembersFromShardName(shardName);
280
281         if(cluster.getCurrentMemberName() != null) {
282             members.remove(cluster.getCurrentMemberName());
283         }
284
285         /**
286          * FIXME: Instead of sending remote shard actor path back to sender,
287          * forward FindPrimary message to remote shard manager
288          */
289         // There is no way for us to figure out the primary (for now) so assume
290         // that one of the remote nodes is a primary
291         for(String memberName : members) {
292             Address address = memberNameToAddress.get(memberName);
293             if(address != null){
294                 String path =
295                     getShardActorPath(shardName, memberName);
296                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
297                 return;
298             }
299         }
300         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
301     }
302
303     private String getShardActorPath(String shardName, String memberName) {
304         Address address = memberNameToAddress.get(memberName);
305         if(address != null) {
306             StringBuilder builder = new StringBuilder();
307             builder.append(address.toString())
308                 .append("/user/")
309                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
310                 .append("/")
311                 .append(getShardIdentifier(memberName, shardName));
312             return builder.toString();
313         }
314         return null;
315     }
316
317     /**
318      * Construct the name of the shard actor given the name of the member on
319      * which the shard resides and the name of the shard
320      *
321      * @param memberName
322      * @param shardName
323      * @return
324      */
325     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
326         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
327     }
328
329     /**
330      * Create shards that are local to the member on which the ShardManager
331      * runs
332      *
333      */
334     private void createLocalShards(SchemaContext schemaContext) {
335         String memberName = this.cluster.getCurrentMemberName();
336         List<String> memberShardNames =
337             this.configuration.getMemberShardNames(memberName);
338
339         List<String> localShardActorNames = new ArrayList<>();
340         for(String shardName : memberShardNames){
341             ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
342             Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
343             ActorRef actor = getContext()
344                 .actorOf(Shard.props(shardId, peerAddresses, datastoreContext, schemaContext),
345                     shardId.toString());
346             localShardActorNames.add(shardId.toString());
347             localShards.put(shardName, new ShardInformation(shardName, actor, peerAddresses));
348         }
349
350         mBean = ShardManagerInfo.createShardManagerMBean("shard-manager-" + this.type,
351                     datastoreContext.getDataStoreMXBeanType(), localShardActorNames);
352     }
353
354     /**
355      * Given the name of the shard find the addresses of all it's peers
356      *
357      * @param shardName
358      * @return
359      */
360     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
361
362         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
363
364         List<String> members =
365             this.configuration.getMembersFromShardName(shardName);
366
367         String currentMemberName = this.cluster.getCurrentMemberName();
368
369         for(String memberName : members){
370             if(!currentMemberName.equals(memberName)){
371                 ShardIdentifier shardId = getShardIdentifier(memberName,
372                     shardName);
373                 String path =
374                     getShardActorPath(shardName, currentMemberName);
375                 peerAddresses.put(shardId, path);
376             }
377         }
378         return peerAddresses;
379     }
380
381     @Override
382     public SupervisorStrategy supervisorStrategy() {
383
384         return new OneForOneStrategy(10, Duration.create("1 minute"),
385             new Function<Throwable, SupervisorStrategy.Directive>() {
386                 @Override
387                 public SupervisorStrategy.Directive apply(Throwable t) {
388                     StringBuilder sb = new StringBuilder();
389                     for(StackTraceElement element : t.getStackTrace()) {
390                        sb.append("\n\tat ")
391                          .append(element.toString());
392                     }
393                     LOG.warning("Supervisor Strategy of resume applied {}",sb.toString());
394                     return SupervisorStrategy.resume();
395                 }
396             }
397         );
398
399     }
400
401     @Override public String persistenceId() {
402         return "shard-manager-" + type;
403     }
404
405     @VisibleForTesting public Collection<String> getKnownModules() {
406         return knownModules;
407     }
408
409     private class ShardInformation {
410         private final String shardName;
411         private final ActorRef actor;
412         private final ActorPath actorPath;
413         private final Map<ShardIdentifier, String> peerAddresses;
414         private boolean shardInitialized = false; //flag that determines if the actor is ready for business
415
416         private ShardInformation(String shardName, ActorRef actor,
417             Map<ShardIdentifier, String> peerAddresses) {
418             this.shardName = shardName;
419             this.actor = actor;
420             this.actorPath = actor.path();
421             this.peerAddresses = peerAddresses;
422         }
423
424         public String getShardName() {
425             return shardName;
426         }
427
428         public ActorRef getActor(){
429             return actor;
430         }
431
432         public ActorPath getActorPath() {
433             return actorPath;
434         }
435
436         public void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
437             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
438                 peerAddress);
439             if(peerAddresses.containsKey(peerId)){
440                 peerAddresses.put(peerId, peerAddress);
441                 if(LOG.isDebugEnabled()) {
442                     LOG.debug(
443                         "Sending PeerAddressResolved for peer {} with address {} to {}",
444                         peerId, peerAddress, actor.path());
445                 }
446                 actor
447                     .tell(new PeerAddressResolved(peerId, peerAddress),
448                         getSelf());
449
450             }
451         }
452
453         public boolean isShardInitialized() {
454             return shardInitialized;
455         }
456
457         public void setShardInitialized(boolean shardInitialized) {
458             this.shardInitialized = shardInitialized;
459         }
460     }
461
462     private static class ShardManagerCreator implements Creator<ShardManager> {
463         private static final long serialVersionUID = 1L;
464
465         final String type;
466         final ClusterWrapper cluster;
467         final Configuration configuration;
468         final DatastoreContext datastoreContext;
469
470         ShardManagerCreator(String type, ClusterWrapper cluster,
471                 Configuration configuration, DatastoreContext datastoreContext) {
472             this.type = type;
473             this.cluster = cluster;
474             this.configuration = configuration;
475             this.datastoreContext = datastoreContext;
476         }
477
478         @Override
479         public ShardManager create() throws Exception {
480             return new ShardManager(type, cluster, configuration, datastoreContext);
481         }
482     }
483
484     static class SchemaContextModules implements Serializable {
485         private final Set<String> modules;
486
487         SchemaContextModules(Set<String> modules){
488             this.modules = modules;
489         }
490
491         public Set<String> getModules() {
492             return modules;
493         }
494     }
495 }
496
497
498