sonatype-lift[bot] commented on code in PR #2018:
URL: https://github.com/apache/zookeeper/pull/2018#discussion_r1236642403


##########
zookeeper-server/src/main/java/org/apache/zookeeper/common/BatchedArrayBlockingQueue.java:
##########
@@ -0,0 +1,405 @@
+/*
+ *
+ * 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.zookeeper.common;
+
+import java.util.*;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * This implements a {@link BlockingQueue} backed by an array with fixed 
capacity.
+ *
+ * <p>This queue only allows 1 consumer thread to dequeue items and multiple 
producer threads.
+ */
+public class BatchedArrayBlockingQueue<T>
+        extends AbstractQueue<T>
+        implements BlockingQueue<T>, BatchedBlockingQueue<T> {
+
+    private final ReentrantLock lock = new ReentrantLock();
+
+    private final Condition notEmpty = lock.newCondition();
+    private final Condition notFull = lock.newCondition();
+
+    private final int capacity;
+    private final T[] data;
+
+    private int size;
+
+    private int consumerIdx;
+    private int producerIdx;
+
+    @SuppressWarnings("unchecked")
+    public BatchedArrayBlockingQueue(int capacity) {
+        this.capacity = capacity;
+        this.data = (T[]) new Object[this.capacity];
+    }
+
+    private T dequeueOne() {
+        T item = data[consumerIdx];
+        data[consumerIdx] = null;
+        if (++consumerIdx == capacity) {
+            consumerIdx = 0;
+        }
+
+        if (size-- == capacity) {
+            notFull.signalAll();
+        }
+
+        return item;
+    }
+
+    private void enqueueOne(T item) {
+        data[producerIdx] = item;
+        if (++producerIdx == capacity) {
+            producerIdx = 0;
+        }
+
+        if (size++ == 0) {
+            notEmpty.signalAll();
+        }
+    }
+
+    @Override
+    public T poll() {
+        lock.lock();
+
+        try {
+            if (size == 0) {
+                return null;
+            }
+
+            return dequeueOne();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public T peek() {
+        lock.lock();
+
+        try {
+            if (size == 0) {
+                return null;
+            }
+
+            return data[consumerIdx];
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public boolean offer(T e) {
+        lock.lock();
+
+        try {
+            if (size == capacity) {
+                return false;
+            }
+
+            enqueueOne(e);
+
+            return true;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public void put(T e) throws InterruptedException {
+        lock.lockInterruptibly();
+
+        try {
+            while (size == capacity) {
+                notFull.await();
+            }
+
+            enqueueOne(e);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    public int putAll(List<T> c) throws InterruptedException {
+        lock.lockInterruptibly();
+
+        try {
+            while (size == capacity) {
+                notFull.await();
+            }
+
+            int availableCapacity = capacity - size;
+
+            int toInsert = Math.min(availableCapacity, c.size());
+
+            int producerIdx = this.producerIdx;
+            for (int i = 0; i < toInsert; i++) {
+                data[producerIdx] = c.get(i);
+                if (++producerIdx == capacity) {
+                    producerIdx = 0;
+                }
+            }
+
+            this.producerIdx = producerIdx;
+
+            if (size == 0) {
+                notEmpty.signalAll();
+            }
+
+            size += toInsert;
+
+            return toInsert;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public void putAll(T[] a, int offset, int len) throws InterruptedException 
{
+        while (len > 0) {
+            int published = internalPutAll(a, offset, len);
+            offset += published;
+            len -= published;
+        }
+    }
+
+    private int internalPutAll(T[] a, int offset, int len) throws 
InterruptedException {
+        lock.lockInterruptibly();
+
+        try {
+            while (size == capacity) {
+                notFull.await();
+            }
+
+            int availableCapacity = capacity - size;
+            int toInsert = Math.min(availableCapacity, len);
+            int producerIdx = this.producerIdx;
+
+            // First span
+            int firstSpan = Math.min(toInsert, capacity - producerIdx);
+            System.arraycopy(a, offset, data, producerIdx, firstSpan);
+            producerIdx += firstSpan;
+
+            int secondSpan = toInsert - firstSpan;
+            if (secondSpan > 0) {
+                System.arraycopy(a, offset + firstSpan, data, 0, secondSpan);
+                producerIdx = secondSpan;
+            }
+
+            if (producerIdx == capacity) {
+                producerIdx = 0;
+            }
+
+            this.producerIdx = producerIdx;
+
+            if (size == 0) {
+                notEmpty.signalAll();
+            }
+
+            size += toInsert;
+            return toInsert;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public boolean offer(T e, long timeout, TimeUnit unit) throws 
InterruptedException {
+        long remainingTimeNanos = unit.toNanos(timeout);
+
+        lock.lockInterruptibly();
+        try {
+            while (size == capacity) {
+                if (remainingTimeNanos <= 0L) {
+                    return false;
+                }
+
+                remainingTimeNanos = notFull.awaitNanos(remainingTimeNanos);
+            }
+
+            enqueueOne(e);
+            return true;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public T take() throws InterruptedException {
+        lock.lockInterruptibly();
+
+        try {
+            while (size == 0) {
+                notEmpty.await();
+            }
+
+            return dequeueOne();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public T poll(long timeout, TimeUnit unit) throws InterruptedException {
+        long remainingTimeNanos = unit.toNanos(timeout);
+
+        lock.lockInterruptibly();
+        try {
+            while (size == 0) {
+                if (remainingTimeNanos <= 0L) {
+                    return null;
+                }
+
+                remainingTimeNanos = notEmpty.awaitNanos(remainingTimeNanos);
+            }
+
+            return dequeueOne();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public int remainingCapacity() {
+        return capacity - size;

Review Comment:
   <picture><img alt="7% of developers fix this issue" 
src="https://lift.sonatype.com/api/commentimage/fixrate/7/display.svg";></picture>
   
   <b>*THREAD_SAFETY_VIOLATION:</b>*  Read/Write race. Non-private method 
`BatchedArrayBlockingQueue.remainingCapacity()` reads without synchronization 
from `this.size`. Potentially races with write in method 
`BatchedArrayBlockingQueue.clear()`.
    Reporting because another access to the same memory occurs on a background 
thread, although this access may not.
   
   ---
   
   <details><summary>ℹ️ Expand to see all <b>@sonatype-lift</b> 
commands</summary>
   
   You can reply with the following commands. For example, reply with 
***@sonatype-lift ignoreall*** to leave out all findings.
   | **Command** | **Usage** |
   | ------------- | ------------- |
   | `@sonatype-lift ignore` | Leave out the above finding from this PR |
   | `@sonatype-lift ignoreall` | Leave out all the existing findings from this 
PR |
   | `@sonatype-lift exclude <file\|issue\|path\|tool>` | Exclude specified 
`file\|issue\|path\|tool` from Lift findings by updating your config.toml file |
   
   **Note:** When talking to LiftBot, you need to **refresh** the page to see 
its response.
   <sub>[Click here](https://github.com/apps/sonatype-lift/installations/new) 
to add LiftBot to another repo.</sub></details>
   
   



-- 
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: notifications-unsubscr...@zookeeper.apache.org

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

Reply via email to