github-actions[bot] commented on code in PR #70282:
URL: https://github.com/apache/doris/pull/70282#discussion_r4616587630


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTVFCommand.java:
##########
@@ -98,65 +116,129 @@ public void run(ConnectContext ctx, StmtExecutor 
executor) throws Exception {
         executor.setPlanner(planner);
         executor.checkBlockRules();
 
-        // FE-side deletion of existing files (before BE execution)
-        PhysicalPlan physicalPlan = planner.getPhysicalPlan();
-        if (physicalPlan instanceof PhysicalTVFTableSink) {
-            PhysicalTVFTableSink<?> tvfSink = (PhysicalTVFTableSink<?>) 
physicalPlan;
-            String sinkTvfName = tvfSink.getTvfName();
-            Map<String, String> sinkProps = tvfSink.getProperties();
-            boolean deleteExisting = Boolean.parseBoolean(
-                    sinkProps.getOrDefault("delete_existing_files", "false"));
+        return planner;
+    }
 
-            if (deleteExisting && !"local".equals(sinkTvfName)) {
-                deleteExistingFilesInFE(sinkTvfName, sinkProps);
-            }
-        }
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
+        ExecutionState execution = new ExecutionState();
+        executor.registerQueryCancellationCallback(execution::cancel);
+        execution.checkCanceled();
+        NereidsPlanner planner = preparePlan(ctx, executor);
+        execution.checkCanceled();
 
-        // 3. Create coordinator
         Coordinator coordinator = EnvFactory.getInstance().createCoordinator(
                 ctx, planner, ctx.getStatsErrorEstimator());
-
         TUniqueId queryId = ctx.queryId();
-        QeProcessorImpl.INSTANCE.registerQuery(queryId,
-                new QueryInfo(ctx, "INSERT INTO TVF", coordinator));
-
+        boolean registered = false;
         try {
-            // Audit and cancellation must observe the same coordinator that 
dispatches the fragments.
+            execution.activeCoordinator = coordinator;
             executor.setCoord(coordinator);
-            coordinator.exec();
-
-            // Wait for completion
-            int timeoutS = ctx.getExecTimeoutS();
-            if (coordinator.join(timeoutS)) {
-                if (!coordinator.isDone()) {
-                    coordinator.cancel(new Status(TStatusCode.INTERNAL_ERROR, 
"Insert into TVF timeout"));
-                    ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, 
"Insert into TVF timeout");
-                    return;
+            QeProcessorImpl.INSTANCE.registerQuery(queryId,
+                    new QueryInfo(ctx, "INSERT INTO TVF", coordinator));
+            registered = true;
+
+            synchronized (execution.executionLock) {
+                execution.checkCanceled();
+                deleteExistingFiles(planner);
+                execution.checkCanceled();
+                if (ctx.getConnectType() == ConnectType.MYSQL && 
ctx.getMysqlChannel() != null) {
+                    ctx.getMysqlChannel().reset();
                 }
+                coordinator.exec();
             }
+            execution.checkCanceled();
 
-            if (coordinator.getExecStatus().ok()) {
+            // join(false) is a timeout. join(true) may also indicate a failed 
BE.
+            boolean completed = coordinator.join(ctx.getExecTimeoutS());
+            execution.checkCanceled();
+            Status executionStatus = coordinator.getExecStatus();
+            if (!completed || !coordinator.isDone()) {
+                String error = executionStatus.ok() ? "Insert into TVF 
timeout" : executionStatus.getErrorMsg();
+                coordinator.cancel(new Status(TStatusCode.INTERNAL_ERROR, 
error));
+                ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, error);
+                return;
+            }
+
+            if (executionStatus.ok()) {
                 String label = labelName.orElse(
                         String.format("tvf_insert_%x_%x", ctx.queryId().hi, 
ctx.queryId().lo));
                 long loadedRows = 0;
-                String loadedRowsStr = coordinator.getLoadCounters()
-                        .get(LoadEtlTask.DPP_NORMAL_ALL);
+                String loadedRowsStr = 
coordinator.getLoadCounters().get(LoadEtlTask.DPP_NORMAL_ALL);
                 if (loadedRowsStr != null) {
                     loadedRows = Long.parseLong(loadedRowsStr);
                 }
                 ctx.getState().setOk(loadedRows, 0, "Insert into TVF 
succeeded. label: " + label);
             } else {
-                String errMsg = coordinator.getExecStatus().getErrorMsg();
-                LOG.warn("insert into TVF failed, error: {}", errMsg);
-                ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, errMsg);
+                String error = executionStatus.getErrorMsg();
+                coordinator.cancel(executionStatus);
+                ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, error);
             }
         } catch (Exception e) {
-            LOG.warn("insert into TVF failed", e);
-            ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR,
-                    e.getMessage() == null ? "unknown error" : e.getMessage());
+            String error = e.getMessage() == null ? "unknown error" : 
e.getMessage();
+            coordinator.cancel(new Status(
+                    execution.canceled.get() ? TStatusCode.CANCELLED : 
TStatusCode.INTERNAL_ERROR, error));
+            ctx.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, error);
         } finally {
-            QeProcessorImpl.INSTANCE.unregisterQuery(queryId);
-            coordinator.close();
+            try {
+                if (registered) {
+                    QeProcessorImpl.INSTANCE.unregisterQuery(queryId);
+                }
+            } finally {
+                synchronized (execution.executionLock) {
+                    execution.activeCoordinator = null;
+                }
+                coordinator.close();
+            }
+        }
+    }
+
+    // A prepared statement reuses this command. Each callback must retain 
only its own execution's state.
+    private static final class ExecutionState {
+        // Planning does not hold this lock. Cancellation may interrupt 
planning, but must
+        // synchronize with the side effects and fragment dispatch before 
acknowledging it.
+        private final Object executionLock = new Object();
+        private final AtomicBoolean canceled = new AtomicBoolean();
+        private volatile Coordinator activeCoordinator;
+
+        /** Called by StmtExecutor even when cancellation arrives before a 
Coordinator exists. */
+        private void cancel() {
+            canceled.set(true);
+            Coordinator coordinator = activeCoordinator;
+            if (coordinator != null) {
+                // Wake a Coordinator that is waiting for workload admission 
during exec().
+                coordinator.cancel(new Status(TStatusCode.CANCELLED, "Insert 
into TVF canceled"));

Review Comment:
   [P2] Preserve the executor's timeout reason in the TVF callback
   
   When a direct S3 INSERT exceeds its query timeout during execution or 
workload admission, `ConnectContext.killByTimeout` calls `executor.cancel` with 
`TIMEOUT`. `StmtExecutor` invokes this callback before forwarding that status 
to the coordinator, so the hardcoded `CANCELLED` becomes its first terminal 
status and `CoordinatorContext.updateStatusIfOk` rejects the later `TIMEOUT`. 
`checkCanceled()` also replaces the client error with "Insert into TVF 
canceled", so timed-out inserts lose both their timeout classification and 
diagnostic. Preserve the executor's pending cancellation reason in the 
per-execution state and use it for coordinator cancellation and the propagated 
error; cover executor-originated `TIMEOUT` rather than calling 
`Coordinator.cancel` directly.



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