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



##########
File path: server/src/main/java/org/apache/iotdb/db/wal/WALManager.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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;
+
+import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory;
+import org.apache.iotdb.commons.concurrent.ThreadName;
+import org.apache.iotdb.commons.exception.StartupException;
+import org.apache.iotdb.commons.service.IService;
+import org.apache.iotdb.commons.service.ServiceType;
+import org.apache.iotdb.commons.utils.TestOnly;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.conf.directories.FolderManager;
+import org.apache.iotdb.db.conf.directories.strategy.DirectoryStrategyType;
+import org.apache.iotdb.db.exception.DiskSpaceInsufficientException;
+import org.apache.iotdb.db.wal.node.IWALNode;
+import org.apache.iotdb.db.wal.node.WALFakeNode;
+import org.apache.iotdb.db.wal.node.WALNode;
+import org.apache.iotdb.db.wal.utils.WALMode;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/** This class is used to manage all wal nodes */
+public class WALManager implements IService {
+  public static final long FSYNC_CHECKPOINT_FILE_DELAY_IN_MS = 200;
+  public static final long DELETE_WAL_FILES_DELAY_IN_MS = 10 * 60 * 1000;
+
+  private static final Logger logger = 
LoggerFactory.getLogger(WALManager.class);
+  private static final IoTDBConfig config = 
IoTDBDescriptor.getInstance().getConfig();
+  private static final int MAX_WAL_NODE_NUM =
+      config.getMaxWalNodeNum() > 0 ? config.getMaxWalNodeNum() : 
config.getWalDirs().length * 2;
+
+  /** manage wal folders */
+  private FolderManager folderManager;
+  /** protect concurrent safety of wal nodes, including walNodes, nodeCursor 
and nodeIdCounter */
+  private final Lock nodesLock = new ReentrantLock();
+  // region these variables should be protected by nodesLock
+  /** wal nodes, the max number of wal nodes is MAX_WAL_NUM */
+  private final List<WALNode> walNodes = new ArrayList<>(MAX_WAL_NODE_NUM);
+  /** help allocate node for users */
+  private int nodeCursor = -1;
+  /** each wal node has a unique long value identifier */
+  private long nodeIdCounter = -1;
+  // endregion
+  /** single thread to fsync .checkpoint files */
+  private ScheduledExecutorService checkpointThread;
+  /** single thread to delete old .wal files */
+  private ScheduledExecutorService walDeleteThread;
+
+  private WALManager() {}
+
+  /** Apply for a wal node */
+  public IWALNode applyForWALNode() {
+    if (config.getWalMode() == WALMode.DISABLE) {
+      return WALFakeNode.getSuccessInstance();
+    }
+
+    WALNode selectedNode;
+    nodesLock.lock();
+    try {
+      if (walNodes.size() < MAX_WAL_NODE_NUM) {
+        nodeIdCounter++;
+        String identifier = String.valueOf(nodeIdCounter);
+        String folder;
+        // get wal folder
+        try {
+          folder = folderManager.getNextFolder();
+        } catch (DiskSpaceInsufficientException e) {
+          logger.error("All disks of wal folders are full, change system mode 
to read-only.", e);
+          config.setReadOnly(true);

Review comment:
       No need to minus nodeIdCounter here, this can be a trick to help us 
locate wal node allocated error.

##########
File path: server/src/main/java/org/apache/iotdb/db/wal/WALManager.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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;
+
+import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory;
+import org.apache.iotdb.commons.concurrent.ThreadName;
+import org.apache.iotdb.commons.exception.StartupException;
+import org.apache.iotdb.commons.service.IService;
+import org.apache.iotdb.commons.service.ServiceType;
+import org.apache.iotdb.commons.utils.TestOnly;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.conf.directories.FolderManager;
+import org.apache.iotdb.db.conf.directories.strategy.DirectoryStrategyType;
+import org.apache.iotdb.db.exception.DiskSpaceInsufficientException;
+import org.apache.iotdb.db.wal.node.IWALNode;
+import org.apache.iotdb.db.wal.node.WALFakeNode;
+import org.apache.iotdb.db.wal.node.WALNode;
+import org.apache.iotdb.db.wal.utils.WALMode;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/** This class is used to manage all wal nodes */
+public class WALManager implements IService {
+  public static final long FSYNC_CHECKPOINT_FILE_DELAY_IN_MS = 200;
+  public static final long DELETE_WAL_FILES_DELAY_IN_MS = 10 * 60 * 1000;
+
+  private static final Logger logger = 
LoggerFactory.getLogger(WALManager.class);
+  private static final IoTDBConfig config = 
IoTDBDescriptor.getInstance().getConfig();
+  private static final int MAX_WAL_NODE_NUM =
+      config.getMaxWalNodeNum() > 0 ? config.getMaxWalNodeNum() : 
config.getWalDirs().length * 2;
+
+  /** manage wal folders */
+  private FolderManager folderManager;
+  /** protect concurrent safety of wal nodes, including walNodes, nodeCursor 
and nodeIdCounter */
+  private final Lock nodesLock = new ReentrantLock();
+  // region these variables should be protected by nodesLock
+  /** wal nodes, the max number of wal nodes is MAX_WAL_NUM */
+  private final List<WALNode> walNodes = new ArrayList<>(MAX_WAL_NODE_NUM);
+  /** help allocate node for users */
+  private int nodeCursor = -1;
+  /** each wal node has a unique long value identifier */
+  private long nodeIdCounter = -1;
+  // endregion
+  /** single thread to fsync .checkpoint files */
+  private ScheduledExecutorService checkpointThread;
+  /** single thread to delete old .wal files */
+  private ScheduledExecutorService walDeleteThread;
+
+  private WALManager() {}
+
+  /** Apply for a wal node */
+  public IWALNode applyForWALNode() {
+    if (config.getWalMode() == WALMode.DISABLE) {
+      return WALFakeNode.getSuccessInstance();
+    }
+
+    WALNode selectedNode;
+    nodesLock.lock();
+    try {
+      if (walNodes.size() < MAX_WAL_NODE_NUM) {
+        nodeIdCounter++;
+        String identifier = String.valueOf(nodeIdCounter);
+        String folder;
+        // get wal folder
+        try {
+          folder = folderManager.getNextFolder();
+        } catch (DiskSpaceInsufficientException e) {
+          logger.error("All disks of wal folders are full, change system mode 
to read-only.", e);
+          config.setReadOnly(true);
+          return WALFakeNode.getFailureInstance(e);
+        }
+        folder = folder + File.separator + identifier;
+        // create new wal node
+        try {
+          selectedNode = new WALNode(identifier, folder);
+        } catch (FileNotFoundException e) {
+          logger.error("Fail to create wal node", e);

Review comment:
       No need to minus nodeIdCounter here, this can be a trick to help us 
locate wal node allocated error.




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