d7b4932d5dd6f8304aee3701509001ff0ded5ff7
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardManagerInfo.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.shardmanager;
10
11 import akka.actor.ActorRef;
12 import akka.pattern.Patterns;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Throwables;
15 import java.util.List;
16 import org.opendaylight.controller.cluster.access.concepts.MemberName;
17 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
18 import org.opendaylight.controller.cluster.raft.RaftState;
19 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Await;
23 import scala.concurrent.duration.Duration;
24
25 final class ShardManagerInfo extends AbstractMXBean implements ShardManagerInfoMBean {
26
27     public static final String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
28
29     private static final Logger LOG = LoggerFactory.getLogger(ShardManagerInfo.class);
30     private static final long ASK_TIMEOUT_MILLIS = 5000;
31
32     private final ActorRef shardManager;
33     private final MemberName memberName;
34
35     private volatile boolean syncStatus = false;
36
37
38     ShardManagerInfo(final ActorRef shardManager, final MemberName memberName, final String name,
39         final String mxBeanType) {
40         super(name, mxBeanType, JMX_CATEGORY_SHARD_MANAGER);
41         this.shardManager = Preconditions.checkNotNull(shardManager);
42         this.memberName = Preconditions.checkNotNull(memberName);
43     }
44
45     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
46     @Override
47     public List<String> getLocalShards() {
48         try {
49             return (List<String>) Await.result(
50                 Patterns.ask(shardManager, GetLocalShardIds.INSTANCE, ASK_TIMEOUT_MILLIS), Duration.Inf());
51         } catch (Exception e) {
52             throw Throwables.propagate(e);
53         }
54     }
55
56     @Override
57     public boolean getSyncStatus() {
58         return syncStatus;
59     }
60
61     void setSyncStatus(boolean syncStatus) {
62         this.syncStatus = syncStatus;
63     }
64
65     @Override
66     public String getMemberName() {
67         return memberName.getName();
68     }
69
70     @SuppressWarnings("checkstyle:IllegalCatch")
71     private void requestSwitchShardState(final ShardIdentifier shardId, final String newState, final long term) {
72         // Validates strings argument
73         final RaftState state = RaftState.valueOf(newState);
74
75         // Leader and Follower are the only states to which we can switch externally
76         switch (state) {
77             case Follower:
78             case Leader:
79                 try {
80                     Await.result(Patterns.ask(shardManager, new SwitchShardBehavior(shardId, state, term),
81                         ASK_TIMEOUT_MILLIS), Duration.Inf());
82                 } catch (Exception e) {
83                     throw Throwables.propagate(e);
84                 }
85                 break;
86             case Candidate:
87             case IsolatedLeader:
88             default:
89                 throw new IllegalArgumentException("Illegal target state " + state);
90         }
91     }
92
93     @Override
94     public void switchAllLocalShardsState(String newState, long term) {
95         LOG.info("switchAllLocalShardsState called newState = {}, term = {}", newState, term);
96         requestSwitchShardState(null, newState, term);
97     }
98
99     @Override
100     public void switchShardState(String shardId, String newState, long term) {
101         final ShardIdentifier identifier = ShardIdentifier.fromShardIdString(shardId);
102         LOG.info("switchShardState called shardName = {}, newState = {}, term = {}", shardId, newState, term);
103         requestSwitchShardState(identifier, newState, term);
104     }
105 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.