snvijaya commented on a change in pull request #1907: HADOOP-16854 Fix to 
prevent OutOfMemoryException and Make the threadpool and bytebuffer pool common 
across all AbfsOutputStream instances
URL: https://github.com/apache/hadoop/pull/1907#discussion_r399029674
 
 

 ##########
 File path: 
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsByteBufferPool.java
 ##########
 @@ -0,0 +1,140 @@
+/**
+ * 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.hadoop.fs.azurebfs.services;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+import java.util.concurrent.ArrayBlockingQueue;
+
+import static 
org.apache.hadoop.fs.azurebfs.constants.FileSystemConfigurations.MAX_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE;
+import static 
org.apache.hadoop.fs.azurebfs.constants.FileSystemConfigurations.MIN_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE;
+
+/**
+ * Pool for byte[]
+ */
+public class AbfsByteBufferPool {
+
+  /**
+   * Queue holding the free buffers.
+   */
+  private ArrayBlockingQueue<byte[]> freeBuffers;
+  /**
+   * Count to track the buffers issued and yet to be returned.
+   */
+  private int numBuffersInUse;
+  /**
+   * Maximum number of buffers that can be in use.
+   */
+  private int maxBuffersInUse;
+  private int bufferSize;
+
+  /**
+   * @param bufferSize                 Size of the byte[] to be returned.
+   * @param maxFreeBuffers             Maximum number of buffers that cab
+   *                                   reside in the pool.
+   * @param maxWriteMemUsagePercentage Maximum percentage of memory that can
+   *                                   be used by the pool from the max
+   *                                   available memory.
+   */
+  public AbfsByteBufferPool(final int bufferSize, final int maxFreeBuffers,
+      final int maxWriteMemUsagePercentage) {
+    Preconditions.checkArgument(maxWriteMemUsagePercentage
+            >= MIN_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE
+            && maxWriteMemUsagePercentage
+            <= MAX_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE,
+        "maxWriteMemUsagePercentage should be in range (%s - %s)",
+        MIN_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE,
+        MAX_VALUE_MAX_AZURE_WRITE_MEM_USAGE_PERCENTAGE);
+    Preconditions
+        .checkArgument(maxFreeBuffers > 0, "maxFreeBuffers cannot be < 1");
+    this.bufferSize = bufferSize;
+    this.numBuffersInUse = 0;
+    freeBuffers = new ArrayBlockingQueue<>(maxFreeBuffers);
+
+    double maxMemoryAllowedForPool =
+        Runtime.getRuntime().maxMemory() * maxWriteMemUsagePercentage / 100;
+    double bufferCountByMemory = maxMemoryAllowedForPool / bufferSize;
+    double bufferCountByMaxFreeBuffers =
+        maxFreeBuffers + Runtime.getRuntime().availableProcessors();
+
+    maxBuffersInUse = (int) Math
+        .ceil(Math.min(bufferCountByMemory, bufferCountByMaxFreeBuffers));
+    if (maxBuffersInUse < 2) {
+      maxBuffersInUse = 2;
+    }
+  }
+
+  /**
+   * @return byte[] from the pool if available otherwise new byte[] is 
returned.
+   * Waits if pool is empty and already maximum number of buffers are in use.
+   */
+  public byte[] get() {
+    byte[] byteArray = null;
+    synchronized (this) {
 
 Review comment:
   instead of locking for counter, why not use AtomicInteger ?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to