use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.
the class InMemStorageTest method testReadWriteSequentialIds.
@Test(timeout = 10000)
public void testReadWriteSequentialIds() throws InterruptedException {
InMemStorage storage = new InMemStorage();
DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///inmemstoragetest")));
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
List<StreamElement> result = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
CommitLogObserver observer = LogObserverUtils.toList(result, Assert::assertTrue, elem -> {
latch.countDown();
return false;
});
reader.observe("test", observer);
writer.online().write(StreamElement.upsert(entity, data, 1L, "key", data.getName(), System.currentTimeMillis(), new byte[] { 1, 2, 3 }), (succ, exc) -> {
});
latch.await();
assertEquals(1, result.size());
assertTrue(result.get(0).hasSequentialId());
assertEquals(1L, result.get(0).getSequentialId());
}
use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.
the class TransactionCommitTransformationTest method testTransformTransactionUpdate.
@Test
public void testTransformTransactionUpdate() throws InterruptedException {
long now = System.currentTimeMillis();
Commit commit = Commit.of(Arrays.asList(new TransactionUpdate("all-transaction-commit-log-state", StreamElement.upsert(transaction, stateDesc, UUID.randomUUID().toString(), "t", stateDesc.getName(), now, stateDesc.getValueSerializer().serialize(State.empty()))), new TransactionUpdate("all-transaction-commit-log-response", StreamElement.upsert(transaction, responseDesc, UUID.randomUUID().toString(), "t", responseDesc.toAttributePrefix() + "1", now, responseDesc.getValueSerializer().serialize(Response.forRequest(Request.builder().responsePartitionId(0).build()))))));
List<StreamElement> requests = new ArrayList<>();
List<StreamElement> states = new ArrayList<>();
CommitLogObserver requestObserver = LogObserverUtils.toList(requests, ign -> {
});
CommitLogObserver stateObserver = LogObserverUtils.toList(states, ign -> {
});
Optionals.get(direct.getFamilyByName("all-transaction-commit-log-state").getCommitLogReader()).observe("first", stateObserver);
Optionals.get(direct.getFamilyByName("all-transaction-commit-log-response").getCommitLogReader()).observe("second", requestObserver);
transformation.transform(StreamElement.upsert(transaction, commitDesc, UUID.randomUUID().toString(), UUID.randomUUID().toString(), commitDesc.getName(), System.currentTimeMillis(), commitDesc.getValueSerializer().serialize(commit)), (succ, exc) -> assertTrue(succ));
assertEquals(1, requests.size());
assertEquals(1, states.size());
}
use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.
the class InMemStorageTest method testFetchOffsets.
private void testFetchOffsets(int numPartitions) throws InterruptedException {
InMemStorage storage = new InMemStorage();
DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///test"), numPartitions));
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
long now = System.currentTimeMillis();
List<StreamElement> updates = new ArrayList<>();
for (int i = 0; i < 2 * numPartitions; i++) {
updates.add(StreamElement.upsert(entity, data, UUID.randomUUID().toString(), "key" + (i + 1), data.getName(), now + i, new byte[] { 1, 2, 3 }));
}
updates.forEach(el -> writer.online().write(el, (succ, exc) -> {
}));
Map<Partition, Offset> startingOffsets = reader.fetchOffsets(Position.OLDEST, reader.getPartitions());
assertEquals(numPartitions, startingOffsets.size());
List<StreamElement> elements = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch startLatch = new CountDownLatch(1);
CommitLogObserver observer = LogObserverUtils.toList(elements, ign -> latch.countDown(), el -> {
ExceptionUtils.ignoringInterrupted(startLatch::await);
return true;
});
ObserveHandle handle = reader.observeBulkOffsets(startingOffsets.values(), true, observer);
assertFalse(ObserveHandleUtils.isAtHead(handle, reader));
startLatch.countDown();
latch.await();
assertEquals(2 * numPartitions, elements.size());
List<Offset> committed = handle.getCommittedOffsets();
Map<Partition, Offset> endOffsets2 = reader.fetchOffsets(Position.NEWEST, committed.stream().map(Offset::getPartition).collect(Collectors.toList()));
assertTrue(ObserveHandleUtils.isAtHead(handle, reader));
assertEquals(IntStream.range(0, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
elements.clear();
Map<Partition, Offset> endOffsets = reader.fetchOffsets(Position.NEWEST, reader.getPartitions());
assertEquals(numPartitions, endOffsets.size());
CountDownLatch latch2 = new CountDownLatch(1);
observer = LogObserverUtils.toList(elements, ign -> latch2.countDown());
reader.observeBulkOffsets(endOffsets.values(), true, observer);
latch2.await();
assertEquals(numPartitions, elements.size());
assertEquals(IntStream.range(numPartitions, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
}
use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.
the class TransactionCommitTransformationTest method testTransformUpdates.
@Test
public void testTransformUpdates() throws InterruptedException {
StreamElement upsert = StreamElement.upsert(gateway, status, UUID.randomUUID().toString(), "key", status.getName(), 0L, new byte[] { 1, 2, 3 });
StreamElement delete = StreamElement.delete(gateway, status, UUID.randomUUID().toString(), "key", status.getName(), 1L);
Commit commit = Commit.of(1L, 1234567890000L, Arrays.asList(upsert, delete));
List<StreamElement> outputs = new ArrayList<>();
CommitLogObserver observer = LogObserverUtils.toList(outputs, ign -> {
});
Optionals.get(direct.getCommitLogReader(status)).observe("name", observer).waitUntilReady();
transformation.transform(StreamElement.upsert(transaction, commitDesc, UUID.randomUUID().toString(), UUID.randomUUID().toString(), commitDesc.getName(), System.currentTimeMillis(), commitDesc.getValueSerializer().serialize(commit)), (succ, exc) -> assertTrue(succ));
assertEquals(2, outputs.size());
assertEquals(commit.getUpdates().get(0), outputs.get(0));
assertEquals(commit.getUpdates().get(1), outputs.get(1));
}
Aggregations