Merge "Bug 1569 - [DEV] Too small variable for OF-Port-Number"
[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
21 import com.google.common.base.Preconditions;
22
23 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
24 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
25 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfo;
26 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shardmanager.ShardManagerInfoMBean;
27 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
28 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
29 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
30 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
31 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
32 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
33 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
34 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
35
36 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
37 import scala.concurrent.duration.Duration;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 /**
45  * The ShardManager has the following jobs,
46  * <ul>
47  * <li> Create all the local shard replicas that belong on this cluster member
48  * <li> Find the address of the local shard
49  * <li> Find the primary replica for any given shard
50  * <li> Monitor the cluster members and store their addresses
51  * <ul>
52  */
53 public class ShardManager extends AbstractUntypedActor {
54
55     // Stores a mapping between a member name and the address of the member
56     // Member names look like "member-1", "member-2" etc and are as specified
57     // in configuration
58     private final Map<String, Address> memberNameToAddress = new HashMap<>();
59
60     // Stores a mapping between a shard name and it's corresponding information
61     // Shard names look like inventory, topology etc and are as specified in
62     // configuration
63     private final Map<String, ShardInformation> localShards = new HashMap<>();
64
65     // The type of a ShardManager reflects the type of the datastore itself
66     // A data store could be of type config/operational
67     private final String type;
68
69     private final ClusterWrapper cluster;
70
71     private final Configuration configuration;
72
73     private ShardManagerInfoMBean mBean;
74
75     private final ShardContext shardContext;
76
77     /**
78      * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
79      *             configuration or operational
80      */
81     private ShardManager(String type, ClusterWrapper cluster, Configuration configuration,
82             ShardContext shardContext) {
83
84         this.type = Preconditions.checkNotNull(type, "type should not be null");
85         this.cluster = Preconditions.checkNotNull(cluster, "cluster should not be null");
86         this.configuration = Preconditions.checkNotNull(configuration, "configuration should not be null");
87         this.shardContext = shardContext;
88
89         // Subscribe this actor to cluster member events
90         cluster.subscribeToMemberEvents(getSelf());
91
92         // Create all the local Shards and make them a child of the ShardManager
93         // TODO: This may need to be initiated when we first get the schema context
94         createLocalShards();
95     }
96
97     public static Props props(final String type,
98         final ClusterWrapper cluster,
99         final Configuration configuration,
100         final ShardContext shardContext) {
101
102         Preconditions.checkNotNull(type, "type should not be null");
103         Preconditions.checkNotNull(cluster, "cluster should not be null");
104         Preconditions.checkNotNull(configuration, "configuration should not be null");
105
106         return Props.create(new ShardManagerCreator(type, cluster, configuration, shardContext));
107     }
108
109     @Override
110     public void handleReceive(Object message) throws Exception {
111         if (message.getClass().equals(FindPrimary.SERIALIZABLE_CLASS)) {
112             findPrimary(
113                 FindPrimary.fromSerializable(message));
114         } else if(message instanceof FindLocalShard){
115             findLocalShard((FindLocalShard) message);
116         } else if (message instanceof UpdateSchemaContext) {
117             updateSchemaContext(message);
118         } else if (message instanceof ClusterEvent.MemberUp){
119             memberUp((ClusterEvent.MemberUp) message);
120         } else if(message instanceof ClusterEvent.MemberRemoved) {
121             memberRemoved((ClusterEvent.MemberRemoved) message);
122         } else if(message instanceof ClusterEvent.UnreachableMember) {
123             ignoreMessage(message);
124         } else{
125             unknownMessage(message);
126         }
127
128     }
129
130     private void findLocalShard(FindLocalShard message) {
131         ShardInformation shardInformation =
132             localShards.get(message.getShardName());
133
134         if(shardInformation != null){
135             getSender().tell(new LocalShardFound(shardInformation.getActor()), getSelf());
136             return;
137         }
138
139         getSender().tell(new LocalShardNotFound(message.getShardName()),
140             getSelf());
141     }
142
143     private void memberRemoved(ClusterEvent.MemberRemoved message) {
144         memberNameToAddress.remove(message.member().roles().head());
145     }
146
147     private void memberUp(ClusterEvent.MemberUp message) {
148         String memberName = message.member().roles().head();
149
150         memberNameToAddress.put(memberName , message.member().address());
151
152         for(ShardInformation info : localShards.values()){
153             String shardName = info.getShardName();
154             info.updatePeerAddress(getShardIdentifier(memberName, shardName),
155                 getShardActorPath(shardName, memberName));
156         }
157     }
158
159     /**
160      * Notifies all the local shards of a change in the schema context
161      *
162      * @param message
163      */
164     private void updateSchemaContext(Object message) {
165         for(ShardInformation info : localShards.values()){
166             info.getActor().tell(message,getSelf());
167         }
168     }
169
170     private void findPrimary(FindPrimary message) {
171         String shardName = message.getShardName();
172
173         // First see if the there is a local replica for the shard
174         ShardInformation info = localShards.get(shardName);
175         if(info != null) {
176             ActorPath shardPath = info.getActorPath();
177             if (shardPath != null) {
178                 getSender()
179                     .tell(
180                         new PrimaryFound(shardPath.toString()).toSerializable(),
181                         getSelf());
182                 return;
183             }
184         }
185
186         List<String> members =
187             configuration.getMembersFromShardName(shardName);
188
189         if(cluster.getCurrentMemberName() != null) {
190             members.remove(cluster.getCurrentMemberName());
191         }
192
193         // There is no way for us to figure out the primary (for now) so assume
194         // that one of the remote nodes is a primary
195         for(String memberName : members) {
196             Address address = memberNameToAddress.get(memberName);
197             if(address != null){
198                 String path =
199                     getShardActorPath(shardName, memberName);
200                 getSender().tell(new PrimaryFound(path).toSerializable(), getSelf());
201                 return;
202             }
203         }
204         getSender().tell(new PrimaryNotFound(shardName).toSerializable(), getSelf());
205     }
206
207     private String getShardActorPath(String shardName, String memberName) {
208         Address address = memberNameToAddress.get(memberName);
209         if(address != null) {
210             StringBuilder builder = new StringBuilder();
211             builder.append(address.toString())
212                 .append("/user/")
213                 .append(ShardManagerIdentifier.builder().type(type).build().toString())
214                 .append("/")
215                 .append(getShardIdentifier(memberName, shardName));
216             return builder.toString();
217         }
218         return null;
219     }
220
221     /**
222      * Construct the name of the shard actor given the name of the member on
223      * which the shard resides and the name of the shard
224      *
225      * @param memberName
226      * @param shardName
227      * @return
228      */
229     private ShardIdentifier getShardIdentifier(String memberName, String shardName){
230         return ShardIdentifier.builder().memberName(memberName).shardName(shardName).type(type).build();
231     }
232
233     /**
234      * Create shards that are local to the member on which the ShardManager
235      * runs
236      *
237      */
238     private void createLocalShards() {
239         String memberName = this.cluster.getCurrentMemberName();
240         List<String> memberShardNames =
241             this.configuration.getMemberShardNames(memberName);
242
243         List<String> localShardActorNames = new ArrayList<>();
244         for(String shardName : memberShardNames){
245             ShardIdentifier shardId = getShardIdentifier(memberName, shardName);
246             Map<ShardIdentifier, String> peerAddresses = getPeerAddresses(shardName);
247             ActorRef actor = getContext()
248                 .actorOf(Shard.props(shardId, peerAddresses, shardContext).
249                     withMailbox(ActorContext.MAILBOX), shardId.toString());
250
251             localShardActorNames.add(shardId.toString());
252             localShards.put(shardName, new ShardInformation(shardName, actor, peerAddresses));
253         }
254
255         mBean = ShardManagerInfo
256             .createShardManagerMBean("shard-manager-" + this.type, localShardActorNames);
257
258     }
259
260     /**
261      * Given the name of the shard find the addresses of all it's peers
262      *
263      * @param shardName
264      * @return
265      */
266     private Map<ShardIdentifier, String> getPeerAddresses(String shardName){
267
268         Map<ShardIdentifier, String> peerAddresses = new HashMap<>();
269
270         List<String> members =
271             this.configuration.getMembersFromShardName(shardName);
272
273         String currentMemberName = this.cluster.getCurrentMemberName();
274
275         for(String memberName : members){
276             if(!currentMemberName.equals(memberName)){
277                 ShardIdentifier shardId = getShardIdentifier(memberName,
278                     shardName);
279                 String path =
280                     getShardActorPath(shardName, currentMemberName);
281                 peerAddresses.put(shardId, path);
282             }
283         }
284         return peerAddresses;
285     }
286
287     @Override
288     public SupervisorStrategy supervisorStrategy() {
289
290         return new OneForOneStrategy(10, Duration.create("1 minute"),
291             new Function<Throwable, SupervisorStrategy.Directive>() {
292                 @Override
293                 public SupervisorStrategy.Directive apply(Throwable t) {
294                     StringBuilder sb = new StringBuilder();
295                     for(StackTraceElement element : t.getStackTrace()) {
296                        sb.append("\n\tat ")
297                          .append(element.toString());
298                     }
299                     LOG.warning("Supervisor Strategy of resume applied {}",sb.toString());
300                     return SupervisorStrategy.resume();
301                 }
302             }
303         );
304
305     }
306
307     private class ShardInformation {
308         private final String shardName;
309         private final ActorRef actor;
310         private final ActorPath actorPath;
311         private final Map<ShardIdentifier, String> peerAddresses;
312
313         private ShardInformation(String shardName, ActorRef actor,
314             Map<ShardIdentifier, String> peerAddresses) {
315             this.shardName = shardName;
316             this.actor = actor;
317             this.actorPath = actor.path();
318             this.peerAddresses = peerAddresses;
319         }
320
321         public String getShardName() {
322             return shardName;
323         }
324
325         public ActorRef getActor(){
326             return actor;
327         }
328
329         public ActorPath getActorPath() {
330             return actorPath;
331         }
332
333         public void updatePeerAddress(ShardIdentifier peerId, String peerAddress){
334             LOG.info("updatePeerAddress for peer {} with address {}", peerId,
335                 peerAddress);
336             if(peerAddresses.containsKey(peerId)){
337                 peerAddresses.put(peerId, peerAddress);
338
339                 LOG.debug(
340                     "Sending PeerAddressResolved for peer {} with address {} to {}",
341                     peerId, peerAddress, actor.path());
342
343                 actor
344                     .tell(new PeerAddressResolved(peerId, peerAddress),
345                         getSelf());
346
347             }
348         }
349     }
350
351     private static class ShardManagerCreator implements Creator<ShardManager> {
352         private static final long serialVersionUID = 1L;
353
354         final String type;
355         final ClusterWrapper cluster;
356         final Configuration configuration;
357         final ShardContext shardContext;
358
359         ShardManagerCreator(String type, ClusterWrapper cluster,
360                 Configuration configuration, ShardContext shardContext) {
361             this.type = type;
362             this.cluster = cluster;
363             this.configuration = configuration;
364             this.shardContext = shardContext;
365         }
366
367         @Override
368         public ShardManager create() throws Exception {
369             return new ShardManager(type, cluster, configuration, shardContext);
370         }
371     }
372 }
373
374
375