cloud ready distributed lambdas - rainfocus · •enable hybrid, object-oriented / functional...
TRANSCRIPT
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
CloudReady:
BrianOliverArchitect|JavaSpecificationLeadOracleCloudEngineeringApril2017
Email:[email protected]:@pinocchiocode
IntroductiontoDistributedLambdas
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.
3
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
ProgramAgenda
Lambdas
RemoteFunctionalInterfaces&Lambdas
Demonstrations!
TheChallengesandLimitations
NextSteps
1
2
3
4
5
4
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
IntroductiontoLambdas
5
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));
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));
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!")
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();}
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
But…
10
Cantheybedistributedandinvokedacrossdevices,machines,data-centers…cloud?
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]
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
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
IntroductiontoLambdas
• Andsomeofouroldfriendsarealsofunctionalinterfaces:– java.lang.Runnable– java.util.concurrent.Callable<V>– java.util.Comparator<T>
• Perhapsallunusableinadistributedenvironment?
NoneoftheexistingfunctionalinterfacesareSerializableL
13
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
RemoteFunctionalInterfaces
14
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 {}
...}
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
Where’stheCloud?IntroductiontoCoherence
16
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);
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
Demotime!
18
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
ChallengesandLimitations
19
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
Oh!“Serialization:Thegiftthatkeepsongiving”
BrianGoetzJavaLanguageArchitect
20
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
Challenge:LambdaSerialization
• Developersusemultipletypesofserialization– Javaonlyprovidesone
• Lambdasneedtobestableacrossapplicationversions– Javadoesnotprovideanysuchguarantees,it’sweakatbest
• Developerswanttointroducenewlambdaswithoutrestarting– Javaexpectsthesameversionofthecapturingclasstoexisteverywhere
• Coherenceprovidesacustomandyetcompletelycompatibleremotingframeworktosolvethesechallenges
Javaprovidesthebareminimum…inadistributedenvironmentweneedalotmore
21
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;};
}}
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));
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
Summary
24
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
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.|
NextSteps
26
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
Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.| 28