+ All Categories
Home > Documents > WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6...

WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6...

Date post: 20-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
82
WebObjects + Scala Building Concurrent WebObjects applications with Scala Ravi Mendis
Transcript
Page 1: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

WebObjects + ScalaBuilding Concurrent WebObjects applications with Scala

Ravi Mendis

Page 2: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Why Concurrent Programming?

Page 3: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

2005The year of Dual Core

Page 4: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12
Page 5: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12
Page 6: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

2010Today

Page 7: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Entry-Level

Cores Threads

AMD Opteron

IBM Power7

Intel Xeon

4 4

4 16

4 8

Page 8: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

High-End

Cores Threads

AMD Opteron

IBM Power7

Intel Xeon

12 12

8 32

6 12

Page 9: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

2011Tomorrow

Page 10: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Roadmap

Cores Threads

AMD Opteron

IBM Power7

Intel Xeon

16 16

? ??

8 16

Page 11: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

“By 2015 we will likely have over 100 cores on a many-core processing chip in our notebook computers.”

- Computerworld

Page 12: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Welcome to the world of Multi-cores!

Page 13: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Q: How do we take advantage of multi-core processors?

Page 14: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

A: Concurrent Programming

Page 15: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

#1 Threads & Locks

Page 16: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

“Concurrency is hard. It involves a lot of problems that are very difficult to think about and reason about and understand”

- Tim Bray co-inventor of XML

Page 17: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

#1 Threads & Locks• HARD to program

• HARD to scale

• Contentious

Page 18: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

java.lang.IllegalArgumentException: Cannot determine primary key for entity ASCCarveout from row: {charge = 3027.00; claimID = 321839138; }! at com.webobjects.eoaccess.EODatabaseChannel._fetchObject(EODatabaseChannel.java:348)! at com.webobjects.eoaccess.EODatabaseContext._objectsWithFetchSpecificationEditingContext(EODatabaseContext.java:3071)! at com.webobjects.eoaccess.EODatabaseContext.objectsWithFetchSpecification(EODatabaseContext.java:3195)! at com.webobjects.eocontrol.EOObjectStoreCoordinator.objectsWithFetchSpecification(EOObjectStoreCoordinator.java:488)! at com.webobjects.eocontrol.EOEditingContext.objectsWithFetchSpecification(EOEditingContext.java:4069)! at er.extensions.eof.ERXEC.objectsWithFetchSpecification(ERXEC.java:1211)! at com.webobjects.eoaccess.EODatabaseContext.objectsForSourceGlobalID(EODatabaseContext.java:4084)! at com.webobjects.eocontrol.EOObjectStoreCoordinator.objectsForSourceGlobalID(EOObjectStoreCoordinator.java:634)! at com.webobjects.eocontrol.EOEditingContext.objectsForSourceGlobalID(EOEditingContext.java:3923)! at er.extensions.eof.ERXEC.objectsForSourceGlobalID(ERXEC.java:1169)! at com.webobjects.eoaccess.EODatabaseContext._fireArrayFault(EODatabaseContext.java:4245)! at com.webobjects.eoaccess.EOAccessArrayFaultHandler.completeInitializationOfObject(EOAccessArrayFaultHandler.java:77)! at com.webobjects.eocontrol._EOCheapCopyMutableArray.willRead(_EOCheapCopyMutableArray.java:37)! at com.webobjects.eocontrol._EOCheapCopyMutableArray.count(_EOCheapCopyMutableArray.java:86)! at com.mpv.evaluation.ClaimEvaluator.validateClaim(ClaimEvaluator.java:398)! ...

...Deadlock!

Page 19: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

BBC2, Top Gear - Series 15, Episode 1 - June 27 ’10

Page 20: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

#2 Actor Model(A Share NOTHING Model)

Page 21: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Slowmation

Page 23: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

html5 <video>

<video poster="/slowmation/screenshots/0/2/4/425.jpg">! <source type="video/ogg" src="/slowmation/videos/6/d/8/423.ogg" />! <source type="video/mp4" src="/slowmation/videos/d/6/4/424.mp4" /></video>

Page 24: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

HTML5 Video Conversion

Screenshot (.jpg)

Video (.mp4)

Video (.ogg)

Thumbnail (.jpg)

Page 25: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• actor ! THUMBNAIL

• actor ! GRAB

• actor ! CONVERT2H264

• actor ! CONVERT2OGG

Slowmation Actor(Video Processor)

Page 26: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

!

Page 27: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Actor Messaging• Asynchronous

• Non-blocking

• Immutable Messages

• NO shared data

Page 28: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala

Page 29: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• Immutable/Mutable datatypes

• Anonymous Functions (~Closures)

• No Static variables and methods

• Extensible

Scala

Page 30: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scalaval greeting: String = “Hello World”;

Page 31: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scalaval greeting = “Hello World”;

Page 32: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scalaval greeting = “Hello World”

Page 33: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scalaval greeting = “Hello World” // immutablevar response = new String() // mutable

response = “Hey!”

Page 34: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Java - Static Varspublic class _Talent extends EOGenericRecord { public static final String ENTITY_NAME = "Talent";}

Page 35: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Companion Objectobject Talent extends EOGenericRecord { val ENTITY_NAME = "Talent"}

Page 36: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Thread-Safe

Page 37: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Pattern Matchingcase a => { ...}

Page 38: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Pattern Matchingtry { var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(entityName, session)} catch { case e: IllegalArgumentException => { var epf: ErrorPageInterface = D2W.factory.errorPage(session) epf.setMessage(e.toString) }}

Page 39: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Case Classescase class PINGcase class PONG

Page 40: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Case Classesactor ! PINGactor ! PONG

Page 41: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Case Classescase PING => { ...}

case PONG => { ...}

Page 42: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Anonymous Functionsx => x^2

Page 43: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Anonymous Functionsx, y => x + y

Page 44: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Anonymous Functionsx, y => {x + y}

Page 45: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala - Anonymous Functionscase (PING => {...})

case (PONG => {...})

Page 46: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

λ - Expressions

Page 47: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Scala Actors - Examplecase class THUMBNAIL(filepath: String) // Actor msg case class

val processor = actor { loop { react() { case THUMBNAIL(filepath: String) => { ... } } }}

// asynchronousprocessor ! THUMBNAIL(“/Library/WebServer/Documents/slowmation/screenshots/0/2/4/422.jpg”)

Page 48: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

#2 Actor Model• EASY to program

• Scalable

• NO Deadlocks!

Page 49: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Q: Why can Scala be used with WebObjects?

Page 50: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

A: Scala compiles to Java Byte-code

Page 51: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

WebObjects + Concurrency

Page 52: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Why?

Page 53: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Performance• Exploit multi-core processors

Page 54: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Ajax• More responsive UI

Page 55: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

How?

Page 56: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

PropertiesWOAllowsConcurrentRequestHandling=true

Page 57: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Java - Synchronize Static Variablesprivate static String greeting = “Hello”; // private

public void setGreeting(String aGreeting) { synchronized(greeting) { // synchronized block greeting = aGreeting; }}

Page 58: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Free!

Page 59: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

WO - What is Shared?• Application

• Session (concurrent WO)

Page 60: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Use ERXEC• Project Wonder

• Automatic lock/unlock handling

Page 61: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Propertieser.extensions.ERXEC.safeLocking=true

Page 62: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Debugging

Page 63: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Demo

Page 64: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Properties-Dcom.sun.management.jmxremote=true-Dcom.sun.management.jmxremote.authenticate=false-Dcom.sun.management.jmxremote.ssl=false

// Specific port-Dcom.sun.management.jmxremote.port=20102

Page 65: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Bottlenecks

Page 66: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• Shared Object-cache

• Antithesis to Share Nothing model

• Uses single database connection

• Single-Threaded

EOF - Bottleneck

Page 67: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

#3 STM(Software Transactional Memory)

Page 68: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• DOESN’T work in Scala Actors!

EOF + Scala

Page 69: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• Use Raw SQL

• Alternative database access

• Squeryl

• ...

Solutions

Page 70: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Squeryl

Page 71: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Demo

Page 72: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Squeryl• POSOs

• Actor Compatible

• Concurrent

Page 73: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

“Scala as a concurrent programming language is powerful, safe and easy-to-use”

Page 74: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Benchmarks

Page 75: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• ERWOAdaptor (Mina)

• WO Worker as Actor

• Apache bench

• c = 5...65

• n = 500

The Test

Page 76: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Results

Page 77: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Time per req. (mean)

0 ms

50 ms

100 ms

150 ms

200 ms

5 10 15 20 25 30 35 40 45 50 55 60 65

WOAdaptor ERWOScalaAdaptor ERWOAdaptor

Page 78: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Requests per second

0

200

400

600

800

5 10 15 20 25 30 35 40 45 50 55 60 65

WOAdaptor ERWOScalaAdaptor ERWOAdaptor

Page 79: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

“Scala as a concurrent programming language is powerful, safe and easy-to-use”

Page 80: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

In life long loveThe key Is R&D

The ladder of inventionHas eternal slide extension

Rung by rung You’ll climb to heaven above.

Constant innovation Guarantees a satiation

Of that ever changing want And deepening need

Nourishment and careTo think anew…

Makes passions flair And lets a culture breedBetween the sheets…

- by Emma Ahmad

The R&D Imperative

Page 81: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

Q&A

Page 82: WebObjects + ScalaWO.pdf · High-End Cores Threads AMD Opteron IBM Power7 Intel Xeon 12 12 8 32 6 12

• What will YOU do with 100 cores?http://www.computerworld.com.au/article/354261/

• WebObjects with Scalahttp://wiki.objectstyle.org/confluence/display/WO/WebObjects+with+Scala

• Case Study: Slowmationhttp://slowmation.uow.edu.au

• Source: ERWOScalaAdaptorhttp://services.wocommunity.org/wowodc/ERWOScalaAdaptor

References


Recommended