-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Improve connection closing in RemoteClusterConnection
#22804
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
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -370,6 +370,9 @@ void collectRemoteNodes(Iterator<DiscoveryNode> seedNodes, | |
| ClusterStateRequest request = new ClusterStateRequest(); | ||
| request.clear(); | ||
| request.nodes(true); | ||
| // here we pass on the connection since we can only close it once the sendRequest returns otherwise | ||
| // due to the async nature (it will return before it's actually send) this can cause the request to fail | ||
| // due to an already closed connection. | ||
| transportService.sendRequest(connection, | ||
| ClusterStateAction.NAME, request, TransportRequestOptions.EMPTY, | ||
| new SniffClusterStateResponseHandler(transportService, connection, listener, seedNodes, | ||
|
|
@@ -443,24 +446,30 @@ public ClusterStateResponse newInstance() { | |
| @Override | ||
| public void handleResponse(ClusterStateResponse response) { | ||
| try { | ||
| cancellableThreads.executeIO(() -> { | ||
| DiscoveryNodes nodes = response.getState().nodes(); | ||
| Iterable<DiscoveryNode> nodesIter = nodes.getNodes()::valuesIt; | ||
| for (DiscoveryNode node : nodesIter) { | ||
| if (nodePredicate.test(node) && connectedNodes.size() < maxNumRemoteConnections) { | ||
| try { | ||
| transportService.connectToNode(node, remoteProfile); // noop if node is connected | ||
| connectedNodes.add(node); | ||
| } catch (ConnectTransportException | IllegalStateException ex) { | ||
| // ISE if we fail the handshake with an version incompatible node | ||
| // fair enough we can't connect just move on | ||
| logger.debug((Supplier<?>) | ||
| () -> new ParameterizedMessage("failed to connect to node {}", node), ex); | ||
| try (Closeable theConnection = connection) { // the connection is unused - see comment in #collectRemoteNodes | ||
| // we have to close this connection before we notify listeners - this is mainly needed for test correctness | ||
| // since if we do it afterwards we might fail assertions that check if all high level connections are closed. | ||
| // from a code correctness perspective we could also close it afterwards. This try/with block will | ||
| // maintain the actual exceptions thrown from within the try block and suppress the ones that are possible thrown | ||
|
||
| // by closing hte connection | ||
|
||
| cancellableThreads.executeIO(() -> { | ||
| DiscoveryNodes nodes = response.getState().nodes(); | ||
| Iterable<DiscoveryNode> nodesIter = nodes.getNodes()::valuesIt; | ||
| for (DiscoveryNode node : nodesIter) { | ||
| if (nodePredicate.test(node) && connectedNodes.size() < maxNumRemoteConnections) { | ||
| try { | ||
| transportService.connectToNode(node, remoteProfile); // noop if node is connected | ||
| connectedNodes.add(node); | ||
| } catch (ConnectTransportException | IllegalStateException ex) { | ||
| // ISE if we fail the handshake with an version incompatible node | ||
| // fair enough we can't connect just move on | ||
| logger.debug((Supplier<?>) | ||
| () -> new ParameterizedMessage("failed to connect to node {}", node), ex); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| connection.close(); | ||
| }); | ||
| } | ||
| listener.onResponse(null); | ||
| } catch (CancellableThreads.ExecutionCancelledException ex) { | ||
| listener.onFailure(ex); // we got canceled - fail the listener and step out | ||
|
|
@@ -469,9 +478,6 @@ public void handleResponse(ClusterStateResponse response) { | |
| () -> new ParameterizedMessage("fetching nodes from external cluster {} failed", | ||
| clusterAlias), ex); | ||
| collectRemoteNodes(seedNodes, transportService, listener); | ||
| } finally { | ||
| // just to make sure we don't leak anything we close the connection here again even if we managed to do so before | ||
| IOUtils.closeWhileHandlingException(connection); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
it's actually sent