Search in sources :

Example 76 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeImpl method removeLearners.

@Override
public void removeLearners(final List<PeerId> learners, final Closure done) {
    checkPeers(learners);
    this.writeLock.lock();
    try {
        final Configuration newConf = new Configuration(this.conf.getConf());
        for (final PeerId peer : learners) {
            newConf.removeLearner(peer);
        }
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 77 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class FSMCallerImpl method doSnapshotLoad.

private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.getOldPeersCount() == 0) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
            final PeerId peer = new PeerId();
            Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
            conf.addPeer(peer);
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.getLastIncludedIndex());
    this.lastAppliedTerm = meta.getLastIncludedTerm();
    done.run(Status.OK());
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) LogId(com.alipay.sofa.jraft.entity.LogId) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 78 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class RouteTable method refreshLeader.

/**
 * Blocking the thread until query_leader finishes.
 *
 * @param groupId   raft group id
 * @param timeoutMs timeout millis
 * @return operation status
 */
public Status refreshLeader(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
    final Configuration conf = getConfiguration(groupId);
    if (conf == null) {
        return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
    }
    final Status st = Status.OK();
    final CliRequests.GetLeaderRequest.Builder rb = CliRequests.GetLeaderRequest.newBuilder();
    rb.setGroupId(groupId);
    final CliRequests.GetLeaderRequest request = rb.build();
    TimeoutException timeoutException = null;
    for (final PeerId peer : conf) {
        if (!cliClientService.connect(peer.getEndpoint())) {
            if (st.isOk()) {
                st.setError(-1, "Fail to init channel to %s", peer);
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, Fail to init channel to %s", savedMsg, peer);
            }
            continue;
        }
        final Future<Message> result = cliClientService.getLeader(peer.getEndpoint(), request, null);
        try {
            final Message msg = result.get(timeoutMs, TimeUnit.MILLISECONDS);
            if (msg instanceof RpcRequests.ErrorResponse) {
                if (st.isOk()) {
                    st.setError(-1, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
                } else {
                    final String savedMsg = st.getErrorMsg();
                    st.setError(-1, "%s, %s", savedMsg, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
                }
            } else {
                final CliRequests.GetLeaderResponse response = (CliRequests.GetLeaderResponse) msg;
                updateLeader(groupId, response.getLeaderId());
                return Status.OK();
            }
        } catch (final TimeoutException e) {
            timeoutException = e;
        } catch (final ExecutionException e) {
            if (st.isOk()) {
                st.setError(-1, e.getMessage());
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, %s", savedMsg, e.getMessage());
            }
        }
    }
    if (timeoutException != null) {
        throw timeoutException;
    }
    return st;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) CliRequests(com.alipay.sofa.jraft.rpc.CliRequests) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 79 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class RouteTable method refreshConfiguration.

public Status refreshConfiguration(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
    final Configuration conf = getConfiguration(groupId);
    if (conf == null) {
        return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
    }
    final Status st = Status.OK();
    PeerId leaderId = selectLeader(groupId);
    if (leaderId == null) {
        refreshLeader(cliClientService, groupId, timeoutMs);
        leaderId = selectLeader(groupId);
    }
    if (leaderId == null) {
        st.setError(-1, "Fail to get leader of group %s", groupId);
        return st;
    }
    if (!cliClientService.connect(leaderId.getEndpoint())) {
        st.setError(-1, "Fail to init channel to %s", leaderId);
        return st;
    }
    final CliRequests.GetPeersRequest.Builder rb = CliRequests.GetPeersRequest.newBuilder();
    rb.setGroupId(groupId);
    rb.setLeaderId(leaderId.toString());
    try {
        final Message result = cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(timeoutMs, TimeUnit.MILLISECONDS);
        if (result instanceof CliRequests.GetPeersResponse) {
            final CliRequests.GetPeersResponse resp = (CliRequests.GetPeersResponse) result;
            final Configuration newConf = new Configuration();
            for (final String peerIdStr : resp.getPeersList()) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                newConf.addPeer(newPeer);
            }
            if (!conf.equals(newConf)) {
                LOG.info("Configuration of replication group {} changed from {} to {}", groupId, conf, newConf);
            }
            updateConfiguration(groupId, newConf);
        } else {
            final RpcRequests.ErrorResponse resp = (RpcRequests.ErrorResponse) result;
            st.setError(resp.getErrorCode(), resp.getErrorMsg());
        }
    } catch (final Exception e) {
        st.setError(-1, e.getMessage());
    }
    return st;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) CliRequests(com.alipay.sofa.jraft.rpc.CliRequests) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 80 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project jdchain-core by blockchain-jd-com.

the class RaftMessageService method refreshConfiguration.

private void refreshConfiguration() throws TimeoutException, InterruptedException {
    if (System.currentTimeMillis() - this.lastConfigurationUpdateTimestamp < this.refreshConfigurationMs) {
        return;
    }
    Status status = RouteTable.getInstance().refreshConfiguration(this.clientService, this.groupId, this.rpcTimeoutMs);
    if (!status.isOk()) {
        LOGGER.warn("refreshConfiguration failed. reason: {}", status.getErrorMsg());
    } else {
        Configuration changedConfiguration = RouteTable.getInstance().getConfiguration(this.groupId);
        if (!changedConfiguration.isEmpty()) {
            Configuration include = new Configuration();
            Configuration exclude = new Configuration();
            changedConfiguration.diff(this.configuration, include, exclude);
            if (!include.isEmpty() || !exclude.isEmpty()) {
                LOGGER.info("configuration changed from: {} to {}", this.configuration, changedConfiguration);
                this.configuration = changedConfiguration;
            }
        }
    }
    this.lastConfigurationUpdateTimestamp = System.currentTimeMillis();
    this.isReloadMonitorNode = true;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3