Skip to content

Commit 4f26c45

Browse files
authored
[hotfix][Optimizer] Preserve keeper visibility on lookup failure (#4325)
[hotfix][Optimizer] Volatile optimizer touch time; keep groups watched on container lookup failure touchTime is written by thrift heartbeat threads and read by the keeper thread without a shared lock - a plain long field is a JMM data race (stale reads can expire live optimizers; word tearing on 32-bit JVMs). Declare it volatile. OptimizerGroupKeeper.processTask called Containers.get outside the try/finally whose finally-block re-queues the group via keepInTouch. An unknown container name threw IllegalArgumentException straight into AbstractKeeper.run's swallow-all catch, permanently removing the group from scale-out monitoring until restart. Move the lookup and the cast inside the try. Regression test testUnknownContainerKeepsGroupWatchedAndResetsMinParallelism (red before: min-parallelism stayed 2, group silently dropped). Fix record: docs/fix-records/2026-08-16-fix-15-keeper-visibility-and-container-lookup.md
1 parent 1c89469 commit 4f26c45

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,11 @@ protected void processTask(OptimizerGroupKeepingTask keepingTask) {
11251125
.setProperties(resourceGroup.getProperties())
11261126
.setThreadCount(requiredCores)
11271127
.build();
1128-
ResourceContainer rc = Containers.get(resource.getContainerName());
11291128
try {
1129+
// Containers.get throws for an unknown container name; it must stay inside the try so
1130+
// the finally-block keepInTouch still re-queues the group - otherwise a single lookup
1131+
// failure silently removes the group from scale-out monitoring until restart.
1132+
ResourceContainer rc = Containers.get(resource.getContainerName());
11301133
((AbstractOptimizerContainer) rc).requestResource(resource);
11311134
optimizerManager.createResource(resource);
11321135
} finally {

‎amoro-ams/src/main/java/org/apache/amoro/server/resource/OptimizerInstance.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class OptimizerInstance extends Resource {
2828

2929
private String token;
3030
private long startTime;
31-
private long touchTime;
31+
// Written by thrift heartbeat threads (touch) and read by the keeper thread for expiry
32+
// detection without a shared lock; volatile guarantees the keeper observes fresh heartbeats.
33+
private volatile long touchTime;
3234

3335
public OptimizerInstance() {}
3436

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,37 @@ public void testMinParallelismResetToZeroWhenNoResource() throws InterruptedExce
276276
+ ":min-parallelism should be reset to 0 when no resources available and no optimizer exists");
277277
}
278278

279+
@Test
280+
public void testUnknownContainerKeepsGroupWatchedAndResetsMinParallelism()
281+
throws InterruptedException {
282+
// Containers.get throws for an unknown container name. The lookup used to sit outside the
283+
// try/finally, so the exception skipped keepInTouch and silently removed the group from
284+
// scale-out monitoring forever. The keeper must keep watching and eventually reset
285+
// min-parallelism like any other permanently-failing scale-out.
286+
scaleOutCallCount.set(0);
287+
String groupName = TEST_GROUP_NAME + "-7";
288+
this.currentGroupName = groupName;
289+
Map<String, String> properties = Maps.newHashMap();
290+
properties.put(OptimizerProperties.OPTIMIZER_GROUP_MIN_PARALLELISM, "2");
291+
properties.put("memory", "1024");
292+
ResourceGroup resourceGroup =
293+
new ResourceGroup.Builder(groupName, "unknown-container-x")
294+
.addProperties(properties)
295+
.build();
296+
297+
optimizerManager().createResourceGroup(resourceGroup);
298+
optimizingService().createResourceGroup(resourceGroup);
299+
300+
Thread.sleep(300);
301+
302+
ResourceGroup updatedGroup = optimizerManager().getResourceGroup(groupName);
303+
Assertions.assertEquals(
304+
"0",
305+
updatedGroup.getProperties().get(OptimizerProperties.OPTIMIZER_GROUP_MIN_PARALLELISM),
306+
groupName
307+
+ ":keeper must keep watching an unknown-container group and reset min-parallelism");
308+
}
309+
279310
/**
280311
* Test scenario 4: When no resources but has optimizer, min-parallelism will be reset to
281312
* optimizer's executionParallel.

0 commit comments

Comments
 (0)