This is an automated email from the ASF dual-hosted git repository.
JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 44c228a6ff7 Fix race dropping failure cause in DriverContext.failed
before local sink abort
44c228a6ff7 is described below
commit 44c228a6ff7017506c5b63579abbb93cefbb0600
Author: Jackie Tien <[email protected]>
AuthorDate: Tue Nov 24 10:59:05 2026 +0800
Fix race dropping failure cause in DriverContext.failed before local sink
abort
---
.../execution/driver/DriverContext.java | 31 +-
.../fragment/FragmentInstanceContext.java | 20 +-
.../execution/schedule/DriverScheduler.java | 23 +-
.../execution/schedule/task/DriverTask.java | 22 +-
.../execution/driver/DriverContextTest.java | 293 ++++++++++++++++++
.../execution/schedule/DriverSchedulerTest.java | 327 +++++++++++++++++++++
6 files changed, 704 insertions(+), 12 deletions(-)
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContext.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContext.java
index 46093810b9b..fbacb33c1d2 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContext.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContext.java
@@ -23,6 +23,7 @@ import
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
import org.apache.iotdb.db.queryengine.execution.operator.OperatorContext;
import
org.apache.iotdb.db.queryengine.execution.operator.source.ExchangeOperator;
import org.apache.iotdb.db.queryengine.execution.schedule.task.DriverTaskId;
@@ -43,6 +44,8 @@ public class DriverContext {
private ExchangeOperator downstreamOperator;
private final AtomicBoolean finished = new AtomicBoolean();
+ private final Object failureLock = new Object();
+ private boolean failureReported = false;
private boolean mayHaveTmpFile = false;
@TestOnly
@@ -127,12 +130,34 @@ public class DriverContext {
}
public void failed(Throwable cause) {
- if (finished.compareAndSet(false, true)) {
- fragmentInstanceContext.failed(cause);
- finished.set(true);
+ // Serialize failure reporting so concurrent aborters wait until the
failure cause has been
+ // recorded on FragmentInstanceContext before deregistering sinks (which
read getFailureCause).
+ // If this driver has already finished, only report while the fragment is
still active so
+ // terminal fragment cleanup (which closes all drivers before calling
abortFragmentInstance)
+ // does not append synthetic aborts or duplicate failure copies.
+ synchronized (failureLock) {
+ if (failureReported) {
+ return;
+ }
+ failureReported = true;
+ try {
+ if (shouldReportFailureToFragment()) {
+ fragmentInstanceContext.failed(cause);
+ }
+ } finally {
+ finished.set(true);
+ }
}
}
+ private boolean shouldReportFailureToFragment() {
+ if (!finished.get()) {
+ return true;
+ }
+ FragmentInstanceStateMachine stateMachine =
fragmentInstanceContext.getStateMachine();
+ return stateMachine == null || !stateMachine.getState().isDone();
+ }
+
public void finished() {
finished.compareAndSet(false, true);
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
index a1369225235..810c529e140 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
@@ -41,6 +41,7 @@ import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
import org.apache.iotdb.db.queryengine.common.DeviceContext;
import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
+import
org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskAbortedException;
import org.apache.iotdb.db.queryengine.metric.DriverSchedulerMetricSet;
import org.apache.iotdb.db.queryengine.metric.QueryRelatedResourceMetricSet;
import org.apache.iotdb.db.queryengine.metric.QueryResourceMetricSet;
@@ -676,11 +677,20 @@ public class FragmentInstanceContext extends QueryContext
{
}
public Optional<Throwable> getFailureCause() {
- return Optional.ofNullable(
- stateMachine.getFailureCauses().stream()
- .filter(e -> e instanceof IoTDBException || e instanceof
IoTDBRuntimeException)
- .findFirst()
- .orElse(stateMachine.getFailureCauses().peek()));
+ Throwable firstNonAborted = null;
+ Throwable first = null;
+ for (Throwable cause : stateMachine.getFailureCauses()) {
+ if (first == null) {
+ first = cause;
+ }
+ if (cause instanceof IoTDBException || cause instanceof
IoTDBRuntimeException) {
+ return Optional.of(cause);
+ }
+ if (firstNonAborted == null && !(cause instanceof
DriverTaskAbortedException)) {
+ firstNonAborted = cause;
+ }
+ }
+ return Optional.ofNullable(firstNonAborted != null ? firstNonAborted :
first);
}
public Filter getGlobalTimeFilter() {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverScheduler.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverScheduler.java
index 7f84e3ee761..56bbf811c5b 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverScheduler.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverScheduler.java
@@ -38,9 +38,11 @@ import
org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
import org.apache.iotdb.db.queryengine.common.QueryId;
import org.apache.iotdb.db.queryengine.exception.CpuNotEnoughException;
import org.apache.iotdb.db.queryengine.execution.driver.DataDriver;
+import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
import
org.apache.iotdb.db.queryengine.execution.exchange.IMPPDataExchangeManager;
import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
import
org.apache.iotdb.db.queryengine.execution.schedule.queue.L1PriorityQueue;
import
org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.DriverTaskHandle;
import
org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.MultilevelPriorityQueue;
@@ -602,7 +604,7 @@ public class DriverScheduler implements IDriverScheduler,
IService {
task.unlock();
}
clearDriverTask(task);
- Optional<Throwable> abortCause = task.getAbortCause();
+ Optional<Throwable> abortCause = resolveAbortCause(task);
QueryId queryId = task.getDriverTaskId().getQueryId();
Map<FragmentInstanceId, Set<DriverTask>> queryRelatedTasks =
queryMap.remove(queryId);
if (queryRelatedTasks != null) {
@@ -626,5 +628,24 @@ public class DriverScheduler implements IDriverScheduler,
IService {
}
}
}
+
+ private Optional<Throwable> resolveAbortCause(DriverTask task) {
+ Optional<Throwable> abortCause = task.getAbortCause();
+ if (abortCause.isPresent() && !(abortCause.get() instanceof
DriverTaskAbortedException)) {
+ return abortCause;
+ }
+ DriverContext driverContext = task.getDriver().getDriverContext();
+ FragmentInstanceContext fragmentInstanceContext =
+ driverContext == null ? null :
driverContext.getFragmentInstanceContext();
+ if (fragmentInstanceContext != null) {
+ Optional<Throwable> fragmentCause =
fragmentInstanceContext.getFailureCause();
+ if (fragmentCause.isPresent()
+ && (!abortCause.isPresent()
+ || !(fragmentCause.get() instanceof
DriverTaskAbortedException))) {
+ return fragmentCause;
+ }
+ }
+ return abortCause;
+ }
}
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/task/DriverTask.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/task/DriverTask.java
index 285c67dd7ba..be5f2373a94 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/task/DriverTask.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/task/DriverTask.java
@@ -27,6 +27,7 @@ import org.apache.iotdb.db.queryengine.common.QueryId;
import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
+import
org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskAbortedException;
import org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskThread;
import org.apache.iotdb.db.queryengine.execution.schedule.ExecutionContext;
import
org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.DriverTaskHandle;
@@ -53,7 +54,7 @@ public class DriverTask implements IDIndexedAccessible {
private final boolean isHighestPriority;
- private Throwable abortCause;
+ private final AtomicReference<Throwable> abortCause = new
AtomicReference<>();
private final AtomicReference<Priority> priority;
@@ -165,11 +166,26 @@ public class DriverTask implements IDIndexedAccessible {
}
public Optional<Throwable> getAbortCause() {
- return Optional.ofNullable(abortCause);
+ return Optional.ofNullable(abortCause.get());
}
public void setAbortCause(Throwable abortCause) {
- this.abortCause = abortCause;
+ if (abortCause == null) {
+ return;
+ }
+ // Preserve the first genuine failure, while allowing a genuine failure to
replace an earlier
+ // synthetic DriverTaskAbortedException fallback (e.g.,
BY_INTERNAL_ERROR_SCHEDULED).
+ this.abortCause.updateAndGet(
+ existing -> {
+ if (existing == null) {
+ return abortCause;
+ }
+ if (existing instanceof DriverTaskAbortedException
+ && !(abortCause instanceof DriverTaskAbortedException)) {
+ return abortCause;
+ }
+ return existing;
+ });
}
public SettableFuture<Void> getBlockedDependencyDriver() {
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContextTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContextTest.java
new file mode 100644
index 00000000000..6996c2bfefe
--- /dev/null
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContextTest.java
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.queryengine.execution.driver;
+
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.memory.MemoryManager;
+import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
+import org.apache.iotdb.db.queryengine.common.PlanFragmentId;
+import org.apache.iotdb.db.queryengine.common.QueryId;
+import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SinkListener;
+import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener;
+import org.apache.iotdb.db.queryengine.execution.exchange.SharedTsBlockQueue;
+import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
+import
org.apache.iotdb.db.queryengine.execution.exchange.sink.LocalSinkChannel;
+import
org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceState;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
+import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
+import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
+import
org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskAbortedException;
+import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.Optional;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
+import static
com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
+
+public class DriverContextTest {
+
+ @Test
+ public void
testConcurrentFailedWaitsUntilFailureCauseRecordedBeforeLocalSinkAbort()
+ throws Exception {
+ QueryId queryId = new QueryId("driver_context_concurrent_failed_test");
+ FragmentInstanceId instanceId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceStateMachine stateMachine =
+ new FragmentInstanceStateMachine(instanceId, directExecutor());
+ FragmentInstanceContext spyContext =
+ Mockito.spy(
+ FragmentInstanceContext.createFragmentInstanceContext(instanceId,
stateMachine));
+
+ CountDownLatch enteredContextFailed = new CountDownLatch(1);
+ CountDownLatch allowRecordFailure = new CountDownLatch(1);
+ Mockito.doAnswer(
+ invocation -> {
+ enteredContextFailed.countDown();
+ Assert.assertTrue(
+ "Timed out waiting to release failure recording",
+ allowRecordFailure.await(5, TimeUnit.SECONDS));
+ return invocation.callRealMethod();
+ })
+ .when(spyContext)
+ .failed(Mockito.any());
+
+ DriverContext driverContext = new DriverContext(spyContext, 0);
+ LocalExchangePair exchange = createLocalExchange(instanceId.toThrift(),
spyContext);
+ ListenableFuture<?> blocked = exchange.sourceHandle.isBlocked();
+ Assert.assertFalse(blocked.isDone());
+
+ SemanticException causeA =
+ new SemanticException("The second and third argument must be greater
than 0");
+ SemanticException causeB = new SemanticException("Cascading abort cause");
+ AtomicReference<Throwable> threadError = new AtomicReference<>();
+
+ Thread driverThread =
+ new Thread(
+ () -> {
+ try {
+ driverContext.failed(causeA);
+ } catch (Throwable t) {
+ threadError.compareAndSet(null, t);
+ }
+ },
+ "test-driver-failed-thread");
+
+ CountDownLatch aborterStarted = new CountDownLatch(1);
+ Thread aborterThread =
+ new Thread(
+ () -> {
+ try {
+ aborterStarted.countDown();
+ // Mirrors DriverScheduler.clearDriverTask: driver.failed(...)
then abort sink.
+ driverContext.failed(causeB);
+ exchange.sinkChannel.abort();
+ } catch (Throwable t) {
+ threadError.compareAndSet(null, t);
+ }
+ },
+ "test-aborter-thread");
+
+ driverThread.start();
+ Assert.assertTrue(
+ "Driver thread did not enter FragmentInstanceContext.failed",
+ enteredContextFailed.await(5, TimeUnit.SECONDS));
+
+ aborterThread.start();
+ Assert.assertTrue(aborterStarted.await(5, TimeUnit.SECONDS));
+
+ long waitDeadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
+ while (aborterThread.getState() != Thread.State.BLOCKED
+ && aborterThread.isAlive()
+ && System.nanoTime() < waitDeadline) {
+ Thread.yield();
+ }
+ // Aborter must wait for the in-flight failed() call instead of aborting
the sink with no cause.
+ Assert.assertEquals(Thread.State.BLOCKED, aborterThread.getState());
+ Assert.assertFalse(exchange.sinkChannel.isAborted());
+ Assert.assertFalse(blocked.isDone());
+
+ allowRecordFailure.countDown();
+ driverThread.join(5000);
+ aborterThread.join(5000);
+ Assert.assertFalse(driverThread.isAlive());
+ Assert.assertFalse(aborterThread.isAlive());
+ Assert.assertNull(threadError.get());
+
+ Assert.assertTrue(driverContext.isDone());
+ Assert.assertEquals(Optional.of(causeA), spyContext.getFailureCause());
+ Assert.assertEquals(1, stateMachine.getFailureCauses().size());
+ Assert.assertTrue(exchange.sinkChannel.isAborted());
+
+ try {
+ blocked.get(5, TimeUnit.SECONDS);
+ Assert.fail("Expected ExecutionException carrying SemanticException");
+ } catch (ExecutionException e) {
+ Assert.assertSame(causeA, e.getCause());
+ }
+ }
+
+ @Test
+ public void
testFailedAfterFinishedOnActiveFragmentRecordsSingleCauseForLocalSinkAbort()
+ throws Exception {
+ QueryId queryId = new QueryId("driver_context_failed_after_finished_test");
+ FragmentInstanceId instanceId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceStateMachine stateMachine =
+ new FragmentInstanceStateMachine(instanceId, directExecutor());
+ FragmentInstanceContext context =
+ FragmentInstanceContext.createFragmentInstanceContext(instanceId,
stateMachine);
+
+ DriverContext finishedDriver = new DriverContext(context, 0);
+ DriverContext siblingDriver1 = new DriverContext(context, 1);
+ DriverContext siblingDriver2 = new DriverContext(context, 2);
+ LocalExchangePair exchange = createLocalExchange(instanceId.toThrift(),
context);
+ ListenableFuture<?> blocked = exchange.sourceHandle.isBlocked();
+
+ finishedDriver.finished();
+ siblingDriver1.finished();
+ siblingDriver2.finished();
+ Assert.assertTrue(finishedDriver.isDone());
+ Assert.assertEquals(FragmentInstanceState.RUNNING,
stateMachine.getState());
+
+ SemanticException cause =
+ new SemanticException("The second and third argument must be greater
than 0");
+ finishedDriver.failed(cause);
+ siblingDriver1.failed(cause);
+ siblingDriver2.failed(cause);
+ exchange.sinkChannel.abort();
+
+ Assert.assertEquals(FragmentInstanceState.FAILED, stateMachine.getState());
+ Assert.assertEquals(Optional.of(cause), context.getFailureCause());
+ Assert.assertEquals(1, stateMachine.getFailureCauses().size());
+ try {
+ blocked.get(5, TimeUnit.SECONDS);
+ Assert.fail("Expected ExecutionException carrying SemanticException");
+ } catch (ExecutionException e) {
+ Assert.assertSame(cause, e.getCause());
+ }
+ }
+
+ @Test
+ public void testFailureCausePrecedenceOverSyntheticAbort() {
+ QueryId queryId = new QueryId("driver_context_cause_precedence");
+ FragmentInstanceId instanceId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceStateMachine stateMachine =
+ new FragmentInstanceStateMachine(instanceId, directExecutor());
+ FragmentInstanceContext context =
+ FragmentInstanceContext.createFragmentInstanceContext(instanceId,
stateMachine);
+
+ DriverTaskAbortedException synthetic =
+ new DriverTaskAbortedException(
+ instanceId.getFullId(),
DriverTaskAbortedException.BY_INTERNAL_ERROR_SCHEDULED);
+ ArithmeticException arithmeticFailure = new ArithmeticException("long
overflow");
+ SemanticException semanticFailure = new SemanticException("invalid
argument");
+
+ context.failed(synthetic);
+ Assert.assertEquals(Optional.of(synthetic), context.getFailureCause());
+
+ context.failed(arithmeticFailure);
+ Assert.assertEquals(Optional.of(arithmeticFailure),
context.getFailureCause());
+
+ context.failed(semanticFailure);
+ Assert.assertEquals(Optional.of(semanticFailure),
context.getFailureCause());
+ }
+
+ @Test
+ public void testFailedMarksDoneEvenIfContextFailedThrows() {
+ FragmentInstanceContext mockContext =
Mockito.mock(FragmentInstanceContext.class);
+ RuntimeException notificationError = new RuntimeException("executor
rejected");
+ Mockito.doThrow(notificationError).when(mockContext).failed(Mockito.any());
+
+ DriverContext driverContext = new DriverContext(mockContext);
+ RuntimeException cause1 = new RuntimeException("cause-1");
+ RuntimeException cause2 = new RuntimeException("cause-2");
+
+ try {
+ driverContext.failed(cause1);
+ Assert.fail("Expected RuntimeException from
FragmentInstanceContext.failed");
+ } catch (RuntimeException e) {
+ Assert.assertSame(notificationError, e);
+ }
+
+ Assert.assertTrue(driverContext.isDone());
+ driverContext.failed(cause2);
+ Mockito.verify(mockContext, Mockito.times(1)).failed(cause1);
+ Mockito.verify(mockContext, Mockito.never()).failed(cause2);
+ }
+
+ private static LocalExchangePair createLocalExchange(
+ TFragmentInstanceId localFragmentInstanceId, FragmentInstanceContext
context) {
+ LocalMemoryManager mockLocalMemoryManager =
Mockito.mock(LocalMemoryManager.class);
+ MemoryManager memoryManager = new MemoryManager(1024L * 1024L);
+ MemoryPool memoryPool = new MemoryPool("test", memoryManager, 1024L *
1024L);
+ Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(memoryPool);
+
+ SharedTsBlockQueue queue =
+ new SharedTsBlockQueue(
+ localFragmentInstanceId,
+ "result_node",
+ mockLocalMemoryManager,
+ newDirectExecutorService());
+ LocalSourceHandle sourceHandle =
+ new LocalSourceHandle(
+ new TFragmentInstanceId(localFragmentInstanceId.getQueryId(), 0,
"0"),
+ "result_node",
+ queue,
+ Mockito.mock(SourceHandleListener.class));
+ SinkListener sinkListener =
+ new SinkListener() {
+ @Override
+ public void onFinish(ISink sink) {}
+
+ @Override
+ public void onEndOfBlocks(ISink sink) {}
+
+ @Override
+ public Optional<Throwable> onAborted(ISink sink) {
+ return context.getFailureCause();
+ }
+
+ @Override
+ public void onFailure(ISink sink, Throwable t) {}
+ };
+ LocalSinkChannel sinkChannel =
+ new LocalSinkChannel(localFragmentInstanceId, queue, sinkListener);
+ return new LocalExchangePair(sinkChannel, sourceHandle);
+ }
+
+ private static final class LocalExchangePair {
+ private final LocalSinkChannel sinkChannel;
+ private final LocalSourceHandle sourceHandle;
+
+ private LocalExchangePair(LocalSinkChannel sinkChannel, LocalSourceHandle
sourceHandle) {
+ this.sinkChannel = sinkChannel;
+ this.sourceHandle = sourceHandle;
+ }
+ }
+}
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverSchedulerTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverSchedulerTest.java
index 69ec3415eeb..6ccfa28f4c3 100644
---
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverSchedulerTest.java
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverSchedulerTest.java
@@ -19,26 +19,52 @@
package org.apache.iotdb.db.queryengine.execution.schedule;
import org.apache.iotdb.calc.exception.MemoryNotEnoughException;
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.commons.memory.MemoryManager;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
import org.apache.iotdb.db.queryengine.common.PlanFragmentId;
import org.apache.iotdb.db.queryengine.common.QueryId;
import org.apache.iotdb.db.queryengine.exception.CpuNotEnoughException;
+import org.apache.iotdb.db.queryengine.execution.driver.DataDriver;
+import org.apache.iotdb.db.queryengine.execution.driver.DataDriverContext;
import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
import
org.apache.iotdb.db.queryengine.execution.exchange.IMPPDataExchangeManager;
+import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager;
+import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SinkListener;
+import
org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener;
+import org.apache.iotdb.db.queryengine.execution.exchange.SharedTsBlockQueue;
+import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
+import
org.apache.iotdb.db.queryengine.execution.exchange.sink.LocalSinkChannel;
+import
org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceExecution;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceState;
+import
org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
+import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
+import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
import org.apache.iotdb.db.queryengine.execution.schedule.task.DriverTask;
import org.apache.iotdb.db.queryengine.execution.schedule.task.DriverTaskId;
import
org.apache.iotdb.db.queryengine.execution.schedule.task.DriverTaskStatus;
+import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
+import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
+import static
com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
public class DriverSchedulerTest {
@@ -182,4 +208,305 @@ public class DriverSchedulerTest {
task3.getAbortCause().get().getMessage());
Assert.assertFalse(task4.getAbortCause().isPresent());
}
+
+ @Test
+ public void
testSetAbortCauseLetsGenuineFailureReplaceFallbackAndPreservesFirstGenuine() {
+ DriverTask task = new DriverTask();
+ DriverTaskAbortedException fallback1 =
+ new DriverTaskAbortedException(
+ "task-0", DriverTaskAbortedException.BY_INTERNAL_ERROR_SCHEDULED);
+ DriverTaskAbortedException fallback2 =
+ new DriverTaskAbortedException(
+ "task-0", DriverTaskAbortedException.BY_QUERY_CASCADING_ABORTED);
+ ArithmeticException firstGenuine = new ArithmeticException("long
overflow");
+ RuntimeException secondGenuine = new RuntimeException("second genuine");
+
+ task.setAbortCause(null);
+ Assert.assertFalse(task.getAbortCause().isPresent());
+
+ task.setAbortCause(fallback1);
+ task.setAbortCause(fallback2);
+ Assert.assertSame(fallback1, task.getAbortCause().get());
+
+ // Genuine failure replaces synthetic scheduler fallback.
+ task.setAbortCause(firstGenuine);
+ Assert.assertSame(firstGenuine, task.getAbortCause().get());
+
+ // Subsequent synthetic fallbacks, genuine failures, or nulls do not
replace the first genuine.
+ task.setAbortCause(fallback2);
+ task.setAbortCause(secondGenuine);
+ task.setAbortCause(null);
+ Assert.assertSame(firstGenuine, task.getAbortCause().get());
+ }
+
+ @Test
+ public void
testToAbortedPropagatesOriginalArithmeticExceptionInsteadOfFallback()
+ throws Exception {
+ verifyArithmeticExceptionPropagatesToLocalResultQueue(true);
+ tearDown();
+ verifyArithmeticExceptionPropagatesToLocalResultQueue(false);
+ }
+
+ private void verifyArithmeticExceptionPropagatesToLocalResultQueue(boolean
replaceBeforeToAborted)
+ throws Exception {
+ QueryId queryId =
+ new QueryId("arithmetic_propagation_" + (replaceBeforeToAborted ?
"replaced" : "resolved"));
+ FragmentInstanceId rootId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceId leafId = new FragmentInstanceId(new
PlanFragmentId(queryId, 2), "0");
+
+ FragmentInstanceStateMachine rootStateMachine =
+ new FragmentInstanceStateMachine(rootId, directExecutor());
+ FragmentInstanceContext rootContext =
+ FragmentInstanceContext.createFragmentInstanceContext(rootId,
rootStateMachine);
+ FragmentInstanceStateMachine leafStateMachine =
+ new FragmentInstanceStateMachine(leafId, directExecutor());
+ FragmentInstanceContext leafContext =
+ FragmentInstanceContext.createFragmentInstanceContext(leafId,
leafStateMachine);
+
+ LocalMemoryManager mockLocalMemoryManager =
Mockito.mock(LocalMemoryManager.class);
+ MemoryManager memoryManager = new MemoryManager(1024L * 1024L);
+ MemoryPool memoryPool = new MemoryPool("test", memoryManager, 1024L *
1024L);
+ Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(memoryPool);
+ SharedTsBlockQueue rootQueue =
+ new SharedTsBlockQueue(
+ rootId.toThrift(), "result_node", mockLocalMemoryManager,
newDirectExecutorService());
+ LocalSourceHandle resultHandle =
+ new LocalSourceHandle(
+ new TFragmentInstanceId(queryId.getId(), 0, "0"),
+ "result_node",
+ rootQueue,
+ Mockito.mock(SourceHandleListener.class));
+ LocalSinkChannel rootSink =
+ new LocalSinkChannel(
+ rootId.toThrift(),
+ rootQueue,
+ new SinkListener() {
+ @Override
+ public void onFinish(ISink sink) {}
+
+ @Override
+ public void onEndOfBlocks(ISink sink) {}
+
+ @Override
+ public Optional<Throwable> onAborted(ISink sink) {
+ return rootContext.getFailureCause();
+ }
+
+ @Override
+ public void onFailure(ISink sink, Throwable t) {}
+ });
+ ListenableFuture<?> resultBlocked = resultHandle.isBlocked();
+
+ IMPPDataExchangeManager mockExchangeManager =
Mockito.mock(IMPPDataExchangeManager.class);
+ Mockito.doAnswer(
+ invocation -> {
+ TFragmentInstanceId fiId = invocation.getArgument(0);
+ if (rootId.toThrift().equals(fiId)) {
+ rootSink.abort();
+ }
+ return null;
+ })
+ .when(mockExchangeManager)
+ .forceDeregisterFragmentInstance(Mockito.any());
+ manager.setBlockManager(mockExchangeManager);
+
+ DataDriverContext rootDriverContext = new DataDriverContext(rootContext,
0);
+ rootDriverContext.setSink(rootSink);
+ IDriver rootDriver = new DataDriver(Mockito.mock(Operator.class),
rootDriverContext, 0L);
+
+ DataDriverContext leafDriverContext = new DataDriverContext(leafContext,
0);
+ leafDriverContext.setSink(Mockito.mock(ISink.class));
+ IDriver leafDriver = new DataDriver(Mockito.mock(Operator.class),
leafDriverContext, 0L);
+
+ manager.submitDrivers(queryId, Collections.singletonList(rootDriver),
QUERY_TIMEOUT_MS, null);
+ manager.submitDrivers(queryId, Collections.singletonList(leafDriver),
QUERY_TIMEOUT_MS, null);
+
+ DriverTask leafTask =
manager.getQueryMap().get(queryId).get(leafId).iterator().next();
+
+ // Leaf driver records the real ArithmeticException on its
FragmentInstanceContext first, then
+ // AbstractDriverThread stores a synthetic DriverTaskAbortedException
fallback on leafTask.
+ ArithmeticException originalFailure = new ArithmeticException("long
overflow");
+ leafDriver.failed(originalFailure);
+ leafTask.setAbortCause(
+ new DriverTaskAbortedException(
+ leafTask.getDriverTaskId().getFullId(),
+ DriverTaskAbortedException.BY_INTERNAL_ERROR_SCHEDULED));
+ if (replaceBeforeToAborted) {
+ // Simulates fragment cleanup supplying the original cause before
toAborted propagates it.
+ leafTask.setAbortCause(leafContext.getFailureCause().orElse(null));
+ }
+
+ manager.getScheduler().toAborted(leafTask);
+
+ Assert.assertEquals(Optional.of(originalFailure),
rootContext.getFailureCause());
+ try {
+ resultBlocked.get(5, TimeUnit.SECONDS);
+ Assert.fail("Expected ExecutionException carrying ArithmeticException");
+ } catch (ExecutionException e) {
+ Assert.assertSame(originalFailure, e.getCause());
+ }
+ }
+
+ @Test
+ public void testAbortFragmentInstanceReplacesSchedulerFallbackOnTask()
throws Exception {
+ manager.setBlockManager(Mockito.mock(IMPPDataExchangeManager.class));
+ QueryId queryId = new QueryId("abort_fi_replaces_fallback");
+ FragmentInstanceId leafId = new FragmentInstanceId(new
PlanFragmentId(queryId, 2), "0");
+ FragmentInstanceStateMachine leafStateMachine =
+ new FragmentInstanceStateMachine(leafId, directExecutor());
+ FragmentInstanceContext leafContext =
+ FragmentInstanceContext.createFragmentInstanceContext(leafId,
leafStateMachine);
+ DataDriverContext leafDriverContext = new DataDriverContext(leafContext,
0);
+ leafDriverContext.setSink(Mockito.mock(ISink.class));
+ IDriver leafDriver = new DataDriver(Mockito.mock(Operator.class),
leafDriverContext, 0L);
+
+ manager.submitDrivers(queryId, Collections.singletonList(leafDriver),
QUERY_TIMEOUT_MS, null);
+ DriverTask leafTask =
manager.getQueryMap().get(queryId).get(leafId).iterator().next();
+
+ ArithmeticException originalFailure = new ArithmeticException("long
overflow");
+ leafDriver.failed(originalFailure);
+ leafTask.setAbortCause(
+ new DriverTaskAbortedException(
+ leafTask.getDriverTaskId().getFullId(),
+ DriverTaskAbortedException.BY_INTERNAL_ERROR_SCHEDULED));
+
+ manager.abortFragmentInstance(leafId,
leafContext.getFailureCause().orElse(null));
+
+ Assert.assertEquals(DriverTaskStatus.ABORTED, leafTask.getStatus());
+ Assert.assertEquals(Optional.of(originalFailure),
leafTask.getAbortCause());
+ Assert.assertEquals(1, leafStateMachine.getFailureCauses().size());
+ Assert.assertEquals(Optional.of(originalFailure),
leafContext.getFailureCause());
+ }
+
+ @Test
+ public void
testToAbortedFallsBackToTaskSyntheticCauseWhenFragmentHasNoFailure()
+ throws Exception {
+ manager.setBlockManager(Mockito.mock(IMPPDataExchangeManager.class));
+ QueryId queryId = new QueryId("to_aborted_synthetic_fallback");
+ FragmentInstanceId rootId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceId leafId = new FragmentInstanceId(new
PlanFragmentId(queryId, 2), "0");
+
+ FragmentInstanceStateMachine rootStateMachine =
+ new FragmentInstanceStateMachine(rootId, directExecutor());
+ FragmentInstanceContext rootContext =
+ FragmentInstanceContext.createFragmentInstanceContext(rootId,
rootStateMachine);
+ FragmentInstanceStateMachine leafStateMachine =
+ new FragmentInstanceStateMachine(leafId, directExecutor());
+ FragmentInstanceContext leafContext =
+ FragmentInstanceContext.createFragmentInstanceContext(leafId,
leafStateMachine);
+
+ DataDriverContext rootDriverContext = new DataDriverContext(rootContext,
0);
+ rootDriverContext.setSink(Mockito.mock(ISink.class));
+ IDriver rootDriver = new DataDriver(Mockito.mock(Operator.class),
rootDriverContext, 0L);
+
+ DataDriverContext leafDriverContext = new DataDriverContext(leafContext,
0);
+ leafDriverContext.setSink(Mockito.mock(ISink.class));
+ IDriver leafDriver = new DataDriver(Mockito.mock(Operator.class),
leafDriverContext, 0L);
+
+ manager.submitDrivers(queryId, Collections.singletonList(rootDriver),
QUERY_TIMEOUT_MS, null);
+ manager.submitDrivers(queryId, Collections.singletonList(leafDriver),
QUERY_TIMEOUT_MS, null);
+
+ DriverTask leafTask =
manager.getQueryMap().get(queryId).get(leafId).iterator().next();
+ DriverTaskAbortedException fallback =
+ new DriverTaskAbortedException(
+ leafTask.getDriverTaskId().getFullId(),
+ DriverTaskAbortedException.BY_ALREADY_BEING_CANCELLED);
+ leafTask.setAbortCause(fallback);
+
+ manager.getScheduler().toAborted(leafTask);
+
+ Assert.assertEquals(Optional.of(fallback), rootContext.getFailureCause());
+ }
+
+ @Test
+ public void
testExplicitCancellationWithFourPipelinesRetainsEmptyFailureList() throws
Exception {
+ int previousDataNodeId =
IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+ IoTDBDescriptor.getInstance().getConfig().setDataNodeId(1);
+ try {
+ manager.setBlockManager(Mockito.mock(IMPPDataExchangeManager.class));
+ QueryId queryId = new QueryId("driver_context_cancel_four_pipelines");
+ FragmentInstanceId instanceId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceStateMachine stateMachine =
+ new FragmentInstanceStateMachine(instanceId, directExecutor());
+ FragmentInstanceContext context =
+ FragmentInstanceContext.createFragmentInstanceContext(instanceId,
stateMachine);
+ ISink sink = Mockito.mock(ISink.class);
+ List<IDriver> drivers = createFourPipelineDrivers(context, sink);
+ context.initializeNumOfDrivers(drivers.size());
+
+ FragmentInstanceExecution.createFragmentInstanceExecution(
+ manager,
+ instanceId,
+ context,
+ drivers,
+ sink,
+ stateMachine,
+ QUERY_TIMEOUT_MS,
+ false,
+ Mockito.mock(MPPDataExchangeManager.class));
+
+ context.cancel();
+
+ Assert.assertEquals(FragmentInstanceState.CANCELLED,
stateMachine.getState());
+ Assert.assertTrue(stateMachine.getFailureCauses().isEmpty());
+ Assert.assertTrue(context.getFailureInfoList().isEmpty());
+ Assert.assertFalse(context.getFailureCause().isPresent());
+ Assert.assertFalse(manager.getQueryMap().containsKey(queryId));
+ } finally {
+
IoTDBDescriptor.getInstance().getConfig().setDataNodeId(previousDataNodeId);
+ }
+ }
+
+ @Test
+ public void
testSingleOperatorFailureWithFourPipelinesRecordsSingleFailureEntry()
+ throws Exception {
+ int previousDataNodeId =
IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+ IoTDBDescriptor.getInstance().getConfig().setDataNodeId(1);
+ try {
+ manager.setBlockManager(Mockito.mock(IMPPDataExchangeManager.class));
+ QueryId queryId = new
QueryId("driver_context_single_failure_four_pipelines");
+ FragmentInstanceId instanceId = new FragmentInstanceId(new
PlanFragmentId(queryId, 1), "0");
+ FragmentInstanceStateMachine stateMachine =
+ new FragmentInstanceStateMachine(instanceId, directExecutor());
+ FragmentInstanceContext context =
+ FragmentInstanceContext.createFragmentInstanceContext(instanceId,
stateMachine);
+ ISink sink = Mockito.mock(ISink.class);
+ List<IDriver> drivers = createFourPipelineDrivers(context, sink);
+ context.initializeNumOfDrivers(drivers.size());
+
+ FragmentInstanceExecution.createFragmentInstanceExecution(
+ manager,
+ instanceId,
+ context,
+ drivers,
+ sink,
+ stateMachine,
+ QUERY_TIMEOUT_MS,
+ false,
+ Mockito.mock(MPPDataExchangeManager.class));
+
+ ArithmeticException operatorFailure = new ArithmeticException("long
overflow");
+ drivers.get(0).failed(operatorFailure);
+
+ Assert.assertEquals(FragmentInstanceState.FAILED,
stateMachine.getState());
+ Assert.assertEquals(1, stateMachine.getFailureCauses().size());
+ Assert.assertEquals(1, context.getFailureInfoList().size());
+ Assert.assertEquals(Optional.of(operatorFailure),
context.getFailureCause());
+ Assert.assertFalse(manager.getQueryMap().containsKey(queryId));
+ } finally {
+
IoTDBDescriptor.getInstance().getConfig().setDataNodeId(previousDataNodeId);
+ }
+ }
+
+ private static List<IDriver> createFourPipelineDrivers(
+ FragmentInstanceContext context, ISink sink) {
+ List<IDriver> drivers = new ArrayList<>(4);
+ for (int i = 0; i < 4; i++) {
+ DataDriverContext driverContext = new DataDriverContext(context, i);
+ driverContext.setSink(sink);
+ Operator operator = Mockito.mock(Operator.class);
+ drivers.add(new DataDriver(operator, driverContext, 0L));
+ }
+ return drivers;
+ }
}