zhangshenghang opened a new pull request, #13762:
URL: https://github.com/apache/seatunnel/pull/13762

   ### Purpose of this pull request
   
   Yes, I am contributing this CDC connector performance change to Apache 
SeaTunnel.
   
   While a CDC split has not entered the pure increment phase, the incremental 
source record emitter resolved the offset of every record twice: once in 
`processElement` to update the split state, and once in 
`markEnterPureIncrementPhase` to compare the record against the maximum 
snapshot split high watermark. Both calls receive the same `SourceRecord`, so 
both build an identical `Offset`. On MySQL each resolution runs 
`IncrementalSourceRecordEmitter#getOffsetPosition`, which copies the whole 
Debezium offset map into a `Map<String, String>` (one `HashMap` plus one 
`String` per numeric field) and then builds a `BinlogOffset`. While snapshot 
splits are still being read, that window covers every binlog record until the 
last snapshot split watermark has passed.
   
   This change resolves the record position once and reuses it for the phase 
check.
   
   Acceptance criteria:
   
   - A record resolves its offset exactly once, and records that carry no 
position still resolve none.
   - The checkpointed split offset and the pure increment phase transition 
(including the `CompletedSnapshotPhaseEvent`) stay unchanged.
   - No configuration option, default value, public API, SPI contract or 
serialized state changes.
   
   ### Implementation
   
   - `IncrementalSourceRecordEmitter` keeps the offset resolved for the record 
currently being emitted (`resolvePosition`), clears it before each record and 
reuses it in the pure increment phase check. The private `getWatermark` helper 
is replaced by the same method because it produced an identical value.
   - The MongoDB emitter overrides `processElement` and updates its change 
stream offset in place, so it never publishes a resolved position and keeps its 
previous behavior.
   
   ### How was this patch tested?
   
   ```bash
   export 
JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
   ./mvnw -o -pl seatunnel-connectors-v2/connector-cdc/connector-cdc-base 
spotless:check
   ./mvnw -o -Pbenchmark -pl seatunnel-benchmarks spotless:check
   ./mvnw -o -pl seatunnel-connectors-v2/connector-cdc/connector-cdc-base test
   ./mvnw -o -pl 
seatunnel-connectors-v2/connector-cdc/connector-cdc-mongodb,seatunnel-connectors-v2/connector-cdc/connector-cdc-mysql
 -DskipTests compile
   ```
   
   Results: `connector-cdc-base` 153 tests, 0 failures, 0 errors; Spotless 
passes for both modules; the MongoDB and MySQL CDC modules compile against the 
changed base.
   
   The new `IncrementalSourceRecordEmitterTest` counts how many times the 
emitter builds an offset from a Debezium offset map for a batch of 10 records:
   
   | Scenario | Offset constructions before | After |
   | --- | ---: | ---: |
   | Before the pure increment phase | 20 | 10 |
   | Phase transition inside the batch | 15 | 10 |
   | Inside the pure increment phase | 10 | 10 |
   | Snapshot split state | 0 | 0 |
   | Single watermark record | 2 | 1 |
   
   The new `CdcEmitterOffsetBenchmark` replays 256 binlog records per 
invocation with deserialization stubbed out, so it measures the emitter 
bookkeeping itself:
   
   ```bash
   java -jar seatunnel-benchmarks/target/benchmarks.jar 
CdcEmitterOffsetBenchmark \
     -f 1 -wi 3 -i 5 -w 1s -r 1s
   ```
   
   | Phase | Base ops/ms | Optimized ops/ms | Change |
   | --- | ---: | ---: | --- |
   | Before the pure increment phase | 3086.322 ± 56.347 | 3607.403 ± 150.640 | 
+16.9% (324 → 277 ns/record) |
   | Inside the pure increment phase | 6346.593 ± 391.757 | 6360.009 ± 175.235 
| unchanged within error |
   
   Both runs used the same host, JDK 17 and benchmark jar, only the emitter 
implementation differs.
   
   ### Remaining risk
   
   - The benchmark isolates the emitter bookkeeping because deserialization is 
stubbed, so the percentage is not an end-to-end job throughput number. On real 
jobs the saving applies only to the window before the split reaches the pure 
increment phase, which is however exactly when snapshot splits are still 
running.
   - No MySQL CDC end-to-end test was run in this environment because 
Docker/Testcontainers is unavailable. The change does not alter emitted rows, 
the checkpointed offset or the phase transition, and the new unit tests assert 
all three.
   
   Follow-up: the offset resolution itself still allocates a string map per 
record. Keeping the raw Debezium map in `Offset` would avoid it, but that 
changes checkpoint state and needs a separate compatibility review, so it is 
out of scope here.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to