Fix task quotas not being released

In I1d2949d84, assumption was made that TaskParker#onNotReadyToStart is
called by WorkQueue when TaskParker#isReadyToStart returns false and
removed the cleaning up of acquired quotas. But that only happens when
different a TaskParker instance returns false but not the current one.
Fix that assumption by re-introducing the explicit cleanup logic.

Change-Id: Iea32b57b86acce0a96d8f29789eba3fcee234d99
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
index aa71530..5d0c23f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
@@ -71,6 +71,11 @@
 
   @Override
   public boolean isReadyToStart(WorkQueue.Task<?> task) {
+    QueueStats.Queue queue = QueueStats.Queue.fromKey(task.getQueueName());
+    if (!QueueStats.acquire(queue, 1)) {
+      return false;
+    }
+
     Optional<Project.NameKey> estimatedProject = estimateProject(task);
     List<TaskQuota> applicableQuotas = new ArrayList<>(globalQuotas);
     applicableQuotas.addAll(
@@ -84,23 +89,22 @@
                 })
             .orElse(List.of()));
 
-    QueueStats.Queue queue = QueueStats.Queue.fromKey(task.getQueueName());
-    if (!QueueStats.acquire(queue, 1)) {
-      return false;
-    }
-
     List<TaskQuota> acquiredQuotas = new ArrayList<>();
-    quotasByTask.put(task.getTaskId(), acquiredQuotas);
-
     for (TaskQuota quota : applicableQuotas) {
       if (quota.isApplicable(task)) {
         if (!quota.isReadyToStart(task)) {
           log.debug("Task [{}] will be parked due task quota rules", task);
+          QueueStats.release(queue, 1);
+          acquiredQuotas.forEach(q -> q.onStop(task));
           return false;
         }
         acquiredQuotas.add(quota);
       }
     }
+
+    if (!acquiredQuotas.isEmpty()) {
+      quotasByTask.put(task.getTaskId(), acquiredQuotas);
+    }
     return true;
   }