SLIDE 1 WebObjects + Scala
Building Concurrent WebObjects applications with Scala Ravi Mendis
SLIDE 2
Why Concurrent Programming?
SLIDE 3 2005
The year of Dual Core
SLIDE 4
SLIDE 5
SLIDE 7 Entry-Level
Cores Threads AMD Opteron IBM Power7 Intel Xeon
4 4 4 16 4 8
SLIDE 8 High-End
Cores Threads AMD Opteron IBM Power7 Intel Xeon
12 12 8 32 6 12
SLIDE 10 Roadmap
Cores Threads AMD Opteron IBM Power7 Intel Xeon
16 16 ? ?? 8 16
SLIDE 11 “By 2015 we will likely have over 100 cores on a many- core processing chip in our notebook computers.”
SLIDE 12
Welcome to the world of Multi-cores!
SLIDE 13
Q: How do we take advantage of multi-core processors?
SLIDE 14
A: Concurrent Programming
SLIDE 15
#1 Threads & Locks
SLIDE 16 “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
SLIDE 17 #1 Threads & Locks
- HARD to program
- HARD to scale
- Contentious
SLIDE 18 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!
SLIDE 19 BBC2, Top Gear - Series 15, Episode 1 - June 27 ’10
SLIDE 20 #2 Actor Model
(A Share NOTHING Model)
SLIDE 21
Slowmation
SLIDE 22
Demo
SLIDE 23 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>
SLIDE 24 HTML5 Video Conversion
Screenshot (.jpg) Video (.mp4)
Video (.ogg)
Thumbnail (.jpg)
SLIDE 25
- actor ! THUMBNAIL
- actor ! GRAB
- actor ! CONVERT2H264
- actor ! CONVERT2OGG
Slowmation Actor
(Video Processor)
SLIDE 26
!
SLIDE 27 Actor Messaging
- Asynchronous
- Non-blocking
- Immutable Messages
- NO shared data
SLIDE 28
Scala
SLIDE 29
- Immutable/Mutable datatypes
- Anonymous Functions (~Closures)
- No Static variables and methods
- Extensible
Scala
SLIDE 30 Scala
val greeting: String = “Hello World”;
SLIDE 31 Scala
val greeting = “Hello World”;
SLIDE 32 Scala
val greeting = “Hello World”
SLIDE 33 Scala
val greeting = “Hello World” // immutable var response = new String() // mutable response = “Hey!”
SLIDE 34 Java - Static Vars
public class _Talent extends EOGenericRecord { public static final String ENTITY_NAME = "Talent"; }
SLIDE 35 Scala - Companion Object
- bject Talent extends EOGenericRecord {
val ENTITY_NAME = "Talent" }
SLIDE 36
Thread-Safe
SLIDE 37 Scala - Pattern Matching
case a => { ... }
SLIDE 38 Scala - Pattern Matching
try { var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(entityName, session) } catch { case e: IllegalArgumentException => { var epf: ErrorPageInterface = D2W.factory.errorPage(session) epf.setMessage(e.toString) } }
SLIDE 39 Scala - Case Classes
case class PING case class PONG
SLIDE 40 Scala - Case Classes
actor ! PING actor ! PONG
SLIDE 41 Scala - Case Classes
case PING => { ... } case PONG => { ... }
SLIDE 42 Scala - Anonymous Functions
x => x^2
SLIDE 43 Scala - Anonymous Functions
x, y => x + y
SLIDE 44 Scala - Anonymous Functions
x, y => {x + y}
SLIDE 45 Scala - Anonymous Functions
case (PING => {...}) case (PONG => {...})
SLIDE 46
λ - Expressions
SLIDE 47 Scala Actors - Example
case class THUMBNAIL(filepath: String) // Actor msg case class val processor = actor { loop { react() { case THUMBNAIL(filepath: String) => { ... } } } } // asynchronous processor ! THUMBNAIL(“/Library/WebServer/Documents/slowmation/screenshots/0/2/4/422.jpg”)
SLIDE 48 #2 Actor Model
- EASY to program
- Scalable
- NO Deadlocks!
SLIDE 49
Q: Why can Scala be used with WebObjects?
SLIDE 50
A: Scala compiles to Java Byte-code
SLIDE 51
WebObjects + Concurrency
SLIDE 52
Why?
SLIDE 53 Performance
- Exploit multi-core processors
SLIDE 55
How?
SLIDE 56 Properties
WOAllowsConcurrentRequestHandling=true
SLIDE 57 Java - Synchronize Static Variables
private static String greeting = “Hello”; // private public void setGreeting(String aGreeting) { synchronized(greeting) { // synchronized block greeting = aGreeting; } }
SLIDE 58
Free!
SLIDE 59 WO - What is Shared?
- Application
- Session (concurrent WO)
SLIDE 60 Use ERXEC
- Project Wonder
- Automatic lock/unlock handling
SLIDE 61 Properties
er.extensions.ERXEC.safeLocking=true
SLIDE 62
Debugging
SLIDE 63
Demo
SLIDE 64 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
SLIDE 65
Bottlenecks
SLIDE 66
- Shared Object-cache
- Antithesis to Share Nothing
model
- Uses single database connection
- Single-Threaded
EOF - Bottleneck
SLIDE 67 #3 STM
(Software Transactional Memory)
SLIDE 68
- DOESN’T work in Scala Actors!
- EOF + Scala
SLIDE 69
- Use Raw SQL
- Alternative database access
- Squeryl
- ...
Solutions
SLIDE 70
Squeryl
SLIDE 71
Demo
SLIDE 72 Squeryl
- POSOs
- Actor Compatible
- Concurrent
SLIDE 73
“Scala as a concurrent programming language is powerful, safe and easy-to-use”
SLIDE 74
Benchmarks
SLIDE 75
- ERWOAdaptor (Mina)
- WO Worker as Actor
- Apache bench
- c = 5...65
- n = 500
The Test
SLIDE 76
Results
SLIDE 77 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
SLIDE 78 Requests per second
200 400 600 800 5 10 15 20 25 30 35 40 45 50 55 60 65
WOAdaptor ERWOScalaAdaptor ERWOAdaptor
SLIDE 79
“Scala as a concurrent programming language is powerful, safe and easy-to-use”
SLIDE 80 In life long love The key Is R&D The ladder of invention Has 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 care To think anew… Makes passions flair And lets a culture breed Between the sheets…
The R&D Imperative
SLIDE 81
Q&A
SLIDE 82
YOU do with 100 cores?
http://www.computerworld.com.au/article/354261/
http://wiki.objectstyle.org/confluence/display/WO/WebObjects+with+Scala
http://slowmation.uow.edu.au
http://services.wocommunity.org/wowodc/ERWOScalaAdaptor
References