Search in sources :

Example 1 with UnaryPredicate

use of cz.o2.proxima.functional.UnaryPredicate 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

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions (com.google.common.base.Preconditions)1 Iterables (com.google.common.collect.Iterables)1 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)1 OffsetCommitter (cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter)1 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)1 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)1 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)1 ObserverUtils (cz.o2.proxima.direct.commitlog.ObserverUtils)1 ObserverUtils.asRepartitionContext (cz.o2.proxima.direct.commitlog.ObserverUtils.asRepartitionContext)1 Offset (cz.o2.proxima.direct.commitlog.Offset)1 OffsetExternalizer (cz.o2.proxima.direct.commitlog.OffsetExternalizer)1 Context (cz.o2.proxima.direct.core.Context)1 BiFunction (cz.o2.proxima.functional.BiFunction)1 UnaryPredicate (cz.o2.proxima.functional.UnaryPredicate)1 SerializationException (cz.o2.proxima.scheme.SerializationException)1