event loop
DESCRIPTION
A presentation to show the workings of the event loop and how vert.x uses this pattern.TRANSCRIPT
![Page 1: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/1.jpg)
Event Loop+ Vert.x
![Page 2: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/2.jpg)
Chief Developer @ Senacor Technologies AG!http://www.senacor.com!
[email protected]!Twitter: @codepitbull
Jochen Mader
![Page 3: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/3.jpg)
CPU
ABC
42
1 2 3
4 5 6
7 8 90
![Page 4: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/4.jpg)
CPU
![Page 5: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/5.jpg)
CPU CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
![Page 6: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/6.jpg)
volatile
CyclicBarrier
AtomicBoolean
synchronized
ReentrantLock
AtomicInteger
AtomicCafe
Thread
ThreadPoolExecutor
RunnableCallable
![Page 7: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/7.jpg)
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
![Page 8: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/8.jpg)
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
wooblehttp://www.wooble.wtf
![Page 9: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/9.jpg)
C10K
ZZZZZZZZZz
ZZZZZZZZZzZZZZZZZZZz
![Page 10: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/10.jpg)
Single Thread
1 Thread w/ lock
2 Threads w/ lock
1 Thread w/ CAS
2 Threads w/ CAS
Volatile
0 12500 25000 37500 50000
Contention
https://github.com/codepitbull/lockperformance
![Page 11: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/11.jpg)
I/OAcces type cycles
L1- Cache 3
L2-Cache 14
RAM 250
Disk 41000000
Network 240000000
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
![Page 12: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/12.jpg)
contentionI/O
![Page 13: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/13.jpg)
Channel AChannel BChannel CChannel D
Selector
Event Handler 1Event Handler 2Event Handler 3Event Handler 4Event Handler 5
REACTOR
DO NOT BLOCK!JAVA NIO
![Page 14: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/14.jpg)
Selector selector = Selector.open();channel.configureBlocking(false); SelectionKey key = channel.register(selector, SelectionKey.OP_READ);while(true) { int readyChannels = selector.select(); if(readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if(key.isAcceptable()) { // a connection was accepted by a ServerSocketChannel. } else if (key.isConnectable()) { // a connection was established with a remote server. } else if (key.isReadable()) { // a channel is ready for reading } else if (key.isWritable()) { // a channel is ready for writing } keyIterator.remove(); }}
![Page 15: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/15.jpg)
I NEEEEDS !NICE API!!!!
![Page 16: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/16.jpg)
Channel AChannel BChannel CChannel D
Selector
Event Handler 1Event Handler 2Event Handler 3Event Handler 4Event Handler 5
12.10.2012
![Page 17: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/17.jpg)
Noch !hübscher?
![Page 18: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/18.jpg)
Quasar
![Page 19: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/19.jpg)
生 ॐ СВОБОДА
![Page 20: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/20.jpg)
Micro Services (Martin Fowler)
http://martinfowler.com/articles/microservices.html
![Page 21: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/21.jpg)
class MustacheRendererVerticle extends Verticle { DefaultMustacheFactory mf = new DefaultMustacheFactory( "de/codepitbull/javaland/mustache"); @Override def start() { vertx.eventBus.registerHandler("template.render", { message -> Mustache mustache = mf.compile(message.body()); StringWriter sw = new StringWriter(); mustache.execute(sw, new Example()).flush(); message.reply(sw.getBuffer().toString()); }); }}
![Page 22: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/22.jpg)
Verticle
Module
1
0..n
0..n
1
System
vert.x
module1
verticle 1 verticle 2
module2
verticle 3 verticle 4
![Page 23: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/23.jpg)
import org.vertx.groovy.platform.Verticleclass DemoVerticle extends Verticle { @Override Object start() { container.deployModule("io.vertx~mod-web-server~2.0.0-final", container.config) }}
io.vertx~mod-web-server~2.0.0-final
![Page 24: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/24.jpg)
officialcommunity
生 ॐ СВОБОДА
![Page 25: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/25.jpg)
Verticle Verticle Verticle Verticle
Verticle Verticle Verticle Verticle
![Page 26: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/26.jpg)
Verticle Verticle Verticle Verticle
Verticle Verticle Verticle Verticle
Verticle Verticle Verticle Verticle
Verticle Verticle Verticle Verticle
Multicast
![Page 27: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/27.jpg)
mod 2mod 1CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
vertx -hamod ? mod ? mod ? mod ?
mod ? mod ? mod ? mod ?
![Page 28: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/28.jpg)
mod 2mod 1CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
vertx -hamod ? mod ? mod ? mod ?
mod ? mod ? mod ? mod ?
![Page 29: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/29.jpg)
mod 2mod 1CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
vertx -hamod ? mod ? mod ? mod ?
mod ? mod ? mod ? mod ?
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
![Page 30: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/30.jpg)
mod 2mod 1CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
vertx -hamod ? mod ? mod ? mod ?
mod ? mod ? mod ? mod ?
CPU
CPU
CPU
CPU
CPU
CPU
CPU
CPU
mod 2mod 1
![Page 31: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/31.jpg)
DEMO
![Page 32: Event loop](https://reader036.vdocument.in/reader036/viewer/2022081721/554f6e0cb4c905bb178b4fc6/html5/thumbnails/32.jpg)
Leseliste/Quellen
http://www.cs.wustl.edu/~schmidt/PDF/reactor-siemens.pdf http://tutorials.jenkov.com/java-nio/selectors.html https://www.ibm.com/developerworks/java/library/j-jtp02225/ http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ http://lmax-exchange.github.io/disruptor/ https://github.com/codepitbull/lockperformance