use of com.alipay.sofa.jraft.util.ThreadId in project sofa-jraft by sofastack.
the class Replicator method start.
public static ThreadId start(final ReplicatorOptions opts, final RaftOptions raftOptions) {
if (opts.getLogManager() == null || opts.getBallotBox() == null || opts.getNode() == null) {
throw new IllegalArgumentException("Invalid ReplicatorOptions.");
}
final Replicator r = new Replicator(opts, raftOptions);
if (!r.rpcService.connect(opts.getPeerId().getEndpoint())) {
LOG.error("Fail to init sending channel to {}.", opts.getPeerId());
// Return and it will be retried later.
return null;
}
// Register replicator metric set.
final MetricRegistry metricRegistry = opts.getNode().getNodeMetrics().getMetricRegistry();
if (metricRegistry != null) {
try {
if (!metricRegistry.getNames().contains(r.metricName)) {
metricRegistry.register(r.metricName, new ReplicatorMetricSet(opts, r));
}
} catch (final IllegalArgumentException e) {
// ignore
}
}
// Start replication
r.id = new ThreadId(r, r);
r.id.lock();
notifyReplicatorStatusListener(r, ReplicatorEvent.CREATED);
LOG.info("Replicator={}@{} is started", r.id, r.options.getPeerId());
r.catchUpClosure = null;
r.lastRpcSendTimestamp = Utils.monotonicMs();
r.startHeartbeatTimer(Utils.nowMs());
// id.unlock in sendEmptyEntries
r.sendProbeRequest();
return r.id;
}
use of com.alipay.sofa.jraft.util.ThreadId in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method stopReplicator.
@Override
public boolean stopReplicator(final PeerId peer) {
LOG.info("Stop replicator to {}.", peer);
this.failureReplicators.remove(peer);
final ThreadId rid = this.replicatorMap.remove(peer);
if (rid == null) {
return false;
}
// erase entry first to avoid race condition
return Replicator.stop(rid);
}
use of com.alipay.sofa.jraft.util.ThreadId in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method stopAll.
@Override
public boolean stopAll() {
final List<ThreadId> rids = new ArrayList<>(this.replicatorMap.values());
this.replicatorMap.clear();
this.failureReplicators.clear();
for (final ThreadId rid : rids) {
Replicator.stop(rid);
}
return true;
}
use of com.alipay.sofa.jraft.util.ThreadId 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.util.ThreadId in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method stopAllAndFindTheNextCandidate.
@Override
public ThreadId stopAllAndFindTheNextCandidate(final ConfigurationEntry conf) {
ThreadId candidate = null;
final PeerId candidateId = findTheNextCandidate(conf);
if (candidateId != null) {
candidate = this.replicatorMap.get(candidateId);
} else {
LOG.info("Fail to find the next candidate.");
}
for (final ThreadId r : this.replicatorMap.values()) {
if (r != candidate) {
Replicator.stop(r);
}
}
this.replicatorMap.clear();
this.failureReplicators.clear();
return candidate;
}
Aggregations