use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testNodesWithPriorityElection.
@Test
public void testNodesWithPriorityElection() throws Exception {
List<Integer> priorities = new ArrayList<>();
priorities.add(100);
priorities.add(40);
priorities.add(40);
final List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
assertEquals(100, leader.getNodeTargetPriority());
assertEquals(100, leader.getLeaderId().getPriority());
assertEquals(2, cluster.getFollowers().size());
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class BaseCliRequestProcessorTest method testManyNodes.
@Test
public void testManyNodes() {
Node node1 = Mockito.mock(Node.class);
Mockito.when(node1.getGroupId()).thenReturn("test");
Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
NodeOptions opts = new NodeOptions();
Mockito.when(node1.getOptions()).thenReturn(opts);
NodeManager.getInstance().addAddress(new Endpoint("localhost", 8081));
NodeManager.getInstance().add(node1);
Node node2 = Mockito.mock(Node.class);
Mockito.when(node2.getGroupId()).thenReturn("test");
Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
Mockito.when(node2.getOptions()).thenReturn(opts);
NodeManager.getInstance().addAddress(new Endpoint("localhost", 8082));
NodeManager.getInstance().add(node2);
this.processor = new MockCliRequestProcessor(null, "test");
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
assertEquals("Peer must be specified since there're 2 nodes in group test", resp.getErrorMsg());
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class BaseCliRequestProcessorTest method testOK.
@Test
public void testOK() {
Node node = mockNode(false);
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(this.processor.done);
assertSame(this.processor.ctx.node, node);
assertNotNull(resp);
assertEquals(0, resp.getErrorCode());
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.
@SuppressWarnings("unchecked")
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, final Connection conn) {
ConcurrentMap<PeerPair, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
if (groupContexts == null) {
groupContexts = new ConcurrentHashMap<>();
final ConcurrentMap<PeerPair, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId, groupContexts);
if (existsCtxs != null) {
groupContexts = existsCtxs;
}
}
PeerRequestContext peerCtx = groupContexts.get(pair);
if (peerCtx == null) {
synchronized (Utils.withLockObject(groupContexts)) {
peerCtx = groupContexts.get(pair);
// double check in lock
if (peerCtx == null) {
// only one thread to process append entries for every jraft node
final PeerId peer = new PeerId();
final boolean parsed = peer.parse(pair.local);
assert (parsed);
final Node node = NodeManager.getInstance().get(groupId, peer);
assert (node != null);
peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
groupContexts.put(pair, peerCtx);
}
}
}
// Add the pair to connection attribute metadata.
if (conn != null) {
Set<PeerPair> pairs;
if ((pairs = (Set<AppendEntriesRequestProcessor.PeerPair>) conn.getAttribute(PAIR_ATTR)) == null) {
pairs = new ConcurrentHashSet<>();
Set<PeerPair> existsPairs = (Set<PeerPair>) conn.setAttributeIfAbsent(PAIR_ATTR, pairs);
if (existsPairs != null) {
pairs = existsPairs;
}
}
pairs.add(pair);
}
return peerCtx;
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class AppendEntriesRequestProcessor method processRequest0.
@Override
public Message processRequest0(final RaftServerService service, final AppendEntriesRequest request, final RpcRequestClosure done) {
final Node node = (Node) service;
if (node.getRaftOptions().isReplicatorPipeline()) {
final String groupId = request.getGroupId();
final PeerPair pair = pairOf(request.getPeerId(), request.getServerId());
boolean isHeartbeat = isHeartbeatRequest(request);
int reqSequence = -1;
if (!isHeartbeat) {
reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getConnection());
}
final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, defaultResp(), groupId, pair, reqSequence, isHeartbeat));
if (response != null) {
if (isHeartbeat) {
done.getRpcCtx().sendResponse(response);
} else {
sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
}
}
return null;
} else {
return service.handleAppendEntriesRequest(request, done);
}
}
Aggregations