Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RATIS-486: Refactoring JMX registration into different class #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> getFollowers() {
return role.getLeaderState().map(LeaderState::getFollowers).orElse(Collections.emptyList())
.stream().map(RaftPeer::toString).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down