Scalable IO in Java -Doug Lea

Scalable IO in Java  -Doug Lea


Scalable network services
" Event-driven processing
" Reactor pattern
Basic version
Multithreaded versions
Other variants
" Walkthrough of java.nio nonblocking IO APIs




Network Services
Web services, Distributed Objects, etc
Most have same basic structure:
Read request
Decode request
Process service
Encode reply
Send reply
But differ in nature and cost of each step
XML parsing, File transfer, Web page
generation, computational services, ...


Classic Service Designs
  Each handler may be started in its own thread


  
 Scalability Goals
" Graceful degradation under increasing load (more clients)
" Continuous improvement with increasing
resources (CPU, memory, disk, bandwidth)
" Also meet availability and performance goals
Short latencies
Meeting peak demand
Tunable quality of service
" Divide-and-conquer is usually the best
approach for achieving any scalability goal


Divide and Conquer
" Divide processing into small tasks
Each task performs an action without blocking
" Execute each task when it is enabled
Here, an IO event usually serves as trigger
Basic mechanisms supported in java.nio
Non-blocking reads and writes
Dispatch tasks associated with sensed IO events
" Endless variation possible
A family of event-driven designs
Event-driven Designs
" Usually more efficient than alternatives
Fewer resources
" Don't usually need a thread per client
Less overhead
" Less context switching, often less locking
But dispatching can be slower
" Must manually bind actions to events
" Usually harder to program
Must break up into simple non-blocking actions
" Similar to GUI event-driven actions
" Cannot eliminate all blocking: GC, page faults, etc
Must keep track of logical state of service
Background: Events in AWT
Event-driven IO uses similar ideas but in different designs

Reactor Pattern
" Reactor responds to IO events by dispatching
the appropriate handler
Similar to AWT thread
" Handlers perform non-blocking actions
Similar to AWT ActionListeners
" Manage by binding handlers to events
Similar to AWT addActionListener
" See Schmidt et al, Pattern-Oriented Software
Architecture, Volume 2 (POSA2)
Also Richard Stevens's networking books, Matt
Welsh's SEDA framework, etc


Basic Reactor Design
java.nio Support
" Channels
Connections to files, sockets etc that support
non-blocking reads
" Buffers
Array-like objects that can be directly read or
written by Channels
" Selectors
Tell which of a set of Channels have IO events
" SelectionKeys
Maintain IO event status and bindings


Multithreaded Designs
" Strategically add threads for scalability
Mainly applicable to multiprocessors
" Worker Threads
Reactors should quickly trigger handlers
" Handler processing slows down Reactor
Offload non-IO processing to other threads
" Multiple Reactor Threads
Reactor threads can saturate doing IO
Distribute load to other reactors
" Load-balance to match CPU and IO rates


Worker Threads
" Offload non-IO processing to speed up
Reactor thread
Similar to POSA2 Proactor designs
" Simpler than reworking compute-bound
processing into event-driven form
Should still be pure nonblocking computation
" Enough processing to outweigh overhead
" But harder to overlap processing with IO
Best when can first read all input into a buffer
" Use thread pool so can tune and control
Normally need many fewer threads than clients
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值