use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
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.errorCode());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
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)));
this.asyncContext.getNodeManager().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)));
this.asyncContext.getNodeManager().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.errorCode());
assertEquals("Peer must be specified since there're 2 nodes in group test", resp.errorMsg());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class Replicator method notifyReplicatorStatusListener.
/**
* Notify replicator event(such as created, error, destroyed) to replicatorStateListener which is implemented by
* users.
*
* @param replicator replicator object
* @param event replicator's state listener event type
* @param status replicator's error detailed status
*/
private static void notifyReplicatorStatusListener(final Replicator replicator, final ReplicatorEvent event, final Status status) {
final ReplicatorOptions replicatorOpts = Requires.requireNonNull(replicator.getOpts(), "replicatorOptions");
final Node node = Requires.requireNonNull(replicatorOpts.getNode(), "node");
final PeerId peer = Requires.requireNonNull(replicatorOpts.getPeerId(), "peer");
final List<ReplicatorStateListener> listenerList = node.getReplicatorStateListeners();
for (int i = 0; i < listenerList.size(); i++) {
final ReplicatorStateListener listener = listenerList.get(i);
if (listener != null) {
try {
switch(event) {
case CREATED:
Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onCreated(peer));
break;
case ERROR:
Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onError(peer, status));
break;
case DESTROYED:
Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onDestroyed(peer));
break;
default:
break;
}
} catch (final Exception e) {
LOG.error("Fail to notify ReplicatorStatusListener, listener={}, event={}.", listener, event);
}
}
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, NodeManager nodeManager) {
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.get(groupId, peer);
assert (node != null);
peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
peerCtx.executor = node.getOptions().getStripedExecutor().next();
groupContexts.put(pair, peerCtx);
}
}
}
return peerCtx;
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class GetLeaderRequestProcessor method processRequest.
@Override
public Message processRequest(final GetLeaderRequest request, final RpcRequestClosure done) {
List<Node> nodes = new ArrayList<>();
final String groupId = getGroupId(request);
if (request.peerId() != null) {
final String peerIdStr = getPeerId(request);
final PeerId peer = new PeerId();
if (peer.parse(peerIdStr)) {
final Status st = new Status();
nodes.add(getNode(groupId, peer, st, done.getRpcCtx().getNodeManager()));
if (!st.isOk()) {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), st);
}
} else {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
}
} else {
nodes = done.getRpcCtx().getNodeManager().getNodesByGroupId(groupId);
}
if (nodes == null || nodes.isEmpty()) {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.ENOENT, "No nodes in group %s", groupId);
}
for (final Node node : nodes) {
final PeerId leader = node.getLeaderId();
if (leader != null && !leader.isEmpty()) {
return msgFactory().getLeaderResponse().leaderId(leader.toString()).currentTerm(node.getCurrentTerm()).build();
}
}
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EAGAIN, "Unknown leader");
}
Aggregations