cloud ready distributed lambdas - rainfocus · •enable hybrid, object-oriented / functional...

29

Upload: others

Post on 04-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand
Page 2: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

CloudReady:

BrianOliverArchitect|JavaSpecificationLeadOracleCloudEngineeringApril2017

Email:[email protected]:@pinocchiocode

IntroductiontoDistributedLambdas

Page 3: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

3

Page 4: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

ProgramAgenda

Lambdas

RemoteFunctionalInterfaces&Lambdas

Demonstrations!

TheChallengesandLimitations

NextSteps

1

2

3

4

5

4

Page 5: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

5

Page 6: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• Enablehybrid,object-oriented/functionalprogramminginJava• Allowyoutoeffectivelytreatcode-as-data– Both asarguments andasreturnvalues

• ExtensiveenhancementstoJavalibrariestosupportthem

AdefiningnewfeatureoftheJava8Platform

6

Map<String, String> rockstars = new LinkedHashMap<>();

rockstars.put(”BG", ”Brian Goetz");rockstars.put(”BO", ”Brian Oliver");rockstars.put(”CP", ”Cameron Purdy");

rockstars.forEach((k, v) -> System.out.printf("key: %s, value: %s\n", k, v));

Page 7: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• Closures=function+environment

AutomaticallyCaptureSurrounding“effectivelyfinal”Context=Closures!

7

String cool = ”Brian”;

Map<String, String> rockstars = new LinkedHashMap<>();

rockstars.put(”BG", ”Brian Goetz");rockstars.put(”BO", ”Brian Oliver");rockstars.put(”CP", ”Cameron Purdy");

rockstars.forEach((k, v) -> System.out.printf("key: %s is cool? %s\n”, k, v.equals(cool));

Page 8: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• Before:

• After:

Replaceanonymous“functionalinterface”inner-classeswithlambdas

8

executor.submit(new Runnable(){public void run()

{System.out.println("Hello from anonymous class!");}

});

executor.submit(() -> System.out.println("Hello from lambda!")

Page 9: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• RunnableInterfaceinJava8:

Anexamplefunctionalinterface

9

@FunctionalInterfacepublic interface Runnable {

/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.*/

public void run();}

Page 10: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

But…

10

Cantheybedistributedandinvokedacrossdevices,machines,data-centers…cloud?

Page 11: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• Wecancastthemthough…

• Output*:

StandardFunctionalInterfacesandthusLambdasarenotserializable bydefaultL

11

Runnable r = (Runnable & Serializable) () -> System.out.println("Hello from lambda!");

SerializedLambda[capturingClass=class JavaExamples, functionalInterfaceMethod=java/lang/Runnable.run:()V, implementation=invokeStatic JavaExamples.lambda$serialization$2feeadd5$1:()V, instantiatedMethodType=()V, numCaptured=0]

Page 12: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• TherearemanynewfunctionalinterfacesinJava8:– java.util.function.Function<T,R>– java.util.function.Predicate<T>– java.util.function.Supplier<T>– java.util.function.Consumer<T>– java.util.function.BiConsumer<T,U>– java.util.function.UnaryOperator<T>– java.util.function.BinaryOperator<T>–…andtheirprimitivevariants

NoneofthenewfunctionalinterfacesareSerializableL

12

Page 13: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

IntroductiontoLambdas

• Andsomeofouroldfriendsarealsofunctionalinterfaces:– java.lang.Runnable– java.util.concurrent.Callable<V>– java.util.Comparator<T>

• Perhapsallunusableinadistributedenvironment?

NoneoftheexistingfunctionalinterfacesareSerializableL

13

Page 14: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

RemoteFunctionalInterfaces

14

Page 15: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

RemoteFunctionalInterfaces

• Eg:TheCoherenceRemoteClassdefinesserializable functionalinterfaces

ExtensionsofexistingfunctionalinterfacestosupportSerializationJ

15

public class Remote{@FunctionalInterfacepublic interface Function<T, R>

extends java.util.function.Function<T, R>, Serializable {}

@FunctionalInterfacepublic interface Runnable

extends java.lang.Runnable, Serializable {}

...}

Page 16: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Where’stheCloud?IntroductiontoCoherence

16

Page 17: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

RemoteFunctionalInterfaces

• InadditiontothestandardMapmethod:

• CoherenceNamedCache alsodefines:

• JavaCompilerresolvestousethe“mostspecificoverloadedmethod”…sowe’rereadytodosomedistributedlambdas!

Mostspecificmethodswin!

17

V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction);

V computeIfAbsent(K key, Remote.Function<? super K, ? extends V> mappingFunction);

Page 18: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Demotime!

18

Page 19: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

ChallengesandLimitations

19

Page 20: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Oh!“Serialization:Thegiftthatkeepsongiving”

BrianGoetzJavaLanguageArchitect

20

Page 21: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Challenge:LambdaSerialization

• Developersusemultipletypesofserialization– Javaonlyprovidesone

• Lambdasneedtobestableacrossapplicationversions– Javadoesnotprovideanysuchguarantees,it’sweakatbest

• Developerswanttointroducenewlambdaswithoutrestarting– Javaexpectsthesameversionofthecapturingclasstoexisteverywhere

• Coherenceprovidesacustomandyetcompletelycompatibleremotingframeworktosolvethesechallenges

Javaprovidesthebareminimum…inadistributedenvironmentweneedalotmore

21

Page 22: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Limitation:ClosureSerialization

• Shouldnotreferencefieldsormethodsofthecapturingclass• Shouldnotreferenceanythingthatisn’tcertaintoexistbothontheclientandontheserver• Shouldonlycapturelocalserializable variables(useastaticfactory!)

Lambdasneedtobeselfcontainedandserializable

22

public static Remote.BiFunction<String, String, String> changeName(String name){return (key, value) ->

{ return name;};

}}

Page 23: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Limitation:ClosureSerialization

• Shouldnotnestothernon-serializable lambdas• Don’tdothis:

• Insteaddothis:

Lambdasneedtobeselfcontainedandserializable

23

Map<String, String> names = rockstars.invokeAll((entry) -> entry.extract(Person::getName));

ValueExtractor<Person, String> extractor = Person::getName;

Map<String, String> names = rockstars.invokeAll((entry) -> entry.extract(extractor));

Page 24: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Summary

24

Page 25: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

Summary

• LambdasareadefiningfeatureofJava8• Coherence12.2.1allowsyoutouselambdas– LikestandardJava,butbothlocally&inadistributedmanner– Allowsin-placeupdatewithoutlocking/synchronization–WithexistingCoherencefeatures(likeEntryProcessors,Listeners…)– Toperformstream-basedoperations

• Coherenceaddssupportforserializationofstandardfunctionalinterfaces• Coherencehandlesdistributedstream&lambdasinadynamicway– Supportsmultipleversionsofclientsseamlesslyrunningside-by-sidewithoutrestart

DistributedLambdasRock!Imaginethepossibilities!

25

Page 26: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

NextSteps

26

Page 27: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|

StartPlaying!

• https://www.oracle.com/goto/coherence• http://coherence.me

CoherenceforDevelopers!

27

https://twitter.com/OracleCoherence

https://www.linkedin.com/grp/home?gid=1782166

https://blogs.oracle.com/OracleCoherence http://www.youtube.com/OracleCoherence

Page 28: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.| 28

Page 29: Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional programming in Java •Allow you to effectively treat code-as-data –Bothas argumentsand