2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
18 import java.util.HashMap;
19 import java.util.List;
23 * The ShardManager has the following jobs,
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
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.
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
41 public class ShardManager extends UntypedActor {
43 // Stores a mapping between a shard name and the address of the current primary
44 private final Map<String, Address> shardNameToPrimaryAddress = new HashMap<>();
46 // Stores a mapping between a member name and the address of the member
47 private final Map<String, Address> memberNameToAddress = new HashMap<>();
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<>();
52 LoggingAdapter log = Logging.getLogger(getContext().system(), this);
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());