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

   ## Contribution acknowledgement
   
   Yes — I confirm that this change is contributed to **Apache SeaTunnel** 
under the Apache License 2.0, and that it follows the repository contribution 
rules: minimal and focused scope, no public API/SPI/config/default-value 
change, English comments and commit message, and local formatting plus tests 
run before submission. No issue is linked; this is a self-contained performance 
fix for a hot path described below.
   
   ## Problem
   
   While a table switches from snapshot to incremental reading, 
`IncrementalSourceStreamFetcher.shouldEmit()` checks every change record of 
that table against the ranges of its finished snapshot splits 
(`IncrementalSourceStreamFetcher.java:293`, and the same call from 
`IncrementalSourceScanFetcher.java:258`). On the JDBC CDC path that check is 
`JdbcSourceFetchTaskContext.isRecordBetween()`:
   
   ```java
   SeaTunnelRowType splitKeyType =
           getSplitType(getDatabaseSchema().tableFor(getTableId(record)));
   Object[] key = SourceRecordUtils.getSplitKey(splitKeyType, record, 
getSchemaNameAdjuster());
   return SourceRecordUtils.splitKeyRangeContains(key, splitStart, splitEnd);
   ```
   
   Everything in that expression only depends on the table schema, but it is 
recomputed for every record:
   
   - `getSplitType()` collects the primary key columns of the table. 
`Table.primaryKeyColumns()` itself builds a new list through a stream 
(`primaryKeyColumnNames().stream().map(...).collect(toList())` plus 
`Collections.unmodifiableList`). The dialect then converts the column type 
again — `MySqlUtils.getSplitType()` → 
`MySqlTypeUtils.convertToSeaTunnelColumn()` constructs a new 
`MySqlValueConverters` and re-reads two config values on every call — and 
finally allocates a new `SeaTunnelRowType` with two new arrays.
   - `SourceRecordUtils.getSplitKey()` runs `SchemaNameAdjuster.adjust()`, a 
regular expression, on the split key field name for every record.
   - `SourceRecordUtils.splitKeyRangeContains()` materialises one or two 
`int[]` arrays and then walks them with three `Stream` pipelines and three 
lambdas per record.
   
   The transition window can cover a large backlog: every change record that 
arrived while the snapshot was running goes through this loop once per finished 
split.
   
   Acceptance criteria for this change:
   
   - The split key type and the adjusted split key field name are resolved once 
per table, not once per record.
   - The range decision is computed without per-record array and stream 
allocation, and returns exactly the same result as before.
   - A table schema change still invalidates the cached metadata.
   - No public API, SPI, config option, default value, or serialized format 
changes.
   
   ## Change
   
   `connector-cdc-base` only (shared by the MySQL, PostgreSQL, Oracle, SQL 
Server and DB2 CDC sources):
   
   1. `JdbcSourceFetchTaskContext` keeps a `ConcurrentHashMap<TableId, 
SplitKeyMetadata>` per fetch task context (one per read task). 
`isRecordBetween()` looks the metadata up and only calls `getSplitType()` plus 
the name adjustment on a miss.
   2. Invalidation uses the schema snapshot the entry was built from: the 
cached `Table` instance is compared by identity with 
`getDatabaseSchema().tableFor(tableId)`. Debezium replaces the `Table` instance 
as a whole when a schema change is applied, so a schema change makes the 
identity check fail and the metadata is rebuilt. The `tableFor()` call is kept 
on every record on purpose, so a DDL cannot be missed.
   3. `SourceRecordUtils.getSplitKey(SourceRecord, String)` is added for 
callers that already hold the adjusted field name; the existing three-argument 
method now delegates to it, so its behaviour and signature are unchanged.
   4. `SourceRecordUtils.splitKeyRangeContains()` evaluates its comparisons in 
plain `boolean` loops instead of `int[]` + 
`Arrays.stream(...).anyMatch/allMatch`. The comparison order and the returned 
decision are unchanged, and no comparison is skipped.
   
   Compatibility: no public API removal or rename, no new dependency, no config 
option change, no default value change, no change to the split key format or to 
checkpoint state.
   
   ## Verification
   
   Executed locally with `JAVA_HOME` pointing at Temurin 17:
   
   ```
   ./mvnw -o -q -pl seatunnel-connectors-v2/connector-cdc/connector-cdc-base 
spotless:apply
   ./mvnw -o -q -Pbenchmark -pl connector-cdc-base,seatunnel-benchmarks 
spotless:check
   ./mvnw -o -pl seatunnel-connectors-v2/connector-cdc/connector-cdc-base test
   ./mvnw -o -Pbenchmark -pl connector-cdc-base,seatunnel-benchmarks compile
   java -cp ... org.apache.seatunnel.benchmark.CdcSplitKeyBenchmark -f 1 -wi 3 
-i 5 -w 1s -r 1s
   ```
   
   - `connector-cdc-base`: **157 tests, 0 failures, 0 errors**.
   - New tests: `JdbcSourceFetchTaskContextTest` proves that 100 records of one 
table resolve the split key type once and that a new `Table` instance forces a 
re-resolution; `SourceRecordUtilsTest` covers all three range branches, a 
composite key and the new `getSplitKey` overload.
   
   JMH, `seatunnel-benchmarks`, 20 columns, one `BIGINT` split key, 
`AverageTime`, ns/op, 1 fork, 3 warmup / 5 measurement iterations:
   
   | Benchmark | ns/op |
   | --- | --- |
   | `checkBeforeOptimization` (resolution + adjustment + extraction + range 
check) | 96.270 ± 7.341 |
   | `checkAfterOptimization` (metadata lookup + extraction + range check) | 
4.441 ± 0.084 |
   | `baselineSplitKeyRangeContains` (previous, stream based) | 33.315 ± 10.684 
|
   | `splitKeyRangeContains` (current) | 3.476 ± 0.328 |
   | `splitKeyTypeResolution` (removed by the cache) | 36.771 ± 1.629 |
   | `adjustedFieldName` (removed by the cache) | 3.202 ± 0.124 |
   | `splitKeyMetadataLookup` (what replaces them) | 1.747 ± 0.254 |
   
   That is ~21.7x less work per checked record (96.270 → 4.441 ns/op) and ~9.6x 
faster for the range check alone (33.315 → 3.476 ns/op).
   
   ## Remaining risks
   
   - The benchmark measures the composed steps, not a full fetcher run against 
a database: the range check itself runs in a few nanoseconds, so it is measured 
with the JMH harness rather than an end-to-end job. The MySQL CDC end-to-end 
suite was not executed here; nothing in this change alters the emitted records, 
so its assertions are unaffected.
   - The cache is per fetch task context and bounded by the number of tables in 
the split. A recovered or newly assigned task starts with an empty cache and 
resolves each table once again.
   - Cached metadata is keyed by `TableId`; a schema change that keeps the same 
`TableId` is still detected through the `Table` identity check. If a dialect 
ever rebuilt an identical `Table` instance on every lookup, the cache would not 
help, but it also would not return stale metadata.
   
   ## Follow-up
   
   - The same per-record lookup and range check is reached from the 
snapshot-to-incremental switch only. Making the transition entry per table 
explicit (instead of re-deriving it per record) would be a scheduler-level 
change and 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