Event loop

1,274 views

Published on

A presentation to show the workings of the event loop and how vert.x uses this pattern.

Published in: Technology

Event loop

  1. 1. Event Loop + Vert.x
  2. 2. Chief Developer @ Senacor Technologies AG! http://www.senacor.com! jochen.mader@senacor.com! Twitter: @codepitbull Jochen Mader
  3. 3. CPU ABC 42 1 2 3 4 5 6 7 8 9 0
  4. 4. CPU
  5. 5. 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
  6. 6. volatile CyclicBarrier AtomicBoolean synchronized ReentrantLock AtomicInteger AtomicCafe Thread ThreadPoolExecutor Runnable Callable
  7. 7. wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf
  8. 8. wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf wooble http://www.wooble.wtf
  9. 9. C10K ZZZZZZZZZz ZZZZZZZZZz ZZZZZZZZZz
  10. 10. 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
  11. 11. I/O Acces 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/
  12. 12. contentionI/O
  13. 13. Channel A Channel B Channel C Channel D Selector Event Handler 1 Event Handler 2 Event Handler 3 Event Handler 4 Event Handler 5 REACTOR DO NOT BLOCK! JAVA NIO
  14. 14. 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();
 }
 }
  15. 15. I NEEEEDS ! NICE API!!!!
  16. 16. Channel A Channel B Channel C Channel D Selector Event Handler 1 Event Handler 2 Event Handler 3 Event Handler 4 Event Handler 5 12.10.2012
  17. 17. Noch ! hübscher?
  18. 18. Quasar
  19. 19. 生 ॐ СВОБОДА
  20. 20. Micro Services (Martin Fowler) http://martinfowler.com/articles/microservices.html
  21. 21. 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());
 });
 }
 
 }
  22. 22. Verticle Module 1 0..n 0..n 1 System vert.x module1 verticle 1 verticle 2 module2 verticle 3 verticle 4
  23. 23. import org.vertx.groovy.platform.Verticle
 
 class 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
  24. 24. official community 生 ॐ СВОБОДА
  25. 25. Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle
  26. 26. Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Verticle Multicast
  27. 27. mod 2mod 1 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 vertx -ha mod ? mod ? mod ? mod ? mod ? mod ? mod ? mod ?
  28. 28. mod 2mod 1 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 vertx -ha mod ? mod ? mod ? mod ? mod ? mod ? mod ? mod ?
  29. 29. mod 2mod 1 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 vertx -ha mod ? mod ? mod ? mod ? mod ? mod ? mod ? mod ? CPU CPU CPU CPU CPU CPU CPU CPU
  30. 30. mod 2mod 1 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 vertx -ha mod ? mod ? mod ? mod ? mod ? mod ? mod ? mod ? CPU CPU CPU CPU CPU CPU CPU CPU mod 2mod 1
  31. 31. DEMO
  32. 32. 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

×