[IOTDB-17179] CLI: support automatic reconnection when connection is lost#17181
Open
miantalha45 wants to merge 2 commits intoapache:masterfrom
Open
[IOTDB-17179] CLI: support automatic reconnection when connection is lost#17181miantalha45 wants to merge 2 commits intoapache:masterfrom
miantalha45 wants to merge 2 commits intoapache:masterfrom
Conversation
- Detect connection-related SQLExceptions (refused, timeout, closed, reset, etc.) - In AbstractCli: rethrow connection-related SQLException from executeQuery, setTimeZone, showTimeZone so CLI can handle them - In Cli: on connection loss, close current connection, retry reconnect up to 3 times with 1s interval, then retry the failed command; print 'Connection lost. Reconnected. Retrying command.' on success; exit with clear message after all retries fail - Add isConnectionRelated() and matchesConnectionFailure() in AbstractCli for shared detection; openConnection(), setupConnection(), closeConnectionQuietly() and ReadLineResult in Cli for reconnect flow - Update AbstractCliTest to declare throws SQLException for handleInputCmd calls Co-authored-by: Cursor <cursoragent@cursor.com>
When reconnect succeeds but the retried command fails with a session/statement error (e.g. StatementId doesn't exist in this session), show a friendly message instead of the raw exception. Apply the same handling in AbstractCli.executeQuery so the message is shown both during reconnect-retry and when the user runs the next command. Add isSessionOrStatement
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Add automatic reconnection to the IoTDB CLI when the connection to the server is lost during an interactive session (e.g. server restart, network blip, or idle timeout). The CLI no longer exits immediately on connection-related errors; it attempts to reconnect with the same parameters and retries the failed command, aligning behavior with the Session API, JDBC, and C++/Python clients.
Content1 — Detection and reconnection flow
SQLException. We treat an exception as connection-related if its message (or cause message, lowercased) contains any of:connection,refused,timeout,closed,reset,network,broken pipe. This logic lives inAbstractCli.isConnectionRelated(SQLException)andmatchesConnectionFailure(String)so it can be shared and reused.DriverManager.getConnectionand the existinginfoproperties. Helper methodsopenConnection(),setupConnection(), andcloseConnectionQuietly()inCliencapsulate open/setup/close so the main loop stays clear.RECONNECT_RETRY_NUMandRECONNECT_RETRY_INTERVAL_MSinClicontrol this; they are not yet user-configurable.Connection lost. Reconnected. Retrying command.If all reconnection attempts fail we print:IoTDB: Could not reconnect after 3 attempts. Please check that the server is running and try again.and exit with error code.Content2 — Class and method organization
isConnectionRelated(SQLException)(package-private static) andmatchesConnectionFailure(String)(private static) for shared detection. InexecuteQuery,setTimeZone, andshowTimeZone, we catchSQLException(orExceptionwhere the API does not throwSQLException) and rethrow whenisConnectionRelated(e); otherwise we keep the existing "print error and return error code" behavior.handleInputCmdandprocessCommandnow declarethrows SQLExceptionso connection failures propagate to the CLI loop instead of being swallowed.ReadLineResult(inner class withstop,failedCommand) and factory methodscontinueLoop(),stopLoop(),reconnectAndRetry(String)so the read-eval loop can signal "continue", "exit", or "reconnect and retry this command".receiveCommands()no longer uses try-with-resources for the connection; it holds the connection in a variable, and whenreaderReadLine()returns a result withfailedCommand != null, it runs the reconnect loop (close → retry open/setup → print message → retry command).readerReadLine()wrapsprocessCommand()in a try-catch; on connection-relatedSQLExceptionit returnsreconnectAndRetry(s)with the current line; on otherSQLExceptionit prints and returnsstopLoop().testHandleInputInputCmd()now declaresthrows SQLExceptionand importsjava.sql.SQLExceptionso it compiles with the updatedhandleInputCmdsignature.Content3 — Corner cases and alternatives
reconnectAndRetryand run the same reconnect/retry flow again (each time with up to 3 reconnect attempts). Non-connectionSQLExceptions still print the error and stop the loop (exit) as before. Interrupt and EOF handling inreaderReadLine()are unchanged.isSessionOrStatementError()in AbstractCli and show: "Reconnected, but the previous command could not be completed. Please run your command again." so the user is not shown the raw exception. This handling is applied both in the reconnect-retry path in Cli and in AbstractCli.executeQuery for the normal command path.This PR has:
Key changed/added classes (or packages if there are too many classes) in this PR
org.apache.iotdb.cli.AbstractCli—isConnectionRelated,matchesConnectionFailure,isSessionOrStatementError,matchesSessionOrStatementFailure; rethrow connection-relatedSQLExceptioninexecuteQuery,setTimeZone,showTimeZone; inexecuteQueryshow friendly message for session/statement errors;handleInputCmd,processCommandnowthrows SQLExceptionorg.apache.iotdb.cli.Cli—ReadLineResult,openConnection(),setupConnection(),closeConnectionQuietly(); refactoredreceiveCommands()andreaderReadLine()for reconnect-and-retry flow; handle session/statement error in reconnect-retry pathorg.apache.iotdb.cli.AbstractCliTest—testHandleInputInputCmd()updated forthrows SQLExceptionCloses #17179