Fault-tolerant UDP
Lecture 03
UDP is not reliable
a packet can be lost in the IP network
the packet order may be modified by the IP network
Observation of faults ?
remote server (hard to set up)
locally, almost no losses
We use a proxy to simulate faults in the network.
it is an intermediary between client and server
it transfers requests from client to server
it transfers answers from server to client
it can introduce drops of packets, swaps, ...
Proxy (1/12)
Proxy (2/12)
Proxy (3/12)
Proxy (4/12)
Proxy (5/12)
Proxy (6/12)
Proxy (7/12)
Proxy (8/12)
Proxy (9/12)
Proxy (10/12)
Proxy (11/12)
Proxy (12/12)
Fault-tolerance
What can a client who did not receive its answer do?
His request was lost ?
The answer was lost ?
The answer/request was simply delayed ?
Only one mechanism : resend the request after a certain waiting time.
How long should we wait before sending again ?
Why not ask the server to send again ?
UDP client in Java
The method datagramChannel.receive()
is blocking by default.
If we are blocked waiting for an answer, we cannot resend.
We need two threads:
one thread to send
one thread to receive
Warning: obey the rules and principles seen in concurrent programming
Producer / consumer
A thread safe queue, used by both threads
Consumer waits until there is something to consume in the queue
Producer waits if there is no room in the queue
Avoids active waiting and limit the memory usage.
Producer = listener inserts the received packet in the queue.
Consumer = sender (main) polls (with a timeout) the received answers from the queue
The key is the ability to put a timeout on the consumer.
BlockingQueue
|
Throws exception |
Special value |
Blocks |
Times out |
Insert |
add(e) |
offer(e) |
put(e) |
offer(e, time, unit) |
Remove |
remove() |
poll() |
take() |
poll(time, unit) |
Examine |
element() |
peek() |
not applicable |
not applicable |
Waiting with a timeout before resending
Important points
Which implementation of BlockingQueue
ArrayBlockingQueue
? LinkedBlockingQueue
?
Maximal size ?
Do not share the same memory via the queue = data race?
Two threads, one DatagramChannel
: which thread halts the other? which thread closes the DatagramChannel ?