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.shardmanager;
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.datastore.identifiers.ShardIdentifier;
17 import org.opendaylight.controller.cluster.raft.RaftState;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.Await;
22 import scala.concurrent.duration.Duration;
24 final class ShardManagerInfo extends AbstractMXBean implements ShardManagerInfoMBean {
26 public static final String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
28 private static final Logger LOG = LoggerFactory.getLogger(ShardManagerInfo.class);
29 private static final long ASK_TIMEOUT_MILLIS = 5000;
31 private final ActorRef shardManager;
32 private final String memberName;
34 private volatile boolean syncStatus = false;
37 ShardManagerInfo(final ActorRef shardManager, final String memberName, final String name,
38 final String mxBeanType) {
39 super(name, mxBeanType, JMX_CATEGORY_SHARD_MANAGER);
40 this.shardManager = Preconditions.checkNotNull(shardManager);
41 this.memberName = Preconditions.checkNotNull(memberName);
44 @SuppressWarnings("unchecked")
46 public List<String> getLocalShards() {
48 return (List<String>) Await.result(
49 Patterns.ask(shardManager, GetLocalShardIds.INSTANCE, ASK_TIMEOUT_MILLIS), Duration.Inf());
50 } catch (Exception e) {
51 throw Throwables.propagate(e);
56 public boolean getSyncStatus() {
60 void setSyncStatus(boolean syncStatus) {
61 this.syncStatus = syncStatus;
65 public String getMemberName() {
69 private void requestSwitchShardState(final ShardIdentifier shardId, final String newState, final long term) {
70 // Validates strings argument
71 final RaftState state = RaftState.valueOf(newState);
73 // Leader and Follower are the only states to which we can switch externally
78 Await.result(Patterns.ask(shardManager, new SwitchShardBehavior(shardId, state, term),
79 ASK_TIMEOUT_MILLIS), Duration.Inf());
80 } catch (Exception e) {
81 throw Throwables.propagate(e);
87 throw new IllegalArgumentException("Illegal target state " + state);
92 public void switchAllLocalShardsState(String newState, long term) {
93 LOG.info("switchAllLocalShardsState called newState = {}, term = {}", newState, term);
94 requestSwitchShardState(null, newState, term);
98 public void switchShardState(String shardId, String newState, long term) {
99 final ShardIdentifier identifier = ShardIdentifier.builder().fromShardIdString(
100 Preconditions.checkNotNull(shardId, "Shard id may not be null")).build();
101 LOG.info("switchShardState called shardName = {}, newState = {}, term = {}", shardId, newState, term);
102 requestSwitchShardState(identifier, newState, term);