-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
refactor: Start batch export runs by tracking total count of records #21134
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d629c36
refactor: Finish batch export runs by tracking total count of records
tomasfarias f9aea2a
refactor: Track count when starting batch export run
tomasfarias eae2ff2
chore: Add migration
tomasfarias 758e200
feat: Add test utilities for batch exports
tomasfarias fafa0eb
chore: Mypy baseline updates
tomasfarias 83eb00e
fix: Correct assertions in snowflake test
tomasfarias 77aaf20
fix: Format character in logger call
tomasfarias 835ba00
fix: BigQuery testing
tomasfarias 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 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
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 |
---|---|---|
|
@@ -12,18 +12,22 @@ | |
from temporalio.common import RetryPolicy | ||
|
||
from posthog.batch_exports.models import BatchExportRun | ||
from posthog.batch_exports.service import BatchExportField, BatchExportSchema, BigQueryBatchExportInputs | ||
from posthog.batch_exports.service import ( | ||
BatchExportField, | ||
BatchExportSchema, | ||
BigQueryBatchExportInputs, | ||
) | ||
from posthog.temporal.batch_exports.base import PostHogWorkflow | ||
from posthog.temporal.batch_exports.batch_exports import ( | ||
BatchExportActivityReturnType, | ||
CreateBatchExportRunInputs, | ||
FinishBatchExportRunInputs, | ||
create_export_run, | ||
RecordsCompleted, | ||
StartBatchExportRunInputs, | ||
default_fields, | ||
execute_batch_export_insert_activity, | ||
finish_batch_export_run, | ||
get_data_interval, | ||
get_rows_count, | ||
iter_records, | ||
start_batch_export_run, | ||
) | ||
from posthog.temporal.batch_exports.metrics import ( | ||
get_bytes_exported_metric, | ||
|
@@ -147,6 +151,7 @@ class BigQueryInsertInputs: | |
include_events: list[str] | None = None | ||
use_json_type: bool = False | ||
batch_export_schema: BatchExportSchema | None = None | ||
run_id: str | None = None | ||
|
||
|
||
@contextlib.contextmanager | ||
|
@@ -196,13 +201,16 @@ def bigquery_default_fields() -> list[BatchExportField]: | |
|
||
|
||
@activity.defn | ||
async def insert_into_bigquery_activity(inputs: BigQueryInsertInputs) -> BatchExportActivityReturnType: | ||
async def insert_into_bigquery_activity(inputs: BigQueryInsertInputs) -> RecordsCompleted: | ||
"""Activity streams data from ClickHouse to BigQuery.""" | ||
logger = await bind_temporal_worker_logger(team_id=inputs.team_id, destination="BigQuery") | ||
logger.info( | ||
"Exporting batch %s - %s", | ||
"Batch exporting range %s - %s to BigQuery: %s.%s.%s", | ||
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. Also, I've standardized these log lines at the beginning of each batch export. I think they look nicer: even if they are not that useful, when I am a user I like to see stuff being printed, makes it seem stuff is working fine. Maybe it's me having too much fun 🤷 |
||
inputs.data_interval_start, | ||
inputs.data_interval_end, | ||
inputs.project_id, | ||
inputs.dataset_id, | ||
inputs.table_id, | ||
) | ||
|
||
should_resume, details = await should_resume_from_activity_heartbeat(activity, BigQueryHeartbeatDetails, logger) | ||
|
@@ -218,25 +226,6 @@ async def insert_into_bigquery_activity(inputs: BigQueryInsertInputs) -> BatchEx | |
if not await client.is_alive(): | ||
raise ConnectionError("Cannot establish connection to ClickHouse") | ||
|
||
count = await get_rows_count( | ||
client=client, | ||
team_id=inputs.team_id, | ||
interval_start=data_interval_start, | ||
interval_end=inputs.data_interval_end, | ||
exclude_events=inputs.exclude_events, | ||
include_events=inputs.include_events, | ||
) | ||
|
||
if count == 0: | ||
logger.info( | ||
"Nothing to export in batch %s - %s", | ||
inputs.data_interval_start, | ||
inputs.data_interval_end, | ||
) | ||
return 0, 0 | ||
|
||
logger.info("BatchExporting %s rows", count) | ||
|
||
if inputs.batch_export_schema is None: | ||
fields = bigquery_default_fields() | ||
query_parameters = None | ||
|
@@ -357,7 +346,7 @@ async def flush_to_bigquery(bigquery_table, table_schema): | |
|
||
jsonl_file.reset() | ||
|
||
return jsonl_file.records_total, count | ||
return jsonl_file.records_total | ||
|
||
|
||
@workflow.defn(name="bigquery-export") | ||
|
@@ -381,15 +370,17 @@ async def run(self, inputs: BigQueryBatchExportInputs): | |
"""Workflow implementation to export data to BigQuery.""" | ||
data_interval_start, data_interval_end = get_data_interval(inputs.interval, inputs.data_interval_end) | ||
|
||
create_export_run_inputs = CreateBatchExportRunInputs( | ||
start_batch_export_run_inputs = StartBatchExportRunInputs( | ||
team_id=inputs.team_id, | ||
batch_export_id=inputs.batch_export_id, | ||
data_interval_start=data_interval_start.isoformat(), | ||
data_interval_end=data_interval_end.isoformat(), | ||
exclude_events=inputs.exclude_events, | ||
include_events=inputs.include_events, | ||
) | ||
run_id = await workflow.execute_activity( | ||
create_export_run, | ||
create_export_run_inputs, | ||
run_id, records_total_count = await workflow.execute_activity( | ||
start_batch_export_run, | ||
start_batch_export_run_inputs, | ||
start_to_close_timeout=dt.timedelta(minutes=5), | ||
retry_policy=RetryPolicy( | ||
initial_interval=dt.timedelta(seconds=10), | ||
|
@@ -403,6 +394,26 @@ async def run(self, inputs: BigQueryBatchExportInputs): | |
id=run_id, status=BatchExportRun.Status.COMPLETED, team_id=inputs.team_id | ||
) | ||
|
||
finish_inputs = FinishBatchExportRunInputs( | ||
id=run_id, | ||
status=BatchExportRun.Status.COMPLETED, | ||
team_id=inputs.team_id, | ||
) | ||
|
||
if records_total_count == 0: | ||
await workflow.execute_activity( | ||
finish_batch_export_run, | ||
finish_inputs, | ||
start_to_close_timeout=dt.timedelta(minutes=5), | ||
retry_policy=RetryPolicy( | ||
initial_interval=dt.timedelta(seconds=10), | ||
maximum_interval=dt.timedelta(seconds=60), | ||
maximum_attempts=0, | ||
non_retryable_error_types=["NotNullViolation", "IntegrityError"], | ||
), | ||
) | ||
return | ||
|
||
insert_inputs = BigQueryInsertInputs( | ||
team_id=inputs.team_id, | ||
table_id=inputs.table_id, | ||
|
@@ -418,6 +429,7 @@ async def run(self, inputs: BigQueryBatchExportInputs): | |
include_events=inputs.include_events, | ||
use_json_type=inputs.use_json_type, | ||
batch_export_schema=inputs.batch_export_schema, | ||
run_id=run_id, | ||
) | ||
|
||
await execute_batch_export_insert_activity( | ||
|
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.
👍