Search in sources :

Example 81 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceImpl method getPeers.

private List<PeerId> getPeers(final String groupId, final Configuration conf, final boolean returnLearners, final boolean onlyGetAlive) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null conf");
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        throw new IllegalStateException(st.getErrorMsg());
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        throw new IllegalStateException("Fail to init channel to leader " + leaderId);
    }
    final GetPeersRequest.Builder rb = // 
    GetPeersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(// 
    leaderId.toString()).setOnlyAlive(onlyGetAlive);
    try {
        final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
        if (result instanceof GetPeersResponse) {
            final GetPeersResponse resp = (GetPeersResponse) result;
            final List<PeerId> peerIdList = new ArrayList<>();
            final ProtocolStringList responsePeers = returnLearners ? resp.getLearnersList() : resp.getPeersList();
            for (final String peerIdStr : responsePeers) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                peerIdList.add(newPeer);
            }
            return peerIdList;
        } else {
            final ErrorResponse resp = (ErrorResponse) result;
            throw new JRaftException(resp.getErrorMsg());
        }
    } catch (final JRaftException e) {
        throw e;
    } catch (final Exception e) {
        throw new JRaftException(e);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersRequest) ProtocolStringList(com.google.protobuf.ProtocolStringList) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 82 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceImpl method removeLearners.

@Override
public Status removeLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
    checkLearnersOpParams(groupId, conf, learners);
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to leader %s", leaderId);
    }
    final RemoveLearnersRequest.Builder rb = // 
    RemoveLearnersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    for (final PeerId peer : learners) {
        rb.addLearners(peer.toString());
    }
    try {
        final Message result = this.cliClientService.removeLearners(leaderId.getEndpoint(), rb.build(), null).get();
        return processLearnersOpResponse(groupId, result, "removing learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RemoveLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.RemoveLearnersRequest) Message(com.google.protobuf.Message) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 83 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceImpl method processLearnersOpResponse.

private Status processLearnersOpResponse(final String groupId, final Message result, final String fmt, final Object... formatArgs) {
    if (result instanceof LearnersOpResponse) {
        final LearnersOpResponse resp = (LearnersOpResponse) result;
        final Configuration oldConf = new Configuration();
        for (final String peerIdStr : resp.getOldLearnersList()) {
            final PeerId oldPeer = new PeerId();
            oldPeer.parse(peerIdStr);
            oldConf.addLearner(oldPeer);
        }
        final Configuration newConf = new Configuration();
        for (final String peerIdStr : resp.getNewLearnersList()) {
            final PeerId newPeer = new PeerId();
            newPeer.parse(peerIdStr);
            newConf.addLearner(newPeer);
        }
        LOG.info("Learners of replication group {} changed from {} to {} after {}.", groupId, oldConf, newConf, String.format(fmt, formatArgs));
        return Status.OK();
    } else {
        return statusFromResponse(result);
    }
}
Also used : LearnersOpResponse(com.alipay.sofa.jraft.rpc.CliRequests.LearnersOpResponse) Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 84 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceImpl method recordConfigurationChange.

private void recordConfigurationChange(final String groupId, final List<String> oldPeersList, final List<String> newPeersList) {
    final Configuration oldConf = new Configuration();
    for (final String peerIdStr : oldPeersList) {
        final PeerId oldPeer = new PeerId();
        oldPeer.parse(peerIdStr);
        oldConf.addPeer(oldPeer);
    }
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : newPeersList) {
        final PeerId newPeer = new PeerId();
        newPeer.parse(peerIdStr);
        newConf.addPeer(newPeer);
    }
    LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 85 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceImpl method resetLearners.

@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
    checkLearnersOpParams(groupId, conf, learners);
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to leader %s", leaderId);
    }
    final ResetLearnersRequest.Builder rb = // 
    ResetLearnersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    for (final PeerId peer : learners) {
        rb.addLearners(peer.toString());
    }
    try {
        final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), rb.build(), null).get();
        return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) ResetLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.ResetLearnersRequest) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8