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
8 package org.opendaylight.controller.cluster.datastore.shardmanager;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.pattern.Patterns;
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;
25 final class ShardManagerInfo extends AbstractMXBean implements ShardManagerInfoMBean {
27 public static final String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
29 private static final Logger LOG = LoggerFactory.getLogger(ShardManagerInfo.class);
30 private static final long ASK_TIMEOUT_MILLIS = 5000;
32 private final ActorRef shardManager;
33 private final MemberName memberName;
35 private volatile boolean syncStatus = false;
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 = requireNonNull(shardManager);
42 this.memberName = requireNonNull(memberName);
45 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
47 public List<String> getLocalShards() {
49 return (List<String>) Await.result(
50 Patterns.ask(shardManager, GetLocalShardIds.INSTANCE, ASK_TIMEOUT_MILLIS), Duration.Inf());
51 } catch (Exception e) {
52 Throwables.throwIfUnchecked(e);
53 throw new IllegalStateException(e);
58 public boolean getSyncStatus() {
62 void setSyncStatus(final boolean syncStatus) {
63 this.syncStatus = syncStatus;
67 public String getMemberName() {
68 return memberName.getName();
71 @SuppressWarnings("checkstyle:IllegalCatch")
72 private void requestSwitchShardState(final ShardIdentifier shardId, final String newState, final long term) {
73 // Validates strings argument
74 final RaftState state = RaftState.valueOf(newState);
76 // Leader and Follower are the only states to which we can switch externally
81 Await.result(Patterns.ask(shardManager, new SwitchShardBehavior(shardId, state, term),
82 ASK_TIMEOUT_MILLIS), Duration.Inf());
83 } catch (Exception e) {
84 Throwables.throwIfUnchecked(e);
85 throw new IllegalStateException(e);
91 throw new IllegalArgumentException("Illegal target state " + state);
96 public void switchAllLocalShardsState(final String newState, final long term) {
97 LOG.info("switchAllLocalShardsState called newState = {}, term = {}", newState, term);
98 requestSwitchShardState(null, newState, term);
102 public void switchShardState(final String shardId, final String newState, final long term) {
103 final ShardIdentifier identifier = ShardIdentifier.fromShardIdString(shardId);
104 LOG.info("switchShardState called shardName = {}, newState = {}, term = {}", shardId, newState, term);
105 requestSwitchShardState(identifier, newState, term);