Skip to content
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

[PLUGIN-1849] Error Management for BigQuery Action plugin #1496

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.auth.Credentials;
import com.google.cloud.StringEnumValue;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
Expand All @@ -33,8 +34,12 @@
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.etl.api.action.Action;
import io.cdap.cdap.etl.api.action.ActionContext;
import io.cdap.plugin.gcp.bigquery.common.BigQueryErrorUtil;
import io.cdap.plugin.gcp.common.GCPUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -70,34 +75,72 @@ public AbstractBigQueryActionConfig getConfig() {
}

@Override
public void run(ActionContext context) throws Exception {
public void run(ActionContext context) {
config.validate(context.getFailureCollector());

QueryJobConfiguration queryConfig = config.getQueryJobConfiguration(context.getFailureCollector());
JobId jobId = JobId.newBuilder().setRandomJob().build();

// API request - starts the query.
Credentials credentials = config.getServiceAccount() == null ?
null : GCPUtils.loadServiceAccountCredentials(config.getServiceAccount(),
config.isServiceAccountFilePath());
Credentials credentials = null;
try {
credentials = config.getServiceAccount() == null ? null :
GCPUtils.loadServiceAccountCredentials(config.getServiceAccount(), config.isServiceAccountFilePath());
} catch (Exception e) {
context.getFailureCollector().addFailure(
String.format("Failed to load service account credentials, %s: %s",
e.getClass().getName(), e.getMessage()), null).withStacktrace(e.getStackTrace());
context.getFailureCollector().getOrThrowException();
}
BigQuery bigQuery = GCPUtils.getBigQuery(config.getProject(), credentials, null);
Job queryJob = bigQuery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

LOG.info("Executing SQL as job {}.", jobId.getJob());
LOG.debug("The BigQuery SQL {}", queryConfig.getQuery());

// Wait for the query to complete
queryJob.waitFor();
try {
queryJob.waitFor();
} catch (BigQueryException | InterruptedException e) {
String errorMessage = String.format("The bigquery query job failed, %s: %s",
e.getClass().getName(), e.getMessage());
if (e instanceof BigQueryException) {
throw BigQueryErrorUtil.getProgramFailureException(errorMessage,
((BigQueryException) e).getReason(), e);
}
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorMessage, errorMessage,
ErrorType.UNKNOWN, true, e);
}

// Check for errors
if (queryJob.getStatus().getError() != null) {
throw new RuntimeException(queryJob.getStatus().getExecutionErrors().toString());
String errorReason = String.format("The bigquery job failed with reason: %s",
queryJob.getStatus().getError().getReason());
ErrorType type = BigQueryErrorUtil.getErrorType(queryJob.getStatus().getError().getReason());
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason, errorReason, type,
Copy link
Member

@itsankit-google itsankit-google Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errorMessage = queryJob.getStatus().getExecutionErrors().toString()

true, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no supportDocUrl added?

}
TableResult queryResults;
try {
queryResults = queryJob.getQueryResults();
} catch (BigQueryException | InterruptedException e) {
String errorMessage = String.format("The bigquery query job failed, %s: %s",
e.getClass().getName(), e.getMessage());
if (e instanceof BigQueryException) {
throw BigQueryErrorUtil.getProgramFailureException(errorMessage,
((BigQueryException) e).getReason(), e);
}
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorMessage, errorMessage,
ErrorType.UNKNOWN, false, e);
}

TableResult queryResults = queryJob.getQueryResults();
if (queryResults.getTotalRows() == 0 || queryResults.getTotalRows() > 1) {
throw new RuntimeException(String.format("The query result total rows should be \"1\" but is \"%d\"",
queryResults.getTotalRows()));
String error = String.format("The query result total rows should be \"1\" but is \"%d\"",
queryResults.getTotalRows());
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
error, error, ErrorType.USER, false, null);
}

Schema schema = queryResults.getSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.plugin.common.ConfigUtil;
import io.cdap.plugin.gcp.bigquery.source.BigQuerySource;
Expand Down Expand Up @@ -224,9 +227,11 @@ private void checkIfArgumentsColumnsExitsInSource(Map<String, String> argumentCo
String nonExistingColumnNames = argumentConditionMap.keySet().stream()
.filter(columnName -> !argumentConditionFields.containsKey(columnName))
.collect(Collectors.joining(" ,"));
throw new RuntimeException(String.format(
String error = String.format(
"Columns: \" %s \"do not exist in table. Argument selections columns must exist in table.",
nonExistingColumnNames));
nonExistingColumnNames);
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
error, error, ErrorType.USER, false, null);
}

static void checkIfArgumentsColumnsListExistsInSource(
Expand Down
116 changes: 92 additions & 24 deletions src/main/java/io/cdap/plugin/gcp/bigquery/action/BigQueryExecute.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.action.Action;
import io.cdap.cdap.etl.api.action.ActionContext;
import io.cdap.cdap.etl.common.Constants;
import io.cdap.plugin.gcp.bigquery.common.BigQueryErrorUtil;
import io.cdap.plugin.gcp.bigquery.exception.BigQueryJobExecutionException;
import io.cdap.plugin.gcp.bigquery.sink.BigQuerySinkUtils;
import io.cdap.plugin.gcp.bigquery.util.BigQueryUtil;
import io.cdap.plugin.gcp.common.CmekUtils;
import io.cdap.plugin.gcp.common.GCPErrorDetailsProviderUtil;
import io.cdap.plugin.gcp.common.GCPUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -93,7 +98,7 @@ public final class BigQueryExecute extends AbstractBigQueryAction {
}

@Override
public void run(ActionContext context) throws Exception {
public void run(ActionContext context) {
FailureCollector collector = context.getFailureCollector();
config.validate(collector, context.getArguments().asMap());
QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder(config.getSql());
Expand Down Expand Up @@ -125,9 +130,16 @@ public void run(ActionContext context) throws Exception {
builder.setUseLegacySql(config.isLegacySQL());

// API request - starts the query.
Credentials credentials = config.getServiceAccount() == null ?
null : GCPUtils.loadServiceAccountCredentials(config.getServiceAccount(),
config.isServiceAccountFilePath());
Credentials credentials = null;
try {
credentials = config.getServiceAccount() == null ?
null : GCPUtils.loadServiceAccountCredentials(config.getServiceAccount(),
config.isServiceAccountFilePath());
} catch (IOException e) {
collector.addFailure(String.format("Failed to load service account credentials, %s: %s",
e.getClass().getName(), e.getMessage()), null).withStacktrace(e.getStackTrace());
collector.getOrThrowException();
}
BigQuery bigQuery = GCPUtils.getBigQuery(config.getProject(), credentials, config.getReadTimeout());
//create dataset to store the results if not exists
if (config.getStoreResults() && !Strings.isNullOrEmpty(datasetName) &&
Expand All @@ -152,23 +164,46 @@ public void run(ActionContext context) throws Exception {
try {
executeQueryWithExponentialBackoff(bigQuery, queryConfig, context);
} catch (Throwable e) {
throw new RuntimeException(e);
String errorMessage = String.format(
"Failed to execute query with exponential backoff, %s: %s", e.getClass().getName(),
e.getMessage());
if (e instanceof BigQueryException) {
throw BigQueryErrorUtil.getProgramFailureException(errorMessage,
((BigQueryException) e).getReason(), (Exception) e);
}
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorMessage, errorMessage,
ErrorType.UNKNOWN, true, e);
}
} else {
executeQuery(bigQuery, queryConfig, context);
try {
executeQuery(bigQuery, queryConfig, context);
} catch (Exception e) {
String errorMessage = String.format("The bigquery query execution failed, %s: %s",
e.getClass().getName(), e.getMessage());
String errorReason = null;
if (e instanceof BigQueryException) {
errorReason = ((BigQueryException) e).getReason();
}
throw BigQueryErrorUtil.getProgramFailureException(errorMessage, errorReason, e);
}
}
}

protected void executeQueryWithExponentialBackoff(BigQuery bigQuery,
QueryJobConfiguration queryConfig, ActionContext context)
throws Throwable {
QueryJobConfiguration queryConfig, ActionContext context) {
try {
Failsafe.with(getRetryPolicy()).run(() -> executeQuery(bigQuery, queryConfig, context));
} catch (FailsafeException e) {
itsankit-google marked this conversation as resolved.
Show resolved Hide resolved
String errorReason = String.format("The bigquery query execution failed with message: %s",
e.getMessage());
if (e.getCause() != null) {
throw e.getCause();
errorReason = String.format("The bigquery query execution failed with message: %s",
e.getCause().getMessage());
}
throw e;
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(
e == null ? e : e.getCause(), errorReason, ErrorType.UNKNOWN, true,
GCPUtils.BQ_SUPPORTED_DOC_URL);
}
}

Expand All @@ -185,7 +220,7 @@ private RetryPolicy<Object> getRetryPolicy() {
}

private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig, ActionContext context)
throws InterruptedException, BigQueryJobExecutionException {
throws BigQueryJobExecutionException {
// Location must match that of the dataset(s) referenced in the query.
JobId jobId = JobId.newBuilder().setRandomJob().setLocation(config.getLocation()).build();
Job queryJob;
Expand All @@ -198,26 +233,58 @@ private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig,

// Wait for the query to complete
queryJob = queryJob.waitFor();
} catch (BigQueryException e) {
LOG.error("The query job {} failed. Error: {}", jobId.getJob(), e.getError().getMessage());
if (RETRY_ON_REASON.contains(e.getError().getReason())) {
throw new BigQueryJobExecutionException(e.getError().getMessage(), e);
} catch (BigQueryException | InterruptedException e) {
String errorMessage = String.format("The bigquery query execution failed, %s: %s",
e.getClass().getName(), e.getMessage());
if (e instanceof BigQueryException) {
LOG.error("The query job {} failed. Error: {}", jobId.getJob(),
((BigQueryException) e).getError().getMessage());
if (RETRY_ON_REASON.contains(((BigQueryException) e).getError().getReason())) {
throw new BigQueryJobExecutionException(((BigQueryException) e).getError().getMessage(),
e);
}
throw BigQueryErrorUtil.getProgramFailureException(errorMessage,
((BigQueryException) e).getReason(), e);
}
throw new RuntimeException(e);
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorMessage, errorMessage,
ErrorType.UNKNOWN, true, e);
}

// Check for errors
if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
LOG.error("The query job {} failed. Error: {}", jobId.getJob(), queryJob.getStatus().getError());
LOG.error(
String.format("The query job %s failed with error %s and reason %s.", jobId.getJob(),
queryJob.getStatus().getError().getReason(), queryJob.getStatus().getError()));
Copy link
Member

@itsankit-google itsankit-google Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.format("The query job %s failed with reason: %s and error: %s.", jobId.getJob(), queryJob.getStatus().getError().getReason(), queryJob.getStatus().getExecutionErrors().toString()))

if (RETRY_ON_REASON.contains(queryJob.getStatus().getError().getReason())) {
itsankit-google marked this conversation as resolved.
Show resolved Hide resolved
throw new BigQueryJobExecutionException(queryJob.getStatus().getError().getMessage());
}
throw new RuntimeException(queryJob.getStatus().getError().getMessage());
String error = String.format(
"The bigquery query execution failed with reason: %s and message: %s",
queryJob.getStatus().getError().getReason(),
queryJob.getStatus().getError().getMessage());
ErrorType type = BigQueryErrorUtil.getErrorType(queryJob.getStatus().getError().getReason());
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), error, error, type, true,
null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no supportDocUrl here?

}

TableResult queryResults = queryJob.getQueryResults();
TableResult queryResults;
try {
queryResults = queryJob.getQueryResults();
} catch (BigQueryException | InterruptedException e) {
String errorMessage = String.format("Failed to retrieve query result, %s: %s",
e.getClass().getName(), e.getMessage());
if (e instanceof BigQueryException) {
throw BigQueryErrorUtil.getProgramFailureException(errorMessage,
((BigQueryException) e).getReason(), e);
}
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorMessage, errorMessage,
ErrorType.UNKNOWN, true, e);
}
long rows = queryResults.getTotalRows();

if (config.shouldSetAsArguments()) {
Expand Down Expand Up @@ -659,11 +726,12 @@ public void validateSQLSyntax(FailureCollector failureCollector, BigQuery bigQue
bigQuery.create(JobInfo.of(queryJobConfiguration));
} catch (BigQueryException e) {
final String errorMessage;
if (e.getCode() == ERROR_CODE_NOT_FOUND) {
errorMessage = String.format("Resource was not found. Please verify the resource name. If the resource " +
"will be created at runtime, then update to use a macro for the resource name. Error message received " +
"was: %s", e.getMessage());
} else {
if (e.getCode() == ERROR_CODE_NOT_FOUND) {
errorMessage = String.format(
"Resource was not found. Please verify the resource name. If the resource will be "
+ "created at runtime, then update to use a macro for the resource name. "
+ "Error message received was %s: %s", e.getClass().getName(), e.getMessage());
} else {
errorMessage = e.getMessage();
}
failureCollector.addFailure(String.format("%s. Error code: %s.", errorMessage, e.getCode()),
Expand Down
Loading
Loading