From f9d4d0940e6b0d1def8bf4db52ab509cc1f5a362 Mon Sep 17 00:00:00 2001 From: Ritesh Kapoor Date: Tue, 26 Feb 2019 21:27:00 +0530 Subject: [PATCH] RATIS-486: Refactoring JMX registration into different class --- .../ratis/server/impl/RaftServerImpl.java | 64 ++---------------- .../server/impl/RaftServerJmxAdapter.java | 67 +++++++++++++++++++ .../ratis/server/impl/TestRaftServerJmx.java | 12 +++- 3 files changed, 83 insertions(+), 60 deletions(-) create mode 100644 ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerJmxAdapter.java diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java index 2cd8d40965..f1d6103039 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java @@ -21,7 +21,6 @@ import org.apache.ratis.proto.RaftProtos.*; import org.apache.ratis.protocol.*; import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.server.RaftServerMXBean; import org.apache.ratis.server.RaftServerRpc; import org.apache.ratis.server.protocol.RaftServerAsynchronousProtocol; import org.apache.ratis.server.protocol.RaftServerProtocol; @@ -37,26 +36,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.management.ObjectName; import java.io.IOException; import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.function.Function; import java.util.function.Supplier; -import java.util.stream.Collectors; -import static org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.INCONSISTENCY; -import static org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.NOT_LEADER; -import static org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.SUCCESS; -import static org.apache.ratis.util.LifeCycle.State.CLOSED; -import static org.apache.ratis.util.LifeCycle.State.CLOSING; -import static org.apache.ratis.util.LifeCycle.State.NEW; -import static org.apache.ratis.util.LifeCycle.State.RUNNING; -import static org.apache.ratis.util.LifeCycle.State.STARTING; +import static org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.*; +import static org.apache.ratis.util.LifeCycle.State.*; public class RaftServerImpl implements RaftServerProtocol, RaftServerAsynchronousProtocol, RaftClientProtocol, RaftClientAsynchronousProtocol { @@ -103,7 +90,7 @@ public class RaftServerImpl implements RaftServerProtocol, RaftServerAsynchronou this.state = new ServerState(id, group, properties, this, stateMachine); this.retryCache = initRetryCache(properties); - this.jmxAdapter = new RaftServerJmxAdapter(); + this.jmxAdapter = new RaftServerJmxAdapter(this, new JmxRegister()); } private RetryCache initRetryCache(RaftProperties prop) { @@ -177,19 +164,11 @@ boolean start() { startInitializing(); } - registerMBean(getId(), getGroupId(), jmxAdapter, jmxAdapter); + jmxAdapter.registerMBean(); state.start(); return true; } - static boolean registerMBean( - RaftPeerId id, RaftGroupId groupdId, RaftServerMXBean mBean, JmxRegister jmx) { - final String prefix = "Ratis:service=RaftServer,group=" + groupdId + ",id="; - final String registered = jmx.register(mBean, Arrays.asList( - () -> prefix + id, - () -> prefix + ObjectName.quote(id.toString()))); - return registered != null; - } /** * The peer belongs to the current configuration, should start as a follower @@ -1141,37 +1120,4 @@ callId, false, null, generateNotLeaderException(), } } } - - private class RaftServerJmxAdapter extends JmxRegister implements RaftServerMXBean { - @Override - public String getId() { - return getState().getSelfId().toString(); - } - - @Override - public String getLeaderId() { - return getState().getLeaderId().toString(); - } - - @Override - public long getCurrentTerm() { - return getState().getCurrentTerm(); - } - - @Override - public String getGroupId() { - return RaftServerImpl.this.getGroupId().toString(); - } - - @Override - public String getRole() { - return role.toString(); - } - - @Override - public List getFollowers() { - return role.getLeaderState().map(LeaderState::getFollowers).orElse(Collections.emptyList()) - .stream().map(RaftPeer::toString).collect(Collectors.toList()); - } - } } diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerJmxAdapter.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerJmxAdapter.java new file mode 100644 index 0000000000..59baf2803f --- /dev/null +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerJmxAdapter.java @@ -0,0 +1,67 @@ +package org.apache.ratis.server.impl; + +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.server.RaftServerMXBean; +import org.apache.ratis.util.JmxRegister; + +import javax.management.JMException; +import javax.management.ObjectName; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class RaftServerJmxAdapter implements RaftServerMXBean { + private RaftServerImpl raftServer; + private JmxRegister jmxRegister; + + public RaftServerJmxAdapter(RaftServerImpl raftServer, JmxRegister jmxRegister) { + this.raftServer = raftServer; + this.jmxRegister = jmxRegister; + } + + @Override + public String getId() { + return raftServer.getState().getSelfId().toString(); + } + + @Override + public String getLeaderId() { + return raftServer.getState().getLeaderId().toString(); + } + + @Override + public long getCurrentTerm() { + return raftServer.getState().getCurrentTerm(); + } + + @Override + public String getGroupId() { + return raftServer.getGroupId().toString(); + } + + @Override + public String getRole() { + return raftServer.getRole().toString(); + } + + @Override + public List getFollowers() { + return raftServer.getRole().getLeaderState().map(LeaderState::getFollowers).orElse(Collections.emptyList()) + .stream().map(RaftPeer::toString).collect(Collectors.toList()); + } + + public boolean registerMBean() { + RaftPeerId id = raftServer.getState().getSelfId(); + final String prefix = "Ratis:service=RaftServer,group=" + raftServer.getGroupId() + ",id="; + final String registered = jmxRegister.register(this, Arrays.asList( + () -> prefix + id, + () -> prefix + ObjectName.quote(id.toString()))); + return registered != null; + } + + public void unregister() throws JMException { + jmxRegister.unregister(); + } +} \ No newline at end of file diff --git a/ratis-test/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java b/ratis-test/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java index f060645ae0..13267442e5 100644 --- a/ratis-test/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java +++ b/ratis-test/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java @@ -36,6 +36,8 @@ import java.util.Set; import static org.apache.ratis.RaftTestUtil.waitForLeader; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class TestRaftServerJmx extends BaseTest { @Test(timeout = 30000) @@ -98,7 +100,15 @@ static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) { }; final RaftPeerId id = RaftPeerId.valueOf(name); final RaftGroupId groupId = RaftGroupId.randomId(); - final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx); + + RaftServerImpl raftServer = mock(RaftServerImpl.class); + ServerState serverState = mock(ServerState.class); + when(raftServer.getState()).thenReturn(serverState); + when(raftServer.getRole()).thenReturn(new RoleInfo(id)); + when(raftServer.getGroupId()).thenReturn(groupId); + + RaftServerJmxAdapter raftServerJmxAdapter = new RaftServerJmxAdapter(raftServer, jmx); + final boolean succeeded = raftServerJmxAdapter.registerMBean(); Assert.assertEquals(expectToSucceed, succeeded); }