[BugFix] Remove redundant status setting in CancelableAnalyzeTask to avoid overriding StatisticsExecutor status (backport #62538) (#62571)

Signed-off-by: stephen <stephen5217@163.com>
Co-authored-by: stephen <91597003+stephen-shelby@users.noreply.github.com>
This commit is contained in:
mergify[bot] 2025-09-01 15:01:14 +08:00 committed by GitHub
parent 5aba68ee1f
commit 95b4145579
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 4 deletions

View File

@ -55,9 +55,6 @@ public class CancelableAnalyzeTask implements RunnableFuture<Void> {
try {
originalTask.run();
if (!cancelled) {
analyzeStatus.setStatus(StatsConstants.ScheduleStatus.FINISH);
}
} catch (Throwable t) {
exception = t;
analyzeStatus.setStatus(StatsConstants.ScheduleStatus.FAILED);

View File

@ -392,6 +392,8 @@ class CancelableAnalyzeTaskTest {
analyzeStatus.setProgress(75);
allowTaskToContinue.await();
analyzeStatus.setProgress(100);
// Simulate StatisticsExecutor setting status to FINISH
analyzeStatus.setStatus(StatsConstants.ScheduleStatus.FINISH);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Task interrupted", e);
@ -429,7 +431,14 @@ class CancelableAnalyzeTaskTest {
Maps.newHashMap(), LocalDateTime.now()
);
task = new CancelableAnalyzeTask(createSimpleTask(), scheduleStatus);
// Create a task that sets FINISH status to simulate StatisticsExecutor behavior
Runnable taskWithStatusUpdate = () -> {
taskExecuted.set(true);
executionCount.incrementAndGet();
scheduleStatus.setStatus(StatsConstants.ScheduleStatus.FINISH);
};
task = new CancelableAnalyzeTask(taskWithStatusUpdate, scheduleStatus);
executeTaskInThread();
assertEquals(StatsConstants.ScheduleStatus.FINISH, scheduleStatus.getStatus());
@ -452,12 +461,29 @@ class CancelableAnalyzeTaskTest {
);
}
@Test
void shouldNotOverrideFailedStatusSetInternallyByTask() throws Exception {
task = new CancelableAnalyzeTask(createTaskThatSetsFailedStatus(), analyzeStatus);
executeTaskInThread();
assertAll("Internal FAILED status preservation",
() -> assertTrue(task.isDone(), "Task should be done"),
() -> assertFalse(task.isCancelled(), "Task should not be cancelled"),
() -> assertTrue(taskExecuted.get(), "Original task should have executed"),
() -> assertEquals(StatsConstants.ScheduleStatus.FAILED, analyzeStatus.getStatus(),
"Status should remain FAILED set by StatisticsExecutor")
);
}
private Runnable createSimpleTask() {
return () -> {
taskExecuted.set(true);
executionCount.incrementAndGet();
try {
Thread.sleep(50);
// Simulate StatisticsExecutor setting status to FINISH
analyzeStatus.setStatus(StatsConstants.ScheduleStatus.FINISH);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Task interrupted", e);
@ -508,6 +534,15 @@ class CancelableAnalyzeTaskTest {
};
}
private Runnable createTaskThatSetsFailedStatus() {
return () -> {
taskExecuted.set(true);
executionCount.incrementAndGet();
// Simulate StatisticsExecutor setting FAILED status internally
analyzeStatus.setStatus(StatsConstants.ScheduleStatus.FAILED);
};
}
private void executeTaskInThread() throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {