-
Notifications
You must be signed in to change notification settings - Fork 599
[JDBC] NetworkTimeout #2522
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
[JDBC] NetworkTimeout #2522
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf76f28
implemented network timeout. no tests
chernser 9b4c5d0
Merge branch 'main' into jdbc_connection_impl
chernser 4a0540d
merge main. a few fixes
chernser 16520a7
complete Connection#setNetworkTimeout() tests
chernser c185ad9
fixed test to wait task completeness
chernser 48b092c
optimized code
chernser 2c7976b
improved code and added one more test
chernser 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
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
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 |
---|---|---|
|
@@ -2,11 +2,15 @@ | |
|
||
import com.clickhouse.client.api.Client; | ||
import com.clickhouse.client.api.ClientConfigProperties; | ||
import com.clickhouse.client.api.ClientException; | ||
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; | ||
import com.clickhouse.client.api.internal.ServerSettings; | ||
import com.clickhouse.client.api.metadata.TableSchema; | ||
import com.clickhouse.client.api.query.GenericRecord; | ||
import com.clickhouse.client.api.query.QueryResponse; | ||
import com.clickhouse.client.api.query.QuerySettings; | ||
import com.clickhouse.data.ClickHouseDataType; | ||
import com.clickhouse.data.ClickHouseFormat; | ||
import com.clickhouse.jdbc.internal.ExceptionUtils; | ||
import com.clickhouse.jdbc.internal.JdbcConfiguration; | ||
import com.clickhouse.jdbc.internal.JdbcUtils; | ||
|
@@ -16,6 +20,7 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.Closeable; | ||
import java.sql.Array; | ||
import java.sql.Blob; | ||
import java.sql.CallableStatement; | ||
|
@@ -43,10 +48,11 @@ | |
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
public class ConnectionImpl implements Connection, JdbcV2Wrapper { | ||
private static final Logger log = LoggerFactory.getLogger(ConnectionImpl.class); | ||
private static final Logger LOG = LoggerFactory.getLogger(ConnectionImpl.class); | ||
|
||
protected final String url; | ||
private final Client client; // this member is private to force using getClient() | ||
|
@@ -65,9 +71,11 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper { | |
|
||
private final SqlParser sqlParser; | ||
|
||
private Executor networkTimeoutExecutor; | ||
|
||
public ConnectionImpl(String url, Properties info) throws SQLException { | ||
try { | ||
log.debug("Creating connection to {}", url); | ||
LOG.debug("Creating connection to {}", url); | ||
this.url = url;//Raw URL | ||
this.config = new JdbcConfiguration(url, info); | ||
this.onCluster = false; | ||
|
@@ -86,10 +94,10 @@ public ConnectionImpl(String url, Properties info) throws SQLException { | |
} | ||
|
||
if (this.config.isDisableFrameworkDetection()) { | ||
log.debug("Framework detection is disabled."); | ||
LOG.debug("Framework detection is disabled."); | ||
} else { | ||
String detectedFrameworks = Driver.FrameworksDetection.getFrameworksDetected(); | ||
log.debug("Detected frameworks: {}", detectedFrameworks); | ||
LOG.debug("Detected frameworks: {}", detectedFrameworks); | ||
if (!detectedFrameworks.trim().isEmpty()) { | ||
clientName += " (" + detectedFrameworks + ")"; | ||
} | ||
|
@@ -210,9 +218,8 @@ public void close() throws SQLException { | |
if (isClosed()) { | ||
return; | ||
} | ||
|
||
client.close(); | ||
closed = true; | ||
closed = true; // mark as closed to prevent further invocations | ||
client.close(); // this will disrupt pending requests. | ||
} | ||
|
||
@Override | ||
|
@@ -597,27 +604,59 @@ public String getSchema() throws SQLException { | |
|
||
@Override | ||
public void abort(Executor executor) throws SQLException { | ||
if (!config.isIgnoreUnsupportedRequests()) { | ||
throw new SQLFeatureNotSupportedException("abort not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); | ||
if (executor == null) { | ||
throw new SQLException("Executor must be not null"); | ||
} | ||
// This method should check permissions with SecurityManager but the one is deprecated. | ||
// There is no replacement for SecurityManger and it is marked for removal. | ||
this.close(); | ||
} | ||
|
||
@Override | ||
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { | ||
//TODO: Should this be supported? | ||
if (!config.isIgnoreUnsupportedRequests()) { | ||
throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); | ||
ensureOpen(); | ||
|
||
// Very good mail thread about this method implementation. https://mail.openjdk.org/pipermail/jdbc-spec-discuss/2017-November/000236.html | ||
|
||
// This method should check permissions with SecurityManager but the one is deprecated. | ||
// There is no replacement for SecurityManger and it is marked for removal. | ||
if (milliseconds > 0 && executor == null) { | ||
// we need executor only for positive timeout values. | ||
throw new SQLException("Executor must be not null"); | ||
} | ||
if (milliseconds < 0) { | ||
throw new SQLException("Timeout must be >= 0"); | ||
} | ||
|
||
// How it should work: | ||
// if timeout is set with this method then any timeout exception should be reported to the connection | ||
// when connection get signal about timeout it uses executor to abort itself | ||
// Some connection pools set timeout before calling Connection#close() to ensure that this operation will not hang | ||
// Socket timeout is propagated with QuerySettings this connection has. | ||
networkTimeoutExecutor = executor; | ||
defaultQuerySettings.setOption(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey(), (long)milliseconds); | ||
chernser marked this conversation as resolved.
Show resolved
Hide resolved
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. Why not use the new method |
||
} | ||
|
||
|
||
// Should be called by child object to notify about timeout. | ||
public void onNetworkTimeout() throws SQLException { | ||
if (isClosed() || networkTimeoutExecutor == null) { | ||
return; // we closed already so do nothing. | ||
} | ||
|
||
networkTimeoutExecutor.execute(() -> { | ||
try { | ||
this.abort(networkTimeoutExecutor); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Failed to abort connection", e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public int getNetworkTimeout() throws SQLException { | ||
//TODO: Should this be supported? | ||
if (!config.isIgnoreUnsupportedRequests()) { | ||
throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); | ||
} | ||
|
||
return -1; | ||
Long networkTimeout = defaultQuerySettings.getNetworkTimeout(); | ||
return networkTimeout == null ? 0 : networkTimeout.intValue(); | ||
} | ||
|
||
/** | ||
|
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
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
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
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
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
Oops, something went wrong.
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.
Those methods are never used. Do you have any plans to add tests?
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.
yes, I'll test them.