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