HeimingZ commented on a change in pull request #5320:
URL: https://github.com/apache/iotdb/pull/5320#discussion_r834170367



##########
File path: server/src/main/java/org/apache/iotdb/db/wal/buffer/WALBuffer.java
##########
@@ -0,0 +1,426 @@
+/*
+ * 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.wal.buffer;
+
+import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory;
+import org.apache.iotdb.commons.concurrent.ThreadName;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.qp.physical.crud.DeletePlan;
+import org.apache.iotdb.db.utils.MmapUtil;
+import org.apache.iotdb.db.wal.exception.WALNodeClosedException;
+import org.apache.iotdb.db.wal.utils.listener.WALFlushListener;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * This buffer guarantees the concurrent safety and uses double buffers 
mechanism to accelerate
+ * writes and avoid waiting for buffer syncing to disk.
+ */
+public class WALBuffer extends AbstractWALBuffer {
+  /** Maximum number of WALEdits in one serialize task */
+  public static final int BATCH_SIZE_LIMIT = 100;
+
+  private static final Logger logger = 
LoggerFactory.getLogger(WALBuffer.class);
+  private static final IoTDBConfig config = 
IoTDBDescriptor.getInstance().getConfig();
+  private static final long SYNC_WAL_DELAY_IN_MS = 
config.getSyncWalDelayInMs();
+  private static final int WAL_BUFFER_SIZE = config.getWalBufferSize();
+  /** notify serializeThread to stop */
+  private static final WALEdit CLOSE_SIGNAL = new WALEdit(-1, new 
DeletePlan());
+
+  /** whether close method is called */
+  private volatile boolean isClosed = false;
+  /** WALEdits */
+  private final BlockingQueue<WALEdit> walEdits = new 
ArrayBlockingQueue<>(BATCH_SIZE_LIMIT * 10);
+  /** two buffers switch between three statuses (there is always 1 buffer 
working) */
+  // buffer in working status, only updated by serializeThread
+  private volatile ByteBuffer workingBuffer;
+  // buffer in idle status
+  private volatile ByteBuffer idleBuffer;
+  // buffer in syncing status, serializeThread makes sure no more writes to 
syncingBuffer
+  private volatile ByteBuffer syncingBuffer;
+  /** lock to provide synchronization for double buffers mechanism, protecting 
buffers status */
+  private final Lock buffersLock = new ReentrantLock();
+  /** condition to guarantee correctness of switching buffers */
+  private final Condition idleBufferReadyCondition = 
buffersLock.newCondition();
+  /** single thread to serialize WALEdit to workingBuffer */
+  private final ExecutorService serializeThread;
+  /** single thread to sync syncingBuffer to disk */
+  private final ExecutorService syncBufferThread;
+
+  public WALBuffer(String identifier, String logDirectory) throws 
FileNotFoundException {
+    super(identifier, logDirectory);
+    allocateBuffers();
+    serializeThread =
+        IoTDBThreadPoolFactory.newSingleThreadExecutor(
+            ThreadName.WAL_SERIALIZE.getName() + "(node-" + identifier + ")");
+    syncBufferThread =
+        IoTDBThreadPoolFactory.newSingleThreadExecutor(
+            ThreadName.WAL_SYNC.getName() + "(node-" + identifier + ")");
+    // start receiving serialize tasks
+    serializeThread.submit(new SerializeTask());
+  }
+
+  private void allocateBuffers() {
+    try {
+      workingBuffer = ByteBuffer.allocateDirect(WAL_BUFFER_SIZE / 2);

Review comment:
       The memory usage is controlled by the max number of wal node (each node 
has a wal buffer), whose corresponding parameter is 
[max_wal_num](https://github.com/apache/iotdb/pull/5320/files#diff-057cb21694ee5d5cf32fbd4e7afc416c5841a3696dcce0883d783a961076ba9cR66-R69).
 You can see wal nodes are reused in 
[WALManger](https://github.com/apache/iotdb/pull/5320/files#diff-672f4b0027b7b2d89495f695447243e85d264fc61a37ee2dcbbcf01a3f59cf0aR108-R112).




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to