presto-main/src/main/java/io/prestosql/execution/resourcegroups/InternalResourceGroup.java/InternalResourceGroup/queryFinished
class InternalResourceGroup:    private void queryFinished(ManagedQueryExecution query)
    {
        synchronized (root) {
            if (!runningQueries.containsKey(query) && !queuedQueries.contains(query)) {
                // Query has already been cleaned up
                return;
            }

            ResourceUsage lastUsage = runningQueries.get(query);

            // The query is present in runningQueries
            if (lastUsage != null) {
                // CPU is measured cumulatively (i.e. total CPU used until this moment by the query). Memory is measured
                // instantaneously (how much memory the query is using at this moment). At query completion, memory usage
                // drops to zero.
                ResourceUsage finalUsage = new ResourceUsage(
                        query.getTotalCpuTime().toMillis(),
                        0L);
                ResourceUsage delta = finalUsage.subtract(lastUsage);

                runningQueries.remove(query);

                // Update usage statistics up to the root
                InternalResourceGroup group = this;
                while (group != null) {
                    group.cachedResourceUsage = group.cachedResourceUsage.add(delta);
                    InternalResourceGroup parent = group.parent.orElse(null);
                    if (parent != null) {
                        parent.descendantRunningQueries--;
                        if (parent.descendantRunningQueries == 0) {
                            parent.dirtySubGroups.remove(group);
                        }
                    }
                    group = parent;
                }
            }
            else {
                // The query must be queued
                queuedQueries.remove(query);
                InternalResourceGroup group = this;
                while (group.parent.isPresent()) {
                    group.parent.get().descendantQueuedQueries--;
                    group = group.parent.get();
                }
            }

            updateEligibility();
            return;
        }
    }