Search in sources :

Example 51 with CommitLogReader

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

the class LocalKafkaCommitLogDescriptorTest method testSlowPollMovesWatermarkSlowly.

@Test(timeout = 10000)
public void testSlowPollMovesWatermarkSlowly() throws InterruptedException {
    Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, and(partitionsCfg(3), cfg(Pair.of(KafkaAccessor.EMPTY_POLL_TIME, "1000")))));
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
    final long now = System.currentTimeMillis();
    AtomicLong watermark = new AtomicLong();
    CountDownLatch latch = new CountDownLatch(30);
    reader.observe("test", Position.NEWEST, new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            return true;
        }

        @Override
        public void onCompleted() {
            fail("This should not be called");
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }

        @Override
        public void onIdle(OnIdleContext context) {
            watermark.set(context.getWatermark());
            latch.countDown();
        }
    }).waitUntilReady();
    // for two seconds we have empty data
    TimeUnit.SECONDS.sleep(2);
    latch.await();
    // watermark should be moved
    assertTrue(watermark.get() > 0);
    assertTrue(watermark.get() < now * 10);
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) AtomicLong(java.util.concurrent.atomic.AtomicLong) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Accessor(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor) Test(org.junit.Test)

Example 52 with CommitLogReader

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

the class LocalKafkaCommitLogDescriptorTest method testPollFromNConsumersMovesWatermark.

void testPollFromNConsumersMovesWatermark(Accessor accessor, int numObservers, boolean expectMoved) throws InterruptedException {
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
    final long now = System.currentTimeMillis();
    CountDownLatch latch = new CountDownLatch(numObservers);
    Set<CommitLogObserver> movedConsumers = Collections.synchronizedSet(new HashSet<>());
    Map<CommitLogObserver, Long> observerWatermarks = new ConcurrentHashMap<>();
    AtomicInteger readyObservers = new AtomicInteger();
    for (int i = 0; i < numObservers; i++) {
        reader.observe("test-" + expectMoved, Position.OLDEST, new CommitLogObserver() {

            @Override
            public boolean onNext(StreamElement ingest, OnNextContext context) {
                log.debug("Received element {} on watermark {}", ingest, context.getWatermark());
                return true;
            }

            @Override
            public boolean onError(Throwable error) {
                throw new RuntimeException(error);
            }

            @Override
            public void onIdle(OnIdleContext context) {
                if (readyObservers.get() == numObservers) {
                    observerWatermarks.compute(this, (k, v) -> Math.max(MoreObjects.firstNonNull(v, Long.MIN_VALUE), context.getWatermark()));
                    if ((!expectMoved || observerWatermarks.get(this) > 0) && movedConsumers.add(this)) {
                        latch.countDown();
                    }
                }
            }
        }).waitUntilReady();
        readyObservers.incrementAndGet();
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(numObservers, observerWatermarks.size());
    long watermark = observerWatermarks.values().stream().min(Long::compare).orElse(Long.MIN_VALUE);
    // watermark should be moved
    assertTrue(!expectMoved || watermark > 0);
    assertTrue("Watermark should not be too far, got " + watermark + " calculated from " + observerWatermarks, watermark < now * 10);
}
Also used : OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 53 with CommitLogReader

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

the class ListCommitLogTest method testObserveNonExternalizableMultipleConsumers.

@Test(timeout = 10000)
public void testObserveNonExternalizableMultipleConsumers() throws InterruptedException {
    int numElements = 1000;
    CommitLogReader reader = ListCommitLog.ofNonExternalizable(data(numElements), direct.getContext());
    ExecutorService executor = Executors.newCachedThreadPool();
    String name = "name" + UUID.randomUUID().toString();
    List<StreamElement> list = Collections.synchronizedList(new ArrayList<>());
    int numThreads = 10;
    CountDownLatch latch = new CountDownLatch(numThreads);
    for (int i = 0; i < numThreads; i++) {
        executor.submit(() -> {
            reader.observe(name, toList(list, ign -> latch.countDown()));
        });
    }
    latch.await();
    assertEquals("Duplicate: " + list.stream().collect(Collectors.toMap(Function.identity(), e -> 1, Integer::sum)).entrySet().stream().filter(e -> e.getValue() > 1).collect(Collectors.toList()).toString(), numElements, list.size());
}
Also used : IntStream(java.util.stream.IntStream) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) LogObserverUtils.toList(cz.o2.proxima.direct.commitlog.LogObserverUtils.toList) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) ConfigFactory(com.typesafe.config.ConfigFactory) SerializationException(cz.o2.proxima.scheme.SerializationException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) Repository(cz.o2.proxima.repository.Repository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 54 with CommitLogReader

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

the class ListCommitLogTest method testObserveExternalizableUnnamedPauseContinue.

@Test(timeout = 10000)
public void testObserveExternalizableUnnamedPauseContinue() throws InterruptedException {
    CommitLogReader reader = ListCommitLog.of(data(10), direct.getContext());
    List<StreamElement> data = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    ObserveHandle handle = reader.observe(null, toList(data, b -> latch.countDown(), v -> v.getValue()[0] < 5));
    latch.await();
    assertEquals(6, data.size());
    assertTrue(handle.getCommittedOffsets().isEmpty());
    assertFalse(handle.getCurrentOffsets().isEmpty());
    CountDownLatch nextLatch = new CountDownLatch(1);
    reader.observeBulkOffsets(handle.getCurrentOffsets(), toList(data, b -> nextLatch.countDown()));
    nextLatch.await();
    assertEquals(11, data.size());
}
Also used : IntStream(java.util.stream.IntStream) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) LogObserverUtils.toList(cz.o2.proxima.direct.commitlog.LogObserverUtils.toList) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) ConfigFactory(com.typesafe.config.ConfigFactory) SerializationException(cz.o2.proxima.scheme.SerializationException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) Repository(cz.o2.proxima.repository.Repository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 55 with CommitLogReader

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

the class ListCommitLogTest method testObserveExternalizableUnnamed.

@Test(timeout = 10000)
public void testObserveExternalizableUnnamed() throws InterruptedException {
    CommitLogReader reader = ListCommitLog.of(data(10), direct.getContext());
    List<StreamElement> data = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    ObserveHandle handle = reader.observe(null, toList(data, b -> latch.countDown()));
    latch.await();
    assertEquals(10, data.size());
    assertTrue(handle.getCommittedOffsets().isEmpty());
    assertFalse(handle.getCurrentOffsets().isEmpty());
}
Also used : IntStream(java.util.stream.IntStream) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) LogObserverUtils.toList(cz.o2.proxima.direct.commitlog.LogObserverUtils.toList) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) ConfigFactory(com.typesafe.config.ConfigFactory) SerializationException(cz.o2.proxima.scheme.SerializationException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) Repository(cz.o2.proxima.repository.Repository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)80 Test (org.junit.Test)70 StreamElement (cz.o2.proxima.storage.StreamElement)67 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)63 CountDownLatch (java.util.concurrent.CountDownLatch)58 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)45 ArrayList (java.util.ArrayList)41 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)34 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 List (java.util.List)31 UUID (java.util.UUID)29 ConfigFactory (com.typesafe.config.ConfigFactory)28 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)28 Offset (cz.o2.proxima.direct.commitlog.Offset)27 Collections (java.util.Collections)27 Collectors (java.util.stream.Collectors)27 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Repository (cz.o2.proxima.repository.Repository)25 Accessor (cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor)24