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