Skip to content

Commit 44c228a

Browse files
authored
Fix race dropping failure cause in DriverContext.failed before local sink abort
1 parent 45cb995 commit 44c228a

6 files changed

Lines changed: 704 additions & 12 deletions

File tree

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/DriverContext.java‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.iotdb.commons.utils.TestOnly;
2424
import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
2525
import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
26+
import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
2627
import org.apache.iotdb.db.queryengine.execution.operator.OperatorContext;
2728
import org.apache.iotdb.db.queryengine.execution.operator.source.ExchangeOperator;
2829
import org.apache.iotdb.db.queryengine.execution.schedule.task.DriverTaskId;
@@ -43,6 +44,8 @@ public class DriverContext {
4344
private ExchangeOperator downstreamOperator;
4445

4546
private final AtomicBoolean finished = new AtomicBoolean();
47+
private final Object failureLock = new Object();
48+
private boolean failureReported = false;
4649
private boolean mayHaveTmpFile = false;
4750

4851
@TestOnly
@@ -127,12 +130,34 @@ public FragmentInstanceContext getFragmentInstanceContext() {
127130
}
128131

129132
public void failed(Throwable cause) {
130-
if (finished.compareAndSet(false, true)) {
131-
fragmentInstanceContext.failed(cause);
132-
finished.set(true);
133+
// Serialize failure reporting so concurrent aborters wait until the failure cause has been
134+
// recorded on FragmentInstanceContext before deregistering sinks (which read getFailureCause).
135+
// If this driver has already finished, only report while the fragment is still active so
136+
// terminal fragment cleanup (which closes all drivers before calling abortFragmentInstance)
137+
// does not append synthetic aborts or duplicate failure copies.
138+
synchronized (failureLock) {
139+
if (failureReported) {
140+
return;
141+
}
142+
failureReported = true;
143+
try {
144+
if (shouldReportFailureToFragment()) {
145+
fragmentInstanceContext.failed(cause);
146+
}
147+
} finally {
148+
finished.set(true);
149+
}
133150
}
134151
}
135152

153+
private boolean shouldReportFailureToFragment() {
154+
if (!finished.get()) {
155+
return true;
156+
}
157+
FragmentInstanceStateMachine stateMachine = fragmentInstanceContext.getStateMachine();
158+
return stateMachine == null || !stateMachine.getState().isDone();
159+
}
160+
136161
public void finished() {
137162
finished.compareAndSet(false, true);
138163
}

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
4242
import org.apache.iotdb.db.queryengine.common.DeviceContext;
4343
import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
44+
import org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskAbortedException;
4445
import org.apache.iotdb.db.queryengine.metric.DriverSchedulerMetricSet;
4546
import org.apache.iotdb.db.queryengine.metric.QueryRelatedResourceMetricSet;
4647
import org.apache.iotdb.db.queryengine.metric.QueryResourceMetricSet;
@@ -676,11 +677,20 @@ public long getExchangeDeadlineMs() {
676677
}
677678

678679
public Optional<Throwable> getFailureCause() {
679-
return Optional.ofNullable(
680-
stateMachine.getFailureCauses().stream()
681-
.filter(e -> e instanceof IoTDBException || e instanceof IoTDBRuntimeException)
682-
.findFirst()
683-
.orElse(stateMachine.getFailureCauses().peek()));
680+
Throwable firstNonAborted = null;
681+
Throwable first = null;
682+
for (Throwable cause : stateMachine.getFailureCauses()) {
683+
if (first == null) {
684+
first = cause;
685+
}
686+
if (cause instanceof IoTDBException || cause instanceof IoTDBRuntimeException) {
687+
return Optional.of(cause);
688+
}
689+
if (firstNonAborted == null && !(cause instanceof DriverTaskAbortedException)) {
690+
firstNonAborted = cause;
691+
}
692+
}
693+
return Optional.ofNullable(firstNonAborted != null ? firstNonAborted : first);
684694
}
685695

686696
public Filter getGlobalTimeFilter() {

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/DriverScheduler.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
import org.apache.iotdb.db.queryengine.common.QueryId;
3939
import org.apache.iotdb.db.queryengine.exception.CpuNotEnoughException;
4040
import org.apache.iotdb.db.queryengine.execution.driver.DataDriver;
41+
import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
4142
import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
4243
import org.apache.iotdb.db.queryengine.execution.exchange.IMPPDataExchangeManager;
4344
import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService;
45+
import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
4446
import org.apache.iotdb.db.queryengine.execution.schedule.queue.L1PriorityQueue;
4547
import org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.DriverTaskHandle;
4648
import org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.MultilevelPriorityQueue;
@@ -602,7 +604,7 @@ public void toAborted(DriverTask task) {
602604
task.unlock();
603605
}
604606
clearDriverTask(task);
605-
Optional<Throwable> abortCause = task.getAbortCause();
607+
Optional<Throwable> abortCause = resolveAbortCause(task);
606608
QueryId queryId = task.getDriverTaskId().getQueryId();
607609
Map<FragmentInstanceId, Set<DriverTask>> queryRelatedTasks = queryMap.remove(queryId);
608610
if (queryRelatedTasks != null) {
@@ -626,5 +628,24 @@ public void toAborted(DriverTask task) {
626628
}
627629
}
628630
}
631+
632+
private Optional<Throwable> resolveAbortCause(DriverTask task) {
633+
Optional<Throwable> abortCause = task.getAbortCause();
634+
if (abortCause.isPresent() && !(abortCause.get() instanceof DriverTaskAbortedException)) {
635+
return abortCause;
636+
}
637+
DriverContext driverContext = task.getDriver().getDriverContext();
638+
FragmentInstanceContext fragmentInstanceContext =
639+
driverContext == null ? null : driverContext.getFragmentInstanceContext();
640+
if (fragmentInstanceContext != null) {
641+
Optional<Throwable> fragmentCause = fragmentInstanceContext.getFailureCause();
642+
if (fragmentCause.isPresent()
643+
&& (!abortCause.isPresent()
644+
|| !(fragmentCause.get() instanceof DriverTaskAbortedException))) {
645+
return fragmentCause;
646+
}
647+
}
648+
return abortCause;
649+
}
629650
}
630651
}

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/schedule/task/DriverTask.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
2828
import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
2929
import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
30+
import org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskAbortedException;
3031
import org.apache.iotdb.db.queryengine.execution.schedule.DriverTaskThread;
3132
import org.apache.iotdb.db.queryengine.execution.schedule.ExecutionContext;
3233
import org.apache.iotdb.db.queryengine.execution.schedule.queue.multilevelqueue.DriverTaskHandle;
@@ -53,7 +54,7 @@ public class DriverTask implements IDIndexedAccessible {
5354

5455
private final boolean isHighestPriority;
5556

56-
private Throwable abortCause;
57+
private final AtomicReference<Throwable> abortCause = new AtomicReference<>();
5758

5859
private final AtomicReference<Priority> priority;
5960

@@ -165,11 +166,26 @@ public boolean equals(Object o) {
165166
}
166167

167168
public Optional<Throwable> getAbortCause() {
168-
return Optional.ofNullable(abortCause);
169+
return Optional.ofNullable(abortCause.get());
169170
}
170171

171172
public void setAbortCause(Throwable abortCause) {
172-
this.abortCause = abortCause;
173+
if (abortCause == null) {
174+
return;
175+
}
176+
// Preserve the first genuine failure, while allowing a genuine failure to replace an earlier
177+
// synthetic DriverTaskAbortedException fallback (e.g., BY_INTERNAL_ERROR_SCHEDULED).
178+
this.abortCause.updateAndGet(
179+
existing -> {
180+
if (existing == null) {
181+
return abortCause;
182+
}
183+
if (existing instanceof DriverTaskAbortedException
184+
&& !(abortCause instanceof DriverTaskAbortedException)) {
185+
return abortCause;
186+
}
187+
return existing;
188+
});
173189
}
174190

175191
public SettableFuture<Void> getBlockedDependencyDriver() {

0 commit comments

Comments
 (0)