-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix racy use of ConcurrentHashMap #36603
Fix racy use of ConcurrentHashMap #36603
Conversation
ConcurrentHashMap does not always behave correctly if removing elements and concurrently checking for its emptyiness. Work around this by protecting all usages with a mutex (there was only one usage unprotected by the mutex anyway) and then we don't even need a ConcurrentHashMap at all.
Pinging @elastic/es-distributed |
The racy use of
|
for (final Peer peer : peersByAddress.values()) { | ||
peersRemoved = peer.handleWakeUp() || peersRemoved; // care: avoid short-circuiting, each peer needs waking up | ||
final List<TransportAddress> peersAddressesToRemove = new ArrayList<>(); | ||
for (final Entry<TransportAddress, Peer> addressAndPeer : peersByAddress.entrySet()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe simpler to just write
final boolean peersRemoved = peersByAddress.values().removeIf(Peer::handleWakeUp);
and then further below just
return peersRemoved;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very slick. I pushed 96b02b0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ConcurrentHashMap does not always behave correctly if removing elements and
concurrently checking for its emptyiness. Work around this in PeerFinder by
protecting all usages with a mutex (there was only one usage unprotected by the
mutex anyway) and then we don't even need a ConcurrentHashMap at all.