: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(batch-exports): Retry send on bigquery storage stream API (#66367)
Tomás Farías Santana1f89a584ceb5
diff --git a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.pyindex 6908c3e270d2..63439ede8877 100644--- a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py+++ b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py@@ -1201,15 +1201,46 @@ class BigQueryStorageConsumer(Consumer):"Send to stream finished",)- def send_data(self, data: bytes) -> None:+ def send_data(self, data: bytes, max_attempts: int = 5) -> None:"""Actually send the data, this is blocking."""req = types.AppendRowsRequest()req.arrow_rows.rows.serialized_record_batch = data- stream = self.stream or self.start_stream()+ # NOTE: This retry loop can generate duplicates. The default stream is+ # at-least-once, which means the chunk could have been ingested before+ # the connection is dropped. We can't tell whether that was the case+ # from here and must retry the chunk. The solution would be to switch+ # to a PENDING stream with offsets, so that we can incrementally+ # commit. But that is more complicated, as it requires keeping track of+ # said offset, so we accept the duplicates for now.+ # TODO: Evaluate switching over and do it.+ attempt = 0+ while True:+ try:+ stream = self.stream or self.start_stream()+ send = stream.send(req)+ return send.result()+ except ServiceUnavailable:+ attempt += 1+ if attempt >= max_attempts:+ raise- send = stream.send(req)- return send.result()+ backoff = min(2**attempt, 32)+ self.logger.warning(+ "Storage stream transient error encountered",+ attempt=attempt,+ backoff=backoff,+ exc_info=True,+ )+ try:+ # Reset the stream for the next attempt+ stream.close()+ except Exception:+ pass+ finally:+ self.stream = None++ time.sleep(backoff)def start_stream(self) -> AppendRowsStream:"""Start an append rows stream."""