Search in sources :

Example 1 with ThreadId

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;
}
Also used : ThreadId(com.alipay.sofa.jraft.util.ThreadId) MetricRegistry(com.codahale.metrics.MetricRegistry)

Example 2 with ThreadId

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);
}
Also used : ThreadId(com.alipay.sofa.jraft.util.ThreadId)

Example 3 with ThreadId

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;
}
Also used : ThreadId(com.alipay.sofa.jraft.util.ThreadId) ArrayList(java.util.ArrayList)

Example 4 with ThreadId

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;
}
Also used : RaftClientService(com.alipay.sofa.jraft.rpc.RaftClientService) ThreadId(com.alipay.sofa.jraft.util.ThreadId) ReplicatorOptions(com.alipay.sofa.jraft.option.ReplicatorOptions)

Example 5 with ThreadId

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;
}
Also used : ThreadId(com.alipay.sofa.jraft.util.ThreadId) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

ThreadId (com.alipay.sofa.jraft.util.ThreadId)9 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 Status (com.alipay.sofa.jraft.Status)1 ReplicatorOptions (com.alipay.sofa.jraft.option.ReplicatorOptions)1 RaftClientService (com.alipay.sofa.jraft.rpc.RaftClientService)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1