1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
// Author: Bela Ban
Concurrent initial connection establishment in ConnectionTable
==============================================================
[JIRA: http://jira.jboss.com/jira/browse/JGRP-549]
If we have members A and B, and they don't yet have connections to each other, and start sending messages to each other
at the exact same time, then we run into the following scenario:
- A attempts to connect to B (new Socket())
- B attempts to connect to A
- A accepts B's connection and adds an entry for B into its table
- B adds an entry for A into its table
- B gets the accept() from A, but doesn't add the new Connection into its table because there is already a Connection.
This leads to a spurious Connection object, which will only be closed and garbage collected once A or B leaves the cluster
- We used to close Connections which were already in the table, but this lead to ping-pong effects where concurrent
initial senders always closed each others connections
- Even worse: a new Socket() always creates a new Connection object and places it into the table, *regardless* of
whether a Connection for a given destination already exists or not !
SOLUTION:
We need to have a deterministic algorithm to pick a 'winner' for concurrent connections.
Let's assume our own address is A
On connect to B:
----------------
- Lock CT
- If connection exists for B: return existing connection
- Else:
- Connect(B)
- Add connection to CT
- Unlock CT
On connection accept from B:
----------------------------
- Lock CT
- If there is no key for B, we create a connection for B and add it to the CT
- Else:
- If B's address > A's address --> remove the existing entry for B (closing the connection !) and add
the newly created connection for B to the CT
- Else close the connection which resulted from accept(B)
- Unlock CT
|