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

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

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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/runtime/EventAccumulator.java:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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 accumulator 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 EventAccumulator<K, T extends EventAccumulator.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 final Random random;
+
+    /**
+     * The map of queues keyed by K.
+     */
+    private final Map<K, Queue<T>> queues;
+
+    /**
+     * The list of available keys. Keys in this list can
+     * be delivered to pollers.
+     */
+    private final List<K> availableKeys;
+
+    /**
+     * The set of keys that are being processed.
+     */
+    private final Set<K> inflightKeys;
+
+    /**
+     * The lock for protecting access to the resources.
+     */
+    private final ReentrantLock lock;
+
+    /**
+     * The condition variable for waking up poller threads.
+     */
+    private final Condition condition;
+
+    /**
+     * The number of events in the queue.
+     */
+    private int size;
+
+    /**
+     * A boolean indicated whether the queue is closed.

Review Comment:
   This is for the whole accumulator right? Not an individual queue? There's a few other instances of queue used both for size and for closed throughout the comments.



-- 
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