Search in sources :

Example 26 with OnNextContext

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

the class CommitLogReaderTest method testOrderedObserverLifycycle.

@Test
public void testOrderedObserverLifycycle() {
    StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), 0, new byte[] { 1, 2 });
    AtomicInteger mask = new AtomicInteger();
    CommitLogObserver inner = new CommitLogObserver() {

        @Override
        public void onCompleted() {
            mask.updateAndGet(m -> m | 0x1);
        }

        @Override
        public void onCancelled() {
            mask.updateAndGet(m -> m | 0x2);
        }

        @Override
        public boolean onError(Throwable error) {
            mask.updateAndGet(m -> m | 0x4);
            return true;
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            mask.updateAndGet(m -> m | 0x8);
            return true;
        }

        @Override
        public void onRepartition(OnRepartitionContext context) {
            mask.updateAndGet(m -> m | 0x10);
        }

        @Override
        public void onIdle(OnIdleContext context) {
            mask.updateAndGet(m -> m | 0x20);
        }
    };
    assertEquals(0x0, mask.get());
    inner.onCompleted();
    assertEquals(0x1, mask.get());
    inner.onCancelled();
    assertEquals(0x3, mask.get());
    inner.onError(null);
    assertEquals(0x7, mask.get());
    inner.onNext(update, asOnNextContext(0));
    assertEquals(0x0F, mask.get());
    inner.onRepartition(Collections::emptyList);
    assertEquals(0x1F, mask.get());
    inner.onIdle(() -> 0);
    assertEquals(0x3F, mask.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) StreamElement(cz.o2.proxima.storage.StreamElement) Collections(java.util.Collections) Test(org.junit.Test)

Example 27 with OnNextContext

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

the class CommitLogReaderTest method testObserveOrdered.

@Test(timeout = 10000)
public void testObserveOrdered() throws InterruptedException {
    List<StreamElement> received = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(100);
    reader.observe("test", CommitLogObservers.withSortBuffer(new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            received.add(ingest);
            latch.countDown();
            context.confirm();
            return true;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    }, Duration.ofMillis(500)));
    long now = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - i, new byte[] { 1, 2 }), (succ, exc) -> {
        });
    }
    // put one latecomer to test it is dropped
    writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - 10000, new byte[] { 1, 2 }), (succ, exc) -> {
    });
    latch.await();
    assertEquals(100, received.size());
    assertEquals(LongStream.range(0, 100).boxed().collect(Collectors.toList()), received.stream().map(s -> s.getStamp() - now).collect(Collectors.toList()));
}
Also used : 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)

Example 28 with OnNextContext

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

the class CommitLogReaderTest method testObserveWithError.

@Test(timeout = 10000)
public void testObserveWithError() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Throwable> caught = new AtomicReference<>();
    reader.observe("test", new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            throw new RuntimeException("fail");
        }

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

Example 29 with OnNextContext

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

the class CommitLogReaderTest method testObserveOrderedPerPartition.

@Test(timeout = 10000)
public void testObserveOrderedPerPartition() throws InterruptedException {
    List<StreamElement> received = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(100);
    reader.observe("test", CommitLogObservers.withSortBufferWithinPartition(new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            received.add(ingest);
            latch.countDown();
            context.confirm();
            return true;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    }, Duration.ofMillis(500)));
    long now = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - i, new byte[] { 1, 2 }), (succ, exc) -> {
        });
    }
    // put one latecomer to test it is dropped
    writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - 10000, new byte[] { 1, 2 }), (succ, exc) -> {
    });
    latch.await();
    assertEquals(100, received.size());
    assertEquals(LongStream.range(0, 100).boxed().collect(Collectors.toList()), received.stream().map(s -> s.getStamp() - now).collect(Collectors.toList()));
}
Also used : 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)

Example 30 with OnNextContext

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

the class RetryableLogObserverTest method testNonRetryableException.

@Test
public void testNonRetryableException() {
    final int numRetries = 10;
    final AtomicReference<Throwable> exception = new AtomicReference<>();
    final RetryableLogObserver observer = new RetryableLogObserver("test", numRetries, withStrategy(exception::set, TerminationStrategy.RETHROW), false, new CommitLogObserver() {

        @Override
        public boolean onError(Throwable error) {
            // Non-Retryable.
            return false;
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            throw new UnsupportedOperationException("Not implemented.");
        }
    });
    try {
        observer.onError(new OutOfMemoryError());
        Assert.fail("Should have thrown exception");
    } catch (OutOfMemoryError ex) {
        Assert.assertEquals(ex, exception.get());
    }
    try {
        observer.onError(new Exception("Test."));
        Assert.fail("Should have thrown exception");
    } catch (IllegalStateException ex) {
        Assert.assertEquals(ex.getCause(), exception.get());
    }
}
Also used : StreamElement(cz.o2.proxima.storage.StreamElement) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) 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