Upload
dennis-zhuang
View
15.426
Download
9
Embed Size (px)
DESCRIPTION
详细介绍了Java NIO编程的技巧和陷阱
Citation preview
2. Agenda
nio
Nio trick
Nio trap
3. IP
4. TCP
5. IO
IO
IO
IO
Unix5IO
IO
IO
IO
IO
IO
IO
IO
6. IO
7. IO
IO
IO
AIO
8. Java NIO
NIO = New I/O
NIO 1:JSR 51
JDK 1.4
http://jcp.org/en/jsr/detail?id=051
NIO 2:JSR203
JDK 7
http://jcp.org/en/jsr/detail?id=203
9. NIOBuffers
java.nio.buffer
ByteBuffer
capacitylimitpositionmark
0 mark position limit capacity
Direct ByteBuffer VS. non-direct ByteBuffer
Non-direct ByteBuffer
HeapByteBufferjava
byte[]JVM
Direct ByteBuffer
JVMnative
-XX:MaxDirectMemorySize=
10. NIO channels
Buffer
java.nio.channels.Channel
java.nio.channels.FileChannel
java.nio.channels.SocketChannel
java.nio.channels.ServerSocketChannel
Blocking or non-blocking
11. NIO selectors
java.nio.channels.Selector
IO
Seletable Channel
SelectionKey Selectorchannel
SelectionKeychannel
Selector.select SelectionKeychannel
Selected Keychannel
12. NIO
IO,IO0
block
IOzero-copy
IOjava
13. NIO
NIO =
NIO
0) {
//try to read from channel
}
}
finally {
if (tmpKey != null) {
tmpKey.cancel();
tmpKey = null;
}
if (readSelector != null) {
// Cancel the key.
readSelector.selectNow();
SelectorFactory.returnSelector(readSelector);
}
}
40.
channelinterestOP_WRITEIO
channel
if queue.isEmpty
if writeLock.tryLock&&
current.compareAndSet(null,msg)
write to channel
else
queue.offer(msg);
interest OP_WRITE
41.
SelectorOP_READOP_WRITEOP_ACCEPT
Reactor
Selector
while(true){
int sel=selector.select(timeout);
processRegister();
if(sel>0)
processSelected();
}
42. Reactor
Boss thread + worker thread
BossOP_ACCEPTOP_CONNECT
WorkerOP_READOP_WRITEIO
Reactor
Netty 1+2*cpu
Mina1+cpu+1
Grizzly 1+1
43.
OP_READOP_ACCEPTreactor
OP_ACCEPTreactorOP_READ
OP_READOP_ACCEPT
OP_READreactorOP_ACCEPT
44.
echounmashall
OP_ACCEPT
unmashallcpu-bound
reactor
45.
MinaNetty
46.
Java
GC
ThreadLocal cache
putString,getStringAPI
-BigEnd
-BigEnd
JavaIOclass-BigEnd
47.
j.u.c
lock-free,spin lock
OutOfMemoryError
GC
48.
PerTickBookkeepingtick
Tick
Selector.select(timeout)
Thread.sleep(timeout);
49.
O(1)
O(1)
PerTickBookkeepingexpire
O(n)
50.
expire
O(n)
O(1)
PerTickBookkeeping
O(1)
51.
expire
O(lg(n)
O(lg(n)) or O(1)
PerTickBookkeepingroot
O(1)
52. Hash Wheel Timer
tick
O(1)
O(1)
PerTickBookKeeping
O(1)
tick
53. Hierarchical Timing Wheel
54. IDLE
IdleIO
IdleIO
IdleIO
BothIO
IOIO
Idle
select(timeout)
Mina: select(timeout)
Netty: HashWheelTimer
55. TCP/IP
TCP/IP
OS
56. SocketSO_RCVBUF SO_SNDBUF
Socket.setReceiveBufferSize/setSendBufferSize
get
4k16K40%
SocketMSSMSS=MTU+40MTU=1500
MSS
MTU
send bufferreceive buffer
48K64KBDP
recv buffersend buffer
ServerSocketrecvRFC132364kacceptsocket
TCP
57. BDP
TCP
BDP= x RTT
58. NagleSO_TCPNODELAY
NAGLE
if there is new data to send
if the window size >= MSS and available data is >= MSS
send complete MSS segment now
else
if there is unconfirmed data still in the pipe
enqueue data in the buffer until an acknowledge is received
else
send data immediately
end if
end if
end if
(telnet
Socket. setTcpNoDelay(true);
,truefalse
59. SO_LINGER
Socket. setSoLinger(boolean linger, int timeout)
socket
:linger=false,timeout=-1
socketcloseCLOSINGFIN-ACKTIME_WAIT
Linger=true,timeout>0
closeFIN-ACK
Linger=true,timeout=0
hard-closeFIN-ACKRST"connection reset"SocketException
TIME_WAIT
TCP
=2*MSL
MSL302
60. SO_REUSEADDR:
Socket.setReuseAddress(boolean)
false
socket1TIME_WAITsocket2
SO_REUSEADDRport()IPIPAlias
SO_REUSEADDRUDPTCP
SO_REUSEPORT
ListenIPacceptweb server
Freebsdgooglelinux patch
http://pdxplumbers.osuosl.org/2010/ocw/proposals/489
http://kerneltrap.org/mailarchive/linux-netdev/2010/4/19/6274993
61.
Performance preference,JDK5
Socket.setPerformancePreferences(connectionTime,latency,bandwidth)
SO_KEEPALIVE
Socket.setKeepAlive(boolean)
TCPHTTPkeep-alive
falseTCP2
Socket.sendUrgentData(data)
62.
Mina1.5
FileChannel.transferTobug
Minatemporarily unavailableIOException0
OP_WRITEIO
channel.write
63.
ACE:Accept > Write > Read
MinaNetty: Read > Write
ACE
select
select
wakeup
64. Javasocket
OSsocketJava Socket
Java
Fundamental NetWorking in java
65. 66. 67. 68. 69. 70.
NIO?
71. 1key
select0Selector.selectedKeys
Iterator it=set.iterator();
While(it.hasNext()){
SelectionKey key=it.next();
it.remove(); //
}
key,CPU 100%
72. 2Selectorkey
Selector.selectedKeys/keys
Selector.selectedKeys
Selector.keys
selected keys
73. 3Channelinterest
channel.register(selector, ops, attachment);
,
ChannelregLock
Selectorkey
:reactor
If(isReactorThread()){
channel.register(selector, ops, attachment);
}
else{
register.offer(new Event(channel,ops,attachment));
selector.wakeup();
}
74. 3Channelinterest
SelectionKey.interest(ops)
linuxselector
win32
channel
if (this.isReactorThread()) {
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
}
else {
this.register.offer(new Event(key,SelectionKey.OP_READ));
selector.wakeup();
}
75. 4:OP_WRITE
OP_WRITECPU 100%
OP_WRITE
:interestOP_WRITE
:
socket
channel
76. 5channel
SelectableChannel
channel.close()key.cancel()
key.cancel();
channelchannel
key.cancel()keycancelledKeys
select
channelsocketfdclose(fd)
77. 5channel
select
channelselectselector
sockfdCLOSE_WAIT
reactor,wakeupselect
selector.selectNow()
Netty256selectNow
78. 5Channelnetty
staticfinalintCLEANUP_INTERVAL=256;
privatebooleancleanUpCancelledKeys()throwsIOException{
if(cancelledKeys>=CLEANUP_INTERVAL){
cancelledKeys=0;
selector.selectNow();
returntrue;
}
returnfalse;
}
//channel
channel.socket.close();
cancelledKeys++;
79. 6OP_ACCPETOP_READOP_CONNECTOP_WRITE
:readwrite
Java NIOOP_ACCEPTOP_CONNECT
OP_ACCEPTOP_READ == Read
OP_CONNECTOP_WRITE == Write
OP_ACCEPTOP_READ ,OP_CONNECTOP_WRITE
80. 7connect
SocketChannel.connectfalse
loopbacktrue
false
channelselector,OP_CONNECT
OP_CONNECTSocketChannel.finishConnect
connect
finishConnect
OP_CONNECTOP_CONNECTSelectionKeyCPU
OP_CONNECTchannel
81. 8NIObug
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933
http://bugs.sun.com/view_bug.do?bug_id=6693490
select(timeout)CPU100%
jdk 6u4
82. 8NIObug
bug?
channel.close()select
Jetty 6
select
interest0key
Selectorkey
selectwakeup
:wakeup
Minabug
Netty3selectNow
83. 8NIObug
JDK
84.
nio
NIOMinaNetty3xSocket
JDK
javabug database
85. NIO
Mina: filter chain
Netty: pipeline
SEDA
API
xSocketAPIAPI
codeccodec
86. NIO
yield/resume
transform
NIO + =
kilim
87. Task task = new Task() {
@Override
public void execute() throws Pausable, Exception {
HttpClient client = new HttpClient();
HttpResponse resp = client.get(http://www.taobao.com);
String body=resp.content()
}
};
task.start();
KilimHttpClient
1HttpClient.gettaskresumetask
2Task
88. NIO 2.0
API
FileSystem
Path
Directory
FileVisitor
Attribute
WatchService
http://java.sun.com/developer/technicalArticles/javase/nio/
Java AIO
Windows:IOCP
Linux:epoll
NIO
AIO
win32
89. QA
90. Thank you