Search in sources :

Example 26 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class InternalTableImpl method awaitLeaderInitialization.

private void awaitLeaderInitialization() {
    List<CompletableFuture<Void>> futs = new ArrayList<>();
    for (RaftGroupService raftSvc : partitionMap.values()) {
        if (raftSvc.leader() == null) {
            futs.add(raftSvc.refreshLeader());
        }
    }
    CompletableFuture.allOf(futs.toArray(CompletableFuture[]::new)).join();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) ArrayList(java.util.ArrayList)

Example 27 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class InternalTableImpl method enlist.

/**
 * Enlists a partition.
 *
 * @param partId Partition id.
 * @param tx     The transaction.
 * @return The enlist future (then will a leader become known).
 */
protected CompletableFuture<RaftGroupService> enlist(int partId, InternalTransaction tx) {
    RaftGroupService svc = partitionMap.get(partId);
    CompletableFuture<Void> fut0 = svc.leader() == null ? svc.refreshLeader() : completedFuture(null);
    // Enlist the leaseholder.
    return fut0.thenAccept(ignored -> tx.enlist(svc)).thenApply(ignored -> svc);
}
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) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService)

Example 28 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService 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 29 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class TxManagerTest method testEnlist.

@Test
public void testEnlist() throws TransactionException {
    NetworkAddress addr = clusterService.topologyService().localMember().address();
    assertEquals(ADDR, addr);
    InternalTransaction tx = txManager.begin();
    RaftGroupService svc = Mockito.mock(RaftGroupService.class);
    tx.enlist(svc);
    assertEquals(1, tx.enlisted().size());
    assertTrue(tx.enlisted().contains(svc));
}
Also used : NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 30 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class ItMetaStorageServiceTest method testCursorsCleanup.

/**
 * Tests {@link MetaStorageService#closeCursors(String)}.
 *
 * @throws Exception If failed.
 */
@Test
public void testCursorsCleanup() throws Exception {
    when(mockStorage.range(EXPECTED_RESULT_ENTRY.key().bytes(), null)).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        when(cursor.hasNext()).thenReturn(true);
        when(cursor.next()).thenReturn(EXPECTED_SRV_RESULT_ENTRY);
        return cursor;
    });
    List<Peer> peers = List.of(new Peer(cluster.get(0).topologyService().localMember().address()));
    RaftGroupService metaStorageRaftGrpSvc = RaftGroupServiceImpl.start(METASTORAGE_RAFT_GROUP_NAME, cluster.get(1), FACTORY, 10_000, peers, true, 200, executor).get(3, TimeUnit.SECONDS);
    try {
        MetaStorageService metaStorageSvc2 = new MetaStorageServiceImpl(metaStorageRaftGrpSvc, NODE_ID_1);
        Cursor<Entry> cursorNode0 = metaStorageSvc.range(EXPECTED_RESULT_ENTRY.key(), null);
        Cursor<Entry> cursor2Node0 = metaStorageSvc.range(EXPECTED_RESULT_ENTRY.key(), null);
        final Cursor<Entry> cursorNode1 = metaStorageSvc2.range(EXPECTED_RESULT_ENTRY.key(), null);
        metaStorageSvc.closeCursors(NODE_ID_0).get();
        assertThrows(NoSuchElementException.class, () -> cursorNode0.iterator().next());
        assertThrows(NoSuchElementException.class, () -> cursor2Node0.iterator().next());
        assertEquals(EXPECTED_RESULT_ENTRY, (cursorNode1.iterator().next()));
    } finally {
        metaStorageRaftGrpSvc.shutdown();
    }
}
Also used : Peer(org.apache.ignite.raft.client.Peer) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Aggregations

RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)46 Test (org.junit.jupiter.api.Test)33 NetworkAddress (org.apache.ignite.network.NetworkAddress)24 Peer (org.apache.ignite.raft.client.Peer)22 List (java.util.List)11 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 TimeoutException (java.util.concurrent.TimeoutException)8 Collectors (java.util.stream.Collectors)8 Loza (org.apache.ignite.internal.raft.Loza)8 IgniteLogger (org.apache.ignite.lang.IgniteLogger)8 NotNull (org.jetbrains.annotations.NotNull)8 ExecutionException (java.util.concurrent.ExecutionException)7 TxManager (org.apache.ignite.internal.tx.TxManager)7 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)7 AfterEach (org.junit.jupiter.api.AfterEach)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 IOException (java.io.IOException)6 UUID (java.util.UUID)6