Search in sources :

Example 1 with ReplicatorOptions

use of io.dingodb.raft.option.ReplicatorOptions in project dingo by dingodb.

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;
}
Also used : ReplicatorOptions(io.dingodb.raft.option.ReplicatorOptions)

Example 2 with ReplicatorOptions

use of io.dingodb.raft.option.ReplicatorOptions in project dingo by dingodb.

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 ReplicatorStateListener.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);
            }
        }
    }
}
Also used : Node(io.dingodb.raft.Node) ReplicatorOptions(io.dingodb.raft.option.ReplicatorOptions) RaftException(io.dingodb.raft.error.RaftException) PeerId(io.dingodb.raft.entity.PeerId)

Example 3 with ReplicatorOptions

use of io.dingodb.raft.option.ReplicatorOptions in project dingo by dingodb.

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(io.dingodb.raft.rpc.RaftClientService) ThreadId(io.dingodb.raft.util.ThreadId) ReplicatorOptions(io.dingodb.raft.option.ReplicatorOptions)

Aggregations

ReplicatorOptions (io.dingodb.raft.option.ReplicatorOptions)3 Node (io.dingodb.raft.Node)1 PeerId (io.dingodb.raft.entity.PeerId)1 RaftException (io.dingodb.raft.error.RaftException)1 RaftClientService (io.dingodb.raft.rpc.RaftClientService)1 ThreadId (io.dingodb.raft.util.ThreadId)1