use of com.alipay.sofa.jraft.option.ReplicatorOptions in project sofa-jraft by sofastack.
the class ReplicatorTest method setup.
@Before
public void setup() {
this.timerManager = new TimerManager(5);
this.opts = new ReplicatorOptions();
this.opts.setRaftRpcService(this.rpcService);
this.opts.setPeerId(this.peerId);
this.opts.setBallotBox(this.ballotBox);
this.opts.setGroupId("test");
this.opts.setTerm(1);
this.opts.setServerId(new PeerId("localhost", 8082));
this.opts.setNode(this.node);
this.opts.setSnapshotStorage(this.snapshotStorage);
this.opts.setTimerManager(this.timerManager);
this.opts.setLogManager(this.logManager);
this.opts.setDynamicHeartBeatTimeoutMs(100);
this.opts.setElectionTimeoutMs(1000);
Mockito.when(this.logManager.getLastLogIndex()).thenReturn(10L);
Mockito.when(this.logManager.getTerm(10)).thenReturn(1L);
Mockito.when(this.rpcService.connect(this.peerId.getEndpoint())).thenReturn(true);
Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(true));
// mock send empty entries
mockSendEmptyEntries();
this.id = Replicator.start(this.opts, this.raftOptions);
}
use of com.alipay.sofa.jraft.option.ReplicatorOptions in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method init.
@Override
public boolean init(final NodeId nodeId, final ReplicatorGroupOptions opts) {
this.dynamicTimeoutMs = opts.getHeartbeatTimeoutMs();
this.electionTimeoutMs = opts.getElectionTimeoutMs();
this.raftOptions = opts.getRaftOptions();
this.commonOptions = new ReplicatorOptions();
this.commonOptions.setDynamicHeartBeatTimeoutMs(this.dynamicTimeoutMs);
this.commonOptions.setElectionTimeoutMs(this.electionTimeoutMs);
this.commonOptions.setRaftRpcService(opts.getRaftRpcClientService());
this.commonOptions.setLogManager(opts.getLogManager());
this.commonOptions.setBallotBox(opts.getBallotBox());
this.commonOptions.setNode(opts.getNode());
this.commonOptions.setTerm(0);
this.commonOptions.setGroupId(nodeId.getGroupId());
this.commonOptions.setServerId(nodeId.getPeerId());
this.commonOptions.setSnapshotStorage(opts.getSnapshotStorage());
this.commonOptions.setTimerManager(opts.getTimerManager());
return true;
}
use of com.alipay.sofa.jraft.option.ReplicatorOptions in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method addReplicator.
@Override
public boolean addReplicator(final PeerId peer, final ReplicatorType replicatorType, final boolean sync) {
Requires.requireTrue(this.commonOptions.getTerm() != 0);
this.failureReplicators.remove(peer);
if (this.replicatorMap.containsKey(peer)) {
return true;
}
final ReplicatorOptions opts = this.commonOptions == null ? new ReplicatorOptions() : this.commonOptions.copy();
opts.setReplicatorType(replicatorType);
opts.setPeerId(peer);
if (!sync) {
final RaftClientService client = opts.getRaftRpcService();
if (client != null && !client.checkConnection(peer.getEndpoint(), true)) {
LOG.error("Fail to check replicator connection to peer={}, replicatorType={}.", peer, replicatorType);
this.failureReplicators.put(peer, replicatorType);
return false;
}
}
final ThreadId rid = Replicator.start(opts, this.raftOptions);
if (rid == null) {
LOG.error("Fail to start replicator to peer={}, replicatorType={}.", peer, replicatorType);
this.failureReplicators.put(peer, replicatorType);
return false;
}
return this.replicatorMap.put(peer, rid) == null;
}
use of com.alipay.sofa.jraft.option.ReplicatorOptions 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);
}
}
}
}
Aggregations