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);
});
}
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());
}
Aggregations