what every java developer should know about network?

39
Things that every Java developer should know about network Alexey Ragozin [email protected]

Upload: aragozin

Post on 12-Jan-2017

684 views

Category:

Software


2 download

TRANSCRIPT

Things that every Java developer should know about network

Alexey Ragozin [email protected]

Good old TCP

Transmission Control Protocol

• TCPv4 (RFC 793) dated 1981

• Stream oriented (in order delivery)

• Reliable (retransmission support)

• Congestion control

Designed to tolerate conditions of post thermonuclear war environment.

Accepting connections

Server

Client

ServerSocket socket = new ServerSocket(); socket.bind(bindAddr); while(true) { Socket conn = socket.accept(); processConnection(conn); }

Socket conn = new Socket();

conn.connect(servAddr); processConnection(conn);

Accepting connections

Alice Bob

connect

accept

bind

?

Accepting connections

Quiz Server socket is bound, but Socket.accept() is not called. What will happen on client attempting to connect to this socket?

1. “Connection refused” error

2. “Connection timeout” error

3. Connection will be established

Closing TCP connection

Quiz In this example, what will happen on server?

1. Server will receive full message.

2. Server may receive partial message.

3. Server will get socket error exception.

Socket conn = new Socket(); conn.connect(servAddr); byte[] msg = encodeMessage(); conn.getOutputStream().write(msg); conn.close();

Closing TCP connection

Alice Bob

write

close

read ?

Closing TCP connection

?

Alice Bob

write

close

read

bu

ffe

r

Closing TCP connection

• FIN – graceful shutdown

All data are delivered

• RST – connection reset

Connection terminated in unknown state

What happens of Socket.close()?

Closing TCP connection

Normal way

• OS will keep connection after close until all buffered data is sent.

• Connection may remain in TIME_WAIT for sometime after that.

SO_LINGER enabled

• OS will keep connection sending data for some timeout. If timeout is reached connection will be reset.

TCP connection states

https://en.wikipedia.org/wiki/Transmission_Control_Protocol

Socket errors in Java

Socket errors Socket read return -1 – graceful stream shutdown

java.net.SocketException: Connection reset

java.net.SocketTimeoutException: Read timed out

java.net.SocketException: Connection reset by peer: socket write error

java.net.ConnectException: Connection refused: connect

java.net.ConnectException: Connection timed out: connect

Socket errors in Java

Socket errors Socket read return -1 – graceful stream shutdown

java.net.SocketException: Connection reset

java.net.SocketTimeoutException: Read timed out

java.net.SocketException: Connection reset by peer: socket write error

java.net.ConnectException: Connection refused: connect

java.net.ConnectException: Connection timed out: connect

TCP Sliding Window

Nagle’s algorithm

Wait for 200ms to send under sized segment Use TCP_NODELAY to disable

TCP sliding window

Sender window size is controlled by receiver

Alice Bob

ACK x

Wait for ACK

Resume

transmission

sq=x+1

n bytes

sq=x+n+1

n bytes

sq=x+2*n+1

n bytes

Se

nd

win

do

w

ACK x+3*n

TCP Receiver Buffer Size

Default receiver buffer size – 64k

• RTT Moscow – New York 160ms

• Sending 64k every 160ms -> 400KiB/s

SO_RCVBUF – receiver buffer size

• Increase buffer on receiving side

TCP Slow Start

TCP slow start – congestion control algorithm

• Initial window size is small

• Window size is growing gradually

• Window size is trimmed down

Packet loss event

Application stops sending data

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

Threshold

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

Threshold

Congestion

avoidance

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

Threshold

Congestion

avoidance 3 d

uplic

ate

AC

Ks

TCP Slow Start

https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm

Slid

ing

win

do

w

T

Quadra

tic

Tim

eout

Threshold

Congestion

avoidance 3 d

uplic

ate

AC

Ks

TCP Slow Start

Socket options summary

Server socket • Server socket backlog size

• SO_REUSEADDRESS

Server and client • SO_RCVBUF 1

• SO_SNDBUF 2

• SO_LINGER

• TCP_NODELAY

• SO_TIMEOUT 1 - On Linux, receiver buffer size is capped on OS level.

Look for net.core.rmem_max sysctrl parameters.

2 - Modern OSes (Linux 2.4, NT 6) use auto tuning for send buffer

Socket coding in Java

Quiz How long TCP connection may stay open without sending any packets?

1. Double round trip time.

2. 30 seconds.

3. 1 Hour.

4. 99 years.

Slow morning of JDBC pool

Observation Each morning very first request to Oracle takes 5 minutes.

Slow morning of JDBC pool

Observation Each morning very first request to Oracle takes 5 minutes.

Cause TCP connection stays overnight.

Firewall in the middle cleans connection from connection table after few hours of inactivity.

Write to connection fails after TCP timeout (30 sec).

JDBC pool attempts to validate connection from pool , validation fails with write timeout

10 connections * 30 sec = 5 minutes

Slow morning of JDBC pool

Solution

Use SO_TIMEOUT for fast invalidation stale connections

Background connection validation is also helpful

Some pools (DBCP) have special timeout for validation query

TCP Keep Alive Option

TCP has keep alive option

SO_KEEPALIVE

Heartbeat interval is OS wide

Default is 2 hours

Useless

Testing for network hazards

Linux traffic control (TC) – simulating hazards

• Latency

• Bandwidth limitation

• Packet loss

WANEM (web front end to TC)

• http://wanem.sourceforge.net/

Loopback IP addresses

Quiz If server socket is bound to 127.0.0.1, is it possible to use public host’s IP to connect?

1. Yes 2. No

If server socket is bound to local public IP, is it possible to 127.0.0.1 to connect?

1. Yes 2. No

Loopback IP addresses

Quiz How many loopback addresses (connection to this address will use loopback interface) are available on typical server?

1. One

2. Number of interfaces + one

3. 16 millions

Loopback IP addresses

Each of 127.*.*.* is treated as individual address routed to loopback interface.

You can bind multiple processes to same port but using different loopback addresses.

Accidental IPv6 deployment

IPv6 is here for ages

No body cares

Until accidently switching over

Use

-Djava.net.preferIPv4Stack=true

to get back to your network.

Beyond TCP

UDP – User Datagram Protocol

• No handshake

• No retransmission

• No guaranties

UDP

Reasons to go UDP • Multicast support • Packet loss is acceptable • Latency is critical

But be beware of • MTU sizing • Flow / congestion control

Out of control UDP flow can easily disrupt TCP communications

• OS tuning Monitor receiver error rates on interfaces

THANK YOU

Alexey Ragozin

[email protected]

http://blog.ragozin.info