You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "dajac (via GitHub)" <gi...@apache.org> on 2023/04/06 07:26:58 UTC

[GitHub] [kafka] dajac commented on a diff in pull request #13505: KAFKA-14462; [5/N] Add ConcurrentEventQueue

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


##########
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();

Review Comment:
   Basically, all the requests for the group coordinator will end up in this queue. Therefore, the throughput could be high. As we don't have numbers at the moment, I went with the simplest implementation for now. I think that we can easily improve it when we start to run benchmarks. I don't want to do premature optimizations.
   
   If locking becomes a concern, my idea would be to deliver events in batch instead of delivering them individually. This would have other benefits as well (e.g. write bigger batches to the log). The batch size could be configurable.



-- 
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-unsubscribe@kafka.apache.org

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