[FLINK-39633][postgres] Fix NPE in incremental snapshot backfill when WAL carries records for other captured tables#4390
Open
JNSimba wants to merge 2 commits intoapache:masterfrom
Conversation
… WAL carries records for other captured tables
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an NPE during PostgreSQL incremental snapshot backfill when the WAL stream includes change events for other captured tables by filtering scan-fetcher change records by tableId before delegating to range checks.
Changes:
- Add
tableIdequality filtering inIncrementalSourceScanFetcher#isChangeRecordInChunkRangeto ignore foreign-table change records. - Expose
isChangeRecordInChunkRangeand a snapshot-split setter for testing via@VisibleForTesting. - Add unit tests covering foreign-table filtering, same-table delegation, and non-data-change short-circuiting.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/source/reader/external/IncrementalSourceScanFetcher.java | Filters WAL change records by TableId during snapshot backfill to prevent NPEs from schema cache misses. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/test/java/org/apache/flink/cdc/connectors/base/source/reader/external/IncrementalSourceScanFetcherTest.java | Adds regression tests ensuring foreign-table records are skipped and non-data events don’t trigger tableId/range checks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+68
to
+70
| Assertions.assertThatCode(() -> fetcher.isChangeRecordInChunkRange(foreignTableRecord)) | ||
| .doesNotThrowAnyException(); | ||
| Assertions.assertThat(fetcher.isChangeRecordInChunkRange(foreignTableRecord)).isFalse(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This closes https://issues.apache.org/jira/browse/FLINK-39633.
Problem
During the snapshot backfill phase of PostgreSQL CDC (incremental snapshot), the WAL stream from the logical replication slot may carry change records for captured tables other than the one whose chunk is currently being snapshotted.
IncrementalSourceScanFetcher#isChangeRecordInChunkRangedoes not filter records by tableId before delegating toJdbcSourceFetchTaskContext#isRecordBetween, which callsgetDatabaseSchema().tableFor(record.tableId). If the foreign table's schema is not yet present inRelationAwarePostgresSchema's lazy cache, the lookup returns null andChunkUtils.getSplitColumnthrows NPE onnull.primaryKeyColumns():The streaming reader (
IncrementalSourceStreamFetcher#shouldEmit) already guards against this case viafinishedSplitsInfo.containsKey(tableId)before invokingisRecordBetween. The scan fetcher was missing the symmetric guard.Fix
Filter change records by tableId in
IncrementalSourceScanFetcher#isChangeRecordInChunkRangebefore delegating toisRecordBetween. Records whose tableId does not equalcurrentSnapshotSplit.getTableId()are skipped.Test plan
IncrementalSourceScanFetcherTestwith three cases:isRecordBetweenis never invoked)isRecordBetweengetTableIdflink-cdc-baseunit tests still pass (16/16)