Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

refactor(transformer): Add multiprocessing to serialized transformer (#67399)

Tomás Farías Santana8efd72eabb01
diff --git a/posthog/settings/batch_exports.py b/posthog/settings/batch_exports.py
index 2cb2c49929e7..1cef47eed040 100644
--- a/posthog/settings/batch_exports.py
+++ b/posthog/settings/batch_exports.py
@@ -31,6 +31,9 @@ BATCH_EXPORT_BIGQUERY_USE_STORAGE_STREAM_API_TEAM_IDS: list[str] = get_list(
)
BATCH_EXPORT_BIGQUERY_MAX_CONSUMERS: int = get_from_env("BATCH_EXPORT_BIGQUERY_MAX_CONSUMERS", 5, type_cast=int)
+BATCH_EXPORT_BIGQUERY_TRANSFORMER_MAX_WORKERS: int = get_from_env(
+ "BATCH_EXPORT_BIGQUERY_TRANSFORMER_MAX_WORKERS", 5, type_cast=int
+)
BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT: str = get_from_env("BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT", "")
BATCH_EXPORT_BIGQUERY_STS_AUDIENCE_FIELD: str = get_from_env("BATCH_EXPORT_BIGQUERY_STS_AUDIENCE_FIELD", "")
diff --git a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
index 3879e9a8980d..fbb18772c281 100644
--- a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
@@ -8,6 +8,8 @@ import functools
import contextlib
import dataclasses
import collections.abc
+import multiprocessing as mp
+import concurrent.futures
from django.conf import settings
@@ -60,6 +62,7 @@ from products.batch_exports.backend.temporal.batch_exports import (
get_data_interval,
start_batch_export_run,
)
+from products.batch_exports.backend.temporal.metrics import ExecutionTimeRecorder
from products.batch_exports.backend.temporal.pipeline.consumer import Consumer, run_consumer_from_stage
from products.batch_exports.backend.temporal.pipeline.entrypoint import execute_batch_export_using_internal_stage
from products.batch_exports.backend.temporal.pipeline.producer import Producer
@@ -73,6 +76,7 @@ from products.batch_exports.backend.temporal.pipeline.transformer import (
SerializedStreamTransformer,
)
from products.batch_exports.backend.temporal.pipeline.types import BatchExportResult, reduce_batch_export_results
+from products.batch_exports.backend.temporal.pipeline.worker_init import init_worker
from products.batch_exports.backend.temporal.spmc import (
RecordBatchQueue,
raise_on_task_failure,
@@ -1214,7 +1218,18 @@ class BigQueryStorageConsumer(Consumer):
)
try:
- await self.send(data)
+ with ExecutionTimeRecorder(
+ "bigquery_storage_send_duration",
+ description="Duration to send a request to BigQuery's Storage API.",
+ log_message=(
+ "Sent chunk with size %(nbytes)d. Process time:"
+ " %(duration_seconds).6f seconds, speed: %(mb_per_second).2f MB/s"
+ ),
+ log_attributes={"nbytes": len(data)},
+ ) as recorder:
+ recorder.add_bytes_processed(len(data))
+
+ await self.send(data)
except Exception:
self.logger.exception("Send to stream failed")
await self.close_stream()
@@ -1399,6 +1414,7 @@ async def run_consumers(
async def run_storage_stream_consumers(
client: BigQueryWriteAsyncClient,
+ pool: concurrent.futures.ProcessPoolExecutor,
table: BigQueryTable,
producer_task: asyncio.Task[None],
queue: RecordBatchQueue,
@@ -1406,7 +1422,7 @@ async def run_storage_stream_consumers(
model: str = "events",
) -> BatchExportResult:
tasks = []
- serialized = SerializedStreamTransformer()
+ serialized = SerializedStreamTransformer(pool)
transformer: ChunkTransformerProtocol = PipelineTransformer(
transformers=(
SchemaTransformer(
@@ -1698,46 +1714,61 @@ async def insert_into_bigquery_activity_from_stage(inputs: BigQueryInsertInputs)
# TODO: Remove this or the else block after we have tested out whether multiple
# consumers are viable.
if use_storage_stream:
- serialized = SerializedStreamTransformer()
- transformer: ChunkTransformerProtocol = PipelineTransformer(
- transformers=(
- SchemaTransformer(
- table=bigquery_consumer_table,
- extra_compatible_types={
- # BigQuery Storage requires INTEGER columns be signed
- # Cast is safe and will throw on overflow.
- (pa.uint64(), pa.int64()): functools.partial(_cast_as_type, type=pa.int64()),
- (pa.uint8(), pa.int64()): functools.partial(_cast_as_type, type=pa.int64()),
- # BigQuery Storage requires that TIMESTAMP columns be 'us'
- (pa.timestamp("ms", tz="UTC"), pa.timestamp("us", tz="UTC")): functools.partial(
- _cast_to_lossless_timestamp, unit="us"
- ),
- (
- pa.timestamp("ms", tz="Etc/UTC"),
- pa.timestamp("us", tz="UTC"),
- ): functools.partial(_cast_to_lossless_timestamp, unit="us"),
- },
- ),
- serialized,
+ async with (
+ bigquery_write_async_client(bq_client.sync_client._credentials) as write_client,
+ ):
+ pool = concurrent.futures.ProcessPoolExecutor(
+ max_workers=settings.BATCH_EXPORT_BIGQUERY_TRANSFORMER_MAX_WORKERS,
+ mp_context=mp.get_context("forkserver"),
+ initializer=init_worker,
)
- )
+ try:
+ serialized = SerializedStreamTransformer(pool)
+ transformer: ChunkTransformerProtocol = PipelineTransformer(
+ transformers=(
+ SchemaTransformer(
+ table=bigquery_consumer_table,
+ extra_compatible_types={
+ # BigQuery Storage requires INTEGER columns be signed
+ # Cast is safe and will throw on overflow.
+ (pa.uint64(), pa.int64()): functools.partial(
+ _cast_as_type, type=pa.int64()
+ ),
+ (pa.uint8(), pa.int64()): functools.partial(
+ _cast_as_type, type=pa.int64()
+ ),
+ # BigQuery Storage requires that TIMESTAMP columns be 'us'
+ (
+ pa.timestamp("ms", tz="UTC"),
+ pa.timestamp("us", tz="UTC"),
+ ): functools.partial(_cast_to_lossless_timestamp, unit="us"),
+ (
+ pa.timestamp("ms", tz="Etc/UTC"),
+ pa.timestamp("us", tz="UTC"),
+ ): functools.partial(_cast_to_lossless_timestamp, unit="us"),
+ },
+ ),
+ serialized,
+ )
+ )
- async with bigquery_write_async_client(bq_client.sync_client._credentials) as write_client:
- consumer: Consumer = BigQueryStorageConsumer(
- client=write_client,
- table=bigquery_consumer_table,
- transformer=serialized,
- model=model.name if isinstance(model, BatchExportModel) else "events",
- )
+ consumer: Consumer = BigQueryStorageConsumer(
+ client=write_client,
+ table=bigquery_consumer_table,
+ transformer=serialized,
+ model=model.name if isinstance(model, BatchExportModel) else "events",
+ )
- result = await run_consumer_from_stage(
- queue=queue,
- producer_task=producer_task,
- consumer=consumer,
- transformer=transformer,
- json_columns=(),
- records_total=inputs.records_total,
- )
+ result = await run_consumer_from_stage(
+ queue=queue,
+ producer_task=producer_task,
+ consumer=consumer,
+ transformer=transformer,
+ json_columns=(),
+ records_total=inputs.records_total,
+ )
+ finally:
+ await asyncio.to_thread(pool.shutdown)
else:
consumer = BigQueryConsumer(
@@ -1782,15 +1813,26 @@ async def insert_into_bigquery_activity_from_stage(inputs: BigQueryInsertInputs)
else:
if use_storage_stream:
- async with bigquery_write_async_client(bq_client.sync_client._credentials) as write_client:
- result = await run_storage_stream_consumers(
- client=write_client,
- table=bigquery_consumer_table,
- producer_task=producer_task,
- queue=queue,
- max_consumers=settings.BATCH_EXPORT_BIGQUERY_MAX_CONSUMERS,
- model=model.name if isinstance(model, BatchExportModel) else "events",
+ async with (
+ bigquery_write_async_client(bq_client.sync_client._credentials) as write_client,
+ ):
+ pool = concurrent.futures.ProcessPoolExecutor(
+ max_workers=settings.BATCH_EXPORT_BIGQUERY_TRANSFORMER_MAX_WORKERS,
+ mp_context=mp.get_context("forkserver"),
+ initializer=init_worker,
)
+ try:
+ result = await run_storage_stream_consumers(
+ client=write_client,
+ pool=pool,
+ table=bigquery_consumer_table,
+ producer_task=producer_task,
+ queue=queue,
+ max_consumers=settings.BATCH_EXPORT_BIGQUERY_MAX_CONSUMERS,
+ model=model.name if isinstance(model, BatchExportModel) else "events",
+ )
+ finally:
+ await asyncio.to_thread(pool.shutdown)
else:
result = await run_consumers(
client=bq_client,
diff --git a/products/batch_exports/backend/temporal/pipeline/transformer.py b/products/batch_exports/backend/temporal/pipeline/transformer.py
index 0b02e8405a65..5d436ea23320 100644
--- a/products/batch_exports/backend/temporal/pipeline/transformer.py
+++ b/products/batch_exports/backend/temporal/pipeline/transformer.py
@@ -722,16 +722,23 @@ class CSVStreamTransformer:
return buffer.getvalue()
+def serialize(record_batch: pa.RecordBatch, column_names: list[str]) -> bytes:
+ return record_batch.select(column_names).serialize().to_pybytes()
+
+
class SerializedStreamTransformer:
"""A transformer to serialize record batches into Arrow IPC format."""
def __init__(
self,
+ pool: concurrent.futures.ProcessPoolExecutor,
include_inserted_at: bool = False,
max_file_size_bytes: int = 0,
):
+ self.pool = pool
self.include_inserted_at = include_inserted_at
self.max_file_size_bytes = max_file_size_bytes
+
self._schema: pa.Schema | None = None
async def iter(
@@ -739,6 +746,7 @@ class SerializedStreamTransformer:
) -> collections.abc.AsyncIterator[Chunk]:
"""Iterate over record batches transforming them into chunks."""
current_file_size = 0
+ loop = asyncio.get_running_loop()
async for record_batch in record_batches:
self.schema = record_batch.schema
@@ -755,10 +763,7 @@ class SerializedStreamTransformer:
) as recorder:
recorder.add_bytes_processed(record_batch.nbytes)
- buf = record_batch.select(self.schema.names).serialize()
- chunk = buf.to_pybytes() # We need bytes downstream
- del buf # Free buf on the next gc run
-
+ chunk = await loop.run_in_executor(self.pool, serialize, record_batch, self.schema.names)
yield Chunk(chunk, False)
if self.max_file_size_bytes and current_file_size + len(chunk) > self.max_file_size_bytes: