The reactor design pattern

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Intro to Computer Org. Pipelining, Part 2 – Data hazards + Stalls.
Processes CSCI 444/544 Operating Systems Fall 2008.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
System Programming Practical session 12 Reactor.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Fundamentals of Python: From First Programs Through Data Structures
Practical Session 11 Multi Client-Server Java NIO.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Apache Mina Dima Ionut Daniel. Contents What is Apache Mina? Why Apache Mina? Mina Architecture Mina Core Mina Advanced JMX Support Spring Integration.
CHAPTER 2: COMPUTER-SYSTEM STRUCTURES Computer system operation Computer system operation I/O structure I/O structure Storage structure Storage structure.
CHEN Ge CSIS, HKU March 9, Jigsaw W3C’s Java Web Server.
The Alternative Larry Moore. 5 Nodes and Variant Input File Sizes Hadoop Alternative.
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
Practical Session 11 Multi Client-Server Java NIO.
Architectural pattern: Reactor Source: POSA II pp 179 – 214POSA II Environment: an application that receives multiple requests simultaneously but may process.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
Threads. Readings r Silberschatz et al : Chapter 4.
Interrupts.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
TURN TCP Rohan Mahy Why split out TCP peers from TURN? Head of line blocking problem and fair sharing –No consensus on best way to solve.
More Project 1 and HW 1 stuff! Athula Balachandran Wolfgang Richter
Input/Output Device Drivers
Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir. A.C. Verschueren Eindhoven University of Technology Section of Digital.
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Processes and threads.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Computer Architecture
Rohan Mahy TURN TCP Rohan Mahy
Grizzly 2.0 Initial propose overview
Lecture 21 Concurrency Introduction
Switching Techniques In large networks there might be multiple paths linking sender and receiver. Information may be switched as it travels through various.
Netty.
Application Protocols
Java Byte IPC: Part 6-Summary
Implementation CAN Communication Engine
Reactor Design Pattern
Cache Coherence Protocols:
Cache Coherence Protocols:
Cs561 Presenter: QIAOQIAO CHEN Spring 2012
The command invocation protocol
MARIE: An Introduction to a Simple Computer
Cancellation.
Switching Techniques.
Process Description and Control
Process Description and Control
Threaded Programming in Python
Process Description and Control
P1 : Distributed Bitcoin Miner
Concurrency: Processes CSE 333 Summer 2018
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
NETWORK PROGRAMMING CNET 441
January 15, 2004 Adrienne Noble
Chapter 5 멀티스레드 u-Network Design Lab 4.
Processes Creation and Threads
Lecture 23: Transactional Memory
CSE 451 Section 1/27/2000.
CS703 – Advanced Operating Systems
Chapter 13: I/O Systems.
CSE 332: Concurrency and Locks
Message Passing Systems Version 2
Exceptions and networking
More concurrency issues
Thread per client and Java NIO
Synchronization and liveness
Presentation transcript:

The reactor design pattern SPL – PS11 The reactor design pattern

Overview Why use the reactor pattern Selector The reactor

Why use the reactor design pattern We saw in the previous PS the thread per-client server. The main disadvantages of the TPC server are: Wastefulness – Creating new threads is relatively expensive, each thread requires a fair amount of memory, threads are blocked most of the time waiting for IO. Not scalable – A TPC server cannot grow to accommodate tens of thousands of requests. Poor availability – Creating a new thread for each client takes a fair amount of time, the response time degrades as the number of clients increase.

What is the reactor design pattern Use non-blocking IO, so threads don’t waste time waiting for IO. Have one thread in charge of the network – accepting new connections and handling IO. Have a fixed number of threads which are in charge of the protocol. This threads will be responsible for encoding and decoding messages, and processing them. Unlike in the TPC server, each thread will handle multiple clients.

Selector When we perform non-blocking IO, read for example read the currently available bytes, and returns the number of bytes read. That means that read/write can return 0, and probably would do that most of the time. We would like to have a way for our thread to wait until any of our channels is ready, and only then perform the read(),write() or accept(), only on the ready channel.

Selector (cont) The Selector registers itself to a channel. Registration means that the channel informs the Selector when some event happens. Such event can be – “The channel is ready to be read”, or “The channel can now be written to”, or “The channel can now accept” During registration, the channel is told which events to monitor. In addition when registering a Selector to a channel, one can add an attachment, which is an object we will have access to when the event occurs.

Selector registration Registration can be done as follows: The data structure the Selector maintains looks like this:

Bitmasks We saw in the earlier slide that the Selector maintains a field “Interested Ops” for every key. SelectionKey.OP_READ is a number representing a read event. Similarly, SelectionKey.OP_WRITE is a number represents a write event. How would we represent a read or write event? We could use bitmasks, We’ll define values of the flags so their 1-bits won’t overlap. Read or write will be OP_READ | OP_WRITE. When checking for read event, it’s enough to ask e & OP_READ.

Selector’s select() The Selector holds 3 lists of selection keys: key – A set of all the selection keys in the Selector. selected-key – A set of all the selection keys with a ready operation in the Selector. cancelled-key – A set of all the selection keys that were cancelled but not yet unregistered in the selector. Once a Selector is registered to some channel, one can use the “select()” method. The method blocks until at least one of the channels is ready for the registered event. Then, a set of SelectionKeys is returned.

The Reactor Now that we know how Java’s non-blocking IO works, we’ll take a closer look at the Reactor implementation which employs it. We will concentrate on the “core” reactor classes – Reactor, NonblockingConnectionHandler, ActorThreadPool. The Reactor is the main thread, which is tasked with reading, writing, and accepting new connections. The reactor also holds a thread pool of workers, these workers are in charge of the protocol handling.

The Reactor (cont)