Search in sources :

Example 36 with ByteArray

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

the class ItMetaStorageServiceTest method testPut.

/**
 * Tests {@link MetaStorageService#put(ByteArray, byte[])}.
 *
 * @throws Exception If failed.
 */
@Test
public void testPut() throws Exception {
    ByteArray expKey = new ByteArray(new byte[] { 1 });
    byte[] expVal = { 2 };
    doNothing().when(mockStorage).put(expKey.bytes(), expVal);
    metaStorageSvc.put(expKey, expVal).get();
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 37 with ByteArray

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

the class ItMetaStorageServiceTest method testRangeHasNext.

/**
 * Tests {@link MetaStorageService#range(ByteArray, ByteArray, long)}} hasNext.
 */
@Test
public void testRangeHasNext() {
    ByteArray expKeyFrom = new ByteArray(new byte[] { 1 });
    when(mockStorage.range(expKeyFrom.bytes(), null)).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        when(cursor.hasNext()).thenReturn(true);
        return cursor;
    });
    Cursor<Entry> cursor = metaStorageSvc.range(expKeyFrom, null);
    assertTrue(cursor.iterator().hasNext());
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 38 with ByteArray

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

the class ItMetaStorageServiceTest method testWatchOnUpdate.

@Test
public void testWatchOnUpdate() throws Exception {
    org.apache.ignite.internal.metastorage.server.WatchEvent expectedEvent = new org.apache.ignite.internal.metastorage.server.WatchEvent(List.of(new org.apache.ignite.internal.metastorage.server.EntryEvent(new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 2 }, new byte[] { 20 }, 1, 1), new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 2 }, new byte[] { 21 }, 2, 4)), new org.apache.ignite.internal.metastorage.server.EntryEvent(new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 3 }, new byte[] { 20 }, 1, 2), new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 3 }, new byte[] {}, 2, 5)), new org.apache.ignite.internal.metastorage.server.EntryEvent(new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 4 }, new byte[] { 20 }, 1, 3), new org.apache.ignite.internal.metastorage.server.Entry(new byte[] { 4 }, new byte[] {}, 3, 6))));
    ByteArray keyFrom = new ByteArray(new byte[] { 1 });
    ByteArray keyTo = new ByteArray(new byte[] { 10 });
    long rev = 2;
    when(mockStorage.watch(keyFrom.bytes(), keyTo.bytes(), rev)).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        when(cursor.hasNext()).thenReturn(true);
        when(cursor.next()).thenReturn(expectedEvent);
        return cursor;
    });
    CountDownLatch latch = new CountDownLatch(1);
    IgniteUuid watchId = metaStorageSvc.watch(keyFrom, keyTo, rev, new WatchListener() {

        @Override
        public boolean onUpdate(@NotNull WatchEvent event) {
            Collection<EntryEvent> expectedEvents = expectedEvent.entryEvents();
            Collection<org.apache.ignite.internal.metastorage.client.EntryEvent> actualEvents = event.entryEvents();
            assertEquals(expectedEvents.size(), actualEvents.size());
            Iterator<EntryEvent> expectedIterator = expectedEvents.iterator();
            Iterator<org.apache.ignite.internal.metastorage.client.EntryEvent> actualIterator = actualEvents.iterator();
            while (expectedIterator.hasNext() && actualIterator.hasNext()) {
                org.apache.ignite.internal.metastorage.server.EntryEvent expectedEntryEvent = expectedIterator.next();
                org.apache.ignite.internal.metastorage.client.EntryEvent actualEntryEvent = actualIterator.next();
                assertArrayEquals(expectedEntryEvent.oldEntry().key(), actualEntryEvent.oldEntry().key().bytes());
                assertArrayEquals(expectedEntryEvent.oldEntry().value(), actualEntryEvent.oldEntry().value());
                assertArrayEquals(expectedEntryEvent.entry().key(), actualEntryEvent.newEntry().key().bytes());
                assertArrayEquals(expectedEntryEvent.entry().value(), actualEntryEvent.newEntry().value());
            }
            latch.countDown();
            return true;
        }

        @Override
        public void onError(@NotNull Throwable e) {
            // Within given test it's not expected to get here.
            fail();
        }
    }).get();
    latch.await();
    metaStorageSvc.stopWatch(watchId).get();
}
Also used : NotNull(org.jetbrains.annotations.NotNull) IgniteUuid(org.apache.ignite.lang.IgniteUuid) EntryEvent(org.apache.ignite.internal.metastorage.server.EntryEvent) ByteArray(org.apache.ignite.lang.ByteArray) EntryEvent(org.apache.ignite.internal.metastorage.server.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 39 with ByteArray

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

the class ItMetaStorageServiceTest method testRangeWitNullAsKeyTo.

/**
 * Tests {@link MetaStorageService#range(ByteArray, ByteArray, long)}} with null keyTo.
 *
 * @throws Exception If failed.
 */
@Test
public void testRangeWitNullAsKeyTo() throws Exception {
    ByteArray expKeyFrom = new ByteArray(new byte[] { 1 });
    when(mockStorage.range(expKeyFrom.bytes(), null)).thenReturn(mock(Cursor.class));
    metaStorageSvc.range(expKeyFrom, null).close();
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Cursor(org.apache.ignite.internal.util.Cursor) Test(org.junit.jupiter.api.Test)

Example 40 with ByteArray

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

the class WatchAggregator method watchListener.

/**
 * Produces the watch listener, which will dispatch events to appropriate watches.
 *
 * @param storeRevision action to commit keys-revision pair to persistent store for processed keys.
 * @return watch listener, which will dispatch events to appropriate watches.
 */
private WatchListener watchListener(BiConsumer<Collection<IgniteBiTuple<ByteArray, byte[]>>, Long> storeRevision) {
    // Copy watches to separate collection, because all changes on the WatchAggregator watches
    // shouldn't be propagated to listener watches immediately.
    // WatchAggregator will be redeployed with new watches if needed instead.
    final LinkedHashMap<Long, Watch> cpWatches = new LinkedHashMap<>(watches);
    return new WatchListener() {

        @Override
        public boolean onUpdate(@NotNull WatchEvent evt) {
            // TODO: IGNITE-15858 Fix stopWatch may solve the issue.
            synchronized (watches) {
                processWatchEvents(evt);
            }
            return true;
        }

        /**
         * Process watch events synchronously.
         *
         * @param evt Watch event.
         */
        private void processWatchEvents(@NotNull WatchEvent evt) {
            var watchIt = cpWatches.entrySet().iterator();
            Collection<Long> toCancel = new ArrayList<>();
            while (watchIt.hasNext()) {
                Map.Entry<Long, Watch> entry = watchIt.next();
                Watch watch = entry.getValue();
                var filteredEvts = new ArrayList<EntryEvent>();
                for (EntryEvent entryEvt : evt.entryEvents()) {
                    if (watch.keyCriterion().contains(entryEvt.oldEntry().key())) {
                        filteredEvts.add(entryEvt);
                    }
                }
                if (!filteredEvts.isEmpty()) {
                    if (!watch.listener().onUpdate(new WatchEvent(filteredEvts))) {
                        watchIt.remove();
                        toCancel.add(entry.getKey());
                    }
                }
            }
            // to prevent finished watches from redeploy.
            if (!toCancel.isEmpty()) {
                cancelAll(toCancel);
            }
            var revision = 0L;
            var entries = new ArrayList<IgniteBiTuple<ByteArray, byte[]>>();
            for (EntryEvent entryEvt : evt.entryEvents()) {
                revision = entryEvt.newEntry().revision();
                entries.add(new IgniteBiTuple<>(entryEvt.newEntry().key(), entryEvt.newEntry().value()));
            }
            storeRevision.accept(entries, revision);
        }

        @Override
        public void onError(@NotNull Throwable e) {
            watches.values().forEach(w -> w.listener().onError(e));
        }
    };
}
Also used : WatchListener(org.apache.ignite.internal.metastorage.client.WatchListener) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) LinkedHashMap(java.util.LinkedHashMap) EntryEvent(org.apache.ignite.internal.metastorage.client.EntryEvent) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteArray(org.apache.ignite.lang.ByteArray) WatchEvent(org.apache.ignite.internal.metastorage.client.WatchEvent) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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