projectreactor.io reactor3 intro

projectreactor.io reactor3 intro

Fda20bf9d9c85c4390ca7237beba45a2?s=128

Simon Baslé

April 04, 2017
Tweet

Transcript

  1. None
  2. the Agenda

  3. 101 Reactive Programming types & operators Reactor 3 and Spring

    Reactor debugging testing and other beasts backpressure and
  4. 101 Reactive Programming types & operators Reactor 3 and Spring

    Reactor debugging testing and reactor-netty other beasts backpressure and reactor-kafka...
  5. Reactive Programming 101 what does it bring to the table?

  6. WHY? because blocking is evil

  7. sync/blocking main thread processing resumes I/O BAD ! app does

    nothing
  8. async & blocking main thread wait & join ! new

    threads, costly ! complex BAD
  9. async & nonblocking “event loop” in non-blocking processing chunks no

    more threads than needed
  10. how do you achieve that without losing your mind ?

  11. Reactive Programming

  12. Composing asynchronous & event-based sequences, using non-blocking operators “ ”

  13. without sacrifice

  14. without sacrifice Callbacks ? Futures ? easy to block hard

    to compose callback hell ! not readable
  15. Pull? Push!

  16. Pull? Push!

  17. Pull? Push! (or actually a little bit of Both)

  18. vs Iterable - Iterator Publisher - Subscriber

  19. Data in Flux

  20. Publisher Subscriber push events produces consumes feedback

  21. interfaces from Reactive Streams spec Publisher Subscriber feedback consumes push

    events produces
  22. Publisher Subscriber push events produces consumes feedback Subscriber<T> onNext(T) onComplete();

    onError(Throwable);
  23. Publisher Subscriber produces consumes feedback 0..N elements + 0..1 (complete

    | error)
  24. Publisher Subscriber push events produces consumes feedback backpressure

  25. Publisher Subscriber push events produces consumes feedback can I have

    an API though?
  26. Publisher Subscriber push events produces consumes feedback

  27. Reactor 3 types and operators

  28. Flux<T> for 0..N elements

  29. None
  30. Mono<T> for at most 1 element

  31. None
  32. Reactive Streams all the way

  33. focus on Java 8

  34. focus on Java 8 Duration, CompletableFuture, Streams

  35. an Rx-inspired API with a vocabulary of operators similar to

    RxJava...
  36. an Rx-inspired API ...but not exactly the same

  37. Flux/Mono generator operator operator operator nothing happens until you subscribe

  38. Flux/Mono generator Subscriber operator operator operator nothing happens until you

    subscribe
  39. Flux/Mono generator Subscriber operator operator operator per Subscription state Sub

    Sub Sub
  40. Flux/Mono generator Subscriber operator operator operator data flows Sub Sub

    Sub
  41. examples

  42. Flux.range Subscriber map filter buffer

  43. None
  44. None
  45. None
  46. Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i

    % 2 == 0) .buffer(3)
  47. Flux.range Subscriber map filter buffer

  48. Flux.range Subscriber map filter buffer

  49. Flux.range Subscriber map filter buffer

  50. Flux.range Subscriber map filter buffer

  51. Flux.range Subscriber map filter buffer

  52. Flux.range Subscriber map filter buffer

  53. Flux.range Subscriber map filter buffer

  54. Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i

    % 2 == 0) .buffer(3) 5, 6, 7 | 8, 9, 10 | 8, 10 | [8,10]|
  55. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

  56. None
  57. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

  58. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

    resubscribe
  59. go DEEPER! async sub-processes with flatMap

  60. None
  61. flatMap(user -> tweetStream(user))

  62. flatMap(user -> tweetStream(user))

  63. flatMap(user -> tweetStream(user))

  64. flatMap(user -> tweetStream(user))

  65. None
  66. & much more...

  67. threading contexts

  68. Reactor is agnostic

  69. however it facilitates switching

  70. Schedulers

  71. Schedulers elastic, parallel, single, timer...

  72. publishOn switch rest of the flux on a thread

  73. subscribeOn make the subscription and request happen on a particular

    thread
  74. Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber

    Sub Sub Sub Sub Sub Sub
  75. Flux/Mono generator operator subscribe On operator publish On operator operator

    Subscriber Sub Sub Sub Sub Sub Sub
  76. Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber

    Sub Sub Sub Sub Sub Sub
  77. Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber

    Sub Sub Sub Sub Sub Sub
  78. Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber

    Sub Sub Sub Sub Sub Sub
  79. Flux/Mono generator operator subscribeO n operator publishOn operator operator Subscriber

    Sub Sub Sub Sub Sub Sub
  80. Testing & Debugging in an asynchronous world

  81. Testing a Publisher StepVerifier

  82. Testing a Publisher with Virtual Time support

  83. Simulate a source TestPublisher

  84. Debugging Issues stacktraces get hard to decipher

  85. usually just show where Subscription happens

  86. java.lang.IndexOutOfBoundsException: Source emitted more than one item at reactor.core.publisher.MonoSingle$SingleSubscriber.onNext(MonoSingle.java:120) at

    reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:314) ... ... at reactor.core.publisher.Mono.subscribeWith(Mono.java:2668) at reactor.core.publisher.Mono.subscribe(Mono.java:2629) at reactor.core.publisher.Mono.subscribe(Mono.java:2604) at reactor.core.publisher.Mono.subscribe(Mono.java:2582) at reactor.guide.GuideTests.debuggingActivated(GuideTests.java:727)
  87. Find where the Flux was instantiated (assembly)

  88. Checkpoint() or full assembly tracing

  89. costly! Checkpoint() or full assembly tracing

  90. Assembly trace from producer [reactor.core.publisher.MonoSingle] : reactor.core.publisher.Flux.single(Flux.java:5335) reactor.guide.GuideTests.scatterAndGather(GuideTests.java:689) reactor.guide.GuideTests.populateDebug(GuideTests.java:702)

  91. BACKPRESSURE and other beasts

  92. Publisher Subscriber subscribe

  93. Publisher Subscriber push data as fast as possible

  94. Publisher Subscriber subscribe with small request (eg. 1)

  95. Publisher Subscriber 1 onNext

  96. Publisher Subscriber request more (eg. 2)

  97. Publisher Subscriber 2 onNext

  98. Publisher Subscriber backpressure

  99. other ways of dealing with backpressure eg. drop, buffer...

  100. internal optimisations

  101. macro FUSION avoids unnecessary request back-and-forth

  102. micro FUSION share internal structures for less allocation

  103. lock free operators

  104. lock free operators and Work Stealing cpu 1 cpu 2

    cpu 3 cpu 4 cpu 5
  105. Reactor and Spring

  106. Reactor and Spring and do I need Spring to use

    Reactor ?
  107. NO philosoraptor you don’t

  108. Reactor 3 is a dependency of Spring 5 not the

    other way around
  109. 5

  110. Java 8 baseline

  111. reactive focus

  112. new WEB stack WebFlux

  113. @RestController(“/user”) public class UserController { @GetMapping(“/{id}”) Mono<User> getUser(String id) {...}

    }
  114. functional option for Routing

  115. Spring Data reactive repositories

  116. @GetMapping(“/{id}”) Mono<User> getUser(String id) { return reactiveRepo.findOne(id); }

  117. Reactor and the Network reactor-netty

  118. reactor-netty builds on Netty to provide reactive I/O

  119. Client / Server

  120. TCP or udp

  121. Http and WebSockets

  122. HttpServer.create(0) .newHandler((in, out) -> out .sendWebsocket((i, o) -> o.options(opt ->

    opt.flushOnEach()) .sendString(Flux.just("test") .delayElementsMillis(100) .repeat()) ) ) .block();
  123. still a bit low level

  124. still a bit low level

  125. reactor-kafka topics as Flux<T>

  126. reactive API over Kafka Producer / Consumer

  127. send(Flux) into Kafka

  128. Flux receive() from Kafka

  129. (currently in MILESTONE 1)

  130. Credits • Springfield Plant: copyright FOX • Raised Hand: CC0

    (via Pixabay) • Checklist: CC-By Crispy (via Flickr) • Robot Devil: copyright FOX • Volume Knob: CC0 (via Pixabay) • Camel Shape: CC0 (via Pixabay) • Dromedary Shape: CC-By-SA USPN,Whidou (via Wikimedia) • Dam: CC-By-SA Matthew Hatton (via geograph.org.uk) • Cogs: CC0 (via publicdomainpictures.net) • Thread Balls: CC0 (via Pixabay) • The Fortune Teller: Georges de la Tour (public domain) • Microphone: CC0 (via Pexels) • End Sands: CC0 (via Pixabay) • logos: Pivotal, Spring, Twitter and Github logo copyright their respective companies.