Search in sources :

Example 21 with OnNextContext

use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext 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 22 with OnNextContext

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

the class ModelTest method testReadWrite.

@Test
public void testReadWrite() throws InterruptedException {
    Regular<BaseEvent> desc = model.getEvent().getDataDescriptor();
    Optional<OnlineAttributeWriter> writer = model.directOperator().getWriter(desc);
    writer.get().write(desc.upsert(UUID.randomUUID().toString(), System.currentTimeMillis(), BaseEvent.newBuilder().setUserName("user").build()), (succ, exc) -> {
    });
    Optional<CommitLogReader> reader = model.directOperator().getCommitLogReader(desc);
    List<String> users = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    reader.get().observeBulk("consumer", Position.OLDEST, true, new CommitLogObserver() {

        @Override
        public boolean onError(Throwable error) {
            return false;
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            Optional<BaseEvent> baseEvent = desc.valueOf(ingest);
            users.add(baseEvent.get().getUserName());
            return true;
        }
    });
    latch.await();
    assertEquals("user", users.get(0));
}
Also used : Optional(java.util.Optional) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) BaseEvent(cz.o2.proxima.example.event.Event.BaseEvent) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) Test(org.junit.Test)

Example 23 with OnNextContext

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

the class BatchLogReaderTest method testObserveWithContext.

@Test(timeout = 5000)
public void testObserveWithContext() throws InterruptedException {
    write("gw", new byte[] { 1 });
    BatchLogReader reader = getBatchReader();
    BlockingQueue<StreamElement> read = new SynchronousQueue<>();
    reader.observe(reader.getPartitions(), Collections.singletonList(attr), new BatchLogObserver() {

        @Override
        public boolean onNext(StreamElement element, OnNextContext context) {
            assertEquals(0, context.getPartition().getId());
            assertEquals(Long.MIN_VALUE, context.getWatermark());
            ExceptionUtils.unchecked(() -> read.put(element));
            return false;
        }
    });
    StreamElement element = read.take();
    assertEquals("gw", element.getKey());
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Example 24 with OnNextContext

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

the class BatchLogReaderTest method testObserveReadOffset.

@Test
public void testObserveReadOffset() throws InterruptedException {
    final List<StreamElement> firstPartition = newPartition("first_", 10);
    final List<StreamElement> secondPartition = newPartition("second_", 20);
    final List<StreamElement> thirdPartition = newPartition("third_", 30);
    final ListBatchReader reader = ListBatchReader.ofPartitioned(direct.getContext(), Arrays.asList(firstPartition, secondPartition, thirdPartition));
    final ConcurrentMap<Partition, Offset> lastOffsets = new ConcurrentHashMap<>();
    final CountDownLatch doneConsuming = new CountDownLatch(1);
    reader.observe(Arrays.asList(Partition.of(0), Partition.of(1), Partition.of(2)), Collections.singletonList(attr), new BatchLogObserver() {

        @Override
        public boolean onNext(StreamElement element, OnNextContext context) {
            lastOffsets.merge(context.getPartition(), context.getOffset(), (oldValue, newValue) -> {
                assertTrue(oldValue.getElementIndex() < newValue.getElementIndex());
                return newValue;
            });
            return true;
        }

        @Override
        public void onCompleted() {
            doneConsuming.countDown();
        }
    });
    doneConsuming.await();
    assertEquals(Offset.of(Partition.of(0), 9, true), lastOffsets.get(Partition.of(0)));
    assertEquals(Offset.of(Partition.of(1), 19, true), lastOffsets.get(Partition.of(1)));
    assertEquals(Offset.of(Partition.of(2), 29, true), lastOffsets.get(Partition.of(2)));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Partition(cz.o2.proxima.storage.Partition) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) ListBatchReader(cz.o2.proxima.direct.storage.ListBatchReader) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) SynchronousQueue(java.util.concurrent.SynchronousQueue) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Streams(com.google.common.collect.Streams) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) ReplicationRunner(cz.o2.proxima.util.ReplicationRunner) List(java.util.List) CommitLogReaderTest.withNumRecordsPerSec(cz.o2.proxima.direct.commitlog.CommitLogReaderTest.withNumRecordsPerSec) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Partition(cz.o2.proxima.storage.Partition) ListBatchReader(cz.o2.proxima.direct.storage.ListBatchReader) StreamElement(cz.o2.proxima.storage.StreamElement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 25 with OnNextContext

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

the class CommitLogReaderTest method testObserveWithRetry.

@Test(timeout = 10000)
public void testObserveWithRetry() throws InterruptedException {
    List<StreamElement> received = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger count = new AtomicInteger();
    final CommitLogObserver observer = CommitLogObservers.withNumRetriedExceptions("test", 2, new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext confirm) {
            if (count.incrementAndGet() == 0) {
                throw new RuntimeException("fail");
            }
            received.add(ingest);
            latch.countDown();
            return true;
        }

        @Override
        public boolean onError(Throwable error) {
            return false;
        }
    });
    reader.observe("test", observer);
    writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
    });
    latch.await();
    assertEquals(1, received.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

StreamElement (cz.o2.proxima.storage.StreamElement)83 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)67 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)64 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)50 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)40 ArrayList (java.util.ArrayList)39 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)29 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)28 Accessor (cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor)26 List (java.util.List)26 UUID (java.util.UUID)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)24 ConfigFactory (com.typesafe.config.ConfigFactory)23 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)23 Collections (java.util.Collections)23 Offset (cz.o2.proxima.direct.commitlog.Offset)22 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)22