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();
}
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());
}
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();
}
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();
}
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));
}
};
}
Aggregations