Skip to content
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

Only connect to new nodes on new cluster state #31547

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public void connectToNodes(DiscoveryNodes discoveryNodes) {
for (final DiscoveryNode node : discoveryNodes) {
final boolean connected;
try (Releasable ignored = nodeLocks.acquire(node)) {
nodes.putIfAbsent(node, 0);
connected = transportService.nodeConnected(node);
connected = (nodes.putIfAbsent(node, 0) != null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid going on the management thread if we are truly connected? This will be relevant on the master where where the node is connected to during join validation. Also, I think the name connected for the variable is a bit mislead now. Maybe flip it and call it shouldConnect?

It will also good to have a comment explaining the rational for this check.

PS - as discussed, it will be good to explore using this component on non-elected-master nodes which will make my condition tweak obsolete.

}
if (connected) {
latch.countDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.junit.Before;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -131,6 +130,43 @@ public void testReconnect() {
assertConnectedExactlyToNodes(event.state());
}

public void testDoesNotReconnectToKnownNodesOnNewClusterState() {
List<DiscoveryNode> nodes = generateNodes();
NodeConnectionsService service = new NodeConnectionsService(Settings.EMPTY, threadPool, transportService);

final ClusterState state1 = clusterStateFromNodes(randomSubsetOf(nodes));
final ClusterState state2 = clusterStateFromNodes(randomSubsetOf(nodes));

service.connectToNodes(state1.nodes());

final Set<DiscoveryNode> disconnectedNodes = new HashSet<>(randomSubsetOf(nodes));
for (final DiscoveryNode node : disconnectedNodes) {
transport.disconnectFromNode(node);
}

service.connectToNodes(state2.nodes());

final Set<DiscoveryNode> expectedNodes = new HashSet<>(nodes.size());
state1.nodes().forEach(expectedNodes::add);
disconnectedNodes.forEach(expectedNodes::remove);
for (final DiscoveryNode discoveryNode : state2.nodes()) {
if (state1.nodes().get(discoveryNode.getId()) == null) {
// Only expect to be connected to _new_ nodes in state2.
expectedNodes.add(discoveryNode);
}
}

assertConnected(expectedNodes);
assertThat(transport.connectedNodes.size(), equalTo(expectedNodes.size()));

service.new ConnectionChecker().run();

state1.nodes().forEach(expectedNodes::add);

assertConnected(expectedNodes);
assertThat(transport.connectedNodes.size(), equalTo(expectedNodes.size()));
}

private void assertConnectedExactlyToNodes(ClusterState state) {
assertConnected(state.nodes());
assertThat(transport.connectedNodes.size(), equalTo(state.nodes().getSize()));
Expand All @@ -142,12 +178,6 @@ private void assertConnected(Iterable<DiscoveryNode> nodes) {
}
}

private void assertNotConnected(Iterable<DiscoveryNode> nodes) {
for (DiscoveryNode node : nodes) {
assertFalse("still connected to " + node, transport.connectedNodes.contains(node));
}
}

@Override
@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -190,7 +220,7 @@ public Map<String, BoundTransportAddress> profileBoundAddresses() {
}

@Override
public TransportAddress[] addressesFromString(String address, int perAddressLimit) throws UnknownHostException {
public TransportAddress[] addressesFromString(String address, int perAddressLimit) {
return new TransportAddress[0];
}

Expand Down Expand Up @@ -226,19 +256,19 @@ public DiscoveryNode getNode() {

@Override
public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options)
throws IOException, TransportException {
throws TransportException {

}

@Override
public void close() throws IOException {
public void close() {

}
};
}

@Override
public Connection openConnection(DiscoveryNode node, ConnectionProfile profile) throws IOException {
public Connection openConnection(DiscoveryNode node, ConnectionProfile profile) {
return getConnection(node);
}

Expand Down