Search in sources :

Example 21 with Message

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

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

Example 22 with Message

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

the class CliServiceImpl method transferLeader.

@Override
public Status transferLeader(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");
    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);
    }
    TransferLeaderRequest rb = cliOptions.getRaftMessagesFactory().transferLeaderRequest().groupId(groupId).leaderId(leaderId.toString()).peerId(peer.isEmpty() ? null : peer.toString()).build();
    try {
        final Message result = this.cliClientService.transferLeader(leaderId.getEndpoint(), rb, 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) TransferLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.TransferLeaderRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 23 with Message

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

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);
    }
    GetPeersRequest req = cliOptions.getRaftMessagesFactory().getPeersRequest().groupId(groupId).leaderId(leaderId.toString()).onlyAlive(onlyGetAlive).build();
    try {
        final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), req, 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 Collection<String> responsePeers = returnLearners ? resp.learnersList() : resp.peersList();
            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.errorMsg());
        }
    } catch (final JRaftException e) {
        throw e;
    } catch (final Exception e) {
        throw new JRaftException(e);
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 24 with Message

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

the class CliServiceImpl method addLearners.

@Override
public Status addLearners(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);
    }
    AddLearnersRequest rb = cliOptions.getRaftMessagesFactory().addLearnersRequest().groupId(groupId).leaderId(leaderId.toString()).learnersList(learners.stream().map(Object::toString).collect(toList())).build();
    try {
        final Message result = this.cliClientService.addLearners(leaderId.getEndpoint(), rb, null).get();
        return processLearnersOpResponse(groupId, result, "adding 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) AddLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.AddLearnersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 25 with Message

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

the class FileServiceTest method testGetFileNotFound.

@Test
public void testGetFileNotFound() {
    long readerId = FileService.getInstance().addReader(this.fileReader);
    RpcRequests.GetFileRequest request = msgFactory.getFileRequest().count(Integer.MAX_VALUE).filename("data").offset(0).readerId(readerId).build();
    RpcContext asyncContext = Mockito.mock(RpcContext.class);
    Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext, msgFactory));
    assertTrue(msg instanceof RpcRequests.ErrorResponse);
    RpcRequests.ErrorResponse response = (RpcRequests.ErrorResponse) msg;
    assertEquals(RaftError.EIO.getNumber(), response.errorCode());
    assertEquals(String.format("Fail to read from path=%s filename=data", this.path), response.errorMsg());
}
Also used : RpcContext(org.apache.ignite.raft.jraft.rpc.RpcContext) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) 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