Search in sources :

Example 21 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class VaultServiceTest method testRemove.

/**
 * Tests regular behaviour of the {@link VaultService#remove} method.
 */
@Test
public void testRemove() throws Exception {
    ByteArray key = getKey(1);
    // Remove non-existent value.
    doAwait(() -> vaultService.remove(key));
    assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, null))));
    byte[] val = getValue(1);
    doAwait(() -> vaultService.put(key, val));
    assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, val))));
    // Remove existing value.
    doAwait(() -> vaultService.remove(key));
    assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, null))));
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 22 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class VaultServiceTest method testPutWithNull.

/**
 * Tests that the {@link VaultService#put} method removes the given {@code key} if {@code value} equalTo {@code null}.
 */
@Test
public void testPutWithNull() throws Exception {
    ByteArray key = getKey(1);
    byte[] val = getValue(1);
    doAwait(() -> vaultService.put(key, val));
    assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, val))));
    doAwait(() -> vaultService.put(key, null));
    assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, null))));
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 23 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class ItPersistencePropertiesVaultServiceTest method testPersistentRestart.

/**
 * Tests that the Vault Service correctly persists data after multiple service restarts.
 */
@Test
void testPersistentRestart() throws Exception {
    var data = Map.of(new ByteArray("key" + 1), fromString("value" + 1), new ByteArray("key" + 2), fromString("value" + 2), new ByteArray("key" + 3), fromString("value" + 3));
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        vaultService.putAll(data).get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        assertThat(vaultService.get(new ByteArray("key" + 1)), willBe(equalTo(new VaultEntry(new ByteArray("key" + 1), fromString("value" + 1)))));
    }
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        try (var cursor = vaultService.range(new ByteArray("key" + 1), new ByteArray("key" + 4))) {
            var actualData = new ArrayList<VaultEntry>();
            cursor.forEachRemaining(actualData::add);
            List<VaultEntry> expectedData = data.entrySet().stream().map(e -> new VaultEntry(e.getKey(), e.getValue())).sorted(Comparator.comparing(VaultEntry::key)).collect(Collectors.toList());
            assertThat(actualData, is(expectedData));
        }
    }
}
Also used : VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ArrayList(java.util.ArrayList) ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 24 with ByteArray

use of org.apache.ignite.lang.ByteArray 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 25 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class ItMetaStorageServicePersistenceTest method snapshotCheckClosure.

/**
 * {@inheritDoc}
 */
@Override
public BooleanSupplier snapshotCheckClosure(JraftServerImpl restarted, boolean interactedAfterSnapshot) {
    KeyValueStorage storage = getListener(restarted, raftGroupId()).getStorage();
    byte[] lastKey = interactedAfterSnapshot ? SECOND_KEY.bytes() : FIRST_KEY.bytes();
    byte[] lastValue = interactedAfterSnapshot ? SECOND_VALUE : FIRST_VALUE;
    int expectedRevision = interactedAfterSnapshot ? 4 : 3;
    int expectedUpdateCounter = interactedAfterSnapshot ? 4 : 3;
    EntryImpl expectedLastEntry = new EntryImpl(new ByteArray(lastKey), lastValue, expectedRevision, expectedUpdateCounter);
    return () -> {
        org.apache.ignite.internal.metastorage.server.Entry e = storage.get(lastKey);
        return e.empty() == expectedLastEntry.empty() && e.tombstone() == expectedLastEntry.tombstone() && e.revision() == expectedLastEntry.revision() && e.updateCounter() == expectedLastEntry.revision() && Arrays.equals(e.key(), expectedLastEntry.key().bytes()) && Arrays.equals(e.value(), expectedLastEntry.value());
    };
}
Also used : KeyValueStorage(org.apache.ignite.internal.metastorage.server.KeyValueStorage) RocksDbKeyValueStorage(org.apache.ignite.internal.metastorage.server.persistence.RocksDbKeyValueStorage) ByteArray(org.apache.ignite.lang.ByteArray)

Aggregations

ByteArray (org.apache.ignite.lang.ByteArray)51 Test (org.junit.jupiter.api.Test)35 Cursor (org.apache.ignite.internal.util.Cursor)13 List (java.util.List)12 WatchAggregator (org.apache.ignite.internal.metastorage.watch.WatchAggregator)12 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Map (java.util.Map)10 NotNull (org.jetbrains.annotations.NotNull)9 Collection (java.util.Collection)8 Iterator (java.util.Iterator)8 Collectors (java.util.stream.Collectors)8 AfterEach (org.junit.jupiter.api.AfterEach)8 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)8 Assertions.fail (org.junit.jupiter.api.Assertions.fail)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ByteBuffer (java.nio.ByteBuffer)7 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)7 ArrayList (java.util.ArrayList)7 NoSuchElementException (java.util.NoSuchElementException)7 Function.identity (java.util.function.Function.identity)7