Search in sources :

Example 16 with RaftGroupService

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

the class TransactionImpl method finish.

/**
 * Finishes a transaction.
 *
 * @param commit {@code true} to commit, false to rollback.
 * @return The future.
 */
private CompletableFuture<Void> finish(boolean commit) {
    Map<NetworkAddress, Set<String>> tmp = new HashMap<>();
    // Group by common leader addresses.
    for (RaftGroupService svc : enlisted) {
        NetworkAddress addr = svc.leader().address();
        tmp.computeIfAbsent(addr, k -> new HashSet<>()).add(svc.groupId());
    }
    CompletableFuture[] futs = new CompletableFuture[tmp.size() + 1];
    int i = 0;
    for (Map.Entry<NetworkAddress, Set<String>> entry : tmp.entrySet()) {
        boolean local = address.equals(entry.getKey());
        futs[i++] = txManager.finishRemote(entry.getKey(), timestamp, commit, entry.getValue());
        LOG.debug("finish [addr={}, commit={}, ts={}, local={}, groupIds={}", address, commit, timestamp, local, entry.getValue());
    }
    // Handle coordinator's tx.
    futs[i] = tmp.containsKey(address) ? CompletableFuture.completedFuture(null) : commit ? txManager.commitAsync(timestamp) : txManager.rollbackAsync(timestamp);
    return CompletableFuture.allOf(futs);
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Timestamp(org.apache.ignite.internal.tx.Timestamp) IgniteLogger(org.apache.ignite.lang.IgniteLogger) TxManager(org.apache.ignite.internal.tx.TxManager) NetworkAddress(org.apache.ignite.network.NetworkAddress) HashSet(java.util.HashSet) ExecutionException(java.util.concurrent.ExecutionException) Nullable(org.jetbrains.annotations.Nullable) TxState(org.apache.ignite.internal.tx.TxState) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkAddress(org.apache.ignite.network.NetworkAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 17 with RaftGroupService

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

the class ItMetaStorageRaftGroupTest method testRangeNextWorksCorrectlyAfterLeaderChange.

/**
 * Tests that {@link MetaStorageService#range(ByteArray, ByteArray, long)}} next command works correctly
 * after leader changing.
 *
 * @throws Exception If failed.
 */
@Test
public void testRangeNextWorksCorrectlyAfterLeaderChange() throws Exception {
    final AtomicInteger replicatorStartedCounter = new AtomicInteger(0);
    final AtomicInteger replicatorStoppedCounter = new AtomicInteger(0);
    when(mockStorage.range(EXPECTED_RESULT_ENTRY1.key().bytes(), new byte[] { 4 })).thenAnswer(invocation -> {
        List<org.apache.ignite.internal.metastorage.server.Entry> entries = new ArrayList<>(List.of(EXPECTED_SRV_RESULT_ENTRY1, EXPECTED_SRV_RESULT_ENTRY2));
        return new Cursor<org.apache.ignite.internal.metastorage.server.Entry>() {

            private final Iterator<org.apache.ignite.internal.metastorage.server.Entry> it = entries.iterator();

            @Override
            public void close() {
            }

            @NotNull
            @Override
            public Iterator<org.apache.ignite.internal.metastorage.server.Entry> iterator() {
                return it;
            }

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public org.apache.ignite.internal.metastorage.server.Entry next() {
                return it.next();
            }
        };
    });
    List<Pair<RaftServer, RaftGroupService>> raftServersRaftGroups = prepareJraftMetaStorages(replicatorStartedCounter, replicatorStoppedCounter);
    List<RaftServer> raftServers = raftServersRaftGroups.stream().map(p -> p.key).collect(Collectors.toList());
    NetworkAddress oldLeader = raftServersRaftGroups.get(0).value.leader().address();
    Optional<RaftServer> oldLeaderServer = raftServers.stream().filter(s -> s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
    // Server that will be alive after we stop leader.
    Optional<RaftServer> liveServer = raftServers.stream().filter(s -> !s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
    if (oldLeaderServer.isEmpty() || liveServer.isEmpty()) {
        fail();
    }
    RaftGroupService raftGroupServiceOfLiveServer = raftServersRaftGroups.stream().filter(p -> p.key.equals(liveServer.get())).findFirst().get().value;
    MetaStorageService metaStorageSvc = new MetaStorageServiceImpl(raftGroupServiceOfLiveServer, "some_node");
    Cursor<Entry> cursor = metaStorageSvc.range(EXPECTED_RESULT_ENTRY1.key(), new ByteArray(new byte[] { 4 }));
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
    assertTrue(cursor.hasNext());
    assertEquals(EXPECTED_RESULT_ENTRY1, (cursor.iterator().next()));
    // Ensure that leader has not been changed.
    // In a stable topology unexpected leader election shouldn't happen.
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
    // stop leader
    oldLeaderServer.get().stopRaftGroup(METASTORAGE_RAFT_GROUP_NAME);
    oldLeaderServer.get().stop();
    cluster.stream().filter(c -> c.topologyService().localMember().address().equals(oldLeader)).findFirst().get().stop();
    raftGroupServiceOfLiveServer.refreshLeader().get();
    assertNotSame(oldLeader, raftGroupServiceOfLiveServer.leader().address());
    // ensure that leader has been changed only once
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 4, 5_000), replicatorStartedCounter.get() + "");
    assertTrue(TestUtils.waitForCondition(() -> replicatorStoppedCounter.get() == 2, 5_000), replicatorStoppedCounter.get() + "");
    assertTrue(cursor.hasNext());
    assertEquals(EXPECTED_RESULT_ENTRY2, (cursor.iterator().next()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) Path(java.nio.file.Path) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) KeyValueStorage(org.apache.ignite.internal.metastorage.server.KeyValueStorage) Cursor(org.apache.ignite.internal.util.Cursor) Status(org.apache.ignite.raft.jraft.Status) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Collectors(java.util.stream.Collectors) TestUtils(org.apache.ignite.raft.jraft.test.TestUtils) TestInfo(org.junit.jupiter.api.TestInfo) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) MetaStorageListener(org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Replicator(org.apache.ignite.raft.jraft.core.Replicator) NotNull(org.jetbrains.annotations.NotNull) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) ClusterServiceTestUtils.findLocalAddresses(org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) Mock(org.mockito.Mock) Loza(org.apache.ignite.internal.raft.Loza) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ArrayList(java.util.ArrayList) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) TestUtils.waitForTopology(org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Iterator(java.util.Iterator) ByteArray(org.apache.ignite.lang.ByteArray) Mockito.when(org.mockito.Mockito.when) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) ArrayList(java.util.ArrayList) Cursor(org.apache.ignite.internal.util.Cursor) NetworkAddress(org.apache.ignite.network.NetworkAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 18 with RaftGroupService

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

the class ItJraftCounterServerTest method testCreateSnapshotAbnormalFailure.

@Test
public void testCreateSnapshotAbnormalFailure() throws Exception {
    listenerFactory = () -> new CounterListener() {

        @Override
        public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
            doneClo.accept(new IgniteInternalException("Very bad"));
        }
    };
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    long val = applyIncrements(client1, 1, 10);
    assertEquals(sum(10), val);
    Peer peer = servers.get(0).localPeer(COUNTER_GROUP_0);
    try {
        client1.snapshot(peer).get();
        fail();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof RaftException);
    }
}
Also used : Path(java.nio.file.Path) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) TimeoutException(java.util.concurrent.TimeoutException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IOException(java.io.IOException) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) Test(org.junit.jupiter.api.Test)

Example 19 with RaftGroupService

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

the class ItJraftCounterServerTest method testCounterCommandListener.

@Test
public void testCounterCommandListener() throws Exception {
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    assertNotNull(client1.leader());
    assertNotNull(client2.leader());
    assertEquals(2, client1.<Long>run(new IncrementAndGetCommand(2)).get());
    assertEquals(2, client1.<Long>run(new GetValueCommand()).get());
    assertEquals(3, client1.<Long>run(new IncrementAndGetCommand(1)).get());
    assertEquals(3, client1.<Long>run(new GetValueCommand()).get());
    assertEquals(4, client2.<Long>run(new IncrementAndGetCommand(4)).get());
    assertEquals(4, client2.<Long>run(new GetValueCommand()).get());
    assertEquals(7, client2.<Long>run(new IncrementAndGetCommand(3)).get());
    assertEquals(7, client2.<Long>run(new GetValueCommand()).get());
}
Also used : RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Example 20 with RaftGroupService

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

the class ItJraftCounterServerTest method testCreateSnapshot.

@Test
public void testCreateSnapshot() throws Exception {
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    JraftServerImpl server = servers.get(0);
    long val = applyIncrements(client1, 1, 10);
    assertEquals(sum(10), val);
    client1.snapshot(server.localPeer(COUNTER_GROUP_0)).get();
    long val2 = applyIncrements(client2, 1, 20);
    assertEquals(sum(20), val2);
    client2.snapshot(server.localPeer(COUNTER_GROUP_1)).get();
    Path snapshotDir0 = server.getServerDataPath(COUNTER_GROUP_0).resolve("snapshot");
    assertEquals(1L, countFiles(snapshotDir0));
    Path snapshotDir1 = server.getServerDataPath(COUNTER_GROUP_1).resolve("snapshot");
    assertEquals(1L, countFiles(snapshotDir1));
}
Also used : Path(java.nio.file.Path) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) 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