use of org.apache.ignite.raft.client.Command in project ignite-3 by apache.
the class InternalTableImpl method enlistInTx.
/**
* Enlists multiple rows into a transaction.
*
* @param keyRows Rows.
* @param tx The transaction.
* @param op Command factory.
* @param reducer The reducer.
* @param <R> Reducer's input.
* @param <T> Reducer's output.
* @return The future.
*/
private <R, T> CompletableFuture<T> enlistInTx(Collection<BinaryRow> keyRows, InternalTransaction tx, BiFunction<Collection<BinaryRow>, InternalTransaction, Command> op, Function<CompletableFuture<R>[], CompletableFuture<T>> reducer) {
final boolean implicit = tx == null;
final InternalTransaction tx0 = implicit ? txManager.begin() : tx;
Int2ObjectOpenHashMap<List<BinaryRow>> keyRowsByPartition = mapRowsToPartitions(keyRows);
CompletableFuture<R>[] futures = new CompletableFuture[keyRowsByPartition.size()];
int batchNum = 0;
for (Int2ObjectOpenHashMap.Entry<List<BinaryRow>> partToRows : keyRowsByPartition.int2ObjectEntrySet()) {
CompletableFuture<RaftGroupService> fut = enlist(partToRows.getIntKey(), tx0);
futures[batchNum++] = fut.thenCompose(svc -> svc.run(op.apply(partToRows.getValue(), tx0)));
}
CompletableFuture<T> fut = reducer.apply(futures);
return postEnlist(fut, implicit, tx0);
}
use of org.apache.ignite.raft.client.Command 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