Skip to content

Fix racy use of ConcurrentHashMap #36603

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions server/src/main/java/org/elasticsearch/discovery/PeerFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.elasticsearch.cluster.coordination.Coordinator.isZen1Node;
import static org.elasticsearch.cluster.coordination.DiscoveryUpgradeService.createDiscoveryNodeWithImpossiblyHighId;
import static java.util.Collections.emptyList;
import static org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap;

public abstract class PeerFinder {

Expand Down Expand Up @@ -95,7 +95,7 @@ public abstract class PeerFinder {
private volatile long currentTerm;
private boolean active;
private DiscoveryNodes lastAcceptedNodes;
private final Map<TransportAddress, Peer> peersByAddress = newConcurrentMap();
private final Map<TransportAddress, Peer> peersByAddress = new LinkedHashMap<>();
private Optional<DiscoveryNode> leader = Optional.empty();
private volatile List<TransportAddress> lastResolvedAddresses = emptyList();

Expand Down Expand Up @@ -150,6 +150,7 @@ protected final boolean holdsLock() {
}

private boolean assertInactiveWithNoKnownPeers() {
assert holdsLock() : "PeerFinder mutex not held";
assert active == false;
assert peersByAddress.isEmpty() : peersByAddress.keySet();
return true;
Expand Down Expand Up @@ -256,11 +257,7 @@ private Peer createConnectingPeer(TransportAddress transportAddress) {
private boolean handleWakeUp() {
assert holdsLock() : "PeerFinder mutex not held";

boolean peersRemoved = false;

for (final Peer peer : peersByAddress.values()) {
peersRemoved = peer.handleWakeUp() || peersRemoved; // care: avoid short-circuiting, each peer needs waking up
}
final boolean peersRemoved = peersByAddress.values().removeIf(Peer::handleWakeUp);

if (active == false) {
logger.trace("not active");
Expand Down Expand Up @@ -344,7 +341,6 @@ boolean handleWakeUp() {
assert holdsLock() : "PeerFinder mutex not held";

if (active == false) {
removePeer();
return true;
}

Expand All @@ -358,7 +354,6 @@ boolean handleWakeUp() {
}
} else {
logger.trace("{} no longer connected", this);
removePeer();
return true;
}
}
Expand Down Expand Up @@ -394,18 +389,13 @@ public void onResponse(DiscoveryNode remoteNode) {
@Override
public void onFailure(Exception e) {
logger.debug(() -> new ParameterizedMessage("{} connection failed", Peer.this), e);
removePeer();
synchronized (mutex) {
peersByAddress.remove(transportAddress);
}
}
});
}

void removePeer() {
final Peer removed = peersByAddress.remove(transportAddress);
// assert removed == Peer.this : removed + " != " + Peer.this;
// ^ This assertion sometimes trips if we are deactivated and reactivated while a request is in flight.
// TODO be more careful about avoiding multiple active Peer objects for each address
}

private void requestPeers() {
assert holdsLock() : "PeerFinder mutex not held";
assert peersRequestInFlight == false : "PeersRequest already in flight";
Expand Down