Skip to content

Commit bc77e6b

Browse files
[BugFix] Fix Warehouse Idle Check bug when switching warehouse (backport #56552) (#56605)
Signed-off-by: gengjun-git <[email protected]> Co-authored-by: gengjun-git <[email protected]>
1 parent 0765901 commit bc77e6b

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

fe/fe-core/src/main/java/com/starrocks/qe/StmtExecutor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,10 @@ public void execute() throws Exception {
495495
httpResultSender = new HttpResultSender((HttpConnectContext) context);
496496
}
497497

498-
if (shouldMarkIdleCheck(parsedStmt)) {
499-
WarehouseIdleChecker.increaseRunningSQL(context.getCurrentWarehouseId());
498+
final boolean shouldMarkIdleCheck = shouldMarkIdleCheck(parsedStmt);
499+
final long originWarehouseId = context.getCurrentWarehouseId();
500+
if (shouldMarkIdleCheck) {
501+
WarehouseIdleChecker.increaseRunningSQL(originWarehouseId);
500502
}
501503
try {
502504
context.getState().setIsQuery(parsedStmt instanceof QueryStatement);
@@ -807,8 +809,8 @@ public void execute() throws Exception {
807809
// restore session variable in connect context
808810
context.setSessionVariable(sessionVariableBackup);
809811

810-
if (shouldMarkIdleCheck(parsedStmt)) {
811-
WarehouseIdleChecker.decreaseRunningSQL(context.getCurrentWarehouseId());
812+
if (shouldMarkIdleCheck) {
813+
WarehouseIdleChecker.decreaseRunningSQL(originWarehouseId);
812814
}
813815

814816
recordExecStatsIntoContext();

fe/fe-core/src/main/java/com/starrocks/server/WarehouseManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ public Optional<Long> selectWorkerGroupByWarehouseId(long warehouseId) {
285285
return workerGroupId;
286286
}
287287

288+
public long getWarehouseResumeTime(long warehouseId) {
289+
try (LockCloseable ignored = new LockCloseable(rwLock.readLock())) {
290+
Warehouse warehouse = idToWh.get(warehouseId);
291+
if (warehouse == null) {
292+
return -1;
293+
} else {
294+
return warehouse.getResumeTime();
295+
}
296+
}
297+
}
298+
288299
private Optional<Long> selectWorkerGroupInternal(long warehouseId) {
289300
Warehouse warehouse = getWarehouse(warehouseId);
290301
List<Long> ids = warehouse.getWorkerGroupIds();

fe/fe-core/src/main/java/com/starrocks/warehouse/DefaultWarehouse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public List<List<String>> getWarehouseNodesInfo() {
7272
public ProcResult fetchResult() {
7373
return new BaseProcResult();
7474
}
75+
76+
@Override
77+
public long getResumeTime() {
78+
return -1L;
79+
}
7580
}

fe/fe-core/src/main/java/com/starrocks/warehouse/Warehouse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public void write(DataOutput out) throws IOException {
5656
Text.writeString(out, json);
5757
}
5858

59+
public abstract long getResumeTime();
60+
5961
public abstract Long getAnyWorkerGroupId();
6062

6163
public abstract List<Long> getWorkerGroupIds();

fe/fe-core/src/main/java/com/starrocks/warehouse/WarehouseIdleChecker.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ protected void runAfterCatalogReady() {
7676
if (runningJobCnt == 0
7777
&& lastFinishedJobTime <
7878
System.currentTimeMillis() - Config.warehouse_idle_check_interval_seconds * 2000) {
79-
warehouseIdleTime.putIfAbsent(wId, System.currentTimeMillis());
79+
long resumeTime = GlobalStateMgr.getCurrentState().getWarehouseMgr().getWarehouseResumeTime(wId);
80+
warehouseIdleTime.compute(wId, (k, v) -> {
81+
// If this is the first time to become idle, set idleTime to now.
82+
// If resumed during an idle period, change idleTime to resumeTime.
83+
if (v == null) {
84+
return System.currentTimeMillis();
85+
} else {
86+
return v < resumeTime ? resumeTime : v;
87+
}
88+
});
8089
LOG.info("warehouse: {} is idle, idle start time: {}",
8190
wId, TimeUtils.longToTimeString(warehouseIdleTime.get(wId)));
8291
} else {

0 commit comments

Comments
 (0)