Search in sources :

Example 16 with ObserveHandle

use of cz.o2.proxima.direct.commitlog.ObserveHandle 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();
    }
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Partition(cz.o2.proxima.storage.Partition) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Example 17 with ObserveHandle

use of cz.o2.proxima.direct.commitlog.ObserveHandle 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);
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) PubsubMessage(com.google.pubsub.v1.PubsubMessage) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 18 with ObserveHandle

use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.

the class PubSubReaderTest method testObserveBulk.

@Test(timeout = 10000)
public void testObserveBulk() 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);
    AtomicReference<OffsetCommitter> commit = new AtomicReference<>();
    CommitLogObserver observer = new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            elems.add(ingest);
            commit.set(context);
            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.observeBulk("dummy", observer)) {
        latch.await();
        commit.get().confirm();
    }
    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);
}
Also used : ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PubsubMessage(com.google.pubsub.v1.PubsubMessage) LinkedList(java.util.LinkedList) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) Test(org.junit.Test)

Example 19 with ObserveHandle

use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.

the class KafkaLogReaderIT method testReadAndWriteSequentialId.

@Test(timeout = 30_000L)
public void testReadAndWriteSequentialId() throws InterruptedException {
    initializeTestWithUri("kafka://\"${broker}\"/topic", "[commit-log]");
    final EmbeddedKafkaBroker embeddedKafka = rule.getEmbeddedKafka();
    final int numPartitions = 3;
    embeddedKafka.addTopics(new NewTopic("topic", numPartitions, (short) 1));
    final CommitLogReader commitLogReader = Optionals.get(operator.getCommitLogReader(fooDescriptor));
    final TestLogObserver observer = new TestLogObserver();
    final ObserveHandle handle = commitLogReader.observe("test-reader", Position.OLDEST, observer);
    handle.waitUntilReady();
    final int numElements = 100;
    writeElements(numElements, true);
    while (observer.getNumReceivedElements() < numElements) {
        TimeUnit.MILLISECONDS.sleep(100);
    }
    observer.getReceivedElements().forEach(e -> assertTrue(e.hasSequentialId()));
    handle.close();
    Assert.assertEquals(numElements, observer.getNumReceivedElements());
    Assert.assertEquals(numElements, numCommittedElements(handle));
}
Also used : ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) NewTopic(org.apache.kafka.clients.admin.NewTopic) EmbeddedKafkaBroker(org.springframework.kafka.test.EmbeddedKafkaBroker) Test(org.junit.Test)

Example 20 with ObserveHandle

use of cz.o2.proxima.direct.commitlog.ObserveHandle in project proxima-platform by O2-Czech-Republic.

the class PubSubReader method observeBulk.

private ObserveHandle observeBulk(@Nullable String name, Position position, boolean stopAtCurrent, long minWatermark, CommitLogObserver observer) {
    validateNotStopAtCurrent(stopAtCurrent);
    validatePosition(position);
    AtomicReference<List<AckReplyConsumer>> unconfirmed = new AtomicReference<>(new ArrayList<>());
    Object lock = new Object();
    Object listLock = new Object();
    AtomicLong globalOffset = new AtomicLong();
    String consumerName = asConsumerName(name);
    AtomicLong committedWatermark = new AtomicLong(minWatermark);
    PubSubPartition partition = new PubSubPartition(consumerName);
    return consume(consumerName, (e, w, c) -> {
        final long confirmUntil;
        synchronized (listLock) {
            List<AckReplyConsumer> list = unconfirmed.get();
            list.add(c);
            confirmUntil = list.size() + globalOffset.get();
        }
        OffsetCommitter committer = createBulkCommitter(listLock, confirmUntil, globalOffset, unconfirmed, w, committedWatermark);
        // ensure explicit synchronization here
        synchronized (lock) {
            try {
                Offset offset = new PubSubOffset(consumerName, w.getWatermark());
                if (!observer.onNext(e, asOnNextContext(committer, offset))) {
                    observer.onCompleted();
                    return false;
                }
                return true;
            } catch (Exception ex) {
                log.error("Error calling on next", ex);
                committer.fail(ex);
                throw new RuntimeException(ex);
            }
        }
    }, observer::onError, () -> observer.onRepartition(asRepartitionContext(Arrays.asList(partition))), () -> observer.onRepartition(asRepartitionContext(Arrays.asList(partition))), observer::onCancelled, committedWatermark);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) IOException(java.io.IOException) Offset(cz.o2.proxima.direct.commitlog.Offset) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter)

Aggregations

StreamElement (cz.o2.proxima.storage.StreamElement)58 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)56 Test (org.junit.Test)52 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)47 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)45 CountDownLatch (java.util.concurrent.CountDownLatch)43 ArrayList (java.util.ArrayList)38 Offset (cz.o2.proxima.direct.commitlog.Offset)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 List (java.util.List)29 Partition (cz.o2.proxima.storage.Partition)26 HashMap (java.util.HashMap)26 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)25 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)24 UUID (java.util.UUID)24 WatermarkEstimator (cz.o2.proxima.time.WatermarkEstimator)23 Collections (java.util.Collections)23 Collectors (java.util.stream.Collectors)23 Repository (cz.o2.proxima.repository.Repository)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22