use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testOffsetExternalizer.
@Test
public void testOffsetExternalizer() {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(1)));
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
assertTrue(reader.hasExternalizableOffsets());
assertEquals(TopicOffsetExternalizer.class, reader.getOffsetExternalizer().getClass());
}
use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testCustomIdlePolicy.
@Test(timeout = 10000)
public void testCustomIdlePolicy() throws InterruptedException {
Map<String, Object> cfg = and(partitionsCfg(3), cfg(Pair.of(KafkaAccessor.EMPTY_POLL_TIME, "1000")));
cfg.put("watermark.idle-policy-factory", FixedWatermarkIdlePolicyFactory.class.getName());
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, cfg));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
long now = System.currentTimeMillis();
final StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 2000, new byte[] { 1, 2 });
AtomicLong watermark = new AtomicLong();
CountDownLatch latch = new CountDownLatch(2);
reader.observe("test", Position.NEWEST, new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
watermark.set(context.getWatermark());
latch.countDown();
return true;
}
@Override
public void onCompleted() {
fail("This should not be called");
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
}).waitUntilReady();
// then we write single element
writer.write(update, (succ, e) -> {
});
// for two seconds we have empty data
TimeUnit.SECONDS.sleep(2);
// finally, last update to save watermark
writer.write(update, (succ, e) -> {
});
latch.await();
assertEquals(FixedWatermarkIdlePolicyFactory.FIXED_IDLE_WATERMARK, watermark.get());
}
use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testBulkObserveSuccess.
@Test(timeout = 10000)
public void testBulkObserveSuccess() throws InterruptedException {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
final AtomicInteger restarts = new AtomicInteger();
final AtomicReference<Throwable> exc = new AtomicReference<>();
final AtomicReference<StreamElement> input = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(2);
final StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 });
final ObserveHandle handle = reader.observeBulk("test", Position.NEWEST, new CommitLogObserver() {
@Override
public void onRepartition(OnRepartitionContext context) {
restarts.incrementAndGet();
}
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
input.set(ingest);
context.confirm();
latch.countDown();
return true;
}
@Override
public void onCompleted() {
fail("This should not be called");
}
@Override
public boolean onError(Throwable error) {
exc.set(error);
throw new RuntimeException(error);
}
});
writer.write(update, (succ, e) -> {
assertTrue(succ);
latch.countDown();
});
latch.await();
assertNull(exc.get());
assertTrue(restarts.get() > 0);
assertArrayEquals(update.getValue(), input.get().getValue());
assertEquals(3, handle.getCommittedOffsets().size());
assertEquals(handle.getCommittedOffsets().toString(), 1L, (long) (Long) handle.getCommittedOffsets().stream().mapToLong(o -> ((TopicOffset) o).getOffset()).sum());
}
use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testBulkObserveOffsets2.
@Test(timeout = 10000)
public void testBulkObserveOffsets2() throws InterruptedException {
final Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
final LocalKafkaWriter writer = accessor.newWriter();
final CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
final List<KafkaStreamElement> input = new ArrayList<>();
final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(3));
final StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 });
final CommitLogObserver observer = new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
input.add((KafkaStreamElement) ingest);
latch.get().countDown();
// terminate after reading first record
return false;
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
};
final List<Offset> offsets;
try (final ObserveHandle handle = reader.observeBulkPartitions(reader.getPartitions(), Position.NEWEST, observer)) {
// write two elements
for (int i = 0; i < 2; i++) {
writer.write(update, (succ, e) -> {
assertTrue(succ);
latch.get().countDown();
});
}
latch.get().await();
latch.set(new CountDownLatch(1));
offsets = handle.getCurrentOffsets();
}
// restart from old offset
reader.observeBulkOffsets(Lists.newArrayList(offsets), observer);
latch.get().await();
assertEquals(2, input.size());
assertEquals(0, input.get(0).getOffset());
assertEquals(0, input.get(1).getOffset());
}
use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testObserveMovesWatermark.
@Test(timeout = 10000)
public void testObserveMovesWatermark() throws InterruptedException {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
long now = System.currentTimeMillis();
final UnaryFunction<Integer, StreamElement> update = pos -> StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key" + pos, attr.getName(), now + pos, new byte[] { 1, 2 });
AtomicLong watermark = new AtomicLong();
CountDownLatch latch = new CountDownLatch(100);
reader.observe("test", Position.NEWEST, new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
watermark.set(context.getWatermark());
latch.countDown();
return true;
}
@Override
public void onCompleted() {
fail("This should not be called");
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
}).waitUntilReady();
for (int i = 0; i < 100; i++) {
writer.write(update.apply(i), (succ, e) -> {
});
}
latch.await();
assertTrue(watermark.get() > 0);
assertTrue(watermark.get() < now * 10);
}
Aggregations