Search in sources :

Example 6 with WatchAggregator

use of org.apache.ignite.internal.metastorage.watch.WatchAggregator in project ignite-3 by apache.

the class WatchListenerInhibitor method metastorageEventsInhibitor.

/**
 * Creates the specific listener which can inhibit events for real metastorage listener.
 *
 * @param ignite Ignite.
 * @return Listener inhibitor.
 * @throws Exception If something wrong when creating the listener inhibitor.
 */
public static WatchListenerInhibitor metastorageEventsInhibitor(Ignite ignite) throws Exception {
    // TODO: IGNITE-15723 After a component factory will be implemented, need to got rid of reflection here.
    MetaStorageManager metaMngr = (MetaStorageManager) ReflectionUtils.tryToReadFieldValue(IgniteImpl.class, "metaStorageMgr", (IgniteImpl) ignite).get();
    assertNotNull(metaMngr);
    WatchAggregator aggregator = (WatchAggregator) ReflectionUtils.tryToReadFieldValue(MetaStorageManager.class, "watchAggregator", metaMngr).get();
    assertNotNull(aggregator);
    WatchAggregator aggregatorSpy = Mockito.spy(aggregator);
    WatchListenerInhibitor inhibitor = new WatchListenerInhibitor();
    doAnswer(mock -> {
        Optional<AggregatedWatch> op = (Optional<AggregatedWatch>) mock.callRealMethod();
        assertTrue(op.isPresent());
        inhibitor.setRealListener(op.get().listener());
        return Optional.of(new AggregatedWatch(op.get().keyCriterion(), op.get().revision(), inhibitor));
    }).when(aggregatorSpy).watch(anyLong(), any());
    IgniteTestUtils.setFieldValue(metaMngr, "watchAggregator", aggregatorSpy);
    // Redeploy metastorage watch. The Watch inhibitor will be used after.
    metaMngr.unregisterWatch(-1);
    return inhibitor;
}
Also used : Optional(java.util.Optional) MetaStorageManager(org.apache.ignite.internal.metastorage.MetaStorageManager) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) AggregatedWatch(org.apache.ignite.internal.metastorage.watch.AggregatedWatch)

Example 7 with WatchAggregator

use of org.apache.ignite.internal.metastorage.watch.WatchAggregator in project ignite-3 by apache.

the class WatchAggregatorTest method testOneCriterionInference.

@Test
public void testOneCriterionInference() {
    var watchAggregator = new WatchAggregator();
    watchAggregator.add(new ByteArray("key"), null);
    var keyCriterion = watchAggregator.watch(0, null).get().keyCriterion();
    assertEquals(new KeyCriterion.ExactCriterion(new ByteArray("key")), keyCriterion);
}
Also used : KeyCriterion(org.apache.ignite.internal.metastorage.watch.KeyCriterion) ByteArray(org.apache.ignite.lang.ByteArray) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Example 8 with WatchAggregator

use of org.apache.ignite.internal.metastorage.watch.WatchAggregator in project ignite-3 by apache.

the class WatchAggregatorTest method testCancel.

@Test
public void testCancel() {
    var watchAggregator = new WatchAggregator();
    var lsnr1 = mock(WatchListener.class);
    when(lsnr1.onUpdate(any())).thenReturn(true);
    var lsnr2 = mock(WatchListener.class);
    when(lsnr2.onUpdate(any())).thenReturn(true);
    final var id1 = watchAggregator.add(new ByteArray("1"), lsnr1);
    var id2 = watchAggregator.add(new ByteArray("2"), lsnr2);
    var entryEvt1 = new EntryEvent(entry("1", "value1", 1, 1), entry("1", "value1n", 1, 1));
    var entryEvt2 = new EntryEvent(entry("2", "value2", 1, 1), entry("2", "value2n", 1, 1));
    watchAggregator.watch(1, (v1, v2) -> {
    }).get().listener().onUpdate(new WatchEvent(List.of(entryEvt1, entryEvt2)));
    verify(lsnr1, times(1)).onUpdate(any());
    verify(lsnr2, times(1)).onUpdate(any());
    watchAggregator.cancel(id1);
    watchAggregator.watch(1, (v1, v2) -> {
    }).get().listener().onUpdate(new WatchEvent(List.of(entryEvt1, entryEvt2)));
    verify(lsnr1, times(1)).onUpdate(any());
    verify(lsnr2, times(2)).onUpdate(any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) WatchEvent(org.apache.ignite.internal.metastorage.client.WatchEvent) WatchListener(org.apache.ignite.internal.metastorage.client.WatchListener) ByteArray(org.apache.ignite.lang.ByteArray) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) KeyCriterion(org.apache.ignite.internal.metastorage.watch.KeyCriterion) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EntryEvent(org.apache.ignite.internal.metastorage.client.EntryEvent) NotNull(org.jetbrains.annotations.NotNull) Mockito.mock(org.mockito.Mockito.mock) Entry(org.apache.ignite.internal.metastorage.client.Entry) EntryEvent(org.apache.ignite.internal.metastorage.client.EntryEvent) ByteArray(org.apache.ignite.lang.ByteArray) WatchEvent(org.apache.ignite.internal.metastorage.client.WatchEvent) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Example 9 with WatchAggregator

use of org.apache.ignite.internal.metastorage.watch.WatchAggregator in project ignite-3 by apache.

the class WatchAggregatorTest method testNullKeyAsEndOfRangeCriterion.

@Test
public void testNullKeyAsEndOfRangeCriterion() {
    var watchAggregator = new WatchAggregator();
    watchAggregator.add(new ByteArray("key3"), null);
    watchAggregator.add(new ByteArray("key1"), null, null);
    var keyCriterion = watchAggregator.watch(0, null).get().keyCriterion();
    var expKeyCriterion = new KeyCriterion.RangeCriterion(new ByteArray("key1"), null);
    assertEquals(expKeyCriterion, keyCriterion);
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Example 10 with WatchAggregator

use of org.apache.ignite.internal.metastorage.watch.WatchAggregator in project ignite-3 by apache.

the class WatchAggregatorTest method testExactInTheMiddleAndRangeCriteriaOnTheEdgesUnion.

@Test
public void testExactInTheMiddleAndRangeCriteriaOnTheEdgesUnion() {
    var watchAggregator = new WatchAggregator();
    watchAggregator.add(new ByteArray("key1"), null);
    watchAggregator.add(new ByteArray("key0"), new ByteArray("key2"), null);
    var keyCriterion = watchAggregator.watch(0, null).get().keyCriterion();
    var expKeyCriterion = new KeyCriterion.RangeCriterion(new ByteArray("key0"), new ByteArray("key2"));
    assertEquals(expKeyCriterion, keyCriterion);
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Aggregations

WatchAggregator (org.apache.ignite.internal.metastorage.watch.WatchAggregator)13 ByteArray (org.apache.ignite.lang.ByteArray)12 Test (org.junit.jupiter.api.Test)12 KeyCriterion (org.apache.ignite.internal.metastorage.watch.KeyCriterion)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Entry (org.apache.ignite.internal.metastorage.client.Entry)3 EntryEvent (org.apache.ignite.internal.metastorage.client.EntryEvent)3 WatchEvent (org.apache.ignite.internal.metastorage.client.WatchEvent)3 WatchListener (org.apache.ignite.internal.metastorage.client.WatchListener)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.times (org.mockito.Mockito.times)3 Mockito.verify (org.mockito.Mockito.verify)3