alex-plekhanov commented on code in PR #11833:
URL: https://github.com/apache/ignite/pull/11833#discussion_r1933833146


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/task/QueryTasksQueue.java:
##########
@@ -0,0 +1,366 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.task;
+
+import java.lang.reflect.Array;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * A tasks queue with filtering (based on linked nodes).
+ */
+class QueryTasksQueue {
+    /**
+     * Linked list node class.
+     */
+    private static class Node {
+        /** */
+        QueryAwareTask item;
+
+        /** Next node in chain. */
+        Node next;
+
+        /** */
+        Node(QueryAwareTask item) {
+            this.item = item;
+        }
+    }
+
+    /** Current number of elements */
+    private final AtomicInteger cnt = new AtomicInteger();
+
+    /** Head of linked list. */
+    Node head;
+
+    /** Tail of linked list. */
+    private Node last;
+
+    /** */
+    private final ReentrantLock lock = new ReentrantLock();
+
+    /** Wait condition for waiting takes. */
+    private final Condition notEmpty = lock.newCondition();
+
+    /** Set of blocked (currently running) queries. */
+    private final Set<QueryKey> blockedQrys = new HashSet<>();
+
+    /**
+     * Creates a {@code LinkedBlockingQueue}.
+     */
+    QueryTasksQueue() {
+        last = head = new Node(null);
+    }
+
+    /** Queue size. */
+    public int size() {
+        return cnt.get();
+    }
+
+    /** Add a task to the queue. */
+    public void addTask(QueryAwareTask task) {
+        lock.lock();
+
+        try {
+            assert last.next == null : "Unexpected last.next: " + last.next;
+
+            last = last.next = new Node(task);
+
+            cnt.getAndIncrement();
+
+            notEmpty.signal();
+        }
+        finally {
+            lock.unlock();
+        }
+    }
+
+    /** Poll task and block query. */
+    public QueryAwareTask pollTaskAndBlockQuery(long timeout, TimeUnit unit) 
throws InterruptedException {
+        lock.lockInterruptibly();
+
+        try {
+            QueryAwareTask res;
+
+            long nanos = unit.toNanos(timeout);
+            final AtomicInteger cnt = this.cnt;
+
+            while (cnt.get() == 0 || (res = dequeue()) == null) {
+                if (nanos <= 0L)
+                    return null;
+
+                nanos = notEmpty.awaitNanos(nanos);
+            }
+
+            blockedQrys.add(res.queryKey());
+
+            if (cnt.decrementAndGet() > 0)
+                notEmpty.signal();
+
+            return res;
+        }
+        finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * Removes a first non-blocked task from the head of the queue.
+     *
+     * @return The task.
+     */
+    private QueryAwareTask dequeue() {
+        assert lock.isHeldByCurrentThread();
+        assert head.item == null : "Unexpected head.item: " + head.item;
+
+        for (Node pred = head, cur = pred.next; cur != null; pred = cur, cur = 
cur.next) {
+            if (!blockedQrys.contains(cur.item.queryKey())) { // Skip tasks 
for blocked queries.
+                QueryAwareTask res = cur.item;
+
+                unlink(pred, cur);
+
+                return res;
+            }
+        }
+
+        return null;
+    }
+
+    /** Unblock query. */
+    public void unblockQuery(QueryKey qryKey) {
+        lock.lock();
+
+        try {
+            blockedQrys.remove(qryKey);

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to