use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class CliServiceTest method testChangePeers.
@Test
public void testChangePeers() throws Exception {
final List<PeerId> newPeers = TestUtils.generatePeers(10);
newPeers.removeAll(this.conf.getPeerSet());
for (final PeerId peer : newPeers) {
assertTrue(this.cluster.start(peer.getEndpoint()));
}
this.cluster.waitLeader();
final Node oldLeaderNode = this.cluster.getLeader();
assertNotNull(oldLeaderNode);
final PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
assertNotNull(oldLeader);
assertTrue(this.cliService.changePeers(this.groupId, this.conf, new Configuration(newPeers)).isOk());
this.cluster.waitLeader();
final PeerId newLeader = this.cluster.getLeader().getNodeId().getPeerId();
assertNotEquals(oldLeader, newLeader);
assertTrue(newPeers.contains(newLeader));
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class TestCluster method stop.
public boolean stop(final Endpoint listenAddr) throws InterruptedException {
final Node node = removeNode(listenAddr);
final CountDownLatch latch = new CountDownLatch(1);
if (node != null) {
node.shutdown(new ExpectClosure(latch));
node.join();
latch.await();
}
final RaftGroupService raftGroupService = this.serverMap.remove(listenAddr.toString());
raftGroupService.shutdown();
raftGroupService.join();
return node != null;
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeImpl method apply.
@Override
public void apply(final Task task) {
if (this.shutdownLatch != null) {
Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down."));
throw new IllegalStateException("Node is shutting down");
}
Requires.requireNonNull(task, "Null task");
final LogEntry entry = new LogEntry();
entry.setData(task.getData());
final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> {
event.reset();
event.done = task.getDone();
event.entry = entry;
event.expectedTerm = task.getExpectedTerm();
};
switch(this.options.getApplyTaskMode()) {
case Blocking:
this.applyQueue.publishEvent(translator);
break;
case NonBlocking:
default:
if (!this.applyQueue.tryPublishEvent(translator)) {
String errorMsg = "Node is busy, has too many tasks, queue is full and bufferSize=" + this.applyQueue.getBufferSize();
Utils.runClosureInThread(task.getDone(), new Status(RaftError.EBUSY, errorMsg));
LOG.warn("Node {} applyQueue is overload.", getNodeId());
this.metrics.recordTimes("apply-task-overload-times", 1);
if (task.getDone() == null) {
throw new OverloadException(errorMsg);
}
}
break;
}
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
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 ReplicatorState newState) {
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.getReplicatorStatueListeners();
for (int i = 0; i < listenerList.size(); i++) {
final ReplicatorStateListener listener = listenerList.get(i);
if (listener != null) {
try {
switch(event) {
case CREATED:
RpcUtils.runInThread(() -> listener.onCreated(peer));
break;
case ERROR:
RpcUtils.runInThread(() -> listener.onError(peer, status));
break;
case DESTROYED:
RpcUtils.runInThread(() -> listener.onDestroyed(peer));
break;
case STATE_CHANGED:
RpcUtils.runInThread(() -> listener.stateChanged(peer, newState));
default:
break;
}
} catch (final Exception e) {
LOG.error("Fail to notify ReplicatorStatusListener, listener={}, event={}.", listener, event);
}
}
}
}
use of com.alipay.sofa.jraft.Node in project incubator-hugegraph by apache.
the class RaftGroupManagerImpl method setLeader.
@Override
public String setLeader(String endpoint) {
PeerId newLeaderId = PeerId.parsePeer(endpoint);
Node node = this.raftNode.node();
// If expected endpoint has already been raft leader
if (node.getLeaderId().equals(newLeaderId)) {
return newLeaderId.toString();
}
if (this.raftNode.selfIsLeader()) {
// If current node is the leader, transfer directly
this.transferLeaderTo(endpoint);
} else {
// If current node is not leader, forward request to leader
SetLeaderRequest request = SetLeaderRequest.newBuilder().setEndpoint(endpoint).build();
try {
RaftClosure<SetLeaderResponse> future;
future = this.forwardToLeader(request);
future.waitFinished();
} catch (Throwable e) {
throw new BackendException("Failed to set leader to '%s'", e, endpoint);
}
}
return newLeaderId.toString();
}
Aggregations