hello-stephen commented on code in PR #70352:
URL: https://github.com/apache/doris/pull/70352#discussion_r4731096996


##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -3477,25 +3540,89 @@ public void buildOrDeleteTableInvertedIndices(Database 
db, OlapTable olapTable,
         }
     }
 
+    // Inverted index supports light schema change in both cloud and local 
mode.
+    // NGRAM_BF supports it only in cloud mode.
+    private static boolean isLightDropIndexSupported(Index index, boolean 
enableAddIndexForNewData) {
+        if (Config.isCloudMode()) {
+            return enableAddIndexForNewData
+                    && (index.getIndexType() == IndexType.NGRAM_BF
+                    || index.getIndexType() == IndexType.INVERTED);
+        }
+        return index.getIndexType() == IndexType.INVERTED || 
index.getIndexType() == IndexType.ANN;
+    }
+
+    // Reject conflicting jobs and mixed add/drop operations before publishing 
index metadata.
+    private void checkLightDropIndexPreconditions(Database db, OlapTable 
olapTable, List<AlterClause> alterClauses,
+            boolean enableAddIndexForNewData) throws UserException {
+        if (!Config.enable_light_index_change) {
+            return;
+        }
+        List<Index> droppedIndexes = new ArrayList<>();
+        for (AlterClause alterClause : alterClauses) {
+            if (!(alterClause instanceof DropIndexClause)
+                    || ((DropIndexClause) alterClause).hasPartitionSpec()) {
+                continue;
+            }
+            String indexName = ((DropIndexClause) alterClause).getIndexName();
+            olapTable.getIndexes().stream()
+                    .filter(index -> 
index.getIndexName().equalsIgnoreCase(indexName))
+                    .filter(index -> isLightDropIndexSupported(index, 
enableAddIndexForNewData))
+                    .findFirst()
+                    .ifPresent(droppedIndexes::add);
+        }
+        if (droppedIndexes.isEmpty()) {
+            return;
+        }
+        if (alterClauses.stream().anyMatch(alterClause -> alterClause 
instanceof CreateIndexClause
+                && ((CreateIndexClause) 
alterClause).getIndex().isLightAddIndexSupported(enableAddIndexForNewData))) {
+            throw new DdlException("Can not add and drop indexes in one ALTER 
statement,"
+                    + " please run them in separate statements.");
+        }
+        getCloudClusterName();
+        checkNoIndexChangeJobOnPartitions(db, olapTable, droppedIndexes, new 
HashSet<>(olapTable.getPartitionIds()));
+    }
+
+    // Throws if any of the partitions still has an unfinished index change 
job on the base index.
+    // Jobs are matched by partition ID because a partition can be renamed 
while its job is unfinished.
+    private void checkNoIndexChangeJobOnPartitions(Database db, OlapTable 
olapTable, List<Index> alterIndexes,
+            Set<Long> partitionIds) throws DdlException {
+        // Scan the retained job history once instead of once per partition, 
the table write lock is held here.
+        for (IndexChangeJob indexChangeJob : 
ImmutableList.copyOf(indexChangeJobs.values())) {
+            // Cheap partition check first, the predicate compares index lists.
+            if (partitionIds.contains(indexChangeJob.getPartitionId())
+                    && isConflictingIndexChangeJob(indexChangeJob, 
olapTable.getBaseIndexId(), db.getId(),
+                    olapTable.getId(), alterIndexes)) {
+                throw new DdlException("partition " + 
olapTable.getPartition(indexChangeJob.getPartitionId()).getName()
+                        + " of table " + olapTable.getName()
+                        + " has an unfinished index change job, please retry 
after it finishes.");
+            }
+        }
+    }
+
+    // Jobs are matched by partition ID because a partition can be renamed 
while its job is unfinished.
     public boolean hasIndexChangeJobOnPartition(
-            long originIndexId, long dbId, long tableId, String partitionName,
-            List<Index> alterIndexes, boolean isDrop) {
+            long originIndexId, long dbId, long tableId, long partitionId, 
List<Index> alterIndexes) {
         // TODO: this is temporary methods
         for (IndexChangeJob indexChangeJob : 
ImmutableList.copyOf(indexChangeJobs.values())) {
-            if (indexChangeJob.getOriginIndexId() == originIndexId
-                    && indexChangeJob.getDbId() == dbId
-                    && indexChangeJob.getTableId() == tableId
-                    && indexChangeJob.getPartitionName().equals(partitionName)
-                    && ((Config.isNotCloudMode() && 
indexChangeJob.hasSameAlterInvertedIndex(isDrop, alterIndexes))
-                    || Config.isCloudMode())
-                    && !indexChangeJob.isDone()) {
-                // if JobState is done (CANCELLED or FINISHED), also allow 
user to create job again
+            if (indexChangeJob.getPartitionId() == partitionId
+                    && isConflictingIndexChangeJob(indexChangeJob, 
originIndexId, dbId, tableId, alterIndexes)) {
                 return true;
             }
         }
         return false;
     }
 
+    private static boolean isConflictingIndexChangeJob(IndexChangeJob 
indexChangeJob, long originIndexId,
+            long dbId, long tableId, List<Index> alterIndexes) {
+        // if JobState is done (CANCELLED or FINISHED), also allow user to 
create job again
+        return indexChangeJob.getOriginIndexId() == originIndexId
+                && indexChangeJob.getDbId() == dbId
+                && indexChangeJob.getTableId() == tableId
+                && ((Config.isNotCloudMode() && 
indexChangeJob.hasAlterInvertedIndex(alterIndexes))
+                || Config.isCloudMode())
+                && !indexChangeJob.isDone();

Review Comment:
   [P2] Fence cancelled partition jobs before allowing another operation
   
   `isDone()` includes CANCELLED, but `cancelInternal()` only removes FE queue 
entries; an already submitted `AgentBatchTask` retains and sends its requests 
without consulting that queue. If a partition DROP is cancelled while its 
sender is delayed before one BE, a subsequent `BUILD INDEX idx ON t 
PARTITION(p1)` can reach FINISHED first, then the old DROP can arrive and 
delete the same index ID. Partition DROP retains the catalog definition, so 
this BUILD remains valid; local BE does not check job/schema generations. Keep 
the conflict until cancelled requests are quiescent, or fence stale requests 
before permitting the next operation.



-- 
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]

Reply via email to