Search in sources :

Example 41 with ObserveHandle

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

the class OffsetTrackingBatchLogReaderTest method test.

@Test
void test() throws InterruptedException {
    final Repository repository = Repository.ofTest(ConfigFactory.parseString(MODEL));
    final DirectDataOperator direct = repository.getOrCreateOperator(DirectDataOperator.class);
    final List<StreamElement> firstPartition = Arrays.asList(newData(repository, "first", Instant.now(), "value"), newData(repository, "first", Instant.now(), "value2"), newData(repository, "first", Instant.now(), "value3"));
    final List<StreamElement> secondPartition = Arrays.asList(newData(repository, "second", Instant.now(), "value"), newData(repository, "second", Instant.now(), "value2"), newData(repository, "second", Instant.now(), "value3"));
    final BatchLogReader reader = OffsetTrackingBatchLogReader.of(ListBatchReader.ofPartitioned(direct.getContext(), firstPartition, secondPartition));
    final CountDownLatch latch = new CountDownLatch(1);
    final ObserveHandle handle = reader.observe(reader.getPartitions(), Collections.singletonList(repository.getEntity("test").getAttribute("data")), new BatchLogObserver() {

        @Override
        public boolean onNext(StreamElement element, OnNextContext context) {
            final OffsetTrackingBatchLogReader.OffsetCommitter committer = (OffsetTrackingBatchLogReader.OffsetCommitter) context;
            committer.markOffsetAsConsumed();
            return !context.getOffset().isLast();
        }

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    });
    latch.await();
    final OffsetTrackingBatchLogReader.OffsetTrackingObserveHandle otHandle = (OffsetTrackingBatchLogReader.OffsetTrackingObserveHandle) handle;
    Assertions.assertEquals(Arrays.asList(Offset.of(Partition.of(0), firstPartition.size() - 1, true), Offset.of(Partition.of(1), -1, false)), otHandle.getCurrentOffsets());
}
Also used : ObserveHandle(cz.o2.proxima.direct.batch.ObserveHandle) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Repository(cz.o2.proxima.repository.Repository) BatchLogReader(cz.o2.proxima.direct.batch.BatchLogReader) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) Test(org.junit.jupiter.api.Test)

Example 42 with ObserveHandle

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

the class PubSubReaderTest method testObserveBulkOffsetsWithWatermark.

@Test
public void testObserveBulkOffsetsWithWatermark() throws InterruptedException {
    long now = System.currentTimeMillis();
    Offset off = new PubSubOffset("dummy", now);
    reader.setSupplier(() -> {
        LockSupport.park();
        return null;
    });
    List<Offset> offsets = Collections.singletonList(off);
    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.observeBulkOffsets(offsets, observer)) {
        handle.waitUntilReady();
        assertEquals(1, handle.getCommittedOffsets().size());
        assertEquals(now, handle.getCommittedOffsets().get(0).getWatermark());
    }
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) PubSubOffset(cz.o2.proxima.direct.pubsub.PubSubReader.PubSubOffset) StreamElement(cz.o2.proxima.storage.StreamElement) Offset(cz.o2.proxima.direct.commitlog.Offset) PubSubOffset(cz.o2.proxima.direct.pubsub.PubSubReader.PubSubOffset) Test(org.junit.Test)

Example 43 with ObserveHandle

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

the class PubSubReaderTest method testObserveError.

@Test(timeout = 10000)
public void testObserveError() 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();
    });
    final AtomicBoolean cancelled = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(3);
    CommitLogObserver observer = new CommitLogObserver() {

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

        @Override
        public void onCancelled() {
            cancelled.set(true);
        }

        @Override
        public boolean onError(Throwable error) {
            assertEquals("Fail", error.getCause().getMessage());
            latch.countDown();
            return true;
        }
    };
    try (final ObserveHandle handle = reader.observe("dummy", observer)) {
        latch.await();
        assertFalse(cancelled.get());
        assertTrue(reader.acked.isEmpty());
        assertFalse(reader.nacked.isEmpty());
    }
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 44 with ObserveHandle

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

the class BatchLogSourceFunction method observePartitions.

@Override
UnifiedObserveHandle<Offset> observePartitions(BatchLogReader reader, List<Partition> partitions, List<AttributeDescriptor<?>> attributeDescriptors, LogObserver<OutputT> observer) {
    final ObserveHandle batchReaderHandle = reader.observe(partitions, attributeDescriptors, wrapSourceObserver(observer));
    // We've wrapped BatchLogReader with the OffsetTrackingBatchLogReader, so we can safely cast its
    // handle, to get access to offsets.
    final OffsetTrackingBatchLogReader.OffsetTrackingObserveHandle offsetTrackingHandle = (OffsetTrackingBatchLogReader.OffsetTrackingObserveHandle) batchReaderHandle;
    return new UnifiedObserveHandle<Offset>() {

        @Override
        public List<Offset> getConsumedOffsets() {
            // Filter out finished partitions, as we don't need them for restoring the state.
            return offsetTrackingHandle.getCurrentOffsets().stream().filter(offset -> !offset.isLast()).collect(Collectors.toList());
        }

        @Override
        public void close() {
            offsetTrackingHandle.close();
        }
    };
}
Also used : VisibleForTesting(org.apache.flink.shaded.guava18.com.google.common.annotations.VisibleForTesting) BatchLogReader(cz.o2.proxima.direct.batch.BatchLogReader) Partition(cz.o2.proxima.storage.Partition) Offset(cz.o2.proxima.direct.batch.Offset) RepositoryFactory(cz.o2.proxima.repository.RepositoryFactory) OffsetTrackingBatchLogReader(cz.o2.proxima.flink.core.batch.OffsetTrackingBatchLogReader) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Set(java.util.Set) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Experimental(cz.o2.proxima.annotations.Experimental) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) ObserveHandle(cz.o2.proxima.direct.batch.ObserveHandle) OffsetTrackingBatchLogReader(cz.o2.proxima.flink.core.batch.OffsetTrackingBatchLogReader) ObserveHandle(cz.o2.proxima.direct.batch.ObserveHandle) Offset(cz.o2.proxima.direct.batch.Offset)

Example 45 with ObserveHandle

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

the class ListCommitLog method pushToObserverBulk.

private ObserveHandle pushToObserverBulk(@Nonnull String name, UnaryPredicate<Integer> allowOffsetPredicate, CommitLogObserver observer) {
    observer.onRepartition(asRepartitionContext(Collections.singletonList(PARTITION)));
    Consumer consumer = CONSUMERS.get(uuid).computeIfAbsent(name, k -> new Consumer(uuid, name, watermarkEstimator));
    ListObserveHandle handle = new ListObserveHandle(uuid, name);
    Set<Integer> consumerFedOffsets = Collections.synchronizedSet(new HashSet<>());
    pushTo((element, offset) -> {
        if (handle.isClosed()) {
            return false;
        }
        final CommitLogObserver.OffsetCommitter committer = (succ, exc) -> {
            if (exc != null) {
                observer.onError(exc);
            }
        };
        final boolean acceptable;
        OnNextContext context = null;
        synchronized (consumer) {
            acceptable = (externalizableOffsets || !consumer.getAckedOffsets().contains(offset) && !consumer.getInflightOffsets().contains(offset)) && allowOffsetPredicate.apply(offset);
            if (acceptable) {
                context = consumer.asOnNextContextBulk(committer, offset, consumerFedOffsets);
            }
        }
        if (acceptable) {
            return observer.onNext(element, context);
        }
        return true;
    }, externalizableOffsets ? () -> true : allMatchOffset(consumer::isAcked), observer::onCompleted, observer::onCancelled);
    return handle;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Context(cz.o2.proxima.direct.core.Context) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) Getter(lombok.Getter) Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) Function(java.util.function.Function) ObserverUtils(cz.o2.proxima.direct.commitlog.ObserverUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Watermarks(cz.o2.proxima.time.Watermarks) UnaryPredicate(cz.o2.proxima.functional.UnaryPredicate) SerializationException(cz.o2.proxima.scheme.SerializationException) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) OffsetExternalizer(cz.o2.proxima.direct.commitlog.OffsetExternalizer) BiFunction(cz.o2.proxima.functional.BiFunction) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MoreObjects(com.google.common.base.MoreObjects) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Offset(cz.o2.proxima.direct.commitlog.Offset) Objects(java.util.Objects) List(java.util.List) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) ObserverUtils.asRepartitionContext(cz.o2.proxima.direct.commitlog.ObserverUtils.asRepartitionContext) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) 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