Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/parallel/ParallelJoin.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.parallel;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.*;
24 import io.reactivex.rxjava3.operators.SimplePlainQueue;
25 import io.reactivex.rxjava3.operators.SimpleQueue;
26 import io.reactivex.rxjava3.operators.SpscArrayQueue;
27 import io.reactivex.rxjava3.parallel.ParallelFlowable;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29
30 /**
31 * Merges the individual 'rails' of the source ParallelFlowable, unordered,
32 * into a single regular Publisher sequence (exposed as Flowable).
33 *
34 * @param <T> the value type
35 */
36 public final class ParallelJoin<T> extends Flowable<T> {
37
38 final ParallelFlowable<? extends T> source;
39
40 final int prefetch;
41
42 final boolean delayErrors;
43
44 public ParallelJoin(ParallelFlowable<? extends T> source, int prefetch, boolean delayErrors) {
45 this.source = source;
46 this.prefetch = prefetch;
47 this.delayErrors = delayErrors;
48 }
49
50 @Override
51 protected void subscribeActual(Subscriber<? super T> s) {
52 JoinSubscriptionBase<T> parent;
53 if (delayErrors) {
54 parent = new JoinSubscriptionDelayError<>(s, source.parallelism(), prefetch);
55 } else {
56 parent = new JoinSubscription<>(s, source.parallelism(), prefetch);
57 }
58 s.onSubscribe(parent);
59 source.subscribe(parent.subscribers);
60 }
61
62 abstract static class JoinSubscriptionBase<T> extends AtomicInteger
63 implements Subscription {
64
65 private static final long serialVersionUID = 3100232009247827843L;
66
67 final Subscriber<? super T> downstream;
68
69 final JoinInnerSubscriber<T>[] subscribers;
70
71 final AtomicThrowable errors = new AtomicThrowable();
72
73 final AtomicLong requested = new AtomicLong();
74
75 volatile boolean cancelled;
76
77 final AtomicInteger done = new AtomicInteger();
78
79 JoinSubscriptionBase(Subscriber<? super T> actual, int n, int prefetch) {
80 this.downstream = actual;
81 @SuppressWarnings("unchecked")
82 JoinInnerSubscriber<T>[] a = new JoinInnerSubscriber[n];
83
84 for (int i = 0; i < n; i++) {
85 a[i] = new JoinInnerSubscriber<>(this, prefetch);
86 }
87
88 this.subscribers = a;
89 done.lazySet(n);
90 }
91
92 @Override
93 public void request(long n) {
94 if (SubscriptionHelper.validate(n)) {
95 BackpressureHelper.add(requested, n);
96 drain();
97 }
98 }
99
100 @Override
101 public void cancel() {
102 if (!cancelled) {
103 cancelled = true;
104
105 cancelAll();
106
107 if (getAndIncrement() == 0) {
108 cleanup();
109 }
110 }
111 }
112
113 void cancelAll() {
114 for (JoinInnerSubscriber<T> s : subscribers) {
115 s.cancel();
116 }
117 }
118
119 void cleanup() {
120 for (JoinInnerSubscriber<T> s : subscribers) {
121 s.queue = null;
122 }
123 }
124
125 abstract void onNext(JoinInnerSubscriber<T> inner, T value);
126
127 abstract void onError(Throwable e);
128
129 abstract void onComplete();
130
131 abstract void drain();
132 }
133
134 static final class JoinSubscription<T> extends JoinSubscriptionBase<T> {
135
136 private static final long serialVersionUID = 6312374661811000451L;
137
138 JoinSubscription(Subscriber<? super T> actual, int n, int prefetch) {
139 super(actual, n, prefetch);
140 }
141
142 @Override
143 public void onNext(JoinInnerSubscriber<T> inner, T value) {
144 if (get() == 0 && compareAndSet(0, 1)) {
145 if (requested.get() != 0) {
146 downstream.onNext(value);
147 if (requested.get() != Long.MAX_VALUE) {
148 requested.decrementAndGet();
149 }
150 inner.request(1);
151 } else {
152 SimplePlainQueue<T> q = inner.getQueue();
153
154 if (!q.offer(value)) {
155 cancelAll();
156 Throwable mbe = new MissingBackpressureException("Queue full?!");
157 if (errors.compareAndSet(null, mbe)) {
158 downstream.onError(mbe);
159 } else {
160 RxJavaPlugins.onError(mbe);
161 }
162 return;
163 }
164 }
165 if (decrementAndGet() == 0) {
166 return;
167 }
168 } else {
169 SimplePlainQueue<T> q = inner.getQueue();
170
171 if (!q.offer(value)) {
172 cancelAll();
173 onError(new MissingBackpressureException("Queue full?!"));
174 return;
175 }
176
177 if (getAndIncrement() != 0) {
178 return;
179 }
180 }
181
182 drainLoop();
183 }
184
185 @Override
186 public void onError(Throwable e) {
187 if (errors.compareAndSet(null, e)) {
188 cancelAll();
189 drain();
190 } else {
191 if (e != errors.get()) {
192 RxJavaPlugins.onError(e);
193 }
194 }
195 }
196
197 @Override
198 public void onComplete() {
199 done.decrementAndGet();
200 drain();
201 }
202
203 @Override
204 void drain() {
205 if (getAndIncrement() != 0) {
206 return;
207 }
208
209 drainLoop();
210 }
211
212 void drainLoop() {
213 int missed = 1;
214
215 JoinInnerSubscriber<T>[] s = this.subscribers;
216 int n = s.length;
217 Subscriber<? super T> a = this.downstream;
218
219 for (;;) {
220
221 long r = requested.get();
222 long e = 0;
223
224 middle:
225 while (e != r) {
226 if (cancelled) {
227 cleanup();
228 return;
229 }
230
231 Throwable ex = errors.get();
232 if (ex != null) {
233 cleanup();
234 a.onError(ex);
235 return;
236 }
237
238 boolean d = done.get() == 0;
239
240 boolean empty = true;
241
242 for (int i = 0; i < s.length; i++) {
243 JoinInnerSubscriber<T> inner = s[i];
244 SimplePlainQueue<T> q = inner.queue;
245 if (q != null) {
246 T v = q.poll();
247
248 if (v != null) {
249 empty = false;
250 a.onNext(v);
251 inner.requestOne();
252 if (++e == r) {
253 break middle;
254 }
255 }
256 }
257 }
258
259 if (d && empty) {
260 a.onComplete();
261 return;
262 }
263
264 if (empty) {
265 break;
266 }
267 }
268
269 if (e == r) {
270 if (cancelled) {
271 cleanup();
272 return;
273 }
274
275 Throwable ex = errors.get();
276 if (ex != null) {
277 cleanup();
278 a.onError(ex);
279 return;
280 }
281
282 boolean d = done.get() == 0;
283
284 boolean empty = true;
285
286 for (int i = 0; i < n; i++) {
287 JoinInnerSubscriber<T> inner = s[i];
288
289 SimpleQueue<T> q = inner.queue;
290 if (q != null && !q.isEmpty()) {
291 empty = false;
292 break;
293 }
294 }
295
296 if (d && empty) {
297 a.onComplete();
298 return;
299 }
300 }
301
302 if (e != 0) {
303 BackpressureHelper.produced(requested, e);
304 }
305
306 missed = addAndGet(-missed);
307 if (missed == 0) {
308 break;
309 }
310 }
311 }
312 }
313
314 static final class JoinSubscriptionDelayError<T> extends JoinSubscriptionBase<T> {
315
316 private static final long serialVersionUID = -5737965195918321883L;
317
318 JoinSubscriptionDelayError(Subscriber<? super T> actual, int n, int prefetch) {
319 super(actual, n, prefetch);
320 }
321
322 @Override
323 void onNext(JoinInnerSubscriber<T> inner, T value) {
324 if (get() == 0 && compareAndSet(0, 1)) {
325 if (requested.get() != 0) {
326 downstream.onNext(value);
327 if (requested.get() != Long.MAX_VALUE) {
328 requested.decrementAndGet();
329 }
330 inner.request(1);
331 } else {
332 SimplePlainQueue<T> q = inner.getQueue();
333
334 if (!q.offer(value)) {
335 inner.cancel();
336 errors.tryAddThrowableOrReport(new MissingBackpressureException("Queue full?!"));
337 done.decrementAndGet();
338 drainLoop();
339 return;
340 }
341 }
342 if (decrementAndGet() == 0) {
343 return;
344 }
345 } else {
346 SimplePlainQueue<T> q = inner.getQueue();
347
348 if (!q.offer(value)) {
349 inner.cancel();
350 errors.tryAddThrowableOrReport(new MissingBackpressureException("Queue full?!"));
351 done.decrementAndGet();
352 }
353
354 if (getAndIncrement() != 0) {
355 return;
356 }
357 }
358
359 drainLoop();
360 }
361
362 @Override
363 void onError(Throwable e) {
364 if (errors.tryAddThrowableOrReport(e)) {
365 done.decrementAndGet();
366 drain();
367 }
368 }
369
370 @Override
371 void onComplete() {
372 done.decrementAndGet();
373 drain();
374 }
375
376 @Override
377 void drain() {
378 if (getAndIncrement() != 0) {
379 return;
380 }
381
382 drainLoop();
383 }
384
385 void drainLoop() {
386 int missed = 1;
387
388 JoinInnerSubscriber<T>[] s = this.subscribers;
389 int n = s.length;
390 Subscriber<? super T> a = this.downstream;
391
392 for (;;) {
393
394 long r = requested.get();
395 long e = 0;
396
397 middle:
398 while (e != r) {
399 if (cancelled) {
400 cleanup();
401 return;
402 }
403
404 boolean d = done.get() == 0;
405
406 boolean empty = true;
407
408 for (int i = 0; i < n; i++) {
409 JoinInnerSubscriber<T> inner = s[i];
410
411 SimplePlainQueue<T> q = inner.queue;
412 if (q != null) {
413 T v = q.poll();
414
415 if (v != null) {
416 empty = false;
417 a.onNext(v);
418 inner.requestOne();
419 if (++e == r) {
420 break middle;
421 }
422 }
423 }
424 }
425
426 if (d && empty) {
427 errors.tryTerminateConsumer(a);
428 return;
429 }
430
431 if (empty) {
432 break;
433 }
434 }
435
436 if (e == r) {
437 if (cancelled) {
438 cleanup();
439 return;
440 }
441
442 boolean d = done.get() == 0;
443
444 boolean empty = true;
445
446 for (int i = 0; i < n; i++) {
447 JoinInnerSubscriber<T> inner = s[i];
448
449 SimpleQueue<T> q = inner.queue;
450 if (q != null && !q.isEmpty()) {
451 empty = false;
452 break;
453 }
454 }
455
456 if (d && empty) {
457 errors.tryTerminateConsumer(a);
458 return;
459 }
460 }
461
462 if (e != 0) {
463 BackpressureHelper.produced(requested, e);
464 }
465
466 missed = addAndGet(-missed);
467 if (missed == 0) {
468 break;
469 }
470 }
471 }
472 }
473
474 static final class JoinInnerSubscriber<T>
475 extends AtomicReference<Subscription>
476 implements FlowableSubscriber<T> {
477
478 private static final long serialVersionUID = 8410034718427740355L;
479
480 final JoinSubscriptionBase<T> parent;
481
482 final int prefetch;
483
484 final int limit;
485
486 long produced;
487
488 volatile SimplePlainQueue<T> queue;
489
490 JoinInnerSubscriber(JoinSubscriptionBase<T> parent, int prefetch) {
491 this.parent = parent;
492 this.prefetch = prefetch ;
493 this.limit = prefetch - (prefetch >> 2);
494 }
495
496 @Override
497 public void onSubscribe(Subscription s) {
498 SubscriptionHelper.setOnce(this, s, prefetch);
499 }
500
501 @Override
502 public void onNext(T t) {
503 parent.onNext(this, t);
504 }
505
506 @Override
507 public void onError(Throwable t) {
508 parent.onError(t);
509 }
510
511 @Override
512 public void onComplete() {
513 parent.onComplete();
514 }
515
516 public void requestOne() {
517 long p = produced + 1;
518 if (p == limit) {
519 produced = 0;
520 get().request(p);
521 } else {
522 produced = p;
523 }
524 }
525
526 public void request(long n) {
527 long p = produced + n;
528 if (p >= limit) {
529 produced = 0;
530 get().request(p);
531 } else {
532 produced = p;
533 }
534 }
535
536 public boolean cancel() {
537 return SubscriptionHelper.cancel(this);
538 }
539
540 SimplePlainQueue<T> getQueue() {
541 SimplePlainQueue<T> q = queue;
542 if (q == null) {
543 q = new SpscArrayQueue<>(prefetch);
544 this.queue = q;
545 }
546 return q;
547 }
548 }
549 }