Merge "BUG-1200 added functions for help depends on argument (-help arg)"
[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.Props;
15 import akka.actor.UntypedActor;
16 import akka.event.Logging;
17 import akka.event.LoggingAdapter;
18 import akka.japi.Creator;
19 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
20 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
21 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
22 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 /**
29  * The ShardManager has the following jobs,
30  * <p>
31  * <li> Create all the local shard replicas that belong on this cluster member
32  * <li> Find the primary replica for any given shard
33  * <li> Engage in shard replica elections which decide which replica should be the primary
34  * </p>
35  * <p/>
36  * <h3>>Creation of Shard replicas</h3
37  * <p>
38  * When the ShardManager is constructed it reads the cluster configuration to find out which shard replicas
39  * belong on this member. It finds out the name of the current cluster member from the Akka Clustering Service.
40  * </p>
41  * <p/>
42  * <h3> Replica Elections </h3>
43  * <p/>
44  * <p>
45  * The Shard Manager uses multiple cues to initiate election.
46  * <li> When a member of the cluster dies
47  * <li> When a local shard replica dies
48  * <li> When a local shard replica comes alive
49  * </p>
50  */
51 public class ShardManager extends UntypedActor {
52
53   // Stores a mapping between a shard name and the address of the current primary
54   private final Map<String, Address> shardNameToPrimaryAddress = new HashMap<>();
55
56   // Stores a mapping between a member name and the address of the member
57   private final Map<String, Address> memberNameToAddress = new HashMap<>();
58
59   // Stores a mapping between the shard name and all the members on which a replica of that shard are available
60   private final Map<String, List<String>> shardNameToMembers = new HashMap<>();
61
62   private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
63
64   private final ActorPath defaultShardPath;
65
66   /**
67    *
68    * @param type defines the kind of data that goes into shards created by this shard manager. Examples of type would be
69    *             configuration or operational
70    */
71   private ShardManager(String type){
72     ActorRef actor = getContext().actorOf(Shard.props(Shard.DEFAULT_NAME + "-" + type));
73     defaultShardPath = actor.path();
74   }
75
76   public static Props props(final String type){
77     return Props.create(new Creator<ShardManager>(){
78
79       @Override
80       public ShardManager create() throws Exception {
81         return new ShardManager(type);
82       }
83     });
84   }
85
86   @Override
87   public void onReceive(Object message) throws Exception {
88     if (message instanceof FindPrimary) {
89       FindPrimary msg = ((FindPrimary) message);
90       String shardName = msg.getShardName();
91       if(Shard.DEFAULT_NAME.equals(shardName)){
92         getSender().tell(new PrimaryFound(defaultShardPath.toString()), getSelf());
93       } else {
94         getSender().tell(new PrimaryNotFound(shardName), getSelf());
95       }
96     } else if(message instanceof UpdateSchemaContext){
97         // FIXME : Notify all local shards of a context change
98         getContext().system().actorSelection(defaultShardPath).forward(message, getContext());
99     }
100   }
101
102
103 }