zookeeper-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [zookeeper] hanm commented on a change in pull request #1007: ZOOKEEPER-3448: Introduce MessageTracker to assist debug leader and leaner connectivity issues.
Date Mon, 08 Jul 2019 19:19:46 GMT
hanm commented on a change in pull request #1007: ZOOKEEPER-3448: Introduce MessageTracker
to assist debug leader and leaner connectivity issues.
URL: https://github.com/apache/zookeeper/pull/1007#discussion_r301259701
 
 

 ##########
 File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/util/CircularBuffer.java
 ##########
 @@ -0,0 +1,105 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.zookeeper.server.util;
+
+import java.lang.reflect.Array;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class CircularBuffer<T> {
+
+    private final T[] buffer;
+    private final int capacity;
+    private int oldest;
+    private AtomicInteger numberOfElements = new AtomicInteger();
+
+    /**
+     * Thread safe FIFO CircularBuffer implementation.
+     * When the buffer is full write operation overwrites the oldest element.
+     *
+     * Fun thing @todo, make this lock free as this is called on every quorum message  
+     * @param clazz
+     * @param capacity
+     */
+    @SuppressWarnings("unchecked")
+    public CircularBuffer(Class<T> clazz, int capacity) {
+        if (capacity <= 0) {
+            throw new IllegalArgumentException("CircularBuffer capacity should be greater
than 0");
+        }
+        this.buffer = (T[]) Array.newInstance(clazz, capacity);
+        this.capacity = capacity;
+    }
+
+    /**
+     * Puts elements in the next available index in the array.
+     * If the array is full the oldest element is replaced with
+     * the new value.
+     * @param element
+     */
+    public synchronized void write(T element) {
+        int newSize = numberOfElements.incrementAndGet();
+        if (newSize > capacity) {
+            buffer[oldest] = element;
+            oldest = ++oldest % capacity;
+            numberOfElements.decrementAndGet();
+        } else {
+            int index = (oldest + numberOfElements.get() -1) % capacity;
+            buffer[index] = element;
+        }
+    }
+
+    /**
+     * Reads from the buffer in a FIFO manner.
+     * Returns the oldest element in the buffer if the buffer ie not empty
+     * Returns null if the buffer is empty
+     * @return
+     */
+    public synchronized T read() {
 
 Review comment:
   done.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message