Search in sources :

Example 1 with Command

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);
}
Also used : DeleteCommand(org.apache.ignite.internal.table.distributed.command.DeleteCommand) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Subscription(java.util.concurrent.Flow.Subscription) BiFunction(java.util.function.BiFunction) InsertCommand(org.apache.ignite.internal.table.distributed.command.InsertCommand) DeleteAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteAllCommand) DeleteExactAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactAllCommand) IgniteLogger(org.apache.ignite.lang.IgniteLogger) GetAndDeleteCommand(org.apache.ignite.internal.table.distributed.command.GetAndDeleteCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) ScanRetrieveBatchCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanRetrieveBatchCommand) GetAllCommand(org.apache.ignite.internal.table.distributed.command.GetAllCommand) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ScanCloseCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanCloseCommand) Collection(java.util.Collection) UUID(java.util.UUID) ReplaceCommand(org.apache.ignite.internal.table.distributed.command.ReplaceCommand) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) TxManager(org.apache.ignite.internal.tx.TxManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) GetAndReplaceCommand(org.apache.ignite.internal.table.distributed.command.GetAndReplaceCommand) ScanInitCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanInitCommand) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) InternalTable(org.apache.ignite.internal.table.InternalTable) UpsertCommand(org.apache.ignite.internal.table.distributed.command.UpsertCommand) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IgniteUuidGenerator(org.apache.ignite.lang.IgniteUuidGenerator) Publisher(java.util.concurrent.Flow.Publisher) GetAndUpsertCommand(org.apache.ignite.internal.table.distributed.command.GetAndUpsertCommand) DeleteExactCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactCommand) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) ReplaceIfExistCommand(org.apache.ignite.internal.table.distributed.command.ReplaceIfExistCommand) MultiRowsResponse(org.apache.ignite.internal.table.distributed.command.response.MultiRowsResponse) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) NetworkAddress(org.apache.ignite.network.NetworkAddress) TestOnly(org.jetbrains.annotations.TestOnly) Peer(org.apache.ignite.raft.client.Peer) Subscriber(java.util.concurrent.Flow.Subscriber) GetCommand(org.apache.ignite.internal.table.distributed.command.GetCommand) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) UpsertAllCommand(org.apache.ignite.internal.table.distributed.command.UpsertAllCommand) InsertAllCommand(org.apache.ignite.internal.table.distributed.command.InsertAllCommand) Comparator(java.util.Comparator) SingleRowResponse(org.apache.ignite.internal.table.distributed.command.response.SingleRowResponse) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with Command

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

ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 IgniteLogger (org.apache.ignite.lang.IgniteLogger)2 NetworkAddress (org.apache.ignite.network.NetworkAddress)2 Command (org.apache.ignite.raft.client.Command)2 Peer (org.apache.ignite.raft.client.Peer)2 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)2 NotNull (org.jetbrains.annotations.NotNull)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 IOException (java.io.IOException)1 System.currentTimeMillis (java.lang.System.currentTimeMillis)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 UUID (java.util.UUID)1