Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional...
Transcript of Cloud Ready Distributed Lambdas - RainFocus · •Enable hybrid, object-oriented / functional...
![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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/1.jpg)
![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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070723/5f02028d7e708231d40221a7/html5/thumbnails/29.jpg)