Skip to content

Commit

Permalink
SNOW-1732777: Log cancel query reason (#1914)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dprzybysz authored Oct 14, 2024
1 parent 51328b7 commit 0466d0e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/snowflake/client/core/CancellationReason.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/
package net.snowflake.client.core;

@SnowflakeJdbcInternalApi
public enum CancellationReason {
UNKNOWN,
CLIENT_REQUESTED,
TIMEOUT
}
14 changes: 14 additions & 0 deletions src/main/java/net/snowflake/client/core/SFBaseStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,23 @@ public abstract SFBaseResultSet asyncExecute(
*
* @throws SFException if the statement is already closed.
* @throws SQLException if there are server-side errors from trying to abort.
* @deprecated use {@link #cancel(CancellationReason)} instead
*/
@Deprecated
public abstract void cancel() throws SFException, SQLException;

/**
* Aborts the statement.
*
* @param cancellationReason reason for the cancellation
* @throws SFException if the statement is already closed.
* @throws SQLException if there are server-side errors from trying to abort.
*/
@SnowflakeJdbcInternalApi
public void cancel(CancellationReason cancellationReason) throws SFException, SQLException {
cancel(); // default cancel is called to keep interface backward compatibility
}

/**
* Sets a property within session properties, i.e., if the sql is using set-sf-property
*
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/net/snowflake/client/core/SFStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private TimeBombTask(SFStatement statement) {
@Override
public Void call() throws SQLException {
try {
statement.cancel();
statement.cancel(CancellationReason.TIMEOUT);
} catch (SFException ex) {
throw new SnowflakeSQLLoggedException(
session, ex.getSqlState(), ex.getVendorCode(), ex, ex.getParams());
Expand Down Expand Up @@ -711,10 +711,11 @@ private void reauthenticate() throws SFException, SnowflakeSQLException {
*
* @param sql sql statement
* @param mediaType media type
* @param cancellationReason reason for the cancellation
* @throws SnowflakeSQLException if failed to cancel the statement
* @throws SFException if statement is already closed
*/
private void cancelHelper(String sql, String mediaType)
private void cancelHelper(String sql, String mediaType, CancellationReason cancellationReason)
throws SnowflakeSQLException, SFException {
synchronized (this) {
if (isClosed) {
Expand All @@ -734,7 +735,7 @@ private void cancelHelper(String sql, String mediaType)
.setMaxRetries(session.getMaxHttpRetries())
.setHttpClientSettingsKey(session.getHttpClientKey());

StmtUtil.cancel(stmtInput);
StmtUtil.cancel(stmtInput, cancellationReason);

synchronized (this) {
/*
Expand Down Expand Up @@ -842,6 +843,12 @@ public void close() {
@Override
public void cancel() throws SFException, SQLException {
logger.trace("void cancel()", false);
cancel(CancellationReason.UNKNOWN);
}

@Override
public void cancel(CancellationReason cancellationReason) throws SFException, SQLException {
logger.trace("void cancel(CancellationReason)", false);

if (canceling.get()) {
logger.debug("Query is already cancelled", false);
Expand All @@ -866,7 +873,7 @@ public void cancel() throws SFException, SQLException {
}

// cancel the query on the server side if it has been issued
cancelHelper(this.sqlText, StmtUtil.SF_MEDIA_TYPE);
cancelHelper(this.sqlText, StmtUtil.SF_MEDIA_TYPE, cancellationReason);
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/net/snowflake/client/core/StmtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,23 @@ protected static JsonNode getQueryResultJSON(String queryId, SFSession session)
* @param stmtInput input statement
* @throws SFException if there is an internal exception
* @throws SnowflakeSQLException if failed to cancel the statement
* @deprecated use {@link #cancel(StmtInput, CancellationReason)} instead
*/
@Deprecated
public static void cancel(StmtInput stmtInput) throws SFException, SnowflakeSQLException {
cancel(stmtInput, CancellationReason.UNKNOWN);
}

/**
* Cancel a statement identifiable by a request id
*
* @param stmtInput input statement
* @param cancellationReason reason for the cancellation
* @throws SFException if there is an internal exception
* @throws SnowflakeSQLException if failed to cancel the statement
*/
public static void cancel(StmtInput stmtInput, CancellationReason cancellationReason)
throws SFException, SnowflakeSQLException {
HttpPost httpRequest = null;

AssertUtil.assertTrue(
Expand All @@ -701,7 +716,7 @@ public static void cancel(StmtInput stmtInput) throws SFException, SnowflakeSQLE

try {
URIBuilder uriBuilder = new URIBuilder(stmtInput.serverUrl);

logger.warn("Cancelling query {} with reason {}", stmtInput.requestId, cancellationReason);
logger.debug("Aborting query: {}", stmtInput.sql);

uriBuilder.setPath(SF_PATH_ABORT_REQUEST_V1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.snowflake.client.core.CancellationReason;
import net.snowflake.client.core.ExecTimeTelemetryData;
import net.snowflake.client.core.ParameterBindingDTO;
import net.snowflake.client.core.ResultUtil;
Expand Down Expand Up @@ -952,7 +953,7 @@ public void cancel() throws SQLException {
raiseSQLExceptionIfStatementIsClosed();

try {
sfBaseStatement.cancel();
sfBaseStatement.cancel(CancellationReason.CLIENT_REQUESTED);
} catch (SFException ex) {
throw new SnowflakeSQLException(ex, ex.getSqlState(), ex.getVendorCode(), ex.getParams());
}
Expand Down

0 comments on commit 0466d0e

Please sign in to comment.