Rxada: an Ada Implementation of the Reactivex API - 1 PREVIOUSLY, in ADA-EUROPE 2007
Total Page:16
File Type:pdf, Size:1020Kb
RxAda: An Ada implementation of the ReactiveX API Alejandro R. Mosteo 2017-jun-13 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 1 PREVIOUSLY, IN ADA-EUROPE 2007... SANCTA: An Ada 2005 General-Purpose Architecture for Mobile Robotics Research 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 2 ABOUT ME Robotics, Perception, and Real-Time group (RoPeRT) http://robots.unizar.es/ Universidad de Zaragoza, Spain 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 3 CONTENTS • Motivation • What is ReactiveX – Language agnostic – Java – Ada • RxAda – Design challenges/decisions – Current implementation status – Future steps 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 4 PERSONAL MOTIVATION • Android development – Questionable design decisions for background tasks that interact with the GUI • Found RxJava – Simpler, saner way of doing multitasking – Documented comprehensively – Very active community in the Rx world • Achievable in Ada? – Aiming for the RxJava simplicity of use 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 5 EVENT-DRIVEN / ASYNCHRONOUS SYSTEMS <User drags map> ↓ Find nearby items ↓⌛ Request images ↓⌛↓⌛↓⌛↓⌛ Crop/Process image ↓⌛ Update GUI markers 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 6 OVERVIEW Event-driven systems ↓ Reactive Programming (philosophy) ↓ ReactiveX / Rx (specification) ↓ Rx.Net, RxJava, RxJS, RxC++, … ↓ RxAda 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 7 REACTIVE MANIFESTO (2014-sep-16 v2.0) www.reactivemanifesto.org “(...) we want systems that are Responsive, Resilient, Elastic and Message Driven. We call these Reactive Systems.” Jonas Bonér Dave Farley Roland Kuhn Martin Thompson 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 8 REACTIVE PROPERTIES • Responsive – “the cornerstone of usability and utility” – “reliable upper bounds [on response time]” – “consistent quality of service” • Resilient – “responsive in the face of failure” – “achieved by replication, containment, isolation and delegation” – “The client of a component is not burdened with handling its failures.” 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 9 REACTIVE PROPERTIES • Elastic – “responsive under varying workload” – “react to changes in the input rate by increasing or decreasing the resources allocated” – “no contention points or central bottlenecks” • Message driven – “rely on asynchronous message-passing (...) ensures loose coupling” – “enables load management, elasticity, and flow control” 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 10 REACTIVE SYSTEMS & ROBOTICS Courtesy of Washington University course CSE481C DISTRIBUTED CONSENSUS ALGORITHMS 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 11 ReactiveX ORIGINS •JavaThere is no “universal” reactive solution • ReactiveX is one among many – Roots in .NET (Reactive Extensions) – Erik Meijner 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 12 GANG OF FOUR’S OBSERVER PATTERN 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 13 PUSH-BASED FRAMEWORK Observable Observer subscribe on_next(Item) on_next(Item) time 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 14 RELATED TECHNOLOGIES • Callbacks • Futures/Promises (polling) – asyncProcess.isDone() – asyncProcess.getResult() – Can become unwieldy when futures depend on other futures • Ada AI12-0197-1 (generator functions) yield Datum; -- “Queues” a ready datum … for X of Datum_Generator do -- Blocks until data available 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 15 Rx NOVELTY Main selling point: composability Observable .from(Map.dragEvent) .inBackground() .do(Map.whoIsNear(event)) .doConcurrently (Users.getPicture(id)) .do(crop(image)) .inGUIthread() .do(updateMarker(image)) .subscribe() 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 16 Rx COMPOSABILITY • Advantages: – Imperative-like sequences • NOT executed when declared • Executed for every emitted item (event) – Abstract-away concerns like: • Low-level tasking • Synchronization • Thread-safety • Concurrent data structures • [Non-]Blocking I/O • Callback interactions 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 17 Rx CONTRACT Observable Observer subscribe on_next on_next on_next on_completed|on_error|none time 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 18 Rx CONTRACT / MARBLE DIAGRAMS On_Next* (On_Completed|On_Error)? • Finite sequence: time Infinite sequence: • Failed sequence: In mutual exclusion 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 19 RxAda DESIGN CONCERNS • Ease of use (library clients) – Number of instantiations • Shallow learning curve – Boilerplate in creation of sequences • Preserve top-bottom order of actions when possible • Ease of development – Reasonable (?) complexity • New features • Ongoing maintenance • Attract contributors – Avoid definite/indefinite multiplicity • Traits-based implementation 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 20 RxAda CONTRACT On_Next* (On_Completed|On_Error)? Requires one instantiation per user type 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 21 CLASSWIDE CONTRACT ALTERNATIVE (DISCARDED) • Pros : – No user instantiations – Dot notation available for all operators • Cons : – User view conversions • Runtime checks only • Code pollution with “downcasts” – Or some kind of marshalling • Would require instantiations anyway 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 22 FLASHBACK: ITEM SEQUENCES COMPOSABILITY Observable .from(Map.dragEvent) .inBackground() .do(Map.whoIsNear(event)) .doConcurrently (Users.getPicture(id)) .do(crop(image)) .inGUIthread() .do(updateMarker(image)) .subscribe() 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 23 COMPOSITION: OPERATORS • What is that between Integer – Observable – … Integers.Observer’Class – … Image – … Strings.Observable’Class – Observer ? String • Operators ≈ Observer + ⚙ + Observable – Arbitrarily long sequences – Apply one operation – Push down the item – Inactive until subscription! 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 24 PREDEFINED OPERATORS (subset) • Creating • Combining – Just, From – Merge, Zip – Interval, Range • Tasking • Transforming – Interval, Timer – Map, FlatMap – ObserveOn – Buffer, Window – SubscribeOn – Scan – Delay, Timeout • Filtering • Logical – Filter, Last, Skip – TakeUntil – Debounce, Sample • Mathematical – Distinct – Reduce 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 25 OPERATOR CONCATENATION (JAVA / C++) • Java: dot notation – Chains an operator – Returns another observable • C++: pipe operator “|” • Ada: “&” 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 26 TOY EXAMPLE Just Hello, world! Map(fn): x → fn(x) 13 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 27 RxAda DESIGN • Ada syntax: – No anonymous/lambdas → no inline code • We cannot have boilerplate in the sequence • We must move logic elsewhere (a-la std containers) – But also... 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 28 Ada DOT NOTATION • First preference, but unusable: – Freezing rules • All subprograms at top package level • But transforming operators require a second type – In a child package » Dot notation no longer available – Instantiate pairs of types » Circular dependencies • Settled on using the “&” function – Inspired by C++ and comp.lang.ada discussions – Requires “use” for every type mapping • Same_Type → Same_Type (e.g., Filter) • From_Type → Into_Type (e.g., Map) 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 29 FINAL EXAMPLE • Every second, – take the corresponding integer (1, 2, … ), – Hash its string representation (in bg task) – Print it (in IO task) 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 30 FINAL EXAMPLE: REQUISITES 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 31 FINAL EXAMPLE • Every second, – take the corresponding integer (1, 2, … ), – Hash its string representation (in bg) – Print it (in IO task) 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 32 WHAT IS HAPPENING? 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 33 SETUP PHASE / OPERATOR ANATOMY Operator’Class From.T Into.T From.Observer’Class Into.Observable’Class 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 34 COMPILE-TIME OPERATOR CONSISTENCY “&” returns the Observable Integer.Observer’Class view of the same Operator Image Strings.Observable’Class Distinct “&” for every & [ From.Observer + Strings.Observer’Class Into.Observable ] Value combination Integer.Observable’Class 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 35 SUBSCRIPTION ACTIVATION calls to Concatenate returns an Operator calls to returns an Observer Subscribe 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 36 SUBSCRIPTION ACTIVATION 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 37 WHAT IS HAPPENING? 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 38 WHAT IS HAPPENING? in current task/ in current task/ in current task Subscribe_On Observe_On 2017-jun-13 - RxAda: An Ada implementation of the ReactiveX API - 39 TASKING Subscribe_On Observe_On Op. param. 2017-jun-13 - RxAda: An Ada implementation of the