SteveYurongSu commented on code in PR #13085:
URL: https://github.com/apache/iotdb/pull/13085#discussion_r1703635664


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/load/AutoLoadTsFileService.java:
##########
@@ -0,0 +1,397 @@
+/*
+ * 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.pipe.receiver.load;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.concurrent.IoTThreadFactory;
+import org.apache.iotdb.commons.concurrent.ThreadName;
+import 
org.apache.iotdb.commons.concurrent.threadpool.WrappedThreadPoolExecutor;
+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.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.pipe.agent.runtime.PipePeriodicalJobExecutor;
+import org.apache.iotdb.db.pipe.receiver.protocol.thrift.IoTDBDataNodeReceiver;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class AutoLoadTsFileService implements IService {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AutoLoadTsFileService.class);
+
+  private static final IoTDBDataNodeReceiver receiver = new 
IoTDBDataNodeReceiver();
+
+  private static final IoTDBConfig IOTDB_CONFIG = 
IoTDBDescriptor.getInstance().getConfig();
+
+  private static String[] LOAD_ACTIVE_LISTENING_DIRS = new String[0];
+  private static String LOAD_ACTIVE_LISTENING_FAIL_DIR = "";
+
+  private static final Long LOAD_ACTIVE_LISTENING_CHECK_INTERVAL_SECONDS =
+      IOTDB_CONFIG.getLoadActiveListeningCheckIntervalSeconds();
+  private static final Integer LOAD_ACTIVE_LISTENING_MAX_THREAD_NUM =
+      Math.min(
+          IOTDB_CONFIG.getLoadActiveListeningMaxThreadNum(),
+          Math.max(1, Runtime.getRuntime().availableProcessors() / 2));
+
+  // the maximum number of times a thread attempts to retrieve an element
+  // end the current thread after exceeding the maximum number of times
+  private static final int RETRY_MAX_NUM = 60;
+  // max length of waitingLoadTsFileQueue
+  private static final int QUEUE_MAX_NUM = 200;
+  // the rest space size of waitingLoadTsFileQueue
+  private int QUEUE_REST_NUM = QUEUE_MAX_NUM;
+
+  // whether the load tsfile thread enable
+  private final AtomicBoolean isShutdown = new AtomicBoolean(false);
+  // whether the tsfileSet and resourceOrModsSet get enough file
+  private final AtomicBoolean isFileListFull = new AtomicBoolean(false);
+
+  private static final String RESOURCE = ".resource";
+  private static final String MODS = ".mods";
+
+  private static final Set<String> tsfileSet = new HashSet<>();
+  private static final Set<String> resourceOrModsSet = new HashSet<>();
+
+  private static final PipePeriodicalJobExecutor 
checkTsFilePeriodicalJobExecutor =
+      new PipePeriodicalJobExecutor();
+  private static WrappedThreadPoolExecutor loadTsFileExecutor;
+
+  private static final LinkedHashSet<String> waitingLoadTsFileQueue =
+      new LinkedHashSet<>(QUEUE_MAX_NUM);
+
+  private static final Lock lock = new ReentrantLock();
+
+  @Override
+  public void start() throws StartupException {
+    registerPeriodicalJob(this::monitoringTsFile);
+    checkTsFilePeriodicalJobExecutor.start();
+    isShutdown.set(false);
+  }
+
+  @Override
+  public void stop() {
+    if (isShutdown.get()) {
+      return;
+    }
+    isShutdown.set(true);
+    checkTsFilePeriodicalJobExecutor.stop();
+    loadTsFileExecutor.shutdown();
+  }
+
+  @Override
+  public ServiceType getID() {
+    return ServiceType.AUTO_LOAD_TSFILE_SERVICE;
+  }
+
+  private void registerPeriodicalJob(Runnable periodicalJob) {
+    checkTsFilePeriodicalJobExecutor.register(
+        "AutoLoadTsFileService#loadTsFiles",
+        periodicalJob,
+        AutoLoadTsFileService.LOAD_ACTIVE_LISTENING_CHECK_INTERVAL_SECONDS);
+  }
+
+  // initial directory
+  private void initializeConfiguration() {
+    try {
+      LOAD_ACTIVE_LISTENING_DIRS = IOTDB_CONFIG.getLoadActiveListeningDirs();
+      LOAD_ACTIVE_LISTENING_FAIL_DIR = 
IOTDB_CONFIG.getLoadActiveListeningFailDir();
+      for (String listenerFileDir : LOAD_ACTIVE_LISTENING_DIRS) {
+        createDirectoriesIfNotExists(listenerFileDir);
+      }
+      createDirectoriesIfNotExists(LOAD_ACTIVE_LISTENING_FAIL_DIR);
+    } catch (Exception e) {
+      LOGGER.warn("failed to init file folder because all disks of folders are 
full.", e);
+    }
+  }
+
+  // create a directory that does not exist in the configuration file
+  private void createDirectoriesIfNotExists(String path) throws 
StartupException {
+    File file = new File(path);
+    boolean isNeeded = (file.exists() && file.isDirectory()) || file.mkdirs();

Review Comment:
   FileUtils apache io commons



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