-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* re:PR https://github.com/dbt-labs/dbt-bigquery/pull/840/files * adding back comment # check if job failed * adding changelog * precommit code format * sleep(2) first in the while loop before the request to eliminate the last 2 seconds sleep if the response is in one of the 3 options * removing empty spaces * update batch request to handle `GetBatchRequest` * conditionally run python model tests and factor out batch functions to own module * Move events to common * fix import * fix mistaken import change * update unit test * clean up and typing --------- Co-authored-by: Zi Wang <[email protected]> Co-authored-by: wazi55 <[email protected]> Co-authored-by: Anders <[email protected]> Co-authored-by: Mike Alfare <[email protected]>
- Loading branch information
1 parent
63ae274
commit e5a89af
Showing
7 changed files
with
123 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Serverless Spark to Poll with .GetBatch() instead of using operation.result() | ||
time: 2023-07-21T10:10:41.64843-07:00 | ||
custom: | ||
Author: wazi55 | ||
Issue: "734" |
This file contains 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 |
---|---|---|
|
@@ -64,6 +64,7 @@ jobs: | |
|
||
outputs: | ||
matrix: ${{ steps.generate-matrix.outputs.result }} | ||
run-python-tests: ${{ steps.filter.outputs.bigquery-python }} | ||
|
||
steps: | ||
- name: Check out the repository (non-PR) | ||
|
@@ -96,6 +97,11 @@ jobs: | |
- 'dbt/**' | ||
- 'tests/**' | ||
- 'dev-requirements.txt' | ||
bigquery-python: | ||
- 'dbt/adapters/bigquery/dataproc/**' | ||
- 'dbt/adapters/bigquery/python_submissions.py' | ||
- 'dbt/include/bigquery/python_model/**' | ||
- name: Generate integration test matrix | ||
id: generate-matrix | ||
uses: actions/github-script@v6 | ||
|
@@ -186,6 +192,21 @@ jobs: | |
GCS_BUCKET: dbt-ci | ||
run: tox -- --ddtrace | ||
|
||
# python models tests are slow so we only want to run them if we're changing them | ||
- name: Run tox (python models) | ||
if: needs.test-metadata.outputs.run-python-tests == 'true' | ||
env: | ||
BIGQUERY_TEST_SERVICE_ACCOUNT_JSON: ${{ secrets.BIGQUERY_TEST_SERVICE_ACCOUNT_JSON }} | ||
BIGQUERY_TEST_ALT_DATABASE: ${{ secrets.BIGQUERY_TEST_ALT_DATABASE }} | ||
BIGQUERY_TEST_NO_ACCESS_DATABASE: ${{ secrets.BIGQUERY_TEST_NO_ACCESS_DATABASE }} | ||
DBT_TEST_USER_1: group:[email protected] | ||
DBT_TEST_USER_2: group:[email protected] | ||
DBT_TEST_USER_3: serviceAccount:[email protected] | ||
DATAPROC_REGION: us-central1 | ||
DATAPROC_CLUSTER_NAME: dbt-test-1 | ||
GCS_BUCKET: dbt-ci | ||
run: tox -e python-tests -- --ddtrace | ||
|
||
- uses: actions/upload-artifact@v3 | ||
if: always() | ||
with: | ||
|
Empty file.
This file contains 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Union, Dict | ||
|
||
import time | ||
from datetime import datetime | ||
from google.cloud.dataproc_v1 import ( | ||
CreateBatchRequest, | ||
BatchControllerClient, | ||
Batch, | ||
GetBatchRequest, | ||
) | ||
from google.protobuf.json_format import ParseDict | ||
|
||
from dbt.adapters.bigquery.connections import DataprocBatchConfig | ||
|
||
_BATCH_RUNNING_STATES = [Batch.State.PENDING, Batch.State.RUNNING] | ||
DEFAULT_JAR_FILE_URI = "gs://spark-lib/bigquery/spark-bigquery-with-dependencies_2.12-0.21.1.jar" | ||
|
||
|
||
def create_batch_request( | ||
batch: Batch, batch_id: str, project: str, region: str | ||
) -> CreateBatchRequest: | ||
return CreateBatchRequest( | ||
parent=f"projects/{project}/locations/{region}", # type: ignore | ||
batch_id=batch_id, # type: ignore | ||
batch=batch, # type: ignore | ||
) | ||
|
||
|
||
def poll_batch_job( | ||
parent: str, batch_id: str, job_client: BatchControllerClient, timeout: int | ||
) -> Batch: | ||
batch_name = "".join([parent, "/batches/", batch_id]) | ||
state = Batch.State.PENDING | ||
response = None | ||
run_time = 0 | ||
while state in _BATCH_RUNNING_STATES and run_time < timeout: | ||
time.sleep(1) | ||
response = job_client.get_batch( # type: ignore | ||
request=GetBatchRequest(name=batch_name), # type: ignore | ||
) | ||
run_time = datetime.now().timestamp() - response.create_time.timestamp() # type: ignore | ||
state = response.state | ||
if not response: | ||
raise ValueError("No response from Dataproc") | ||
if state != Batch.State.SUCCEEDED: | ||
if run_time >= timeout: | ||
raise ValueError( | ||
f"Operation did not complete within the designated timeout of {timeout} seconds." | ||
) | ||
else: | ||
raise ValueError(response.state_message) | ||
return response | ||
|
||
|
||
def update_batch_from_config(config_dict: Union[Dict, DataprocBatchConfig], target: Batch): | ||
try: | ||
# updates in place | ||
ParseDict(config_dict, target._pb) | ||
except Exception as e: | ||
docurl = ( | ||
"https://cloud.google.com/dataproc-serverless/docs/reference/rpc/google.cloud.dataproc.v1" | ||
"#google.cloud.dataproc.v1.Batch" | ||
) | ||
raise ValueError( | ||
f"Unable to parse dataproc_batch as valid batch specification. See {docurl}. {str(e)}" | ||
) from e | ||
return target |
This file contains 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 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 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