-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[BEAM-12356] Make sure DatasetService is always closed #15480
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
+129
−93
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -965,49 +965,53 @@ public void validate(PipelineOptions options) { | |
| // earlier stages of the pipeline or if a query depends on earlier stages of a pipeline. | ||
| // For these cases the withoutValidation method can be used to disable the check. | ||
| if (getValidate()) { | ||
| if (table != null) { | ||
| checkArgument(table.isAccessible(), "Cannot call validate if table is dynamically set."); | ||
| } | ||
| if (table != null && table.get().getProjectId() != null) { | ||
| // Check for source table presence for early failure notification. | ||
| DatasetService datasetService = getBigQueryServices().getDatasetService(bqOptions); | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, table.get()); | ||
| BigQueryHelpers.verifyTablePresence(datasetService, table.get()); | ||
| } else if (getQuery() != null) { | ||
| checkArgument( | ||
| getQuery().isAccessible(), "Cannot call validate if query is dynamically set."); | ||
| JobService jobService = getBigQueryServices().getJobService(bqOptions); | ||
| try { | ||
| jobService.dryRunQuery( | ||
| bqOptions.getBigQueryProject() == null | ||
| ? bqOptions.getProject() | ||
| : bqOptions.getBigQueryProject(), | ||
| new JobConfigurationQuery() | ||
| .setQuery(getQuery().get()) | ||
| .setFlattenResults(getFlattenResults()) | ||
| .setUseLegacySql(getUseLegacySql()), | ||
| getQueryLocation()); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException( | ||
| String.format(QUERY_VALIDATION_FAILURE_ERROR, getQuery().get()), e); | ||
| try (DatasetService datasetService = getBigQueryServices().getDatasetService(bqOptions)) { | ||
| if (table != null) { | ||
| checkArgument( | ||
| table.isAccessible(), "Cannot call validate if table is dynamically set."); | ||
| } | ||
| if (table != null && table.get().getProjectId() != null) { | ||
| // Check for source table presence for early failure notification. | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, table.get()); | ||
| BigQueryHelpers.verifyTablePresence(datasetService, table.get()); | ||
| } else if (getQuery() != null) { | ||
| checkArgument( | ||
| getQuery().isAccessible(), "Cannot call validate if query is dynamically set."); | ||
| JobService jobService = getBigQueryServices().getJobService(bqOptions); | ||
| try { | ||
| jobService.dryRunQuery( | ||
| bqOptions.getBigQueryProject() == null | ||
| ? bqOptions.getProject() | ||
| : bqOptions.getBigQueryProject(), | ||
| new JobConfigurationQuery() | ||
| .setQuery(getQuery().get()) | ||
| .setFlattenResults(getFlattenResults()) | ||
| .setUseLegacySql(getUseLegacySql()), | ||
| getQueryLocation()); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException( | ||
| String.format(QUERY_VALIDATION_FAILURE_ERROR, getQuery().get()), e); | ||
| } | ||
|
|
||
| DatasetService datasetService = getBigQueryServices().getDatasetService(bqOptions); | ||
| // If the user provided a temp dataset, check if the dataset exists before launching the | ||
| // query | ||
| if (getQueryTempDataset() != null) { | ||
| // The temp table is only used for dataset and project id validation, not for table name | ||
| // validation | ||
| TableReference tempTable = | ||
| new TableReference() | ||
| .setProjectId( | ||
| bqOptions.getBigQueryProject() == null | ||
| ? bqOptions.getProject() | ||
| : bqOptions.getBigQueryProject()) | ||
| .setDatasetId(getQueryTempDataset()) | ||
| .setTableId("dummy table"); | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, tempTable); | ||
| // If the user provided a temp dataset, check if the dataset exists before launching the | ||
| // query | ||
| if (getQueryTempDataset() != null) { | ||
| // The temp table is only used for dataset and project id validation, not for table | ||
| // name | ||
| // validation | ||
| TableReference tempTable = | ||
| new TableReference() | ||
| .setProjectId( | ||
| bqOptions.getBigQueryProject() == null | ||
| ? bqOptions.getProject() | ||
| : bqOptions.getBigQueryProject()) | ||
| .setDatasetId(getQueryTempDataset()) | ||
| .setTableId("dummy table"); | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, tempTable); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1401,15 +1405,17 @@ void cleanup(ContextContainer c) throws Exception { | |
| options.getJobName(), jobUuid, JobType.QUERY), | ||
| queryTempDataset); | ||
|
|
||
| DatasetService datasetService = getBigQueryServices().getDatasetService(options); | ||
| LOG.info("Deleting temporary table with query results {}", tempTable); | ||
| datasetService.deleteTable(tempTable); | ||
| // Delete dataset only if it was created by Beam | ||
| boolean datasetCreatedByBeam = !queryTempDataset.isPresent(); | ||
| if (datasetCreatedByBeam) { | ||
| LOG.info( | ||
| "Deleting temporary dataset with query results {}", tempTable.getDatasetId()); | ||
| datasetService.deleteDataset(tempTable.getProjectId(), tempTable.getDatasetId()); | ||
| try (DatasetService datasetService = | ||
| getBigQueryServices().getDatasetService(options)) { | ||
| LOG.info("Deleting temporary table with query results {}", tempTable); | ||
| datasetService.deleteTable(tempTable); | ||
| // Delete dataset only if it was created by Beam | ||
| boolean datasetCreatedByBeam = !queryTempDataset.isPresent(); | ||
| if (datasetCreatedByBeam) { | ||
| LOG.info( | ||
| "Deleting temporary dataset with query results {}", tempTable.getDatasetId()); | ||
| datasetService.deleteDataset(tempTable.getProjectId(), tempTable.getDatasetId()); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
@@ -2484,17 +2490,20 @@ public void validate(PipelineOptions pipelineOptions) { | |
| // The user specified a table. | ||
| if (getJsonTableRef() != null && getJsonTableRef().isAccessible() && getValidate()) { | ||
| TableReference table = getTableWithDefaultProject(options).get(); | ||
| DatasetService datasetService = getBigQueryServices().getDatasetService(options); | ||
| // Check for destination table presence and emptiness for early failure notification. | ||
| // Note that a presence check can fail when the table or dataset is created by an earlier | ||
| // stage of the pipeline. For these cases the #withoutValidation method can be used to | ||
| // disable the check. | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, table); | ||
| if (getCreateDisposition() == BigQueryIO.Write.CreateDisposition.CREATE_NEVER) { | ||
| BigQueryHelpers.verifyTablePresence(datasetService, table); | ||
| } | ||
| if (getWriteDisposition() == BigQueryIO.Write.WriteDisposition.WRITE_EMPTY) { | ||
| BigQueryHelpers.verifyTableNotExistOrEmpty(datasetService, table); | ||
| try (DatasetService datasetService = getBigQueryServices().getDatasetService(options)) { | ||
| // Check for destination table presence and emptiness for early failure notification. | ||
| // Note that a presence check can fail when the table or dataset is created by an earlier | ||
| // stage of the pipeline. For these cases the #withoutValidation method can be used to | ||
| // disable the check. | ||
| BigQueryHelpers.verifyDatasetPresence(datasetService, table); | ||
| if (getCreateDisposition() == BigQueryIO.Write.CreateDisposition.CREATE_NEVER) { | ||
| BigQueryHelpers.verifyTablePresence(datasetService, table); | ||
| } | ||
| if (getWriteDisposition() == BigQueryIO.Write.WriteDisposition.WRITE_EMPTY) { | ||
| BigQueryHelpers.verifyTableNotExistOrEmpty(datasetService, table); | ||
| } | ||
| } catch (Exception e) { | ||
|
Member
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. Not needed? |
||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
Do you need this here? Can you catch more specific exceptions?
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.
close() throws Exception, so this is the best I can do.