Hangleton commented on code in PR #13505:
URL: https://github.com/apache/kafka/pull/13505#discussion_r1162819684


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/runtime/ConcurrentEventQueue.java:
##########
@@ -0,0 +1,258 @@
+/*
+ * 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.kafka.coordinator.group.runtime;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * A concurrent event queue which group events per key and ensure that only one
+ * event with a given key can't be processed concurrently.
+ *
+ * This class is threadsafe.
+ *
+ * @param <K> The type of the key of the event.
+ * @param <T> The type of the event itself. It implements the {{@link Event}} 
interface.
+ *
+ * There are a few examples about how to use it in the unit tests.
+ */
+public class ConcurrentEventQueue<K, T extends ConcurrentEventQueue.Event<K>> 
implements AutoCloseable {
+
+    /**
+     * The interface which must be implemented by all events.
+     *
+     * @param <K> The type of the key of the event.
+     */
+    public interface Event<K> {
+        K key();
+    }
+
+    /**
+     * The random generator used by this class.
+     */
+    private Random random;
+
+    /**
+     * The map of queues keyed by K.
+     */
+    private Map<K, Queue<T>> queues;
+
+    /**
+     * The list of available keys. Keys in this list can
+     * be delivered to pollers.
+     */
+    private List<K> availableKeys;
+
+    /**
+     * The set of keys that are being processed.
+     */
+    private Set<K> inflightKeys;
+
+    /**
+     * The number of events in the queue.
+     */
+    private int size;
+
+    /**
+     * A boolean indicated whether the queue is closed.
+     */
+    private boolean closed;
+
+    /**
+     * The lock for protecting access to the resources.
+     */
+    private ReentrantLock lock;
+
+    /**
+     * The condition variable for waking up poller threads.
+     */
+    private Condition condition;
+
+    public ConcurrentEventQueue() {
+        this(new Random());
+    }
+
+    public ConcurrentEventQueue(
+        Random random
+    ) {
+        this.random = random;
+        this.queues = new HashMap<>();
+        this.availableKeys = new ArrayList<>();
+        this.inflightKeys = new HashSet<>();
+        this.closed = false;
+        this.lock = new ReentrantLock();
+        this.condition = lock.newCondition();
+    }
+
+    /**
+     * Adds an {{@link Event}} to the queue.
+     *
+     * @param event An {{@link Event}}.
+     */
+    public void add(T event) {
+        lock.lock();
+        try {
+            if (closed) throw new IllegalStateException("Can't accept an event 
because the queue is close.");
+
+            K key = event.key();
+            Queue<T> queue = queues.get(key);
+            if (queue == null) {
+                queue = new LinkedList<>();
+                queues.put(key, queue);
+                if (!inflightKeys.contains(key)) {
+                    addAvailableKey(key);
+                }
+            }
+            queue.add(event);
+            size++;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * Returns the next {{@link Event}} available. This method block 
indefinitely until
+     * one event is ready or the queue is closed.
+     *
+     * @return The next event.
+     */
+    public T poll() {
+        return poll(Long.MAX_VALUE, TimeUnit.SECONDS);
+    }
+
+    /**
+     * Returns the next {{@link Event}} available. This method blocks for the 
provided

Review Comment:
   I was thinking something along `KeyedEventSet` with a `pollAny` method to 
highlight an element of any given key not already in-use can be taken. One of 
the reason it is difficult to name is that the class combines a data structure 
with a semaphore semantic over one attribute (the key) of the elements in that 
data structure. If round-robin selection could be used (I don't know) instead 
of random selection, an actual FIFO or LIFO queue of key -> [events] augmented 
with the set of keys could also be used. But I cannot think about another 
approach which is as efficient as the bespoke implementation here.
   
   On a separate note, I wonder if the random selection of keys could be 
extracted and provided to the component to make testing deterministic?
   
   On the `EventDispatcher` name: isn't dispatcher typically used for 
components which actually performs a dispatch/scheduling action? (e.g. a 
dispatcher thread)



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to