Introducing ShardManager
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardManager.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.Address;
4 import akka.actor.UntypedActor;
5 import akka.event.Logging;
6 import akka.event.LoggingAdapter;
7 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
8 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
9
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 /**
15  * The ShardManager has the following jobs,
16  *
17  *  - Create all the local shard replicas that belong on this cluster member
18  *  - Find the primary replica for any given shard
19  *  - Engage in shard replica elections which decide which replica should be the primary
20  *
21  * Creation of Shard replicas
22  * ==========================
23  *  When the ShardManager is constructed it reads the cluster configuration to find out which shard replicas
24  *  belong on this member. It finds out the name of the current cluster member from the Akka Clustering Service.
25  *
26  * Replica Elections
27  * =================
28  *  The Shard Manager uses multiple cues to initiate election.
29  *      - When a member of the cluster dies
30  *      - When a local shard replica dies
31  *      - When a local shard replica comes alive
32  */
33 public class ShardManager extends UntypedActor {
34
35     // Stores a mapping between a shard name and the address of the current primary
36     private final Map<String, Address> shardNameToPrimaryAddress = new HashMap<>();
37
38     // Stores a mapping between a member name and the address of the member
39     private final Map<String, Address> memberNameToAddress = new HashMap<>();
40
41     // Stores a mapping between the shard name and all the members on which a replica of that shard are available
42     private final Map<String, List<String>> shardNameToMembers = new HashMap<>();
43
44     LoggingAdapter log = Logging.getLogger(getContext().system(), this);
45
46     @Override
47     public void onReceive(Object message) throws Exception {
48         if(message instanceof FindPrimary ){
49             FindPrimary msg = ((FindPrimary) message);
50             getSender().tell(new PrimaryNotFound(msg.getShardName()), getSelf());
51         }
52     }
53 }