DevoxxFR 2018: Spéléo Reactor

DevoxxFR 2018: Spéléo Reactor

Fda20bf9d9c85c4390ca7237beba45a2?s=128

Simon Baslé

April 18, 2018
Tweet

Transcript

  1. 3.

    • introduction / reactive streams • map • la fusion

    • concatMap • la “drain loop”
  2. 16.

    Publisher Subscriber pousse des éléments produit consomme feedback “0 à

    N onNext suivi d’au plus 1 (onComplete ou onError)”
  3. 23.

    grâce à elle, le Subscriber peut notifier de sa capacité

    de traitement via Subscription#request(long)
  4. 29.

    Modèle dit “Asynchronous Push-Pull” Le Publisher envoie au max le

    nombre demandé, en asynchrone (au fil de l’eau)
  5. 31.
  6. 32.
  7. 40.

    les onNext, ainsi que les événements terminaux, sont en série

    ie. pas d’appel parallèle à onNext pendant un onNext
  8. 41.

    request & cancel peuvent se produire en parallèle (de onNext

    et d’eux même) req(2) req(7) req(1)
  9. 44.
  10. 45.
  11. 48.
  12. 52.
  13. 54.

    Autres Concepts Importants souscription vers le haut, données vers le

    bas rien ne se passe avant `subscribe()` enchaîner les opérateurs
  14. 62.

    (dans la plupart des cas) la source des données ne

    commence à émettre que lors de la souscription
  15. 80.

    final class FluxMap<T, R> extends FluxOperator<T, R> { final Function<?

    super T, ? extends R> mapper; FluxMap(Flux<? extends T> source, Function<? super T, ? extends R> mapper) { super(source); this.mapper = Objects.requireNonNull(mapper, "mapper"); } @Override @SuppressWarnings("unchecked") public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new MapSubscriber<>(actual, mapper)); } }
  16. 81.

    final class FluxMap<T, R> extends FluxOperator<T, R> { final Function<?

    super T, ? extends R> mapper; FluxMap(Flux<? extends T> source, Function<? super T, ? extends R> mapper) { super(source); this.mapper = Objects.requireNonNull(mapper, "mapper"); } @Override @SuppressWarnings("unchecked") public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new MapSubscriber<>(actual, mapper)); } }
  17. 83.

    static final class MapSubscriber<T, R> implements InnerOperator<T, R> { final

    CoreSubscriber<? super R> actual; final Function<? super T, ? extends R> mapper; boolean done; Subscription s; MapSubscriber(CoreSubscriber<? super R> actual, Function<? super T, ? extends R> mapper) { this.actual = actual; this.mapper = mapper; } ...
  18. 84.

    static final class MapSubscriber<T, R> implements InnerOperator<T, R> { final

    CoreSubscriber<? super R> actual; final Function<? super T, ? extends R> mapper; boolean done; Subscription s; MapSubscriber(CoreSubscriber<? super R> actual, Function<? super T, ? extends R> mapper) { this.actual = actual; this.mapper = mapper; }
  19. 85.

    final CoreSubscriber<? super R> actual; Subscription s; ... @Override public

    void onSubscribe(Subscription s) { if (Operators.validate(this.s, s)) { this.s = s; actual.onSubscribe(this); } }
  20. 86.

    final CoreSubscriber<? super R> actual; boolean done; ... @Override public

    void onComplete() { if (done) { return; } done = true; actual.onComplete(); }
  21. 87.

    final CoreSubscriber<? super R> actual; boolean done; ... @Override public

    void onError(Throwable t) { if (done) { return; } done = true; actual.onError(t); }
  22. 88.

    static final class MapSubscriber<T, R> implements InnerOperator<T, R> { ...

    Subscription s; ... @Override public void request(long n) { s.request(n); } @Override public void cancel() { s.cancel(); } }
  23. 90.

    @Override public void onNext(T t) { if (done) { return;

    } R v; try { v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value."); } catch (Throwable e) { onError(e); return; } actual.onNext(v); }
  24. 91.

    @Override public void onNext(T t) { if (done) { return;

    } R v; try { v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value."); } catch (Throwable e) { onError(e); return; } actual.onNext(v); }
  25. 93.

    La Fusion map peut être fusionné comment la fusion est-elle

    implémentée ? quel impact sur la chaîne d’opérateurs ?
  26. 100.

    final class FluxMapFuseable<T, R> extends FluxOperator<T, R> implements Fuseable {

    final Function<? super T, ? extends R> mapper; FluxMapFuseable(Flux<? extends T> source, Function<? super T, ? extends R> mapper) { super(source); this.mapper = Objects.requireNonNull(mapper, "mapper"); } @Override public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new MapFuseableSubscriber<>(actual, mapper)); }
  27. 101.

    final class FluxMapFuseable<T, R> extends FluxOperator<T, R> implements Fuseable {

    final Function<? super T, ? extends R> mapper; FluxMapFuseable(Flux<? extends T> source, Function<? super T, ? extends R> mapper) { super(source); this.mapper = Objects.requireNonNull(mapper, "mapper"); } @Override public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new MapFuseableSubscriber<>(actual, mapper)); }
  28. 102.

    static final class MapFuseableSubscriber<T, R> implements InnerOperator<T, R>, QueueSubscription<R> {

    final CoreSubscriber<? super R> actual; final Function<? super T, ? extends R> mapper; boolean done; QueueSubscription<T> s; int sourceMode; MapFuseableSubscriber(CoreSubscriber<? super R> actual, Function<? super T, ? extends R> mapper) { this.actual = actual; this.mapper = mapper; }
  29. 103.

    static final class MapFuseableSubscriber<T, R> implements InnerOperator<T, R>, QueueSubscription<R> {

    final CoreSubscriber<? super R> actual; final Function<? super T, ? extends R> mapper; boolean done; QueueSubscription<T> s; int sourceMode; MapFuseableSubscriber(CoreSubscriber<? super R> actual, Function<? super T, ? extends R> mapper) { this.actual = actual; this.mapper = mapper; }
  30. 105.

    @Override public int requestFusion(int requestedMode) { int m; if ((requestedMode

    & Fuseable.THREAD_BARRIER) != 0) { return Fuseable.NONE; } else { m = s.requestFusion(requestedMode); } sourceMode = m; return m; }
  31. 106.

    @Override public int requestFusion(int requestedMode) { int m; if ((requestedMode

    & Fuseable.THREAD_BARRIER) != 0) { return Fuseable.NONE; } else { m = s.requestFusion(requestedMode); } sourceMode = m; return m; }
  32. 108.

    @Override public void onNext(T t) { if (sourceMode == ASYNC)

    { actual.onNext(null); } else { if (done) { return; } R v; try { v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value."); } catch (Throwable e) { onError(e); return;
  33. 109.

    @Override public void onNext(T t) { if (sourceMode == ASYNC)

    { actual.onNext(null); } else { if (done) { return; } R v; try { v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value."); } catch (Throwable e) { onError(e); return;
  34. 110.

    @Override @Nullable public R poll() { T v = s.poll();

    if (v != null) { //can throw exceptions return Objects.requireNonNull(mapper.apply(v)); } return null; }
  35. 111.

    @Override @Nullable public R poll() { T v = s.poll();

    if (v != null) { //can throw exceptions return Objects.requireNonNull(mapper.apply(v)); } return null; }
  36. 112.

    @Override public boolean isEmpty() { return s.isEmpty(); } @Override public

    void clear() { s.clear(); } @Override public int size() { return s.size(); }
  37. 121.
  38. 122.
  39. 123.
  40. 126.
  41. 135.
  42. 138.

    final class FluxConcatMap<T, R> extends FluxOperator<T, R> { final Function<?

    super T, ? extends Publisher<? extends R>> mapper; final Supplier<? extends Queue<T>> queueSupplier; final int prefetch; FluxConcatMap(Flux<? extends T> source, Function<? super T, ? extends Publisher<? extends R>> mapper, Supplier<? extends Queue<T>> queueSupplier, int prefetch) { super(source); if (prefetch <= 0) throw new IllegalArgumentException("prefetch <= 0"); this.mapper = Objects.requireNonNull(mapper, "mapper"); this.queueSupplier = Objects.requireNonNull(queueSupplier); this.prefetch = prefetch; } @Override public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new ConcatMapImmediate<>(s, mapper, queueSupplier, prefetch)); }
  43. 139.

    final class FluxConcatMap<T, R> extends FluxOperator<T, R> { final Function<?

    super T, ? extends Publisher<? extends R>> mapper; final Supplier<? extends Queue<T>> queueSupplier; final int prefetch; FluxConcatMap(Flux<? extends T> source, Function<? super T, ? extends Publisher<? extends R>> mapper, Supplier<? extends Queue<T>> queueSupplier, int prefetch) { super(source); if (prefetch <= 0) throw new IllegalArgumentException("prefetch <= 0"); this.mapper = Objects.requireNonNull(mapper, "mapper"); this.queueSupplier = Objects.requireNonNull(queueSupplier); this.prefetch = prefetch; } @Override public void subscribe(CoreSubscriber<? super R> actual) { source.subscribe(new ConcatMapImmediate<>(s, mapper, queueSupplier, prefetch)); }
  44. 141.

    static final class ConcatMapImmediate<T, R> { final CoreSubscriber<? super R>

    actual; final Function<? super T, ? extends Publisher<? extends R>> mapper; final Supplier<? extends Queue<T>> queueSupplier; final int prefetch; final int limit; Subscription s; int consumed; volatile Queue<T> queue; volatile boolean done; volatile boolean cancelled; volatile boolean active; volatile Throwable error; volatile int wip; volatile int guard; int sourceMode;
  45. 142.

    static final class ConcatMapImmediate<T, R> { final CoreSubscriber<? super R>

    actual; final Function<? super T, ? extends Publisher<? extends R>> mapper; final Supplier<? extends Queue<T>> queueSupplier; final int prefetch; final int limit; Subscription s; int consumed; volatile Queue<T> queue; volatile boolean done; volatile boolean cancelled; volatile boolean active; volatile Throwable error; volatile int wip; volatile int guard; int sourceMode;
  46. 143.

    static final AtomicReferenceFieldUpdater<ConcatMapImmediate, Throwable> ERROR = AtomicReferenceFieldUpdater.newUpdater(ConcatMapImmediate.class, Throwable.class, "error"); static

    final AtomicIntegerFieldUpdater<ConcatMapImmediate> WIP = AtomicIntegerFieldUpdater.newUpdater(ConcatMapImmediate.class,"wip"); static final AtomicIntegerFieldUpdater<ConcatMapImmediate> GUARD = AtomicIntegerFieldUpdater.newUpdater(ConcatMapImmediate.class, "guard"); final ConcatMapInner<R> inner;
  47. 144.

    @Override public void onSubscribe(Subscription s) { if (Operators.validate(this.s, s)) {

    this.s = s; if (s instanceof Fuseable.QueueSubscription) { if (m == Fuseable.SYNC) { ... } else if (m == Fuseable.ASYNC) { ... } else { queue = queueSupplier.get(); } } else { queue = queueSupplier.get(); } actual.onSubscribe(this); s.request(Operators.unboundedOrPrefetch(prefetch)); } }
  48. 145.

    ... if (s instanceof Fuseable.QueueSubscription) { Fuseable.QueueSubscription<T> f = (Fuseable.QueueSubscription<T>)

    s; int m = f.requestFusion(Fuseable.ANY); if (m == Fuseable.SYNC) { sourceMode = Fuseable.SYNC; queue = f; done = true; actual.onSubscribe(this); drain(); return; } else if (m == Fuseable.ASYNC) { sourceMode = Fuseable.ASYNC; queue = f; } else { queue = queueSupplier.get(); } } ...
  49. 146.

    ... if (s instanceof Fuseable.QueueSubscription) { Fuseable.QueueSubscription<T> f = (Fuseable.QueueSubscription<T>)

    s; int m = f.requestFusion(Fuseable.ANY); if (m == Fuseable.SYNC) { sourceMode = Fuseable.SYNC; queue = f; done = true; actual.onSubscribe(this); drain(); return; } else if (m == Fuseable.ASYNC) { sourceMode = Fuseable.ASYNC; queue = f; } else { queue = queueSupplier.get(); } } ... drain( )? on va y revenir...
  50. 147.

    @Override public void onComplete() { done = true; drain(); }

    @Override public void request(long n) { inner.request(n); } @Override public void cancel() { if (!cancelled) { cancelled = true; inner.cancel(); s.cancel(); } }
  51. 148.

    @Override public void onError(Throwable t) { if (Exceptions.addThrowable(ERROR, this, t))

    { inner.cancel(); if (GUARD.getAndIncrement(this) == 0) { t = Exceptions.terminate(ERROR, this); if (t != TERMINATED) { actual.onError(t); } } } else { Operators.onErrorDropped(t, actual.currentContext()); } }
  52. 149.

    @Override public void onError(Throwable t) { if (Exceptions.addThrowable(ERROR, this, t))

    { inner.cancel(); if (GUARD.getAndIncrement(this) == 0) { t = Exceptions.terminate(ERROR, this); if (t != TERMINATED) { actual.onError(t); } } } else { Operators.onErrorDropped(t, actual.currentContext()); } }
  53. 150.

    @Override public void onNext(T t) { if (sourceMode == Fuseable.ASYNC)

    { drain(); } else if (!queue.offer(t)) { onError(Exceptions.BACKPRESSURE_ERROR_QUEUE_FULL); } else { drain(); } }
  54. 151.

    @Override public void onNext(T t) { if (sourceMode == Fuseable.ASYNC)

    { drain(); } else if (!queue.offer(t)) { onError(Exceptions.BACKPRESSURE_ERROR_QUEUE_FULL); } else { drain(); } } on va y revenir je vous dis...
  55. 152.

    pour l’instant, précisons juste que dans le drain( ) on

    va attacher le inner au Publisher créé par la fonction
  56. 153.

    @Override public void onNext(T t) { if (sourceMode == Fuseable.ASYNC)

    { drain(); } else if (!queue.offer(t)) { onError(Exceptions.BACKPRESSURE_ERROR_QUEUE_FULL); } else { drain(); } }
  57. 155.

    @Override public void innerNext(R value) { if (guard == 0

    && GUARD.compareAndSet(this, 0, 1)) { actual.onNext(value); if (GUARD.compareAndSet(this, 1, 0)) { return; } Throwable e = Exceptions.terminate(ERROR, this); if (e != TERMINATED) { actual.onError(e); } } }
  58. 157.

    @Override public void innerError(Throwable e) { if (Exceptions.addThrowable(ERROR, this, e))

    { s.cancel(); if (GUARD.getAndIncrement(this) == 0) { e = Exceptions.terminate(ERROR, this); if (e != TERMINATED) { actual.onError(e); } } } else { Operators.onErrorDropped(e, actual.currentContext()); } }
  59. 159.

    static final class ConcatMapInner<R> extends Operators.MultiSubscriptionSubscriber<R, R> { final FluxConcatMapImmediate<?,

    R> parent; long produced; ConcatMapInner(FluxConcatMapSupport<?, R> parent) { super(Operators.emptySubscriber()); this.parent = parent; } @Override public void onNext(R t) { produced++; parent.innerNext(t); } ...
  60. 160.

    static final class ConcatMapInner<R> extends Operators.MultiSubscriptionSubscriber<R, R> { final FluxConcatMapImmediate<?,

    R> parent; long produced; ConcatMapInner(FluxConcatMapSupport<?, R> parent) { super(Operators.emptySubscriber()); this.parent = parent; } @Override public void onNext(R t) { produced++; parent.innerNext(t); } ...
  61. 161.

    static final class ConcatMapInner<R> extends Operators.MultiSubscriptionSubscriber<R, R> { ... @Override

    public void onError(Throwable t) { long p = produced; if (p != 0L) { produced = 0L; produced(p); } parent.innerError(t); } ... }
  62. 162.

    static final class ConcatMapInner<R> extends Operators.MultiSubscriptionSubscriber<R, R> { ... @Override

    public void onComplete() { long p = produced; if (p != 0L) { produced = 0L; produced(p); } parent.innerComplete(); } } }
  63. 165.

    void drain() { if (WIP.getAndIncrement(this) == 0) { for (;

    ; ) { if (cancelled) return; if (!active) { ... } if (WIP.decrementAndGet(this) == 0) { break; } } } } }
  64. 166.

    void drain() { if (WIP.getAndIncrement(this) == 0) { for (;

    ; ) { if (cancelled) return; if (!active) { ... } if (WIP.decrementAndGet(this) == 0) { break; } } } } }
  65. 167.

    if (cancelled) return; if (!active) { boolean d = done;

    T v; try { v = queue.poll(); } catch (Throwable e) { actual.onError(e); return; } boolean empty = v == null; if (d && empty) { actual.onComplete(); return; } ...
  66. 168.

    ... if (!empty) { Publisher<? extends R> p; try {

    p = Objects.requireNonNull(mapper.apply(v)); } catch (Throwable e) { actual.onError(e); return; } if (sourceMode != Fuseable.SYNC) { ... } active = true; p.subscribe(inner); } } if (WIP.decrementAndGet(this) == 0) { break; }
  67. 169.

    ... if (sourceMode != Fuseable.SYNC) { int c = consumed

    + 1; if (c == limit) { consumed = 0; s.request(c); } else { consumed = c; } } active = true; p.subscribe(inner); } } if (WIP.decrementAndGet(this) == 0) { break; }
  68. 171.

    void drain() { if (WIP.getAndIncrement(this) == 0) { for (;

    ; ) { if (cancelled) return; if (!active) { ... } if (WIP.decrementAndGet(this) == 0) { break; } } } } }
  69. 173.

    thread 1 thread 2 thread 3 sans work stealing onNext(3)

    empile 3 request( 1 ) émet 1 2, 3
  70. 174.

    request( 2 ) thread 1 thread 2 thread 3 sans

    work stealing émet 2, 3 onNext(3) empile 3 request( 1 ) émet 1
  71. 175.

    thread 1 thread 2 thread 3 sans work stealing onNext(3)

    empile 3 bloqué draine bloqué draine
  72. 177.

    thread 1 thread 2 thread 3 avec work stealing onNext(3)

    empile 3 request( 1 ) émet 1 2, 3
  73. 178.

    request( 2 ) thread 1 thread 2 thread 3 avec

    work stealing onNext(3) empile 3 request( 1 ) émet 1 émet 2, 3
  74. 179.

    request( 2 ) thread 1 thread 2 thread 3 avec

    work stealing onNext(3) empile 3 request( 1 ) émet 1 émet 2, 3 met juste la requête à jour et quitte la boucle WIP cette drainLoop a “gagné”
  75. 180.
  76. 181.
  77. 182.