Search in sources :

Example 1 with Entry

use of org.apache.ignite.internal.metastorage.client.Entry in project ignite-3 by apache.

the class WatchAggregatorTest method testEventsRouting.

@Test
public void testEventsRouting() {
    var watchAggregator = new WatchAggregator();
    var lsnr1 = mock(WatchListener.class);
    var lsnr2 = mock(WatchListener.class);
    watchAggregator.add(new ByteArray("1"), lsnr1);
    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)));
    var watchEvt1Res = ArgumentCaptor.forClass(WatchEvent.class);
    verify(lsnr1).onUpdate(watchEvt1Res.capture());
    assertEquals(List.of(entryEvt1), watchEvt1Res.getValue().entryEvents());
    var watchEvt2Res = ArgumentCaptor.forClass(WatchEvent.class);
    verify(lsnr2).onUpdate(watchEvt2Res.capture());
    assertEquals(List.of(entryEvt2), watchEvt2Res.getValue().entryEvents());
}
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 2 with Entry

use of org.apache.ignite.internal.metastorage.client.Entry in project ignite-3 by apache.

the class DistributedConfigurationStorage method registerConfigurationListener.

/**
 * {@inheritDoc}
 */
@Override
public synchronized void registerConfigurationListener(@NotNull ConfigurationStorageListener lsnr) {
    if (this.lsnr == null) {
        this.lsnr = lsnr;
        // TODO: registerWatchByPrefix could throw OperationTimeoutException and CompactedException and we should
        // TODO: properly handle such cases https://issues.apache.org/jira/browse/IGNITE-14604
        metaStorageMgr.registerWatchByPrefix(DST_KEYS_START_RANGE, new WatchListener() {

            @Override
            public boolean onUpdate(@NotNull WatchEvent events) {
                Map<String, Serializable> data = new HashMap<>();
                Entry masterKeyEntry = null;
                for (EntryEvent event : events.entryEvents()) {
                    Entry e = event.newEntry();
                    if (e.key().equals(MASTER_KEY)) {
                        masterKeyEntry = e;
                    } else {
                        String key = e.key().toString().substring(DISTRIBUTED_PREFIX.length());
                        Serializable value = e.value() == null ? null : ConfigurationSerializationUtil.fromBytes(e.value());
                        data.put(key, value);
                    }
                }
                // That means that masterKey update must be included in the batch.
                assert masterKeyEntry != null;
                long newChangeId = masterKeyEntry.revision();
                assert newChangeId > changeId.get();
                changeId.set(newChangeId);
                lsnr.onEntriesChanged(new Data(data, newChangeId)).join();
                return true;
            }

            @Override
            public void onError(@NotNull Throwable e) {
                // TODO: need to handle this case and there should some mechanism for registering new watch as far as
                // TODO: onError unregisters failed watch https://issues.apache.org/jira/browse/IGNITE-14604
                LOG.error("Meta storage listener issue", e);
            }
        });
    } else {
        LOG.warn("Configuration listener has already been set.");
    }
}
Also used : WatchListener(org.apache.ignite.internal.metastorage.client.WatchListener) Entry(org.apache.ignite.internal.metastorage.client.Entry) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) Serializable(java.io.Serializable) EntryEvent(org.apache.ignite.internal.metastorage.client.EntryEvent) WatchEvent(org.apache.ignite.internal.metastorage.client.WatchEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Entry

use of org.apache.ignite.internal.metastorage.client.Entry in project ignite-3 by apache.

the class DistributedConfigurationStorage method readAllLatest.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Serializable> readAllLatest(String prefix) {
    var data = new HashMap<String, Serializable>();
    var rangeStart = new ByteArray(DISTRIBUTED_PREFIX + prefix);
    var rangeEnd = new ByteArray(incrementLastChar(DISTRIBUTED_PREFIX + prefix));
    try (Cursor<Entry> entries = metaStorageMgr.range(rangeStart, rangeEnd)) {
        for (Entry entry : entries) {
            ByteArray key = entry.key();
            byte[] value = entry.value();
            if (entry.tombstone()) {
                continue;
            }
            // Meta Storage should not return nulls as values
            assert value != null;
            if (key.equals(MASTER_KEY)) {
                continue;
            }
            String dataKey = key.toString().substring(DISTRIBUTED_PREFIX.length());
            data.put(dataKey, ConfigurationSerializationUtil.fromBytes(value));
        }
    } catch (Exception e) {
        throw new StorageException("Exception when closing a Meta Storage cursor", e);
    }
    return data;
}
Also used : Entry(org.apache.ignite.internal.metastorage.client.Entry) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) HashMap(java.util.HashMap) ByteArray(org.apache.ignite.lang.ByteArray)

Example 4 with Entry

use of org.apache.ignite.internal.metastorage.client.Entry 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 5 with Entry

use of org.apache.ignite.internal.metastorage.client.Entry in project ignite-3 by apache.

the class WatchAggregatorTest method testCancelByFalseFromListener.

@Test
public void testCancelByFalseFromListener() {
    var watchAggregator = new WatchAggregator();
    var lsnr1 = mock(WatchListener.class);
    when(lsnr1.onUpdate(any())).thenReturn(false);
    var lsnr2 = mock(WatchListener.class);
    when(lsnr2.onUpdate(any())).thenReturn(true);
    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.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)

Aggregations

Entry (org.apache.ignite.internal.metastorage.client.Entry)5 EntryEvent (org.apache.ignite.internal.metastorage.client.EntryEvent)4 WatchEvent (org.apache.ignite.internal.metastorage.client.WatchEvent)4 WatchListener (org.apache.ignite.internal.metastorage.client.WatchListener)4 ByteArray (org.apache.ignite.lang.ByteArray)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3 List (java.util.List)3 KeyCriterion (org.apache.ignite.internal.metastorage.watch.KeyCriterion)3 WatchAggregator (org.apache.ignite.internal.metastorage.watch.WatchAggregator)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Test (org.junit.jupiter.api.Test)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