Search in sources :

Example 1 with Message

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

the class CliServiceImpl method removePeer.

@Override
public Status removePeer(final String groupId, final Configuration conf, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(peer, "Null peer");
    Requires.requireTrue(!peer.isEmpty(), "Removing peer is blank");
    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);
    }
    RemovePeerRequest req = cliOptions.getRaftMessagesFactory().removePeerRequest().groupId(groupId).leaderId(leaderId.toString()).peerId(peer.toString()).build();
    try {
        final Message result = this.cliClientService.removePeer(leaderId.getEndpoint(), req, null).get();
        if (result instanceof RemovePeerResponse) {
            final RemovePeerResponse resp = (RemovePeerResponse) 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) Message(org.apache.ignite.raft.jraft.rpc.Message) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RemovePeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerResponse) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) RemovePeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerRequest)

Example 2 with Message

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

the class CliServiceImpl method getLeader.

@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(leaderId, "Null leader id");
    if (conf == null || conf.isEmpty()) {
        return new Status(RaftError.EINVAL, "Empty group configuration");
    }
    final Status st = new Status(-1, "Fail to get leader of group %s", groupId);
    for (final PeerId peer : conf) {
        if (!this.cliClientService.connect(peer.getEndpoint())) {
            LOG.error("Fail to connect peer {} to get leader for group {}.", peer, groupId);
            continue;
        }
        GetLeaderRequest rb = cliOptions.getRaftMessagesFactory().getLeaderRequest().groupId(groupId).peerId(peer.toString()).build();
        final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb, null);
        try {
            final Message msg = result.get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
            if (msg instanceof ErrorResponse) {
                if (st.isOk()) {
                    st.setError(-1, ((ErrorResponse) msg).errorMsg());
                } else {
                    final String savedMsg = st.getErrorMsg();
                    st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).errorMsg());
                }
            } else {
                final GetLeaderResponse response = (GetLeaderResponse) msg;
                if (leaderId.parse(response.leaderId())) {
                    break;
                }
            }
        } catch (final Exception e) {
            if (st.isOk()) {
                st.setError(-1, e.getMessage());
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, %s", savedMsg, e.getMessage());
            }
        }
    }
    if (leaderId.isEmpty()) {
        return st;
    }
    return Status.OK();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)

Example 3 with Message

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

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);
    }
    ResetLearnersRequest req = cliOptions.getRaftMessagesFactory().resetLearnersRequest().groupId(groupId).leaderId(leaderId.toString()).learnersList(learners.stream().map(Object::toString).collect(toList())).build();
    try {
        final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), req, null).get();
        return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) ResetLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ResetLearnersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 4 with Message

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

the class CliServiceImpl method snapshot.

@Override
public Status snapshot(final String groupId, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(peer, "Null peer");
    if (!this.cliClientService.connect(peer.getEndpoint())) {
        return new Status(-1, "Fail to init channel to %s", peer);
    }
    SnapshotRequest req = cliOptions.getRaftMessagesFactory().snapshotRequest().groupId(groupId).peerId(peer.toString()).build();
    try {
        final Message result = this.cliClientService.snapshot(peer.getEndpoint(), req, null).get();
        return statusFromResponse(result);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) SnapshotRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.SnapshotRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException)

Example 5 with Message

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

the class CopySessionTest method testOnRpcReturnedRetry.

@Test
public void testOnRpcReturnedRetry() throws Exception {
    assertNull(this.session.getTimer());
    assertNull(this.session.getRpcCall());
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
    this.session.setDestBuf(bufRef);
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final RpcRequests.GetFileRequest rb = raftOpts.getRaftMessagesFactory().getFileRequest().readerId(99).filename("data").count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
    Mockito.when(this.rpcService.getFile(this.address, rb, this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
    assertNotNull(this.session.getTimer());
    Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
    assertNotNull(this.session.getRpcCall());
    assertSame(future, this.session.getRpcCall());
    assertNull(this.session.getTimer());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) Test(org.junit.jupiter.api.Test)

Aggregations

Message (org.apache.ignite.raft.jraft.rpc.Message)35 Status (org.apache.ignite.raft.jraft.Status)18 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)16 Test (org.junit.jupiter.api.Test)15 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)12 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 ByteString (org.apache.ignite.raft.jraft.util.ByteString)7 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)6 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)5 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)5 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)5 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)3 RemotingException (org.apache.ignite.raft.jraft.error.RemotingException)3 AppendEntriesRequestBuilder (org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder)3 RpcContext (org.apache.ignite.raft.jraft.rpc.RpcContext)3 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)3 ByteBufferCollector (org.apache.ignite.raft.jraft.util.ByteBufferCollector)3