You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/01/15 08:20:40 UTC

[GitHub] [iotdb] JackieTien97 commented on a change in pull request #2488: Add DirectByteBufferPool for WAL

JackieTien97 commented on a change in pull request #2488:
URL: https://github.com/apache/iotdb/pull/2488#discussion_r558017828



##########
File path: server/src/main/java/org/apache/iotdb/db/engine/storagegroup/StorageGroupProcessor.java
##########
@@ -261,6 +268,79 @@ public void setReady(boolean ready) {
   private List<CloseFileListener> customCloseFileListeners = Collections.emptyList();
   private List<FlushListener> customFlushListeners = Collections.emptyList();
 
+  private static final int WAL_BUFFER_SIZE =
+      IoTDBDescriptor.getInstance().getConfig().getWalBufferSize() / 2;
+
+  private static final int MAX_WAL_BYTEBUFFER_NUM =
+      IoTDBDescriptor.getInstance().getConfig().getConcurrentWritingTimePartition() * 4;
+
+  private static final long DEFAULT_POOL_TRIM_INTERVAL_MILLIS = 10_000;
+
+  private final Deque<ByteBuffer> walByteBufferPool = new LinkedList<>();
+
+  private int currentWalPoolSize = 0;
+
+
+  /**
+   * get the direct byte buffer from pool, each fetch contains two ByteBuffer
+   */
+  public ByteBuffer[] getWalDirectByteBuffer() {
+    ByteBuffer[] res = new ByteBuffer[2];
+    synchronized (walByteBufferPool) {
+      while (walByteBufferPool.isEmpty() && currentWalPoolSize + 2 > MAX_WAL_BYTEBUFFER_NUM) {
+        try {
+          walByteBufferPool.wait();
+        } catch (InterruptedException e) {
+          Thread.currentThread().interrupt();
+          logger
+              .error("getDirectByteBuffer occurs error while waiting for DirectByteBuffer"
+                  + "group {}", storageGroupName, e);
+        }
+      }
+      // If the queue is not empty, it must have at least two.
+      if (!walByteBufferPool.isEmpty()) {
+        res[0] = walByteBufferPool.pollFirst();
+        res[1] = walByteBufferPool.pollFirst();
+      } else {
+        // if the queue is empty and current size is less than MAX_BYTEBUFFER_NUM
+        // we can construct another two more new byte buffer
+        currentWalPoolSize += 2;
+        res[0] = ByteBuffer.allocateDirect(WAL_BUFFER_SIZE);
+        res[1] = ByteBuffer.allocateDirect(WAL_BUFFER_SIZE);
+      }
+    }
+    return res;
+  }
+
+  /**
+   * put the byteBuffer back to pool
+   */
+  public void releaseWalBuffer(ByteBuffer[] byteBuffers) {
+    for (ByteBuffer byteBuffer : byteBuffers) {
+      byteBuffer.clear();
+    }
+    synchronized (walByteBufferPool) {
+      walByteBufferPool.addLast(byteBuffers[0]);
+      walByteBufferPool.addLast(byteBuffers[1]);
+      walByteBufferPool.notifyAll();
+    }
+  }
+
+  /**
+   * trim the size of the pool and release the memory of needless direct byte buffer
+   */
+  private void trimTask() {
+    synchronized (walByteBufferPool) {
+      int expectedSize =
+          (workSequenceTsFileProcessors.size() + workUnsequenceTsFileProcessors.size()) * 2;
+      while (expectedSize < currentWalPoolSize && !walByteBufferPool.isEmpty()) {
+        MmapUtil.clean((MappedByteBuffer) walByteBufferPool.removeLast());
+        MmapUtil.clean((MappedByteBuffer) walByteBufferPool.removeLast());
+        currentWalPoolSize -= 2;
+      }
+    }
+  }

Review comment:
       fix

##########
File path: server/src/main/java/org/apache/iotdb/db/utils/MmapUtil.java
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.iotdb.db.utils;
+
+import io.netty.util.internal.PlatformDependent;
+import java.nio.MappedByteBuffer;
+
+public class MmapUtil {
+
+  public static void clean(MappedByteBuffer mappedByteBuffer) {
+    if (mappedByteBuffer == null || !mappedByteBuffer.isDirect() || mappedByteBuffer.capacity()== 0)
+      return;

Review comment:
       fix

##########
File path: server/src/main/java/org/apache/iotdb/db/writelog/node/ExclusiveWriteLogNode.java
##########
@@ -92,6 +90,11 @@ public ExclusiveWriteLogNode(String identifier) {
     }
   }
 
+  public void initBuffer(ByteBuffer[] byteBuffers) {
+    this.logBufferWorking = byteBuffers[0];
+    this.logBufferIdle = byteBuffers[1];
+  }

Review comment:
       fix




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