use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class LocalKafkaCommitLogDescriptorTest method testBulkObservePartitionsResetOffsetsSuccess.
@Test(timeout = 10000)
public void testBulkObservePartitionsResetOffsetsSuccess() throws InterruptedException {
Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
LocalKafkaWriter writer = accessor.newWriter();
CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
AtomicInteger restarts = new AtomicInteger();
AtomicReference<Throwable> exc = new AtomicReference<>();
AtomicReference<StreamElement> input = new AtomicReference<>();
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(2));
StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 });
final ObserveHandle handle = reader.observePartitions(reader.getPartitions(), 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.get().countDown();
return true;
}
@Override
public boolean onError(Throwable error) {
exc.set(error);
throw new RuntimeException(error);
}
});
handle.waitUntilReady();
writer.write(update, (succ, e) -> {
assertTrue(succ);
latch.get().countDown();
});
latch.get().await();
latch.set(new CountDownLatch(1));
handle.resetOffsets(reader.getPartitions().stream().map(p -> (PartitionWithTopic) p).map(p -> new TopicOffset(new PartitionWithTopic(p.getTopic(), p.getId()), 0, Watermarks.MIN_WATERMARK)).collect(Collectors.toList()));
latch.get().await();
assertEquals(1L, (long) (Long) handle.getCommittedOffsets().stream().mapToLong(o -> ((TopicOffset) o).getOffset()).sum());
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserver method processTransactionRequest.
private void processTransactionRequest(String transactionId, String requestId, Request request, OnNextContext context) {
log.debug("Processing request to {} with {} for transaction {}", requestId, request, transactionId);
State currentState = manager.getCurrentState(transactionId);
@Nullable State newState = transitionState(transactionId, currentState, request);
if (newState != null) {
// we have successfully computed new state, produce response
Response response = getResponseForNewState(request, currentState, newState);
manager.ensureTransactionOpen(transactionId, newState);
manager.writeResponseAndUpdateState(transactionId, newState, requestId, response, context::commit);
} else if (request.getFlags() == Request.Flags.OPEN && (currentState.getFlags() == State.Flags.OPEN || currentState.getFlags() == State.Flags.COMMITTED)) {
manager.writeResponseAndUpdateState(transactionId, currentState, requestId, Response.forRequest(request).duplicate(currentState.getSequentialId()), context::commit);
} else {
log.warn("Unexpected {} request for transaction {} seqId {} when the state is {}. " + "Refusing to respond, because the correct response is unknown.", request.getFlags(), transactionId, currentState.getSequentialId(), currentState.getFlags());
context.confirm();
}
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class PubSubReaderTest method testObserve.
@Test(timeout = 10000)
public void testObserve() throws InterruptedException {
long now = System.currentTimeMillis();
Deque<PubsubMessage> inputs = new LinkedList<>(Arrays.asList(update("key1", "attr", new byte[] { 1, 2 }, now), delete("key2", "attr", now + 1000), deleteWildcard("key3", wildcard, now)));
reader.setSupplier(() -> {
if (inputs.isEmpty()) {
LockSupport.park();
}
return inputs.pop();
});
List<StreamElement> elems = new ArrayList<>();
AtomicBoolean cancelled = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(3);
CommitLogObserver observer = new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
elems.add(ingest);
context.confirm();
latch.countDown();
return true;
}
@Override
public void onCancelled() {
cancelled.set(true);
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
};
try (ObserveHandle handle = reader.observe("dummy", observer)) {
latch.await();
}
assertEquals(3, elems.size());
StreamElement elem = elems.get(0);
assertEquals("key1", elem.getKey());
assertEquals("attr", elem.getAttribute());
assertFalse(elem.isDelete());
assertFalse(elem.isDeleteWildcard());
assertArrayEquals(new byte[] { 1, 2 }, elem.getValue());
assertEquals(now, elem.getStamp());
elem = elems.get(1);
assertEquals("key2", elem.getKey());
assertEquals("attr", elem.getAttribute());
assertTrue(elem.isDelete());
assertFalse(elem.isDeleteWildcard());
assertEquals(now + 1000L, elem.getStamp());
elem = elems.get(2);
assertEquals("key3", elem.getKey());
assertEquals(wildcard.toAttributePrefix() + "*", elem.getAttribute());
assertTrue(elem.isDelete());
assertTrue(elem.isDeleteWildcard());
assertEquals(now, elem.getStamp());
assertTrue(cancelled.get());
assertEquals(Sets.newHashSet(0, 1, 2), reader.acked);
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class PubSubReaderTest method testPartitionsSplit.
@Test
public void testPartitionsSplit() throws InterruptedException {
List<Partition> partitions = reader.getPartitions();
assertEquals(1, partitions.size());
partitions = new ArrayList<>(partitions.get(0).split(3));
assertEquals(3, partitions.size());
reader.setSupplier(() -> {
LockSupport.park();
return null;
});
CommitLogObserver observer = new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
context.confirm();
return false;
}
@Override
public void onCancelled() {
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
};
try (ObserveHandle handle = reader.observeBulkPartitions(partitions, Position.NEWEST, observer)) {
handle.waitUntilReady();
}
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class PubSubReaderTest method testObserveCommittedOffset.
@Test
public void testObserveCommittedOffset() throws InterruptedException {
long now = System.currentTimeMillis();
Deque<PubsubMessage> inputs = new LinkedList<>(Arrays.asList(update("key1", "attr", new byte[] { 1, 2 }, now), delete("key2", "attr", now + 1000), deleteWildcard("key3", wildcard, now)));
reader.setSupplier(() -> {
if (inputs.isEmpty()) {
LockSupport.park();
}
return inputs.pop();
});
CountDownLatch latch = new CountDownLatch(1);
ObserveHandle handle = reader.observe("dummy", new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
timestampSupplier.addAndGet(1000);
context.confirm();
latch.countDown();
return false;
}
@Override
public void onCancelled() {
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
});
latch.await();
assertEquals(1, handle.getCommittedOffsets().size());
assertTrue(handle.getCommittedOffsets().get(0).getWatermark() > 0);
}
Aggregations