Search in sources :

Example 1 with ActionRequest

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

the class RaftGroupServiceTest method mockUserInput.

/**
 * @param delay {@code True} to create a delay before response.
 * @param peer Fail the request targeted to given peer.
 */
private void mockUserInput(boolean delay, @Nullable Peer peer) {
    when(messagingService.invoke(any(NetworkAddress.class), argThat(new ArgumentMatcher<ActionRequest>() {

        @Override
        public boolean matches(ActionRequest arg) {
            return arg.command() instanceof TestCommand;
        }
    }), anyLong())).then(invocation -> {
        NetworkAddress target = invocation.getArgument(0);
        if (peer != null && target.equals(peer.address()))
            return failedFuture(new IgniteInternalException(new ConnectException()));
        if (delay) {
            return CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    fail();
                }
                return FACTORY.actionResponse().result(new TestResponse()).build();
            });
        }
        Object resp;
        if (leader == null)
            resp = FACTORY.errorResponse().errorCode(RaftError.EPERM.getNumber()).build();
        else if (!target.equals(leader.address()))
            resp = FACTORY.errorResponse().errorCode(RaftError.EPERM.getNumber()).leaderId(PeerId.fromPeer(leader).toString()).build();
        else
            resp = FACTORY.actionResponse().result(new TestResponse()).build();
        return completedFuture(resp);
    });
}
Also used : NetworkAddress(org.apache.ignite.network.NetworkAddress) ActionRequest(org.apache.ignite.raft.jraft.rpc.ActionRequest) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ArgumentMatcher(org.mockito.ArgumentMatcher) ConnectException(java.net.ConnectException)

Example 2 with ActionRequest

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

the class RaftGroupServiceImpl method run.

/**
 * {@inheritDoc}
 */
@Override
public <R> CompletableFuture<R> run(Command cmd) {
    Peer leader = this.leader;
    if (leader == null)
        return refreshLeader().thenCompose(res -> run(cmd));
    ActionRequest req = factory.actionRequest().command(cmd).groupId(groupId).readOnlySafe(true).build();
    CompletableFuture<ActionResponse> fut = new CompletableFuture<>();
    sendWithRetry(leader, req, currentTimeMillis() + timeout, fut);
    return fut.thenApply(resp -> (R) resp.result());
}
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) ActionRequest(org.apache.ignite.raft.jraft.rpc.ActionRequest) Peer(org.apache.ignite.raft.client.Peer) ActionResponse(org.apache.ignite.raft.jraft.rpc.ActionResponse)

Aggregations

NetworkAddress (org.apache.ignite.network.NetworkAddress)2 ActionRequest (org.apache.ignite.raft.jraft.rpc.ActionRequest)2 IOException (java.io.IOException)1 System.currentTimeMillis (java.lang.System.currentTimeMillis)1 ConnectException (java.net.ConnectException)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 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)1