Search in sources :

Example 1 with ThreadId

use of org.apache.ignite.raft.jraft.util.ThreadId in project ignite-3 by apache.

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 {
            final String replicatorMetricName = getReplicatorMetricName(opts);
            if (!metricRegistry.getNames().contains(replicatorMetricName)) {
                metricRegistry.register(replicatorMetricName, 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.sendEmptyEntries(false);
    return r.id;
}
Also used : ThreadId(org.apache.ignite.raft.jraft.util.ThreadId) MetricRegistry(com.codahale.metrics.MetricRegistry) ByteString(org.apache.ignite.raft.jraft.util.ByteString)

Example 2 with ThreadId

use of org.apache.ignite.raft.jraft.util.ThreadId in project ignite-3 by apache.

the class ReplicatorGroupImpl method findTheNextCandidate.

@Override
public PeerId findTheNextCandidate(final ConfigurationEntry conf) {
    PeerId peerId = null;
    int priority = Integer.MIN_VALUE;
    long maxIndex = -1L;
    for (final Map.Entry<PeerId, ThreadId> entry : this.replicatorMap.entrySet()) {
        if (!conf.contains(entry.getKey())) {
            continue;
        }
        final int nextPriority = entry.getKey().getPriority();
        if (nextPriority == ElectionPriority.NotElected) {
            continue;
        }
        final long nextIndex = Replicator.getNextIndex(entry.getValue());
        if (nextIndex > maxIndex) {
            maxIndex = nextIndex;
            peerId = entry.getKey();
            priority = peerId.getPriority();
        } else if (nextIndex == maxIndex && nextPriority > priority) {
            peerId = entry.getKey();
            priority = peerId.getPriority();
        }
    }
    if (maxIndex == -1L) {
        return null;
    } else {
        return peerId;
    }
}
Also used : ThreadId(org.apache.ignite.raft.jraft.util.ThreadId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 3 with ThreadId

use of org.apache.ignite.raft.jraft.util.ThreadId in project ignite-3 by apache.

the class ReplicatorGroupImpl method sendHeartbeat.

@Override
public void sendHeartbeat(final PeerId peer, final RpcResponseClosure<AppendEntriesResponse> closure) {
    final ThreadId rid = this.replicatorMap.get(peer);
    if (rid == null) {
        if (closure != null) {
            closure.run(new Status(RaftError.EHOSTDOWN, "Peer %s is not connected", peer));
        }
        return;
    }
    Replicator.sendHeartbeat(rid, closure, this.commonOptions.getCommonExecutor());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ThreadId(org.apache.ignite.raft.jraft.util.ThreadId)

Example 4 with ThreadId

use of org.apache.ignite.raft.jraft.util.ThreadId in project ignite-3 by apache.

the class Replicator method destroy.

void destroy() {
    final ThreadId savedId = this.id;
    LOG.info("Replicator {} is going to quit", savedId);
    releaseReader();
    // Unregister replicator metric set
    if (this.nodeMetrics.isEnabled()) {
        // 
        this.nodeMetrics.getMetricRegistry().removeMatching(MetricFilter.startsWith(getReplicatorMetricName(this.options)));
    }
    this.state = State.Destroyed;
    notifyReplicatorStatusListener((Replicator) savedId.getData(), ReplicatorEvent.DESTROYED);
    savedId.unlockAndDestroy();
// Avoid nulling id because it's used to sync replicator state on destroy.
}
Also used : ThreadId(org.apache.ignite.raft.jraft.util.ThreadId)

Example 5 with ThreadId

use of org.apache.ignite.raft.jraft.util.ThreadId in project ignite-3 by apache.

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(org.apache.ignite.raft.jraft.util.ThreadId) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

ThreadId (org.apache.ignite.raft.jraft.util.ThreadId)9 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)2 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 Status (org.apache.ignite.raft.jraft.Status)1 ReplicatorOptions (org.apache.ignite.raft.jraft.option.ReplicatorOptions)1 RaftClientService (org.apache.ignite.raft.jraft.rpc.RaftClientService)1 ByteString (org.apache.ignite.raft.jraft.util.ByteString)1