diff --git a/presto-main/src/main/java/com/facebook/presto/execution/resourceGroups/InternalResourceGroup.java b/presto-main/src/main/java/com/facebook/presto/execution/resourceGroups/InternalResourceGroup.java index 58be7ef9e0d7a..6397b482bd89e 100644 --- a/presto-main/src/main/java/com/facebook/presto/execution/resourceGroups/InternalResourceGroup.java +++ b/presto-main/src/main/java/com/facebook/presto/execution/resourceGroups/InternalResourceGroup.java @@ -680,6 +680,7 @@ protected void setDirty() public void run(ManagedQueryExecution query) { + boolean isQueryQueueFull = false; synchronized (root) { if (!subGroups.isEmpty()) { throw new PrestoException(INVALID_RESOURCE_GROUP, format("Cannot add queries to %s. It is not a leaf group.", id)); @@ -697,21 +698,25 @@ public void run(ManagedQueryExecution query) group = group.parent.get(); } if (!canQueue && !canRun) { - query.fail(new QueryQueueFullException(id)); - return; - } - query.setResourceGroupQueryLimits(perQueryLimits); - if (canRun && queuedQueries.isEmpty()) { - startInBackground(query); + isQueryQueueFull = true; } else { - enqueueQuery(query); - } - query.addStateChangeListener(state -> { - if (state.isDone()) { - queryFinished(query); + query.setResourceGroupQueryLimits(perQueryLimits); + if (canRun && queuedQueries.isEmpty()) { + startInBackground(query); } - }); + else { + enqueueQuery(query); + } + query.addStateChangeListener(state -> { + if (state.isDone()) { + queryFinished(query); + } + }); + } + } + if (isQueryQueueFull) { + query.fail(new QueryQueueFullException(id)); } }