Slide 1

Slide 1 text

Flight of the Flux a look at Reactor's execution model

Slide 2

Slide 2 text

Flight of the Flux a look at Reactor's execution model @simonbasle

Slide 3

Slide 3 text

What we'll learn

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Nothing happens until you subscribe, Assembly time vs Execution time

Slide 6

Slide 6 text

Nothing happens until you subscribe, Assembly time vs Execution time Cold or Hot?

Slide 7

Slide 7 text

Nothing happens until you subscribe, Assembly time vs Execution time Cold or Hot? Concurrent agnostic

Slide 8

Slide 8 text

Cold or Hot? Concurrent agnostic Schedulers, PublishOn vs SubscribeOn Nothing happens until you subscribe, Assembly time vs Execution time

Slide 9

Slide 9 text

Cold or Hot? Concurrent agnostic Schedulers, PublishOn vs SubscribeOn Work Stealing Nothing happens until you subscribe, Assembly time vs Execution time

Slide 10

Slide 10 text

Cold or Hot? Concurrent agnostic Schedulers, PublishOn vs SubscribeOn Work Stealing Operator Fusion Nothing happens until you subscribe, Assembly time vs Execution time

Slide 11

Slide 11 text

Cold or Hot? Concurrent agnostic Schedulers, PublishOn vs SubscribeOn Work Stealing Operator Fusion Nothing happens until you subscribe, Assembly time vs Execution time

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Publisher Subscriber

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

...

Slide 16

Slide 16 text

...

Slide 17

Slide 17 text

...

Slide 18

Slide 18 text

Subscription

Slide 19

Slide 19 text

Nothing Happens until you subscribe Assembly time

Slide 20

Slide 20 text

Nothing happen until you subscribe

Slide 21

Slide 21 text

Assembly time

Slide 22

Slide 22 text

this.myFlux = Flux.just("foo") .map(String::length); MyTest#init()

Slide 23

Slide 23 text

Execution time

Slide 24

Slide 24 text

Execution time Subscription

Slide 25

Slide 25 text

this.myFlux .subscribe(); MyTest#test()

Slide 26

Slide 26 text

what would I see in a stack trace?

Slide 27

Slide 27 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx)

Slide 28

Slide 28 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx) ACCIO INTERESTING PARTS!

Slide 29

Slide 29 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx) ACCIO INTERESTING PARTS!

Slide 30

Slide 30 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx)

Slide 31

Slide 31 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx)

Slide 32

Slide 32 text

java.lang.ArithmeticException: / by zero at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115) at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156) at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:49) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90) at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63) at reactor.core.publisher.Flux.subscribe(Flux.java:6873) at mycode.test(MyTest.java:xx)

Slide 33

Slide 33 text

checkpoint()

Slide 34

Slide 34 text

Hot or Cold ?

Slide 35

Slide 35 text

Cold publisher

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

always true... until it's false " Nothing happen until you subscribe "

Slide 44

Slide 44 text

Hot publisher

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Concurrent Agnostic

Slide 48

Slide 48 text

do I have to accept a particular execution model?

Slide 49

Slide 49 text

No, we provide tools to fine tune

Slide 50

Slide 50 text

But, frameworks & libraries built on top might be opinionated

Slide 51

Slide 51 text

what about time operators?

Slide 52

Slide 52 text

...they do need to change threads

Slide 53

Slide 53 text

Schedulers & how to switch threads

Slide 54

Slide 54 text

Scheduler abstraction

Slide 55

Slide 55 text

, part part

Slide 56

Slide 56 text

publishOn

Slide 57

Slide 57 text

Flux.op1.op2.publishOn.op3.op4 .subscribe

Slide 58

Slide 58 text

1 2 Flux.op1.op2.publishOn.op3.op4 .subscribe

Slide 59

Slide 59 text

Thread calling subscribe() is where data flows initially 1

Slide 60

Slide 60 text

After publishOn data flows in Thread 2 2

Slide 61

Slide 61 text

subscribeOn

Slide 62

Slide 62 text

Flux.op1.op2 .subscribeOn .op3.op4 .subscribe

Slide 63

Slide 63 text

2 Flux.op1.op2 .subscribeOn .op3.op4 .subscribe 1

Slide 64

Slide 64 text

subscribeOn() changes where sequence subscription happens... 1

Slide 65

Slide 65 text

... which is also where data flows initially 2

Slide 66

Slide 66 text

and the two together?

Slide 67

Slide 67 text

Flux .op1 .subscribeOn .op2 .op4 .subscribe( v -> doSomething(v)) .publishOn

Slide 68

Slide 68 text

Flux .op1 .subscribeOn .op2 .op4 .subscribe( v -> doSomething(v)) .publishOn

Slide 69

Slide 69 text

Flux .op1 .subscribeOn .op2 .op4 .subscribe( v -> doSomething(v)) .publishOn 1 2 3 4

Slide 70

Slide 70 text

Work Stealing

Slide 71

Slide 71 text

so where do (most) operators run, again?

Slide 72

Slide 72 text

publishOn

Slide 73

Slide 73 text

what is work stealing?

Slide 74

Slide 74 text

1 operator that combines data coming from 2 threads

Slide 75

Slide 75 text

shared queue, drain loop

Slide 76

Slide 76 text

offer

Slide 77

Slide 77 text

offer work

Slide 78

Slide 78 text

offer offer work

Slide 79

Slide 79 text

offer offer work steal exit

Slide 80

Slide 80 text

offer offer work steal exit

Slide 81

Slide 81 text

offer offer work steal exit work

Slide 82

Slide 82 text

offer offer work steal exit work exit

Slide 83

Slide 83 text

Operator Fusion

Slide 84

Slide 84 text

macro fusion

Slide 85

Slide 85 text

a.then(b).then(c).then(d)

Slide 86

Slide 86 text

a.then(b,c,d)

Slide 87

Slide 87 text

micro fusion

Slide 88

Slide 88 text

a.map(->).map(->).filter(->)

Slide 89

Slide 89 text

a.map(->).map(->).filter(->)

Slide 90

Slide 90 text

a.map(->).map(->).filter(->) bounded queues (for backpressure)

Slide 91

Slide 91 text

a.map(->).map(->).filter(->)

Slide 92

Slide 92 text

a.map(->).map(->).filter(->) single transverse bounded queue

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Questions?

Slide 95

Slide 95 text

Thanks!

Slide 96

Slide 96 text

• geese flight: CC0 (via pxhere) • tools: CC0 via (pxhere) • harry potter: © Warner Bros (fair use) • badly drawn ron: CC0 (via pixabay) • fire and ice cubes: CC0 (via pxhere) • blank canvas: CC0 (via publicdomainpictures) • rail switch: CC0 (via pxhere) • pickpocket: CC-By by Wellcome Library, London (via wikimedia commons) • mask icon: CC-BySa by Angelus (via wikimedia commons) • metal fusion: CC-BySa by P Sakthy (via wikimedia commons) • The End sand: CC0 by ElektroPlan (via pixabay) Image Credits