Skip to content

perf(mongodb-to-mongodb): single-stream CDC optimization, stateful deduplication, and timestamp-aware coalescing - #4298

Merged
michaeltle-goog merged 21 commits into
GoogleCloudPlatform:mainfrom
michaeltle-goog:perf/single-stream-cdc-optimization
Nov 11, 2026
Merged

michaeltle-goog merged 21 commits into
GoogleCloudPlatform:mainfrom
michaeltle-goog:perf/single-stream-cdc-optimization

Conversation

@michaeltle-goog

@michaeltle-goog michaeltle-goog commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR provides performance, scalability, and correctness improvements for the mongodb-to-mongodb Dataflow Flex Template across batch BACKFILL, combined BACKFILL_AND_STREAMING, and high-throughput STREAMING_CDC workloads, along with automated End-to-End (E2E) correctness integration tests (MongoDbToMongoDbIT + MongoDbTestHelper) migrating from MongoDB to Cloud Firestore Enterprise (MongoDB compatibility mode):

1. Default migrationMode Set to BACKFILL

  • Updated the default migrationMode in MongoDbToMongoDb.Options from BACKFILL_AND_STREAMING to BACKFILL so bounded batch migrations are the default unless continuous change stream replication (BACKFILL_AND_STREAMING or STREAMING_CDC) is explicitly configured.

2. Fast Canonical BSON Type Discovery & Split Generation (ReadSplitGenerator)

  • Replaced costly collection-wide type scans with fast O(1) covered _id index probes across MongoDB's canonical BSON comparison order (Number, String, Symbol, Document, BinData, ObjectId, Boolean, Date, Timestamp, Regex, MinKey/MaxKey).
  • Handled BSON boundary edge cases including pre-1970 negative epoch Dates (BsonDateTime(Long.MIN_VALUE)), Double.NaN / ±Infinity / -0.0 numeric bounds, Decimal128 full precision, BsonTimestamp(0, 1), and $type probes for unbounded string and binData buckets.

3. Background CDC Cursor Prefetching & Per-Event SDF Token Safety (MongoDbChangeStreamReader)

  • Introduced PartitionCursorHolder with a dedicated background prefetcher thread per change stream partition that drains cursor.tryNext() asynchronously into a bounded ArrayBlockingQueue (PREFETCH_QUEUE_CAPACITY = 4096), pipelining getMore RPCs across Beam Splittable DoFn (SDF) slice boundaries.
  • Guarded cursor.getResumeToken() access on the prefetcher thread via getLatestPostBatchResumeToken() and ensured exact per-event resume token serialization before tracker.tryClaim(...) so SDF checkpoints never rewind or skip events mid-slice.
  • Added $unset: "updateDescription" to the change stream aggregation pipeline when fullDocument post-images are used (updateLookup, whenAvailable, required), avoiding redundant network transfer.

4. Stateful Deduplication & Sharded documentKey Normalization (StatefulDeduplication, DocumentWithMetadata)

  • Enabled StatefulDeduplication whenever includeCdc is true (both BACKFILL_AND_STREAMING and STREAMING_CDC) to enforce Last-Write-Wins (LWW) monotonic ordering per document across partitioned change streams, worker retries, and T0 backfill/CDC overlap.
  • Normalized sharded MongoDB collection documentKeys (which include shard key fields alongside _id) to canonical {"_id": ...} keys via DocumentWithMetadata.serializeIdDocument(...) in computeDedupKey() and extractIdFromDocumentKey().

5. Timestamp-Aware Batch Write Coalescing, Composite _id Key Safety, & Shared MongoClient Lifecycle (MongoDbTransforms)

  • Updated WriteBatchesFn.flushBatch() to compare TimestampSortKey when coalescing multiple mutations for the same _id within a batch, preventing stale inserts/updates from overwriting newer updates or deletes.
  • Fixed WriteBatchesFn.putCoalescedOp() to key colMap by item.getDedupKey() (Canonical Extended JSON) when _id is a Map/Document or byte[], preserving MongoDB subdocument _id field order so composite keys with different field orders (e.g. {"tenant": "acme", "seq": 1} vs {"seq": 1, "tenant": "acme"}) are not coalesced together.
  • Added MongoDbTransforms.closeIfNotShared(MongoClient) and updated @Teardown in MongoDbBackfillReader and MongoDbChangeStreamReader so worker teardown of individual DoFn instances never closes the JVM-wide shared MongoClient in CLIENT_CACHE.

6. Binary BSON Coder, Lazy originalDocument, UUID Codec Hardening, & Stage-Isolated Bounded DLQ (DocumentWithMetadataCoder, MongoDbBackfillReader, MongoDbTransforms)

  • Replaced JSON string serialization in DocumentWithMetadataCoder with raw binary BSON wire-format encoding (RawBsonDocumentCodec) and lazy originalDocument computation (preserving pre-UDF payloads across withDocument(...) transformations for DLQ reporting).
  • Standardized BSON serialization on DocumentWithMetadata.DOCUMENT_CODEC (UuidRepresentation.STANDARD) across DocumentWithMetadata, DocumentWithMetadataCoder, and MongoDbBackfillReader checkpoint _id extraction, preventing CodecConfigurationException on documents with UUID _ids or nested UUID fields.
  • Added @DoFn.BoundedPerElement to MongoDbBackfillReader.ProcessBackfillSlotFn, passed explicit stage prefixes (udf, validate, write) to WriteToDlq (/error-<stage>), and clamped near-GlobalWindow.maxTimestamp() elements so batch DLQ writes from GroupIntoBatches are never dropped or overwritten across stages.

7. Automated End-to-End Integration Tests: MongoDB -> Cloud Firestore Enterprise (MongoDbToMongoDbIT, MongoDbTestHelper)

  • Added MongoDbToMongoDbIT and MongoDbTestHelper migrating from an isolated mongo:7.0 replica-set source Testcontainer (MongoDBResourceManager) to an ephemeral Cloud Firestore Enterprise database with MongoDB compatibility enabled (FirestoreAdminClient) per test (with optional -DsourceMongoUri, -DtargetMongoUri, and -DuseTestcontainersTarget=true overrides):
    1. backfill_heterogeneousBsonTypesAndMultiCollection_preservesAllDocuments: Default BACKFILL mode, database-wide multi-collection auto-discovery, fine-grained _id split probing (targetBackfillChunkSize=3), and byte/type-exact RawBsonDocument verification across ObjectId, String, Int32, Int64 (> 2^53), Double (±Infinity), Binary (0x00, 0x04 UUID, 0x80, 0xFF), and composite Document _ids plus Decimal128, Date, Timestamp, Boolean, IEEE 754 -0.0/NaN, Regex, Null, MinKey/MaxKey, Array, and nested Document payloads.
    2. backfillAndStreaming_concurrentOverlapMutations_deduplicatesAndConverges: BACKFILL_AND_STREAMING with changeStreamFullDocument=whenAvailable, testing concurrent updates, deletes (including UUID _id), delete-then-reinsert resurrections, and net-new inserts during backfill/CDC overlap.
    3. streamingCdc_rapidSameKeyMutationsAndMultiPartition_coalescesAndConverges: STREAMING_CDC with numChangeStreamSplits=4 and changeStreamFullDocument=updateLookup, exercising rapid same-key mutation bursts (INSERT -> UPDATE -> REPLACE -> UPDATE, INSERT -> UPDATE -> DELETE, INSERT -> DELETE -> INSERT resurrection, UUID _id delete), a >64-event burst, and post-idle prefetcher wake-up.
    4. streamingCdcAndBackfill_udfStartAtOperationTimeAndDlq_routesValidAndFailedDocs: startAtOperationTime oplog replay filtering, JavaScript UDF transformation and null filtering, UDF exceptions routed to retryable/ DLQ with lazy originalDocument preserved, and target permanent write rejections (__fs_invalid__ reserved field / $jsonSchema) routed to permanent/ DLQ.

Testing

  • Unit Tests + Spotless + Checkstyle:
    • JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 mvn spotless:check checkstyle:check test -pl v2/mongodb-to-mongodb: 179/179 tests passing (0 failures).
  • Automated E2E Integration Tests (MongoDbToMongoDbIT):
    • Verified in CI across both Dataflow Templates Integration Smoke Tests (DirectRunner -> Cloud Firestore Enterprise: 2/2 passing in 52.3s) and Dataflow Templates Integration Tests (Dataflow Flex Template -> Cloud Firestore Enterprise: 2/2 passing in 569.7s): 4/4 suites passing (0 failures).

…and timestamp-aware coalescing

- Enforce stateful deduplication whenever CDC mutations are enabled (standalone or combined with backfill) to drop stale out-of-order mutations across change streams and retries.
- Add timestamp sort key comparison in WriteBatchesFn batch coalescing so older events within an unordered batch do not overwrite newer replacements or deletes.
- Add unit tests for stale insert after delete and newer insert after delete coalescing.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers comprehensive performance and correctness enhancements for the mongodb-to-mongodb Dataflow Flex Template. By introducing robust stateful deduplication, timestamp-aware batch coalescing, and optimized binary serialization, the changes significantly improve the reliability and efficiency of streaming CDC pipelines. Additionally, the inclusion of a background prefetcher for change stream cursors helps mitigate latency bottlenecks.

Highlights

  • Stateful Deduplication: Enforced stateful deduplication for all CDC mutations to ensure strict monotonic ordering and drop stale, out-of-order events.
  • Timestamp-Aware Coalescing: Implemented logic in WriteBatchesFn to inspect timestamps during batch coalescing, preventing stale inserts or updates from overwriting newer operations.
  • Throughput Optimizations: Reduced network overhead by stripping redundant updateDescription payloads and implemented a binary BSON coder to lower GC pressure.
  • Change Stream Prefetching: Introduced a background prefetcher thread to pipeline network RPCs, improving performance across SDF slice boundaries.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@michaeltle-goog
michaeltle-goog marked this pull request as draft September 24, 2026 14:37

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several performance optimizations and robustness improvements to the MongoDB-to-MongoDB pipeline. Key changes include caching deduplication keys, migrating the DocumentWithMetadataCoder to deterministic binary BSON serialization, implementing a background prefetcher thread to pipeline change stream events across SDF boundaries, and optimizing type-isolated split generation with O(1) covered index scans. Additionally, bulk write coalescing now respects event timestamps to prevent stale updates. The reviewer feedback suggests closing the MongoDB cursor before joining the prefetcher thread to prevent blocking on network calls, uniquely naming prefetcher threads using an atomic counter for better debugging, and catching ArithmeticException when converting Decimal128 values to avoid potential crashes on NaN or infinite values.

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 67.77108% with 428 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.61%. Comparing base (6e68e20) to head (a5a66b2).
⚠️ Report is 206 commits behind head on main.

Files with missing lines Patch % Lines
...loud/teleport/v2/transforms/MongoDbTransforms.java 70.05% 83 Missing and 73 partials ⚠️
...oud/teleport/v2/transforms/ReadSplitGenerator.java 62.00% 98 Missing and 54 partials ⚠️
.../cloud/teleport/v2/templates/MongoDbToMongoDb.java 4.76% 40 Missing ⚠️
...eport/v2/transforms/MongoDbChangeStreamReader.java 80.57% 14 Missing and 20 partials ⚠️
...d/teleport/v2/transforms/DocumentWithMetadata.java 67.90% 17 Missing and 9 partials ⚠️
.../teleport/v2/transforms/MongoDbBackfillReader.java 78.02% 7 Missing and 13 partials ⚠️

❌ Your patch check has failed because the patch coverage (67.77%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4298      +/-   ##
============================================
+ Coverage     56.35%   57.61%   +1.25%     
- Complexity     7858     7963     +105     
============================================
  Files          1154     1170      +16     
  Lines         73194    75594    +2400     
  Branches       8580     9070     +490     
============================================
+ Hits          41252    43553    +2301     
+ Misses        29158    29101      -57     
- Partials       2784     2940     +156     
Components Coverage Δ
spanner-templates 84.00% <ø> (-0.48%) ⬇️
spanner-import-export 72.31% <ø> (+3.25%) ⬆️
spanner-live-forward-migration 87.64% <ø> (-1.33%) ⬇️
spanner-live-reverse-replication 79.84% <ø> (-1.03%) ⬇️
spanner-bulk-migration 88.29% <ø> (-0.71%) ⬇️
gcs-spanner-dv 86.55% <ø> (-1.55%) ⬇️
Files with missing lines Coverage Δ
...eport/v2/transforms/DocumentWithMetadataCoder.java 95.52% <100.00%> (-0.26%) ⬇️
.../teleport/v2/transforms/MongoDbBackfillReader.java 67.22% <78.02%> (+7.82%) ⬆️
...d/teleport/v2/transforms/DocumentWithMetadata.java 71.37% <67.90%> (+1.26%) ⬆️
...eport/v2/transforms/MongoDbChangeStreamReader.java 68.91% <80.57%> (+8.57%) ⬆️
.../cloud/teleport/v2/templates/MongoDbToMongoDb.java 14.53% <4.76%> (-0.66%) ⬇️
...oud/teleport/v2/transforms/ReadSplitGenerator.java 67.85% <62.00%> (+19.87%) ⬆️
...loud/teleport/v2/transforms/MongoDbTransforms.java 71.81% <70.05%> (+12.30%) ⬆️

... and 159 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…dress review feedback

- Initialize PartitionCursorHolder.latestPostBatchToken to null and clear it whenever a non-null event is prefetched so a coalescing poll timeout during an in-flight tryNext() does not rewind lastSeenResumeToken to an older token.
- Add unique thread ID suffix to mongodb-cdc-prefetcher threads for clearer thread dumps.
- Return null from ReadSplitGenerator.asBigDecimal for non-finite Double/Decimal128 bounds (NaN, +/-Infinity) so generateProbedNumberSplits falls back to the unsplit $type filter without excluding NaN or mis-interpolating Infinity, while mapping Decimal128 negative zero (-0) to BigDecimal.ZERO.
- Update dedupStateRetentionHours helpText and apply Spotless formatting in MongoDbToMongoDb.
- Add unit tests covering prefetcher coalescing timeout, token mismatch cursor rebuilds, out-of-order stale delete coalescing, connection string pool/socket timeouts, and non-finite/negative-zero numeric split bounds.
…SON split edge cases

Ensure consistent BSON encoding, exact change stream checkpoint tokens, and safe split generation across edge-case BSON types:

- Configure a shared Document codec with UuidRepresentation.STANDARD in DocumentWithMetadata, DocumentWithMetadataCoder, and ApplyUdfFn so java.util.UUID values round-trip through binary BSON coding and Extended JSON deduplication keys without CodecConfigurationException.
- Store BsonDocument directly in ChangeStreamRestriction with lazy JSON serialization, eliminating RESUME_TOKEN_SERIALIZATION_STRIDE so every event claims its exact resume token without per-event JSON serialization overhead.
- Synchronize PartitionCursorHolder.getLatestPostBatchResumeToken() and guard against non-empty eventQueue, clear lastTryNextWasNull on fresh poll requests, and mark running=false on prefetcher exceptions.
- Guard {$unset: "updateDescription"} in buildChangeStreamPipeline when fullDocumentStrategy is "default".
- Handle non-finite numeric bounds before $sample quantile splitting, support shared BSON sort rank between symbol and string in probeActiveTypeBounds, deduplicate rounded numeric split boundaries, and omit empty filters in generateIndexSliceFilters when numSplits exceeds 62 for string-only keys.
- Return null from DocumentWithMetadata.getId() for empty or malformed documentKey strings, validate 32-bit epoch-second bounds in parseStartAtOperationTime, and avoid duplicate pre-UDF JSON serialization in ApplyUdfFn.
@michaeltle-goog michaeltle-goog added the improvement Making existing code better label Oct 19, 2026
Change the default migrationMode parameter and fallback from BACKFILL_AND_STREAMING to BACKFILL so one-time historical backfill runs by default unless streaming CDC is explicitly requested.
…support UUID in backfill checkpointing

Prevent DoFn @teardown in MongoDbBackfillReader and MongoDbChangeStreamReader from closing JVM-wide shared MongoClient instances cached in MongoDbTransforms.CLIENT_CACHE, and serialize backfill checkpoint _id values using DocumentWithMetadata.DOCUMENT_CODEC so documents with UUID _ids or nested UUID fields do not throw CodecConfigurationException.
…oDbToMongoDbIT) and fix DLQ/composite _id bugs
…bToMongoDbIT and apply Java readability simplifications
Apache Beam's PTransform.getName() returns the default class name unless a name is passed to the PTransform(String) constructor, causing UDF, validation, and write WriteToDlq stages in bounded pipelines to share the same output prefix and overwrite earlier stage DLQ files with empty shards.

Pass explicit stage names ('udf', 'validate', 'write') to WriteToDlq so each pipeline stage writes to distinct error-<stage> output files.
…Helper.

Embedding heterogeneous BSON fixtures, endpoint provisioning, launch configuration, DLQ artifact inspection, and deep BSON parity checks directly inside MongoDbToMongoDbIT obscured the end-to-end test scenarios beneath setup boilerplate.

Move all heterogeneous BSON, CDC, UDF, and DLQ document fixtures into immutable static RawBsonDocument constants in MongoDbTestHelper, and encapsulate MongoDB/Firestore Enterprise provisioning, LaunchConfig construction, UDF/DLQ staging, and BSON parity verification inside TestEnvironment.
@michaeltle-goog
michaeltle-goog marked this pull request as ready for review October 23, 2026 14:05

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant performance optimizations, robustness improvements, and feature enhancements to the MongoDB-to-MongoDB template. Key updates include rewriting DocumentWithMetadataCoder to use binary BSON serialization, implementing a background prefetcher thread for change streams to pipeline network RPCs, optimizing split generation via O(1) covered index seeks, and ensuring strict BSON fidelity (such as standard UUID representation and NaN/Infinity handling). Additionally, the default migration mode is changed to BACKFILL, connection pool/socket timeouts are made configurable, and extensive integration tests are added. The reviewer feedback suggests wrapping database operations within the ConditionCheck in MongoDbTestHelper in a try-catch block to gracefully handle transient errors and prevent immediate test failures.

…c writer pool.

Running bounded backfill pipelines on DataflowRunner previously defaulted to Legacy Batch execution, which replaced GroupIntoBatches with a full-dataset disk-shuffle barrier and skewed shard keys onto a subset of workers. Additionally, WriteBatchesFn created a separate MongoClient connection pool per DoFn instance and executed bulkWrite batches synchronously on the calling thread, causing initial TLS handshake storms and multi-minute bundle processing stalls.

Enable Streaming Engine execution when running on DataflowRunner so bounded backfill reads and sharded batched writes pipeline concurrently across all workers while still terminating automatically upon completion. Share a single JVM-wide MongoClient and bounded async write executor pool across WriteBatchesFn instances on each worker, preserving per-document ordering across overlapping batches within a bundle and reducing initial retry backoff from 2s to 200ms.
Large ObjectId collections experience severe partition size skew under WiredTiger's random cursor $sample stage, which under-samples shallower B-tree branches near the end of high-rate inserts and leaves multi-million-document tail partitions on a single worker. In addition, MongoDbBackfillReader previously processed all assigned partitions within a concurrency slot sequentially without supporting non-zero trySplit requests, preventing idle workers from stealing unstarted partitions from slower slots, and checkpointed every 2,000 documents with 2,000-document getMore batches.

Weight adjacent sampled ObjectId intervals by their 24-bit monotonic counter delta, refine wide multi-second gaps via bounded O(1) covered index seeks at 1-second boundaries, and interpolate partition boundaries while preserving the 5-byte processUnique identifier (raising the $sample cap from 5,000 to 50,000). Track an exclusive upper partition bound on BackfillSlotRestriction so BackfillSlotRestrictionTracker.trySplit can hand off remaining unstarted whole partitions to idle workers, raise default backfill cursor batch size to 5,000 and slice size to 25,000, and expose --backfillCursorBatchSize and --backfillMaxDocsPerSlice for document-size-specific tuning.
Per-DoFn Guava RateLimiter instances previously throttled each deserialized WriteBatchesFn thread independently rather than bounding aggregate worker throughput, continued enforcing a fixed ceiling after target database tablets finished splitting, and started the ramp-up clock during @setup before the first buffered batch arrived.

Share a single WorkerRateLimiter instance per worker JVM per WriteWithDlq transform, lazily anchor the warm-up clock on the first acquire() call, and support uncapped exponential warm-up (when initialWriteRatePerWorker > 0 and maxWriteRatePerWorker <= 0) that doubles per minute over writeRateRampUpMinutes before disabling itself for lock-free steady-state writes.
Collections mixing BSON string and symbol _id values share the same canonical sort rank in the _id index, allowing global min/max probes to miss interleaved sibling types, while same-second 24-bit ObjectId counter wraparounds could weight unoccupied counter gaps during boundary interpolation. Concurrent SDF work stealing in MongoDbBackfillReader could also have its narrowed upper partition bound overwritten by stale worker claims, aborted async write bundles could overlap subsequent bundles or leak DLQ state on reused DoFn instances, and streaming DLQ windowing could fail or drop records flushed at the end of GlobalWindow on Streaming Engine.

Probe shared-rank string/symbol siblings in ReadSplitGenerator.probeActiveTypeBounds and bound same-second ObjectId wraparound intervals via covered range seeks. Clamp narrowed partition upper bounds inside synchronized BackfillSlotRestrictionTracker.tryClaim and handle empty or checkpoint-interrupted slots cleanly in ProcessBackfillSlotFn. Isolate async write futures, DLQ queues, and counters per bundle in BundleWriteState with startBundle/teardown quiescence, canonicalize Pattern/Iterable/CodeWithScope batch coalescing keys, honor bounded WorkerRateLimiter caps below initial rates, and clamp near-maxTimestamp streaming DLQ records with full allowed lateness so end-of-GlobalWindow write failures are preserved.
…ing.

In unbounded STREAMING_CDC pipelines, DLQ failures emitted mid-bundle from UDFs
or validation carry BoundedWindow.TIMESTAMP_MIN_VALUE, while end-of-GlobalWindow
flushes carry GlobalWindow.INSTANCE.maxTimestamp(), causing FixedWindows
assignment in WriteToDlq to either expire immediately or stall indefinitely when
the upstream change-stream watermark remains bounded. Additionally, backfill
split probing ordered rare BSON types after string/symbol rather than canonical
MongoDB comparison order, and WriteBatchesFn relied on raw Java Map keys for
array-backed or cross-width numeric _id values instead of canonical BSON keys.

Key changes:
- Stamp DLQ JSON records with wall-clock Instant.now() in WriteToDlq and add a
  10-second processing-time early pane trigger with allowed lateness so live
  STREAMING_CDC failures flush reliably without dropping panes.
- Order ReadSplitGenerator.KNOWN_TYPE_BUCKETS across all 17 non-array BSON type
  buckets in strict MongoDB canonical comparison order, and simplify
  getSharedRankSibling with explicit string/symbol bucket constants.
- Canonicalize Number, Decimal128, Binary/byte[], BsonValue, Pattern, Symbol,
  Code/CodeWithScope, Map, and Iterable _id values in DocumentWithMetadata and
  WriteBatchesFn.coalesceOperations so rapid same-key mutations coalesce
  accurately across representations.
- Guard invalid target collection names in WriteFn and WriteBatchesFn against
  IllegalArgumentException, routing rejected records to permanent DLQ, and bound
  diagnostic pre-image sample logging in MongoDbChangeStreamReader.
- Expand unit tests and MongoDbToMongoDbIT end-to-end suites to cover live
  STREAMING_CDC retryable/permanent DLQ flushing, Binary and cross-width numeric
  _id coalescing, and canonical rare BSON _id backfill parity.
@michaeltle-goog

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant performance optimizations, scalability enhancements, and bug fixes to the MongoDB-to-MongoDB replication template. Key improvements include a binary BSON coder for DocumentWithMetadata to reduce serialization overhead, a background prefetcher for change stream events to pipeline network RPCs, and a per-worker write rate limiter with exponential or linear warm-up. Additionally, it optimizes split generation with counter-weighted ObjectId boundaries and type-isolated splits, and refactors DLQ writing with wall-clock timestamping. The code review feedback highlights a critical concurrency issue in the WorkerRateLimiter.acquire method, where sleeping while holding a ReentrantLock serializes rate-limited writes and degrades throughput; the reviewer suggests calculating the wait time within a synchronized block and sleeping outside of it to restore concurrency.

Updating Beam Distribution metrics on SharedWritePool's async writer threads
missed Beam's thread-local MetricsContainer and logged MetricsEnvironment
errors. Additionally, DocumentWithMetadata.computeDedupKey() serialized raw
Extended JSON numeric wrappers rather than canonical numeric keys, routing
cross-width numeric _id mutations across distinct dedup/write shards, and
udfStreamConvergedCheck lacked a try/catch guard during ConditionCheck polling.

Key changes:
- Buffer bulkWriteLatencyMs samples in BundleWriteState and drain them on the
  Beam runner thread in WriteBatchesFn.finishBundle.
- Extract DocumentWithMetadata.canonicalNumericIdKey and share it across
  computeDedupKey() and WriteBatchesFn.canonicalCoalesceKey so equivalent Int32,
  Int64, Double, and Decimal128 _id values share identical dedup and shard keys.
- Wrap MongoDbTestHelper.udfStreamConvergedCheck in try/catch for transient poll
  resilience and add unit test coverage for latency metrics and numeric sharding.
@JingqiZhang1102

Copy link
Copy Markdown

[nit nit] Here processElement invokes the 3-argument withFailure. This will keep the old retryCount. The other 4-argument withFailure will update retryCount to newRetryCount. Should we actually use the 4-argument version, and also check dlqMaxRetries?

e.g. if there is a bad document.
In ApplyUdfFn.processElement, the UDF throws and exception. ApplyUdfFn calls withFailure() which copies retryCount = 0. WriteToDlq writes _metadata_retry_count: 0. When an operator replays that DLQ folder, it reads retryCount = 0, fails UDF again, and again writes metadata_retry_count = 0.

@JingqiZhang1102

JingqiZhang1102 commented Nov 6, 2026 •

Copy link
Copy Markdown

[nit maybe I misunderstood this!] I'm curious if the method writeToDlq() has an edge case (https://github.com/michaeltle-goog/DataflowTemplates/blob/17c306f81e26403535d2d6680b722db0926dc603/v2/mongodb-to-mongodb/src/main/java/com/google/cloud/teleport/v2/transforms/MongoDbTransforms.java#L2159)

e.g. dlqMaxRetries = 3 allows 1 initial failure plus 3 DLQ retries before promoting to PERMANENT.

  • initial run fails
  • DLQ replay 1 fails
  • DLQ replay 2 fails
  • On DLQ replay 3, the document (retryCount = 3) is read from retryable/ and times out again, calling writeToDlq() with isPermanent = false
    At the beginning of writeToDlq, we check if isPermanent is true. In this case, it's false, so we will add one to dlqRetriesCount.
    Then, inside the for loop, nextRetryCount = 3 + 1 = 4, and 4>3, so now isPerm = true and errorType = PERMANENT.
    The document is written to permanent/ with retryCount = 4. However, from the metric perspective, we report 1 DLQ retry and 0 permanent failure in this round.

(Sorry the change in this file is quite large so definitely I could have misinterpreted the workflow here!)

…ility.

Reconsuming UDF failures from DLQ called the 3-argument withFailure helper,
leaving _metadata_retry_count at 0 across replays, and writeToDlq tallied
dlqRetriesCount upfront before promoting items exceeding dlqMaxRetries to
PERMANENT. Additionally, ReadSplitGenerator caught broad Exception, exposed
internal helpers as public, and left several data-driven split paths unstubbed
in unit tests.

Key changes:
- Wire dlqMaxRetries into ApplyUdfFn so UDF exceptions and null returns
  increment retryCount via 4-arg withFailure and promote to PERMANENT once
  nextRetryCount > dlqMaxRetries.
- Tally permanentFailuresCount and dlqRetriesCount per item from isPerm inside
  WriteFn.writeToDlq and WriteBatchesFn.writeToDlq.
- Narrow catch (Exception) blocks in ReadSplitGenerator to MongoException,
  restrict internal helpers and nested types to package-private
  @VisibleForTesting, return Optional<BigDecimal> from asBigDecimal, extract
  named TypeBucket constants with ImmutableList.of, and probe null/undefined
  shared-rank siblings.
- Fix unstubbed 0-arg find() mocks in ReadSplitGeneratorTest and
  MongoDbBackfillReaderTest and expand unit test coverage across quantile
  boundaries, proportional split weighting, >2^53 Int64 bounds, and DLQ replay.
@michaeltle-goog

Copy link
Copy Markdown
Contributor Author

[nit nit] Here processElement invokes the 3-argument withFailure. This will keep the old retryCount...

@JingqiZhang1102 Done. Updated ApplyUdfFn in a5a66b225 to accept dlqMaxRetries (wired from options.getDlqMaxRetries()), increment nextRetryCount, promote to ErrorType.PERMANENT once nextRetryCount > dlqMaxRetries, and emit via the 4-argument withFailure(..., retryCount) so reconsumed UDF failures track retry counts and promote to permanent/. Added unit test coverage for DLQ replay promotion.

@michaeltle-goog

Copy link
Copy Markdown
Contributor Author

[nit maybe I misunderstood this!] I'm curious if the method writeToDlq() has an edge case...

@JingqiZhang1102 Done. Updated WriteBatchesFn.writeToDlq and WriteFn.writeToDlq in a5a66b225 to tally permanentFailuresCount and dlqRetriesCount per item from isPerm inside the loop, so items promoted to PERMANENT when nextRetryCount > dlqMaxRetries increment permanentFailures rather than dlqRetries. Added unit test coverage verifying the counter values.

@michaeltle-goog
michaeltle-goog merged commit 7e12239 into GoogleCloudPlatform:main Nov 11, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Making existing code better size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants