924060929 commented on code in PR #69673:
URL: https://github.com/apache/doris/pull/69673#discussion_r4735948683
##########
fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/streaming/StreamingInsertJob.java:
##########
@@ -1205,6 +1271,7 @@ public void replayOnUpdated(StreamingInsertJob replayJob)
{
if (replayJob.getTargetProperties() != null) {
this.targetProperties = replayJob.getTargetProperties();
}
+ this.syncTables = replayJob.syncTables;
Review Comment:
Guard with `if (replayJob.syncTables != null)` (consistent with
`sourceProperties`/`targetProperties` above) to avoid overwriting `syncTables`
with `null` if a replayed record does not carry it.
##########
fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/streaming/StreamingInsertJob.java:
##########
@@ -617,6 +625,64 @@ public void alterJob(AlterJobCommand alterJobCommand)
throws AnalysisException,
}
}
+ public void prepareIncludeTables(AlterJobCommand command) throws
AnalysisException, JobException {
+ List<String> updatedTables = command.getIncludeTables();
+ if (updatedTables == null) {
+ return;
+ }
+ if (new HashSet<>(updatedTables).equals(new HashSet<>(syncTables))) {
+ return;
+ }
+ if
(command.getProperties().containsKey(StreamingJobProperties.OFFSET_PROPERTY)) {
+ throw new AnalysisException("Cannot alter include_tables and
offset together");
+ }
+ validateComputeGroupProperty(command.getProperties());
+ List<String> addedTables = new ArrayList<>(updatedTables);
+ addedTables.removeAll(syncTables);
+ Map<String, String> source = new HashMap<>(sourceProperties);
+ source.putAll(command.getSourceProperties());
+ validateAlterSourceProperties(command, source);
+ source = buildConvertedSourceProperties(source);
+ Map<String, String> target = new HashMap<>(targetProperties);
+ target.putAll(command.getTargetProperties());
+ try {
+ // Prepare resources before entering JobManager's global write
lock.
+ StreamingJobUtils.validateAndCreateTables(targetDb, dataSourceType,
Review Comment:
`prepareIncludeTables` performs irreversible external side effects (creating
Doris target tables and executing `ALTER PUBLICATION ... ADD TABLE` on PG)
before `offsetProvider.validateAlterIncludeTables(updatedTables)` is called in
`alterIncludeTables()`.
If the job has no committed incremental offset yet or tries to remove a
pending added table, `ALTER JOB` fails while leaving newly created tables and a
modified PG publication behind. Also, `prepareIncludeTables` reads
`syncTables`/`sourceProperties`/`targetProperties` without holding the job
lock, leaving a TOCTOU window before `alterJob()`.
Suggestion: validate `PAUSED` and `validateAlterIncludeTables` under the job
read lock first (and snapshot the config), then re-verify inside `alterJob()`'s
write lock that `syncTables` and status have not changed.
##########
fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/streaming/StreamingInsertJob.java:
##########
@@ -617,6 +625,64 @@ public void alterJob(AlterJobCommand alterJobCommand)
throws AnalysisException,
}
}
+ public void prepareIncludeTables(AlterJobCommand command) throws
AnalysisException, JobException {
+ List<String> updatedTables = command.getIncludeTables();
+ if (updatedTables == null) {
+ return;
+ }
+ if (new HashSet<>(updatedTables).equals(new HashSet<>(syncTables))) {
+ return;
+ }
+ if
(command.getProperties().containsKey(StreamingJobProperties.OFFSET_PROPERTY)) {
+ throw new AnalysisException("Cannot alter include_tables and
offset together");
+ }
+ validateComputeGroupProperty(command.getProperties());
+ List<String> addedTables = new ArrayList<>(updatedTables);
+ addedTables.removeAll(syncTables);
+ Map<String, String> source = new HashMap<>(sourceProperties);
+ source.putAll(command.getSourceProperties());
+ validateAlterSourceProperties(command, source);
+ source = buildConvertedSourceProperties(source);
+ Map<String, String> target = new HashMap<>(targetProperties);
+ target.putAll(command.getTargetProperties());
+ try {
+ // Prepare resources before entering JobManager's global write
lock.
+ StreamingJobUtils.validateAndCreateTables(targetDb, dataSourceType,
+ source, target, updatedTables, addedTables);
+ if (dataSourceType == DataSourceType.POSTGRES) {
+ PostgresResourceValidator.ensurePublicationCoversTables(
+ source, String.valueOf(getJobId()), updatedTables);
+ }
+ } catch (Exception e) {
+ throw new JobException("Failed to prepare include_tables change: "
+ e.getMessage(), e);
+ }
+ }
+
+ private void alterIncludeTables(List<String> updatedTables,
+ Map<String, String> newConvertedSourceProperties) throws
JobException {
+ if (updatedTables == null || new HashSet<>(updatedTables).equals(new
HashSet<>(syncTables))) {
+ return;
+ }
+ // A paused job may not have restored its committed progress after an
FE restart.
+ replayOffsetProviderIfNeed();
Review Comment:
Calling `replayOffsetProviderIfNeed()` unconditionally here has two issues:
1. `alterIncludeTables` runs while holding both `JobManager`'s global write
lock and the job's write lock. When `hasPendingIncludeTablesChange()` is true,
`replayIfNeed()` calls `StreamingJobUtils.restoreSplitsToJob()`, which executes
an internal SQL query via `StmtExecutor` — blocking the global `JobManager`
write lock on SQL/BE I/O.
2. If a previous ALTER in the same round already split chunks for a new
table (before any chunk is committed), unconditional `replayIfNeed()`
overwrites in-memory `remainingSplits` and `cdcSplitProgress` from
`offsetProviderPersist` and drops uncommitted tables from `snapshotSplits`,
contradicting the goal of preserving splitting progress.
Suggestion: only replay when the provider has not been restored since FE
restart, and avoid running `restoreSplitsToJob()` under the global write lock.
##########
fe/fe-core/src/main/java/org/apache/doris/job/offset/jdbc/JdbcSourceOffsetProvider.java:
##########
@@ -160,6 +164,63 @@ public void ensureInitialized(Long jobId, Map<String,
String> newProps) throws J
DataSourceConfigKeys.SNAPSHOT_PARALLELISM_DEFAULT));
}
+ @Override
+ public boolean hasPendingIncludeTablesChange() {
+ return addedTables != null;
+ }
+
+ private boolean hasCommittedIncrementalOffset() {
+ return MapUtils.isNotEmpty(binlogOffsetPersist);
+ }
+
+ @Override
+ public void validateAlterIncludeTables(List<String> updatedTables) throws
JobException {
+ synchronized (splitsLock) {
+ if (!hasCommittedIncrementalOffset()) {
+ throw new JobException("Cannot alter include_tables before an
incremental offset is committed");
+ }
+ if (hasPendingIncludeTablesChange() &&
!updatedTables.containsAll(addedTables)) {
+ throw new JobException("Cannot remove newly added tables until
incremental synchronization resumes");
+ }
+ }
+ }
+
+ @Override
+ public void applyIncludeTablesChange(List<String> updatedTables,
List<String> addedTables) throws JobException {
+ synchronized (splitsLock) {
+ // CDC fills missing schemas before resuming incremental reads;
retain existing baselines.
+ String retainedSchemas = filterTableSchemas(tableSchemas,
updatedTables);
+ if (!hasPendingIncludeTablesChange()) {
+ // Reset snapshot progress while preserving the committed
incremental offset for resumption.
+ clearSnapshotState();
+ this.addedTables = new ArrayList<>();
+ }
+ // Extend the pending snapshot without resetting its chunks,
watermarks or splitting cursor.
+ this.addedTables.addAll(addedTables);
+ cachedSyncTables = new ArrayList<>(updatedTables);
+ setTableSchemas(retainedSchemas);
+ hasMoreData = true;
+ }
+ }
+
+ private JdbcOffset createIncrementalOffset() {
Review Comment:
Resuming incremental reads from `T0` without `finishedSplits` high-watermark
filtering means all binlog/WAL events for newly added tables between `T0` and
snapshot time `T1` will be replayed against the `T1` schema:
- If an added table had DDL between `T0` and `T1` (or was created after
`T0`), decoding `T0`-era events with the `T1` schema can fail with schema
mismatch or replay duplicate DDL.
- If an added table had `TRUNCATE` between `T0` and `T1` (or reuses an
existing non-Unique-Key target table), replaying pre-snapshot INSERTs can
resurrect truncated rows or duplicate data.
Please verify whether `finishedSplits` filtering can be applied for added
tables, or explicitly guard/document this limitation.
##########
fe/fe-core/src/main/java/org/apache/doris/job/offset/jdbc/JdbcSourceOffsetProvider.java:
##########
@@ -160,6 +164,63 @@ public void ensureInitialized(Long jobId, Map<String,
String> newProps) throws J
DataSourceConfigKeys.SNAPSHOT_PARALLELISM_DEFAULT));
}
+ @Override
+ public boolean hasPendingIncludeTablesChange() {
+ return addedTables != null;
+ }
+
+ private boolean hasCommittedIncrementalOffset() {
+ return MapUtils.isNotEmpty(binlogOffsetPersist);
+ }
+
+ @Override
+ public void validateAlterIncludeTables(List<String> updatedTables) throws
JobException {
+ synchronized (splitsLock) {
+ if (!hasCommittedIncrementalOffset()) {
+ throw new JobException("Cannot alter include_tables before an
incremental offset is committed");
+ }
+ if (hasPendingIncludeTablesChange() &&
!updatedTables.containsAll(addedTables)) {
+ throw new JobException("Cannot remove newly added tables until
incremental synchronization resumes");
+ }
+ }
+ }
+
+ @Override
+ public void applyIncludeTablesChange(List<String> updatedTables,
List<String> addedTables) throws JobException {
+ synchronized (splitsLock) {
+ // CDC fills missing schemas before resuming incremental reads;
retain existing baselines.
+ String retainedSchemas = filterTableSchemas(tableSchemas,
updatedTables);
+ if (!hasPendingIncludeTablesChange()) {
+ // Reset snapshot progress while preserving the committed
incremental offset for resumption.
+ clearSnapshotState();
+ this.addedTables = new ArrayList<>();
Review Comment:
When `addedTables` is empty (removal-only ALTER) and
`!hasPendingIncludeTablesChange()`, initializing `this.addedTables = new
ArrayList<>()` still makes `hasPendingIncludeTablesChange()` return `true`.
This causes `hasMoreDataToConsume()` to keep returning `true` even with no
upstream changes (idle scheduling until an offset commit) and triggers
unnecessary `restoreSplitsToJob()` on restart. For a removal-only change with
no pending round, keep `addedTables == null` and only update
`cachedSyncTables`, filter `tableSchemas`, and set `needRebuildReader = true`.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterJobCommand.java:
##########
@@ -190,6 +198,22 @@ private void validate() throws Exception {
}
}
+ /** Return the requested table set, or null when include_tables is
unchanged. */
+ public List<String> getIncludeTables() throws AnalysisException {
+ if
(!sourceProperties.containsKey(DataSourceConfigKeys.INCLUDE_TABLES)) {
+ return null;
+ }
+ String tables =
sourceProperties.get(DataSourceConfigKeys.INCLUDE_TABLES);
+ if (StringUtils.isBlank(tables)) {
+ throw new AnalysisException("include_tables cannot be empty");
+ }
+ List<String> result = new ArrayList<>(new
LinkedHashSet<>(Arrays.asList(tables.split(","))));
Review Comment:
`tables.split(",")` does not trim whitespace or reject empty entries. For
example, `"orders, order_items"` yields `" order_items"` and fails source-table
lookup (unlike `ConfigUtil.getTableList()` in `cdc_client`, which trims), and
`"orders,"` produces `""`. Trim each table name and reject blank entries.
Also, if the job was originally created with `exclude_tables`, altering
`include_tables` leaves the old `exclude_tables` in `sourceProperties` —
consider removing it when merging or rejecting the conflict.
##########
fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/streaming/PostgresResourceValidator.java:
##########
@@ -117,6 +101,70 @@ public static void validate(Map<String, String>
sourceProperties, String jobId,
}
}
+ public static void ensurePublicationCoversTables(Map<String, String>
sourceProperties, String jobId,
+ List<String> tableNames) throws JobException {
+ String configuredName =
sourceProperties.get(DataSourceConfigKeys.PUBLICATION_NAME);
+ // Jobs created before per-job publication names use the CDC client's
legacy default.
+ String publicationName = StringUtils.isNotBlank(configuredName)
+ ? configuredName :
DataSourceConfigKeys.LEGACY_PUBLICATION_NAME;
+ List<String> qualifiedTables = qualifyTables(sourceProperties,
tableNames);
+ JdbcClient jdbcClient =
StreamingJobUtils.getJdbcClient(DataSourceType.POSTGRES, sourceProperties);
+ try (Connection conn = jdbcClient.getConnection()) {
+ if
(!DataSourceConfigKeys.defaultPublicationName(jobId).equals(publicationName)) {
+ validateUserPublication(conn, publicationName,
qualifiedTables);
+ } else {
+ List<String> missing = findMissingTables(conn,
publicationName, qualifiedTables);
Review Comment:
For the Doris-managed publication branch, `publicationExists(conn,
publicationName)` is not checked before running `ALTER PUBLICATION ... ADD
TABLE`. If the publication was externally dropped, this throws a raw JDBC SQL
error instead of a clear message.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]