Skip to content

Commit 72a7729

Browse files
authored
[hotfix][Optimizer] Remove zero-valued in-flight task counters (#4328)
[hotfix][Optimizer] Remove per-table in-flight task counters at zero optimizingTasksMap kept a zero-count AtomicInteger entry forever after a table's last task completed: poll created entries via computeIfAbsent, acceptResult decremented but always returned the holder, and neither releaseTable nor dispose cleaned up. Every table that ever had a polled task (including deleted tables and tables that moved groups) permanently occupied one map slot. Switch acceptResult to an atomic compute that removes the entry at zero (keeping the >0 decrement guard; late accepts on a removed entry are no-ops instead of resurrecting it), and clear the entry in releaseTable/dispose. Regression test testTasksMapEntryRemovedAfterTaskCompletes (red before: map size stayed 1). Full TestOptimizingQueue 47/47 green. Fix record: docs/fix-records/2026-08-16-fix-19-optimizing-tasks-map-leak.md
1 parent 7333642 commit 72a7729

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎amoro-ams/src/main/java/org/apache/amoro/server/optimizing/OptimizingQueue.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public void releaseTable(DefaultTableRuntime tableRuntime) {
243243
process.close(false);
244244
clearProcess(process);
245245
}
246+
// Drop the per-table in-flight counter: the table left this queue, and keeping the entry
247+
// leaks one map slot per table that ever had a polled task.
248+
optimizingTasksMap.remove(tableRuntime.getTableIdentifier());
246249
LOG.info(
247250
"Release queue {} with table {}",
248251
optimizerGroup.getName(),
@@ -511,6 +514,7 @@ public void removeOptimizer(OptimizerInstance optimizerInstance) {
511514

512515
public void dispose() {
513516
this.metrics.unregister();
517+
this.optimizingTasksMap.clear();
514518
}
515519

516520
private TableOptimizingProcess findProcess(OptimizingTaskId taskId) {
@@ -697,13 +701,18 @@ private TaskRuntime<?> getTaskRuntime(OptimizingTaskId taskId) {
697701
private void acceptResult(TaskRuntime<?> taskRuntime) {
698702
lock.lock();
699703
try {
700-
optimizingTasksMap.computeIfPresent(
704+
optimizingTasksMap.compute(
701705
tableRuntime.getTableIdentifier(),
702706
(k, v) -> {
707+
if (v == null) {
708+
return null;
709+
}
703710
if (v.get() > 0) {
704711
v.decrementAndGet();
705712
}
706-
return v;
713+
// Remove the entry at zero instead of leaving an empty counter behind:
714+
// every table with a polled task would otherwise occupy a slot forever.
715+
return v.get() > 0 ? v : null;
707716
});
708717
try {
709718
tableRuntime.addTaskQuota(taskRuntime.getCurrentQuota());

‎amoro-ams/src/test/java/org/apache/amoro/server/optimizing/TestOptimizingQueue.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.junit.runner.RunWith;
7777
import org.junit.runners.Parameterized;
7878

79+
import java.lang.reflect.Field;
7980
import java.util.ArrayList;
8081
import java.util.Collections;
8182
import java.util.List;
@@ -956,6 +957,23 @@ public void testReleaseOrphanedPendingTableOnRestart() {
956957
Assert.assertEquals(OptimizingStatus.IDLE, tableRuntime.getOptimizingStatus());
957958
}
958959

960+
@Test
961+
public void testInFlightCounterEntryRemovedAfterTaskCompletes() throws Exception {
962+
DefaultTableRuntime tableRuntime = initTableWithFiles();
963+
OptimizingQueue queue = buildOptimizingGroupService(tableRuntime);
964+
TaskRuntime<?> task = queue.pollTask(optimizerThread, MAX_POLLING_TIME);
965+
Assert.assertNotNull(task);
966+
Assert.assertEquals(1, readOptimizingTasksMapSize(queue));
967+
968+
queue.ackTask(task.getTaskId(), optimizerThread);
969+
queue.completeTask(
970+
optimizerThread,
971+
buildOptimizingTaskResult(task.getTaskId(), optimizerThread.getThreadId()));
972+
973+
Assert.assertEquals(0, readOptimizingTasksMapSize(queue));
974+
queue.dispose();
975+
}
976+
959977
protected DefaultTableRuntime initTableWithFiles() {
960978
MixedTable mixedTable =
961979
(MixedTable) tableService().loadTable(serverTableIdentifier()).originalTable();
@@ -1065,6 +1083,12 @@ private OptimizingTaskResult buildOptimizingTaskResult(OptimizingTaskId taskId,
10651083
return optimizingTaskResult;
10661084
}
10671085

1086+
private int readOptimizingTasksMapSize(OptimizingQueue queue) throws Exception {
1087+
Field field = OptimizingQueue.class.getDeclaredField("optimizingTasksMap");
1088+
field.setAccessible(true);
1089+
return ((Map<?, ?>) field.get(queue)).size();
1090+
}
1091+
10681092
/**
10691093
* Simulate the loadOptimizingQueues logic: tables whose persisted optimizer group no longer
10701094
* exists (e.g., table config changed from an old deleted group to "default", but AMS restarted

0 commit comments

Comments
 (0)