SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●

Slides:



Advertisements
Similar presentations
System Integration and Performance
Advertisements

© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 7 Protocol Software On A Conventional Processor.
What's inside a router? We have yet to consider the switching function of a router - the actual transfer of datagrams from a router's incoming links to.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
System Programming Practical session 12 Reactor.
3.5 Interprocess Communication
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
Computer Science Lecture 2, page 1 CS677: Distributed OS Last Class: Introduction Distributed Systems – A collection of independent computers that appears.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 7 Input/Output. Input/Output Problems Wide variety of peripherals —Delivering different amounts of data —At different speeds —In different formats.
1 I/O Management in Representative Operating Systems.
INPUT-OUTPUT ORGANIZATION
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Practical Session 11 Multi Client-Server Java NIO.
SPL/2010 Protocols 1. SPL/2010 Application Level Protocol Design ● atomic units used by protocol: "messages" ● encoding ● reusable, protocol independent,
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
1-1 Embedded Network Interface (ENI) API Concepts Shared RAM vs. FIFO modes ENI API’s.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
Swapping to Remote Memory over InfiniBand: An Approach using a High Performance Network Block Device Shuang LiangRanjit NoronhaDhabaleswar K. Panda IEEE.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Practical Session 11 Multi Client-Server Java NIO.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Events in General. Agenda Post/wait technique I/O multiplexing Asynchronous I/O Signal-driven I/O Database events Publish/subscribe model Local vs. distributed.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
IT3002 Computer Architecture
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Ying Huang, Marc Sentany Department of Computer Science.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
Copyright © 2004, Keith D Swenson, All Rights Reserved. OASIS Asynchronous Service Access Protocol (ASAP) Tutorial Overview, OASIS ASAP TC May 4, 2004.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
1 Network Communications A Brief Introduction. 2 Network Communications.
Communication in Distributed Systems. . The single most important difference between a distributed system and a uniprocessor system is the interprocess.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Chapter 3 Internet Applications and Network Programming
Last Class: RPCs and RMI
Application Protocols
Programming Models for Distributed Application
ORLEANS Networking Overview
Lecture 11 Socket Programming.
Reactor Design Pattern
Inter Process Communication (IPC)
Operating Systems Chapter 5: Input/Output Management
CS703 - Advanced Operating Systems
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Block 2: Interprocess Communications
Chapter 4: Threads.
Exceptions and networking
The reactor design pattern
Thread per client and Java NIO
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

SPL/2010 Reactor Design Pattern 1

SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ● complications due to asynchronous nature ● impact on message parsing algorithms ● Reactor design pattern ● generic server ● more scalable than our earlier solutions … 2

SPL/2010 Sockets ● Socket API: interaction with RTE when a process requires communication services. ● three properties of Sockets: 1. ServerSockets: accept incoming connections. 2. OutputStream: send bytes through Socket 3. InputStream: receive bytes through Socket 3

SPL/2010 blocking IO operations ● control does not return to calling thread until operation terminates. ● accept() - calling thread is blocked until new connection established. ● write(byte [] buffer) - calling thread is blocked until all buffer sent to network. ● read(byte [] buffer) - calling thread is blocked until buffer.length is received. 4

SPL/2010 Drawbacks server for multiple clients needs: ● thread for each client ● thread to accept (new) incoming connections. ● scalability problems: resources of server process (threads, memory, CPU) increase linearly by number of clients 5

SPL/2010 Goals - design scalable and robust servers ● More scalable - grow while using bounded amount of resources 6

SPL/2010 Non-blocking IO ● Check socket has some data available to read. ● Non-block read available data from socket. Return with any amount of data. ● Check socket can send some data. ● Non-block write data to socket. Return immediately. ● Check new connection is requested. If so, accept it, without blocking! 7

SPL/2010 RTE perspective ● partition solution in two logical parts: 1. Readiness notification 2. Non-blocking input output. ● Modern RTEs supply both mechanisms: ● Is data is available for read in socket ● Is socket ready to send some data ● Is there new connection pending for socket ● Non-blocking interface to read and write 8

SPL/2010 output buffer ● to understand how non-blocking operations work - understand how RTE internally manages IO – buffer associated with each socket – write to socket - RTE copies bytes to internal buffer – RTE proceeds to send bytes from buffer to network. 9

SPL/2010 Block vs. Non-block Write ● network slower than process - output buffer fills more quickly than RTE sends bytes to network. ● if output buffer is full - RTE blocks process writes until output buffer has enough free space for data. ● non-blocking write - RTE copies bytes from process as possible – notifies process how many bytes have been copied. – if bytes need to be re-written, process responsible to re-invoke the write operation 10

SPL/2010 Block vs. Non-block Read ● RTE need to receive bytes over network, ● deliver these bytes to our process. ● RTE buffer bytes until process actually requests them using a read operation. 11

SPL/2010 input buffer ● allocated to each socket ● RTE stores incoming bytes to input buffer ● when process read from socket, RTE copies the from socket's input buffer to process buffer. ● if process request more bytes than available, RTE blocks until enough bytes. ● non-block - RTE copy bytes available in socket's input buffer, and notify number of bytes copied. 12

SPL/2010 Input Buffer Overflow ● input buffer has limited space ● input overflowed - process reads data more slowly than data arrives from network. ● if input buffer full, RTE discard new data arriving from network ● sending side will retransmit the data later? 13

SPL/2010 Java's NIO Package ● Java's interface to non-blocking IO and readiness notification services of RTE ● provides wrapper classes for readiness notification and non-blocking IO. 14

SPL/2010 Channels ● connections to I/O entities ● Represent data source, destination ● Examples: ServerSocketChannel SocketChannel, FileChannel ● default - new channels are in blocking mode ● must be set manually to non-blocking mode. 15

SPL/2010 Channels ● methods for writing and reading bytes. ● ServerSocketChannel provides accept() ● returns a SocketChannel (similar to accept() method of ServerSocket class). 16

SPL/2010 Selectors ● implements readiness notification. ● channels may be registered to a selector for specific readiness events ● read /write /accept ● selector can be polled to get list of ready channels 17

SPL/2010 Selectors ● channel ready for read guarantees that a read operation will return some bytes. ● channel ready for write guarantees that a write operation will write some bytes ● channel ready for accept guarantees that accept() will result in a new connection. 18

SPL/2010 Selectors ● Selector class abstracts service given by os under the sys. call select (epoll). ● receives a collection of sockets (and blocks) until one ready for reading or writing. ● When call returns, caller is informed with ready sockets. 19

SPL/2010 Buffers ● wrapper classes used by NIO to represent data interchanged through a Channel. ● usually backed by array types. ● Example: SocketChannels use ByteBuffer class for sending and receiving bytes. 20

SPL/2010 Charsets ● together with associated decoders and encoders - translate between bytes and Unicode characters; 21

SPL/2010 Reactor Design Pattern 22

SPL/2010 Reactor features ● solve scalability problems we encountered before. ● employs non-blocking IO. ● maintains a set of sockets, using a selector, reactor polls for readiness. ● For each socket, reactor attaches state. 23

SPL/2010 Reactor IO ● if bytes ready to read from socket, reactor read bytes and transfer to protocol (previous lecture) ● if socket is ready for writing, reactor checks if write request - if so, reactor sends data ● reactor is accepting new connections. 24

SPL/2010 Reactor Design ● reactor composed of: ● 3 classes ● main thread ● thread pool. ● abstract protocol and message format behind interfaces 25

SPL/2010 Main Reactor thread ● main reactor thread performs the following: 1. Create new thread pool (executor). 2. Create new ServerSocketChannel, bind to port. 3. Create new Selector. 4. Register ServerSocketChannel in Selector, asking for accept readiness. 5. While(true) - wait for selector notifications 26

SPL/2010 Main Reactor thread For each notification event check: ● Accept notification - server socket is ready to accept new connection - call accept – new socket created - register socket in Selector. ● Write notification - socket ready for writing, if protocol ask to write - write bytes to socket. ● Read notification - socket ready for reading, read bytes and pass them to protocol handler. 27

SPL/2010 pool thread ● actual work done by protocol will be achieved with the use of thread pool; ● protocol processing is assigned as task for pool. 28

SPL/2010 Reactor classes: ReactorData ● encapsulates internal state of reactor need outside of reactor. Contains references to: ● Selector ● executor service (pool) ● protocol factory (produces MessagingProtocol objects - attached to new client). 29

SPL/2010 Reactor classes: ConnectionAcceptor ● accepting incoming connections, creating connection Handler 30

SPL/2010 Reactor classes: ConnectionHandler ● reading/writing bytes from/to sockets. ● bytes read are passed to associated protocol object, which, in turn, updates ConnectionHandler with outgoing bytes. ● performs initializations upon creation – setting new socket to non-blocking mode – creating new protocol object – registering new socket in selector. 31

SPL/2010 difference between reactor and one-thread- per-connection ● ConnectionHandler: ● passive object (instead active object) ● methods are executed by main thread of Reactor in reaction to events relayed by selector ● methods don’t block –execute very fast - copying bytes from one buffer to another. ● parsing/processing messages is delegated to active objects ProtocolTask ● submitted to thread pool executor 32

SPL/2010 Cont’d ● must maintain direct mapping between socket channels and their associated handlers. ● Selector class allows to attach arbitrary object to channel, which can later be retrieved, ● we associate ConnectionAcceptor with the ServerSocketChannel ● we associate ConnectionHandler with socket created when accepting new connection. 33

SPL/2010 Synchronization? ● assume client initiates connection by sending message first ● newly created socket is registered in the selector for read notifications only. ● after some bytes has been received and the protocol wants to reply ● socket registration must change to read/write. ● After all bytes were written, socket is returned to read only notifications. 34

SPL/2010 Reactor Overview ● Reactor diagram depicts the messages sent by reactor object once it starts running. ● reactor is run by a thread we call ReactorThread ● Selector response to new connection or read/write readiness of its bound socket as an event. ● 3 diagrams with the sequence of messages after an event took place. 35

SPL/2010 Reactor Overview ● event handling is done by two threads: 1. ReactorThread pulls the bytes from socket and places them in a buffer. 2. Thread-pool thread: – processes bytes using a tokenizer and protocol. – writes the protocol response back to the connection handler outgoing buffer 36

SPL/

SPL/2010 The Reactor Class ● Reactor is an active object. ● the heart of the architecture - connects other components and triggers operations. ● key components of the Reactor are: ● selector ● thread pool executor 38

SPL/2010 The Reactor Class ● Reactor thread listens to events from selector. ● Initially, only a ServerSocketChannel is connected to the selector. ● Reactor can only react to accept events. 39

SPL/2010 The Reactor Class ● run() method dispatches events from selector, ● reacts by invoking methods of either Accept or ConnectionHandler passive objects. ● attaching new client socket channels to selector ● detaching when client disconnects. ● performed by the Acceptor object. 40

SPL/

SPL/

SPL/