All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.core.publisher.FluxWindowBoundary Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/*
 * Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package reactor.core.publisher;

import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import reactor.core.CoreSubscriber;
import reactor.core.Disposable;
import reactor.core.Exceptions;
import reactor.core.Scannable;
import reactor.util.annotation.Nullable;
import reactor.util.concurrent.Queues;
import reactor.util.context.Context;

import static reactor.core.Exceptions.wrapSource;

/**
 * Splits the source sequence into continuous, non-overlapping windowEnds
 * where the window boundary is signalled by another Publisher
 *
 * @param  the input value type
 * @param  the boundary publisher's type (irrelevant)
 * @see Reactive-Streams-Commons
 */
final class FluxWindowBoundary extends InternalFluxOperator> {

	final Publisher other;

	final Supplier> processorQueueSupplier;

	FluxWindowBoundary(Flux source, Publisher other,
			Supplier> processorQueueSupplier) {
		super(source);
		this.other = Operators.toFluxOrMono(Objects.requireNonNull(other, "other"));
		this.processorQueueSupplier = Objects.requireNonNull(processorQueueSupplier, "processorQueueSupplier");
	}

	@Override
	public int getPrefetch() {
		return Integer.MAX_VALUE;
	}

	@Override
	@Nullable
	public CoreSubscriber subscribeOrReturn(CoreSubscriber> actual) {
		WindowBoundaryMain main = new WindowBoundaryMain<>(actual,
				processorQueueSupplier, processorQueueSupplier.get());

		actual.onSubscribe(main);

		if (main.emit(main.window)) {
			other.subscribe(main.boundary);

			return main;
		}
		else {
			return null;
		}
	}

	@Override
	public Object scanUnsafe(Attr key) {
		if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
		return super.scanUnsafe(key);
	}

	static final class WindowBoundaryMain
			implements InnerOperator>, Disposable {

		final Supplier> processorQueueSupplier;

		final WindowBoundaryOther boundary;

		final Queue                   queue;
		final CoreSubscriber> actual;

		Sinks.Many window;

		volatile Subscription s;

		@SuppressWarnings("rawtypes")
		static final AtomicReferenceFieldUpdater S =
				AtomicReferenceFieldUpdater.newUpdater(WindowBoundaryMain.class, Subscription.class, "s");

		volatile long requested;
		@SuppressWarnings("rawtypes")
		static final AtomicLongFieldUpdater REQUESTED =
				AtomicLongFieldUpdater.newUpdater(WindowBoundaryMain.class, "requested");

		volatile Throwable error;
		@SuppressWarnings("rawtypes")
		static final AtomicReferenceFieldUpdater ERROR =
				AtomicReferenceFieldUpdater.newUpdater(WindowBoundaryMain.class, Throwable.class, "error");

		volatile int cancelled;
		@SuppressWarnings("rawtypes")
		static final AtomicIntegerFieldUpdater CANCELLED =
				AtomicIntegerFieldUpdater.newUpdater(WindowBoundaryMain.class, "cancelled");

		volatile int windowCount;
		@SuppressWarnings("rawtypes")
		static final AtomicIntegerFieldUpdater WINDOW_COUNT =
				AtomicIntegerFieldUpdater.newUpdater(WindowBoundaryMain.class, "windowCount");

		volatile int wip;
		@SuppressWarnings("rawtypes")
		static final AtomicIntegerFieldUpdater WIP =
				AtomicIntegerFieldUpdater.newUpdater(WindowBoundaryMain.class, "wip");

		boolean done;

		static final Object BOUNDARY_MARKER = new Object();

		static final Object DONE = new Object();

		WindowBoundaryMain(CoreSubscriber> actual,
				Supplier> processorQueueSupplier,
				Queue processorQueue) {
			this.actual = actual;
			this.processorQueueSupplier = processorQueueSupplier;
			this.window = Sinks.unsafe().many().unicast().onBackpressureBuffer(processorQueue, this);
			WINDOW_COUNT.lazySet(this, 2);
			this.boundary = new WindowBoundaryOther<>(this);
			this.queue = Queues.unboundedMultiproducer().get();
		}

		@Override
		public final CoreSubscriber> actual() {
			return actual;
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.PARENT) return s;
			if (key == Attr.ERROR) return error;
			if (key == Attr.CANCELLED) return cancelled == 1;
			if (key == Attr.TERMINATED) return done;
			if (key == Attr.PREFETCH) return Integer.MAX_VALUE;
			if (key == Attr.REQUESTED_FROM_DOWNSTREAM) return requested;
			if (key == Attr.BUFFERED) return queue.size();
			if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;

			return InnerOperator.super.scanUnsafe(key);
		}

		@Override
		public Stream inners() {
			return Stream.of(boundary, Scannable.from(window));
		}

		@Override
		public void onSubscribe(Subscription s) {
			if (Operators.setOnce(S, this, s)) {
				s.request(Long.MAX_VALUE);
			}
		}

		@Override
		public void onNext(T t) {
			if (done) {
				Operators.onNextDropped(t, actual.currentContext());
				return;
			}
			synchronized (this) {
				queue.offer(t);
			}
			drain();
		}

		@Override
		public void onError(Throwable t) {
			if (done) {
				Operators.onErrorDropped(t, actual.currentContext());
				return;
			}
			done = true;
			boundary.cancel();
			if (Exceptions.addThrowable(ERROR, this, t)) {
				drain();
			}
		}

		@Override
		public void onComplete() {
			if (done) {
				return;
			}
			done = true;
			boundary.cancel();
			synchronized (this) {
				queue.offer(DONE);
			}
			drain();
		}

		@Override
		public void dispose() {
			if (WINDOW_COUNT.decrementAndGet(this) == 0) {
				cancelMain();
				boundary.cancel();
			}
		}

		@Override
		public boolean isDisposed() {
			return cancelled == 1 || done;
		}

		@Override
		public void request(long n) {
			if (Operators.validate(n)) {
				Operators.addCap(REQUESTED, this, n);
			}
		}

		void cancelMain() {
			Operators.terminate(S, this);
		}

		@Override
		public void cancel() {
			if (CANCELLED.compareAndSet(this, 0, 1)) {
				dispose();
			}
		}

		void boundaryNext() {
			synchronized (this) {
				queue.offer(BOUNDARY_MARKER);
			}

			if (cancelled != 0) {
				boundary.cancel();
			}

			drain();
		}

		void boundaryError(Throwable e) {
			cancelMain();
			if (Exceptions.addThrowable(ERROR, this, e)) {
				drain();
			} else {
				Operators.onErrorDropped(e, actual.currentContext());
			}
		}

		void boundaryComplete() {
			cancelMain();
			synchronized (this) {
				queue.offer(DONE);
			}
			drain();
		}

		void drain() {
			if (WIP.getAndIncrement(this) != 0) {
				return;
			}

			final Subscriber> a = actual;
			final Queue q = queue;
			Sinks.Many w = window;

			int missed = 1;

			for (;;) {

				for (;;) {
					if (error != null) {
						q.clear();
						Throwable e = Exceptions.terminate(ERROR, this);
						if (e != Exceptions.TERMINATED) {
							w.emitError(wrapSource(e),
									Sinks.EmitFailureHandler.FAIL_FAST);

							a.onError(e);
						}
						return;
					}

					Object o = q.poll();

					if (o == null) {
						break;
					}

					if (o == DONE) {
						q.clear();

						w.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);

						a.onComplete();
						return;
					}
					if (o != BOUNDARY_MARKER) {

						@SuppressWarnings("unchecked")
						T v = (T)o;
						w.emitNext(v, Sinks.EmitFailureHandler.FAIL_FAST);
					}
					if (o == BOUNDARY_MARKER) {
						w.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);

						if (cancelled == 0) {
							if (requested != 0L) {
								Queue pq = processorQueueSupplier.get();

								WINDOW_COUNT.getAndIncrement(this);

								w = Sinks.unsafe().many().unicast().onBackpressureBuffer(pq, this);
								window = w;

								a.onNext(w.asFlux());

								if (requested != Long.MAX_VALUE) {
									REQUESTED.decrementAndGet(this);
								}
							} else {
								q.clear();
								cancelMain();
								boundary.cancel();

								a.onError(Exceptions.failWithOverflow("Could not create new window due to lack of requests"));
								return;
							}
						}
					}
				}

				missed = WIP.addAndGet(this, -missed);
				if (missed == 0) {
					break;
				}
			}
		}

		boolean emit(Sinks.Many w) {
			long r = requested;
			if (r != 0L) {
				actual.onNext(w.asFlux());
				if (r != Long.MAX_VALUE) {
					REQUESTED.decrementAndGet(this);
				}
				return true;
			} else {
				cancel();

				actual.onError(Exceptions.failWithOverflow("Could not emit buffer due to lack of requests"));

				return false;
			}
		}
	}

	static final class WindowBoundaryOther
			extends Operators.DeferredSubscription
			implements InnerConsumer {

		final WindowBoundaryMain main;

		WindowBoundaryOther(WindowBoundaryMain main) {
			this.main = main;
		}

		@Override
		public void onSubscribe(Subscription s) {
			if (set(s)) {
				s.request(Long.MAX_VALUE);
			}
		}

		@Override
		public Context currentContext() {
			return main.currentContext();
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.ACTUAL) {
				return main;
			}
			if (key == Attr.RUN_STYLE) {
			    return Attr.RunStyle.SYNC;
			}
			return super.scanUnsafe(key);
		}

		@Override
		public void onNext(U t) {
			main.boundaryNext();
		}

		@Override
		public void onError(Throwable t) {
			main.boundaryError(t);
		}

		@Override
		public void onComplete() {
			main.boundaryComplete();
		}
	}
}