-
Notifications
You must be signed in to change notification settings - Fork 616
[client-v2, jdbc-v2] queryID auto-generation #2673
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
Open
chernser
wants to merge
7
commits into
main
Choose a base branch
from
impl_query_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+340
−53
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b2a8a2
Drafted queryID generation
chernser 5aa3da4
Merge branch 'main' into impl_query_id
chernser 28d9d5d
Merge branch 'main' into impl_query_id
chernser dd139a2
added query id to server exception. Added some tests
chernser d5db2c0
Added queryId to request error messages and clientexception
chernser f92ee73
Merge branch 'main' into impl_query_id
chernser 7a6a28d
added tests. added queryId generator for JDBC
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,21 +137,18 @@ public class Client implements AutoCloseable { | |
| // Server context | ||
| private String dbUser; | ||
| private String serverVersion; | ||
| private Object metricsRegistry; | ||
| private int retries; | ||
| private final Object metricsRegistry; | ||
| private final int retries; | ||
| private LZ4Factory lz4Factory = null; | ||
| private final Supplier<String> queryIdGenerator; | ||
|
|
||
| private Client(Collection<Endpoint> endpoints, Map<String,String> configuration, | ||
| ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) { | ||
| this(endpoints, configuration, sharedOperationExecutor, columnToMethodMatchingStrategy, null); | ||
| } | ||
|
|
||
| private Client(Collection<Endpoint> endpoints, Map<String,String> configuration, | ||
| ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy, Object metricsRegistry) { | ||
| // Simple initialization | ||
| ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy, | ||
| Object metricsRegistry, Supplier<String> queryIdGenerator) { | ||
| this.configuration = ClientConfigProperties.parseConfigMap(configuration); | ||
| this.readOnlyConfig = Collections.unmodifiableMap(configuration); | ||
| this.metricsRegistry = metricsRegistry; | ||
| this.queryIdGenerator = queryIdGenerator; | ||
|
|
||
| // Serialization | ||
| this.pojoSerDe = new POJOSerDe(columnToMethodMatchingStrategy); | ||
|
|
@@ -266,6 +263,8 @@ public static class Builder { | |
| private ExecutorService sharedOperationExecutor = null; | ||
| private ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy; | ||
| private Object metricRegistry = null; | ||
| private Supplier<String> queryIdGenerator; | ||
|
|
||
| public Builder() { | ||
| this.endpoints = new HashSet<>(); | ||
| this.configuration = new HashMap<>(); | ||
|
|
@@ -1085,6 +1084,16 @@ public Builder useHttpFormDataForQuery(boolean enable) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets query id generator. Will be used when operation settings (InsertSettings, QuerySettings) do not have query id set. | ||
| * @param supplier | ||
| * @return | ||
| */ | ||
| public Builder queryIdGenerator(Supplier<String> supplier) { | ||
|
Contributor
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. Should be renamed to |
||
| this.queryIdGenerator = supplier; | ||
| return this; | ||
| } | ||
|
|
||
| public Client build() { | ||
| // check if endpoint are empty. so can not initiate client | ||
| if (this.endpoints.isEmpty()) { | ||
|
|
@@ -1143,7 +1152,7 @@ public Client build() { | |
| } | ||
|
|
||
| return new Client(this.endpoints, this.configuration, this.sharedOperationExecutor, | ||
| this.columnToMethodMatchingStrategy, this.metricRegistry); | ||
| this.columnToMethodMatchingStrategy, this.metricRegistry, this.queryIdGenerator); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1282,6 +1291,9 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, | |
| final int maxRetries = retry == null ? 0 : retry; | ||
|
|
||
| requestSettings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format); | ||
| if (requestSettings.getQueryId() == null && queryIdGenerator != null) { | ||
| requestSettings.setQueryId(queryIdGenerator.get()); | ||
| } | ||
| Supplier<InsertResponse> supplier = () -> { | ||
| long startTime = System.nanoTime(); | ||
| // Selecting some node | ||
|
|
@@ -1328,8 +1340,8 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, | |
| metrics.setQueryId(queryId); | ||
| return new InsertResponse(metrics); | ||
| } catch (Exception e) { | ||
| lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)", | ||
| (i + 1), (maxRetries + 1), durationSince(startTime)), e); | ||
| String msg = requestExMsg("Insert", (i + 1), durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
| lastException = httpClientHelper.wrapException(msg, e, requestSettings.getQueryId()); | ||
| if (httpClientHelper.shouldRetry(e, requestSettings.getAllSettings())) { | ||
| LOG.warn("Retrying.", e); | ||
| selectedEndpoint = getNextAliveNode(); | ||
|
|
@@ -1338,8 +1350,10 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, | |
| } | ||
| } | ||
| } | ||
| throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1) + " - Duration: " + durationSince(startTime), lastException); | ||
| }; | ||
|
|
||
| String errMsg = requestExMsg("Insert", retries, durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
chernser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOG.warn(errMsg); | ||
| throw (lastException == null ? new ClientException(errMsg) : lastException); }; | ||
|
|
||
| return runAsyncOperation(supplier, requestSettings.getAllSettings()); | ||
|
|
||
|
|
@@ -1499,6 +1513,9 @@ public CompletableFuture<InsertResponse> insert(String tableName, | |
| } | ||
| sqlStmt.append(" FORMAT ").append(format.name()); | ||
| requestSettings.serverSetting(ClickHouseHttpProto.QPARAM_QUERY_STMT, sqlStmt.toString()); | ||
| if (requestSettings.getQueryId() == null && queryIdGenerator != null) { | ||
| requestSettings.setQueryId(queryIdGenerator.get()); | ||
| } | ||
| responseSupplier = () -> { | ||
| long startTime = System.nanoTime(); | ||
| // Selecting some node | ||
|
|
@@ -1530,8 +1547,8 @@ public CompletableFuture<InsertResponse> insert(String tableName, | |
| metrics.setQueryId(queryId); | ||
| return new InsertResponse(metrics); | ||
| } catch (Exception e) { | ||
| lastException = httpClientHelper.wrapException(String.format("Insert failed (Attempt: %s/%s - Duration: %s)", | ||
| (i + 1), (retries + 1), durationSince(startTime)), e); | ||
| String msg = requestExMsg("Insert", (i + 1), durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
| lastException = httpClientHelper.wrapException(msg, e, requestSettings.getQueryId()); | ||
| if (httpClientHelper.shouldRetry(e, requestSettings.getAllSettings())) { | ||
| LOG.warn("Retrying.", e); | ||
| selectedEndpoint = getNextAliveNode(); | ||
|
|
@@ -1548,8 +1565,9 @@ public CompletableFuture<InsertResponse> insert(String tableName, | |
| } | ||
| } | ||
| } | ||
| LOG.warn("Insert request failed after attempts: {} - Duration: {}", retries + 1, durationSince(startTime)); | ||
| throw (lastException == null ? new ClientException("Failed to complete insert operation") : lastException); | ||
| String errMsg = requestExMsg("Insert", retries, durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
| LOG.warn(errMsg); | ||
| throw (lastException == null ? new ClientException(errMsg) : lastException); | ||
| }; | ||
|
|
||
| return runAsyncOperation(responseSupplier, requestSettings.getAllSettings()); | ||
|
|
@@ -1598,7 +1616,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, QuerySettings set | |
| * | ||
| * <b>Notes:</b> | ||
| * <ul> | ||
| * <li>Server response format can be specified thru {@code settings} or in SQL query.</li> | ||
| * <li>Server response format can be specified through {@code settings} or in SQL query.</li> | ||
| * <li>If specified in both, the {@code sqlQuery} will take precedence.</li> | ||
| * </ul> | ||
| * | ||
|
|
@@ -1623,6 +1641,10 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec | |
| requestSettings.setOption(HttpAPIClientHelper.KEY_STATEMENT_PARAMS, queryParams); | ||
| } | ||
|
|
||
| if (requestSettings.getQueryId() == null && queryIdGenerator != null) { | ||
| requestSettings.setQueryId(queryIdGenerator.get()); | ||
| } | ||
|
|
||
| Supplier<QueryResponse> responseSupplier = () -> { | ||
| long startTime = System.nanoTime(); | ||
| // Selecting some node | ||
|
|
@@ -1666,8 +1688,8 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec | |
|
|
||
| } catch (Exception e) { | ||
| HttpAPIClientHelper.closeQuietly(httpResponse); | ||
| lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)", | ||
| (i + 1), (retries + 1), durationSince(startTime)), e); | ||
| String msg = requestExMsg("Query", (i + 1), durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
| lastException = httpClientHelper.wrapException(msg, e, requestSettings.getQueryId()); | ||
| if (httpClientHelper.shouldRetry(e, requestSettings.getAllSettings())) { | ||
| LOG.warn("Retrying.", e); | ||
| selectedEndpoint = getNextAliveNode(); | ||
|
|
@@ -1676,8 +1698,9 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec | |
| } | ||
| } | ||
| } | ||
| LOG.warn("Query request failed after attempts: {} - Duration: {}", retries + 1, durationSince(startTime)); | ||
| throw (lastException == null ? new ClientException("Failed to complete query") : lastException); | ||
| String errMsg = requestExMsg("Query", retries, durationSince(startTime).toMillis(), requestSettings.getQueryId()); | ||
| LOG.warn(errMsg); | ||
| throw (lastException == null ? new ClientException(errMsg) : lastException); | ||
| }; | ||
|
|
||
| return runAsyncOperation(responseSupplier, requestSettings.getAllSettings()); | ||
|
|
@@ -2155,4 +2178,8 @@ private Map<String, Object> buildRequestSettings(Map<String, Object> opSettings) | |
| private Duration durationSince(long sinceNanos) { | ||
| return Duration.ofNanos(System.nanoTime() - sinceNanos); | ||
| } | ||
|
|
||
| private String requestExMsg(String operation, int attempt, long operationDuration, String queryId) { | ||
| return operation + " request failed (attempt: " + attempt +", duration: " + operationDuration + "ms, queryId: " + queryId + ")"; | ||
| } | ||
| } | ||
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.
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.
Maybe I'm missing something. We introduce a new functionality,
queryIdGenerator, but we never test it, and, by default, it is null.