zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [zookeeper] BELUGABEHR commented on a change in pull request #880: ZOOKEEPER-3340: Improve Queue Usage in QuorumCnxManager.java
Date Mon, 08 Apr 2019 13:52:48 GMT
BELUGABEHR commented on a change in pull request #880: ZOOKEEPER-3340: Improve Queue Usage
in QuorumCnxManager.java
URL: https://github.com/apache/zookeeper/pull/880#discussion_r273057287
 
 

 ##########
 File path: zookeeper-server/src/main/java/org/apache/zookeeper/util/CircularBlockingQueue.java
 ##########
 @@ -0,0 +1,246 @@
+/**
+ * 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.util;
+
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A bounded blocking queue backed by an array. This queue orders elements FIFO
+ * (first-in-first-out). The head of the queue is that element that has been on
+ * the queue the longest time. The tail of the queue is that element that has
+ * been on the queue the shortest time. New elements are inserted at the tail of
+ * the queue, and the queue retrieval operations obtain elements at the head of
+ * the queue. If the queue is full, the head of the queue (the oldest element)
+ * will be removed to make room for the newest element.
+ */
+public class CircularBlockingQueue<E> implements BlockingQueue<E> {
+
+  private static final Logger LOG = LoggerFactory.getLogger(CircularBlockingQueue.class);
+
+  /** Main lock guarding all access */
+  private final ReentrantLock lock;
+
+  /** Condition for waiting takes */
+  private final Condition notEmpty;
+
+  /** The array-backed queue */
+  private final ArrayDeque<E> queue;
+
+  private final int maxSize;
+
+  public CircularBlockingQueue(int queueSize) {
+    this.queue = new ArrayDeque<>(queueSize);
+    this.maxSize = queueSize;
+
+    this.lock =  new ReentrantLock();
+    this.notEmpty = this.lock.newCondition();
+  }
+
+  /**
+   * This method differs from {@link BlockingQueue#offer(Object)} in that it
+   * will remove the oldest queued element (the element at the front of the
+   * queue) in order to make room for any new elements if the queue is full.
+   *
+   * @param e the element to add
+   * @return true since it will make room for any new elements if required
+   */
+  @Override
+  public boolean offer(E e) {
 
 Review comment:
   I'm checking in the caller just so that if someone changes this down the road, it will
break.  Also,... a plugin is complaining about ignoring the return value.

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