use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testHandleRebalanceInProgressException.
@Test(timeout = 10000)
public void testHandleRebalanceInProgressException() throws InterruptedException {
final AtomicInteger invokedCount = new AtomicInteger();
final int numElements = 2000;
final LocalKafkaCommitLogDescriptor descriptor = new LocalKafkaCommitLogDescriptor() {
@Override
public Accessor createAccessor(DirectDataOperator direct, AttributeFamilyDescriptor family) {
return new Accessor(family.getEntity(), family.getStorageUri(), family.getCfg(), id) {
@Override
<K, V> KafkaConsumer<K, V> mockKafkaConsumer(String name, ConsumerGroup group, ElementSerializer<K, V> serializer, @Nullable Collection<Partition> assignedPartitions, @Nullable ConsumerRebalanceListener listener) {
final Map<TopicPartition, OffsetAndMetadata> committed = new HashMap<>();
KafkaConsumer<K, V> mock = super.mockKafkaConsumer(name, group, serializer, assignedPartitions, listener);
doAnswer(invocationOnMock -> {
if (invokedCount.getAndIncrement() == 1) {
throw new RebalanceInProgressException();
}
Map<TopicPartition, OffsetAndMetadata> toCommit = invocationOnMock.getArgument(0);
committed.putAll(toCommit);
return null;
}).when(mock).commitSync(anyMap());
doAnswer(invocationOnMock -> {
Set<TopicPartition> parts = invocationOnMock.getArgument(0);
return parts.stream().map(tp -> Pair.of(tp, committed.get(tp))).filter(p -> p.getSecond() != null).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
}).when(mock).committed(anySet());
return mock;
}
};
}
};
Accessor accessor = descriptor.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(1)));
LocalKafkaLogReader reader = accessor.newReader(direct.getContext());
Map<String, StreamElement> observedAfterRepartition = new HashMap<>();
LocalKafkaWriter<?, ?> writer = accessor.newWriter();
CountDownLatch latch = new CountDownLatch(1);
try (ObserveHandle handle = reader.observe("dummy", new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
observedAfterRepartition.put(ingest.getKey(), ingest);
context.confirm();
if (ingest.getKey().equals("last-key")) {
latch.countDown();
return false;
}
return true;
}
@Override
public boolean onError(Throwable error) {
return false;
}
})) {
for (int i = 0; i < numElements; i++) {
writer.write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key" + i, attr.getName(), System.currentTimeMillis(), new byte[] {}), (succ, exc) -> {
});
}
writer.write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "last-key", attr.getName(), System.currentTimeMillis(), new byte[] {}), (succ, exc) -> {
});
latch.await();
}
assertEquals(numElements + 1, observedAfterRepartition.size());
assertTrue(invokedCount.get() > 1);
}
use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testObserveSuccess.
@Test(timeout = 10000)
public void testObserveSuccess() throws InterruptedException {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
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.observe("test", Position.NEWEST, new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
context.confirm();
latch.countDown();
return true;
}
@Override
public void onCompleted() {
fail("This should not be called");
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
});
writer.write(update, (succ, e) -> {
assertTrue(succ);
latch.countDown();
});
latch.await();
assertEquals(3, handle.getCommittedOffsets().size());
long sum = handle.getCommittedOffsets().stream().mapToLong(o -> {
TopicOffset tpo = (TopicOffset) o;
assertTrue(tpo.getOffset() <= 1);
return tpo.getOffset();
}).sum();
// single partition has committed one element
assertEquals(1, sum);
}
use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testCurrentOffsetsReflectSeek.
@Test(timeout = 60000)
public void testCurrentOffsetsReflectSeek() throws InterruptedException {
final Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
final CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
final LocalKafkaWriter writer = accessor.newWriter();
final CountDownLatch latch = new CountDownLatch(10);
final StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 });
for (int i = 0; i < 10; i++) {
writer.write(update, (succ, exc) -> latch.countDown());
}
latch.await();
ObserveHandle handle = reader.observe("name", Position.OLDEST, new CommitLogObserver() {
@Override
public boolean onError(Throwable error) {
return false;
}
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
return false;
}
});
handle.waitUntilReady();
handle.close();
assertEquals(3, handle.getCurrentOffsets().size());
assertEquals(0, handle.getCurrentOffsets().stream().mapToLong(o -> ((TopicOffset) o).getOffset()).filter(o -> o >= 0).sum());
}
use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testObserveBulkCommitsCorrectly.
@Test(timeout = 10000)
public void testObserveBulkCommitsCorrectly() throws InterruptedException {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, cfg(Pair.of(KafkaAccessor.ASSIGNMENT_TIMEOUT_MS, 1L), Pair.of(LocalKafkaCommitLogDescriptor.CFG_NUM_PARTITIONS, 3))));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
long now = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key-" + i, attr.getName(), now + 2000, new byte[] { 1, 2 });
// then we write single element
writer.write(update, (succ, e) -> {
});
}
CountDownLatch latch = new CountDownLatch(1);
ObserveHandle handle = reader.observeBulk("test", Position.OLDEST, true, new CommitLogObserver() {
int processed = 0;
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
if (++processed == 100) {
context.confirm();
}
return true;
}
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
});
latch.await();
long offsetSum = handle.getCommittedOffsets().stream().mapToLong(o -> ((TopicOffset) o).getOffset()).sum();
assertEquals(100, offsetSum);
KafkaConsumer<Object, Object> consumer = ((LocalKafkaCommitLogDescriptor.LocalKafkaLogReader) reader).getConsumer();
String topic = accessor.getTopic();
assertEquals(100, consumer.committed(handle.getCommittedOffsets().stream().map(o -> new TopicPartition(topic, o.getPartition().getId())).collect(Collectors.toSet())).values().stream().mapToLong(OffsetAndMetadata::offset).sum());
}
use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testObserveWithException.
@Test(timeout = 10000)
public void testObserveWithException() 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 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.observe("test", Position.NEWEST, new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
restarts.incrementAndGet();
throw new RuntimeException("FAIL!");
}
@Override
public void onCompleted() {
fail("This should not be called");
}
@Override
public boolean onError(Throwable error) {
exc.set(error);
latch.countDown();
throw new RuntimeException(error);
}
});
writer.write(update, (succ, e) -> {
assertTrue(succ);
latch.countDown();
});
latch.await();
assertEquals("FAIL!", exc.get().getMessage());
assertEquals(1, restarts.get());
assertEquals(3, handle.getCommittedOffsets().size());
List<Long> startedOffsets = handle.getCurrentOffsets().stream().map(o -> ((TopicOffset) o).getOffset()).filter(o -> o >= 0).collect(Collectors.toList());
assertEquals(Collections.singletonList(0L), startedOffsets);
}
Aggregations