Skip to content

Commit c4ae4c6

Browse files
committed
Ordering
1 parent d69c92e commit c4ae4c6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

core/src/main/java/org/elasticsearch/transport/TcpTransport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,6 @@ public final NodeChannels openConnection(DiscoveryNode node, ConnectionProfile c
589589
}
590590
};
591591
nodeChannels = connectToChannels(node, connectionProfile, this::onChannelOpen, onClose);
592-
if (!Arrays.stream(nodeChannels.channels).allMatch(this::isOpen)) {
593-
throw new ConnectTransportException(node, "a channel closed while connecting");
594-
}
595592
final Channel channel = nodeChannels.getChannels().get(0); // one channel is guaranteed by the connection profile
596593
final TimeValue connectTimeout = connectionProfile.getConnectTimeout() == null ?
597594
defaultConnectionProfile.getConnectTimeout() :
@@ -600,8 +597,11 @@ public final NodeChannels openConnection(DiscoveryNode node, ConnectionProfile c
600597
connectTimeout : connectionProfile.getHandshakeTimeout();
601598
final Version version = executeHandshake(node, channel, handshakeTimeout);
602599
nodeChannels = new NodeChannels(nodeChannels, version); // clone the channels - we now have the correct version
603-
transportService.onConnectionOpened(nodeChannels);
604600
connectionRef.set(nodeChannels);
601+
transportService.onConnectionOpened(nodeChannels);
602+
if (!Arrays.stream(nodeChannels.channels).allMatch(this::isOpen)) {
603+
throw new ConnectTransportException(node, "a channel closed while connecting");
604+
}
605605
success = true;
606606
return nodeChannels;
607607
} catch (ConnectTransportException e) {

test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,17 @@ public void testProfilesIncludesDefault() {
26242624
}
26252625

26262626
public void testChannelCloseWhileConnecting() throws IOException {
2627-
final MockTransportService service = buildService("service", version0, clusterSettings, Settings.EMPTY, true, true, this::close);
2627+
/*
2628+
* We have to keep the first connection open so the handshake succeeds and then we later fail when checking if all the connections
2629+
* are open.
2630+
*/
2631+
final AtomicBoolean first = new AtomicBoolean();
2632+
final MockTransportService service =
2633+
buildService("service", version0, clusterSettings, Settings.EMPTY, true, true, channel -> {
2634+
if (!first.compareAndSet(false, true)) {
2635+
close(channel);
2636+
}
2637+
});
26282638
final TcpTransport underlyingTransport = (TcpTransport) service.getOriginalTransport();
26292639

26302640
final String otherName = "other_service";

0 commit comments

Comments
 (0)