Repository navigation
Fix SessionConnection.reconnect() self-closing transport when enableRedirection is false #19543
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,6 +165,7 @@ public class Session implements ISession { | |
| protected SessionConnection defaultSessionConnection; | ||
| private boolean isClosed = true; | ||
| private volatile boolean closing; | ||
| private final Object connectionStateLock = new Object(); | ||
|
|
||
| // this field only take effect in write request, nothing to do with any other type requests, | ||
| // like query, load and so on. | ||
|
|
@@ -722,14 +723,18 @@ public synchronized void openWithTableModelDatabaseCache( | |
|
|
||
| @Override | ||
| public synchronized void close() throws IoTDBConnectionException { | ||
| closing = true; | ||
| Throwable closeException = null; | ||
| try { | ||
| if (isClosed) { | ||
| return; | ||
| List<SessionConnection> connectionsToClose; | ||
| synchronized (connectionStateLock) { | ||
| closing = true; | ||
| if (isClosed) { | ||
| return; | ||
| } | ||
| connectionsToClose = getSessionConnections(); | ||
| } | ||
|
|
||
| for (SessionConnection sessionConnection : getSessionConnections()) { | ||
| for (SessionConnection sessionConnection : connectionsToClose) { | ||
| try { | ||
| sessionConnection.close(); | ||
| } catch (IoTDBConnectionException | RuntimeException e) { | ||
|
|
@@ -754,12 +759,16 @@ public synchronized void close() throws IoTDBConnectionException { | |
|
|
||
| /** Closes the underlying transports without waiting for a close-session RPC. */ | ||
| public synchronized void forceClose() { | ||
| closing = true; | ||
| if (isClosed) { | ||
| return; | ||
| List<SessionConnection> connectionsToClose; | ||
| synchronized (connectionStateLock) { | ||
| closing = true; | ||
| if (isClosed) { | ||
| return; | ||
| } | ||
| connectionsToClose = getSessionConnections(); | ||
| } | ||
| try { | ||
| for (SessionConnection sessionConnection : getSessionConnections()) { | ||
| for (SessionConnection sessionConnection : connectionsToClose) { | ||
| sessionConnection.forceCloseTransport(); | ||
| } | ||
| } finally { | ||
|
|
@@ -784,14 +793,16 @@ private List<SessionConnection> getSessionConnections() { | |
| } | ||
|
|
||
| private void finishClose() { | ||
| if (!isClosed) { | ||
| if (endPointToSessionConnection != null) { | ||
| // Do not wait for a concurrent computeIfAbsent that is still opening a connection. | ||
| endPointToSessionConnection = new ConcurrentHashMap<>(); | ||
| synchronized (connectionStateLock) { | ||
| if (!isClosed) { | ||
| if (endPointToSessionConnection != null) { | ||
| // Do not wait for a concurrent computeIfAbsent that is still opening a connection. | ||
| endPointToSessionConnection = new ConcurrentHashMap<>(); | ||
| } | ||
| setDefaultSessionConnection(null); | ||
| } | ||
| setDefaultSessionConnection(null); | ||
| isClosed = true; | ||
| } | ||
| isClosed = true; | ||
| // if executorService is null, it means that availableNodes is got from SessionPool and we | ||
| // shouldn't clean that | ||
| if (this.executorService != null) { | ||
|
|
@@ -1519,43 +1530,79 @@ public String getTimestampPrecision() throws TException, IoTDBConnectionExceptio | |
| return getDefaultSessionConnection().getClient().getProperties().getTimestampPrecision(); | ||
| } | ||
|
|
||
| boolean isClosing() { | ||
| return closing; | ||
| } | ||
|
|
||
| boolean registerReconnectedConnection(SessionConnection connection, TEndPoint newEndPoint) { | ||
| SessionConnection staleConnection = null; | ||
| synchronized (connectionStateLock) { | ||
| if (closing || connection.isForceClosed()) { | ||
| return false; | ||
| } | ||
| removeBrokenSessionConnection(connection); | ||
| defaultEndPoint = newEndPoint; | ||
| setDefaultSessionConnection(connection); | ||
| if (endPointToSessionConnection != null || enableRedirection || enableQueryRedirection) { | ||
| if (endPointToSessionConnection == null) { | ||
| endPointToSessionConnection = new ConcurrentHashMap<>(); | ||
| } | ||
| SessionConnection previous = endPointToSessionConnection.put(newEndPoint, connection); | ||
| if (previous != null && previous != connection) { | ||
| staleConnection = previous; | ||
| } | ||
| } | ||
| } | ||
| if (staleConnection != null | ||
| && staleConnection.transport != null | ||
| && staleConnection.transport.isOpen()) { | ||
| try { | ||
| staleConnection.close(); | ||
| } catch (IoTDBConnectionException e) { | ||
| logger.warn(SessionMessages.CLOSE_CONNECTION_FAILED, e.getMessage()); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // TODO https://issues.apache.org/jira/browse/IOTDB-1399 | ||
| public void removeBrokenSessionConnection(SessionConnection sessionConnection) { | ||
| // remove the cached broken leader session | ||
| if (enableRedirection) { | ||
| TEndPoint endPoint = null; | ||
| if (endPointToSessionConnection != null) { | ||
| for (Iterator<Entry<TEndPoint, SessionConnection>> it = | ||
| endPointToSessionConnection.entrySet().iterator(); | ||
| it.hasNext(); ) { | ||
| Entry<TEndPoint, SessionConnection> entry = it.next(); | ||
| if (entry.getValue().equals(sessionConnection)) { | ||
| endPoint = entry.getKey(); | ||
| it.remove(); | ||
| break; | ||
| } | ||
| if (endPointToSessionConnection == null) { | ||
| return; | ||
| } | ||
| Set<TEndPoint> removedEndPoints = new HashSet<>(); | ||
| for (Iterator<Entry<TEndPoint, SessionConnection>> it = | ||
| endPointToSessionConnection.entrySet().iterator(); | ||
| it.hasNext(); ) { | ||
| Entry<TEndPoint, SessionConnection> entry = it.next(); | ||
| if (entry.getValue().equals(sessionConnection)) { | ||
| removedEndPoints.add(entry.getKey()); | ||
| it.remove(); | ||
| } | ||
| } | ||
| if (!enableRedirection || removedEndPoints.isEmpty()) { | ||
| return; | ||
| } | ||
| if (deviceIdToEndpoint != null && !deviceIdToEndpoint.isEmpty()) { | ||
| for (Iterator<Entry<String, TEndPoint>> it = deviceIdToEndpoint.entrySet().iterator(); | ||
| it.hasNext(); ) { | ||
| Entry<String, TEndPoint> entry = it.next(); | ||
| if (removedEndPoints.contains(entry.getValue())) { | ||
| it.remove(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Guard device-route cleanup with
Please keep the endpoint-to-connection cleanup independent of the write flag and only invalidate the tree and table device-route caches when write redirection is enabled. |
||
| } | ||
| } | ||
| if (deviceIdToEndpoint != null && !deviceIdToEndpoint.isEmpty()) { | ||
| for (Iterator<Entry<String, TEndPoint>> it = deviceIdToEndpoint.entrySet().iterator(); | ||
| } | ||
| if (tableModelDeviceIdToEndpoint != null && !tableModelDeviceIdToEndpoint.isEmpty()) { | ||
| for (Map<IDeviceID, TEndPoint> databaseToEndpoint : tableModelDeviceIdToEndpoint.values()) { | ||
| for (Iterator<Entry<IDeviceID, TEndPoint>> it = databaseToEndpoint.entrySet().iterator(); | ||
| it.hasNext(); ) { | ||
| Entry<String, TEndPoint> entry = it.next(); | ||
| if (entry.getValue().equals(endPoint)) { | ||
| Entry<IDeviceID, TEndPoint> entry = it.next(); | ||
| if (removedEndPoints.contains(entry.getValue())) { | ||
| it.remove(); | ||
| } | ||
| } | ||
| } | ||
| if (tableModelDeviceIdToEndpoint != null && !tableModelDeviceIdToEndpoint.isEmpty()) { | ||
| for (Map<IDeviceID, TEndPoint> databaseToEndpoint : tableModelDeviceIdToEndpoint.values()) { | ||
| for (Iterator<Entry<IDeviceID, TEndPoint>> it = databaseToEndpoint.entrySet().iterator(); | ||
| it.hasNext(); ) { | ||
| Entry<IDeviceID, TEndPoint> entry = it.next(); | ||
| if (Objects.equals(entry.getValue(), endPoint)) { | ||
| it.remove(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -4811,7 +4858,23 @@ public boolean isEnableQueryRedirection() { | |
|
|
||
| @Override | ||
| public void setEnableQueryRedirection(boolean enableQueryRedirection) { | ||
| this.enableQueryRedirection = enableQueryRedirection; | ||
| synchronized (connectionStateLock) { | ||
| this.enableQueryRedirection = enableQueryRedirection; | ||
| if (enableQueryRedirection && !isClosed && !closing) { | ||
| if (defaultSessionConnection != null) { | ||
| defaultSessionConnection.setEnableRedirect(true); | ||
| } | ||
| if (endPointToSessionConnection == null) { | ||
| Map<TEndPoint, SessionConnection> connections = new ConcurrentHashMap<>(); | ||
| if (defaultEndPoint != null && defaultSessionConnection != null) { | ||
| connections.put(defaultEndPoint, defaultSessionConnection); | ||
| } | ||
| endPointToSessionConnection = connections; | ||
| } else if (defaultEndPoint != null && defaultSessionConnection != null) { | ||
| endPointToSessionConnection.putIfAbsent(defaultEndPoint, defaultSessionConnection); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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.
[P2] Keep a reconnecting query connection visible to
forceClose().With only query redirection enabled, a query on non-default connection B can reopen its transport and remove B here before
reconnect()makes B the default. A concurrentforceClose()then sees only A. I paused the reconnect in this window. B received the retried query afterforceClose()returned and still had one open socket. On the base revision the retry stopped and both nodes had zero open sockets.Please coordinate registration of the reopened connection with the session's close state so that a close in this window releases the new transport and stops the retry.