Search in sources :

Example 1 with ChangePeersResponse

use of org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse in project ignite-3 by apache.

the class CliServiceImpl method changePeers.

// TODO refactor addPeer/removePeer/changePeers/transferLeader, remove duplicated code IGNITE-14832
@Override
public Status changePeers(final String groupId, final Configuration conf, final Configuration newPeers) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(newPeers, "Null new peers");
    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);
    }
    ChangePeersRequest req = cliOptions.getRaftMessagesFactory().changePeersRequest().groupId(groupId).leaderId(leaderId.toString()).newPeersList(newPeers.getPeers().stream().map(Object::toString).collect(toList())).build();
    try {
        final Message result = this.cliClientService.changePeers(leaderId.getEndpoint(), req, null).get();
        if (result instanceof ChangePeersResponse) {
            final ChangePeersResponse resp = (ChangePeersResponse) result;
            final Configuration oldConf = new Configuration();
            for (final String peerIdStr : resp.oldPeersList()) {
                final PeerId oldPeer = new PeerId();
                oldPeer.parse(peerIdStr);
                oldConf.addPeer(oldPeer);
            }
            final Configuration newConf = new Configuration();
            for (final String peerIdStr : resp.newPeersList()) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                newConf.addPeer(newPeer);
            }
            LOG.info("Configuration of replication group {} changed from {} to {}", groupId, oldConf, newConf);
            return Status.OK();
        } else {
            return statusFromResponse(result);
        }
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ChangePeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersRequest) Message(org.apache.ignite.raft.jraft.rpc.Message) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ChangePeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 2 with ChangePeersResponse

use of org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse in project ignite-3 by apache.

the class RaftGroupServiceImpl method changePeers.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Void> changePeers(List<Peer> peers) {
    Peer leader = this.leader;
    if (leader == null)
        return refreshLeader().thenCompose(res -> changePeers(peers));
    List<String> peersToChange = peers.stream().map(p -> PeerId.fromPeer(p).toString()).collect(Collectors.toList());
    ChangePeersRequest req = factory.changePeersRequest().groupId(groupId).newPeersList(peersToChange).build();
    CompletableFuture<ChangePeersResponse> fut = new CompletableFuture<>();
    sendWithRetry(leader, req, currentTimeMillis() + timeout, fut);
    return fut.thenApply(resp -> {
        this.peers = parsePeerList(resp.newPeersList());
        return null;
    });
}
Also used : System.currentTimeMillis(java.lang.System.currentTimeMillis) IgniteException(org.apache.ignite.lang.IgniteException) AddPeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerRequest) S(org.apache.ignite.internal.tostring.S) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) AddPeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerResponse) TransferLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.TransferLeaderRequest) ArrayList(java.util.ArrayList) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) RemoveLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemoveLearnersRequest) ResetLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ResetLearnersRequest) RemovePeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerRequest) ActionResponse(org.apache.ignite.raft.jraft.rpc.ActionResponse) ChangePeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) Collection(java.util.Collection) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) NetworkMessage(org.apache.ignite.network.NetworkMessage) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) ActionRequest(org.apache.ignite.raft.jraft.rpc.ActionRequest) GetPeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest) NetworkAddress(org.apache.ignite.network.NetworkAddress) ReadCommand(org.apache.ignite.raft.client.ReadCommand) GetPeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) List(java.util.List) Peer(org.apache.ignite.raft.client.Peer) RemovePeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerResponse) SnapshotRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.SnapshotRequest) ClusterService(org.apache.ignite.network.ClusterService) LearnersOpResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.LearnersOpResponse) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ChangePeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersRequest) AddLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.AddLearnersRequest) RaftError(org.apache.ignite.raft.jraft.error.RaftError) CompletableFuture(java.util.concurrent.CompletableFuture) ChangePeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersRequest) ChangePeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse) Peer(org.apache.ignite.raft.client.Peer)

Example 3 with ChangePeersResponse

use of org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse in project ignite-3 by apache.

the class ChangePeersRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final ChangePeersRequest request, final IgniteCliRpcRequestClosure done) {
    final List<PeerId> oldConf = ctx.node.listPeers();
    final Configuration conf = new Configuration();
    for (final String peerIdStr : request.newPeersList()) {
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            conf.addPeer(peer);
        } else {
            return // 
            RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    }
    LOG.info("Receive ChangePeersRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), conf);
    ctx.node.changePeers(conf, status -> {
        if (!status.isOk()) {
            done.run(status);
        } else {
            ChangePeersResponse req = msgFactory().changePeersResponse().oldPeersList(oldConf.stream().map(Object::toString).collect(toList())).newPeersList(conf.getPeers().stream().map(Object::toString).collect(toList())).build();
            done.sendResponse(req);
        }
    });
    return null;
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ChangePeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

PeerId (org.apache.ignite.raft.jraft.entity.PeerId)3 ChangePeersResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersResponse)3 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)2 ChangePeersRequest (org.apache.ignite.raft.jraft.rpc.CliRequests.ChangePeersRequest)2 IOException (java.io.IOException)1 System.currentTimeMillis (java.lang.System.currentTimeMillis)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ThreadLocalRandom.current (java.util.concurrent.ThreadLocalRandom.current)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 BiConsumer (java.util.function.BiConsumer)1 Collectors (java.util.stream.Collectors)1 S (org.apache.ignite.internal.tostring.S)1 IgniteException (org.apache.ignite.lang.IgniteException)1