xingtanzjr commented on code in PR #8644:
URL: https://github.com/apache/iotdb/pull/8644#discussion_r1063981664


##########
server/src/main/java/org/apache/iotdb/db/engine/settle/SettleRequestHandler.java:
##########
@@ -0,0 +1,321 @@
+/*
+ * 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.engine.settle;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSettleReq;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.StorageEngine;
+import org.apache.iotdb.db.engine.compaction.CompactionTaskManager;
+import org.apache.iotdb.db.engine.compaction.inner.InnerSpaceCompactionTask;
+import org.apache.iotdb.db.engine.compaction.task.AbstractCompactionTask;
+import org.apache.iotdb.db.engine.modification.ModificationFile;
+import org.apache.iotdb.db.engine.storagegroup.DataRegion;
+import org.apache.iotdb.db.engine.storagegroup.TsFileManager;
+import org.apache.iotdb.db.engine.storagegroup.TsFileNameGenerator;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResourceList;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResourceStatus;
+import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsFileUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class SettleRequestHandler {
+  private static final Logger logger = 
LoggerFactory.getLogger(SettleRequestHandler.class);
+
+  private boolean testMode = false;
+
+  public boolean isTestMode() {
+    return testMode;
+  }
+
+  public void setTestMode(boolean testMode) {
+    this.testMode = testMode;
+  }
+
+  public static SettleRequestHandler getInstance() {
+    return SettleRequestHandlerHolder.INSTANCE;
+  }
+
+  public TSStatus handleSettleRequest(TSettleReq req) {
+    List<String> paths = req.getPaths();
+
+    SettleRequestContext context = new SettleRequestContext(paths);
+    TSStatus validationResult = context.validate();
+    if (validationResult.getCode() != 
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+      return validationResult;
+    }
+    if (testMode) {
+      return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
+    }
+    List<TsFileResource> selectedTsFileResources = 
context.getTsFileResourcesByFileNames();
+    return context.submitCompactionTask(selectedTsFileResources);
+  }
+
+  private static class SettleRequestContext {
+    private ConsistentSettleInfo consistentSettleInfo;
+
+    private boolean hasSeqFiles;
+    private boolean hasUnseqFiles;
+    private boolean hasModsFiles;
+    private List<String> paths;
+    private Set<String> tsFileNames;
+    private TsFileResourceList allTsFileResourceList;
+
+    private TsFileManager tsFileManager;
+    private IoTDBConfig config;
+
+    private SettleRequestContext(List<String> paths) {
+      this.paths = paths;
+      this.tsFileNames = new HashSet<>();
+      this.config = IoTDBDescriptor.getInstance().getConfig();
+    }
+
+    private TSStatus validate() {
+      if (paths == null || paths.size() == 0) {
+        return RpcUtils.getStatus(
+            TSStatusCode.ILLEGAL_PARAMETER, "The files to settle is not 
offered.");
+      }
+
+      int maxInnerCompactionCandidateFileNum = 
config.getMaxInnerCompactionCandidateFileNum();
+      if (paths.size() > maxInnerCompactionCandidateFileNum) {
+        return RpcUtils.getStatus(
+            TSStatusCode.UNSUPPORTED_OPERATION,
+            "Too many files offered, the limited count of system config is "
+                + maxInnerCompactionCandidateFileNum
+                + ", the input file count is "
+                + tsFileNames.size());
+      }
+
+      TSStatus validationResult;
+      Pair<ConsistentSettleInfo, TSStatus> consistentSettleInfoStatusPair;
+
+      for (String path : paths) {
+        File currentTsFile = new File(path);
+        if (!currentTsFile.exists()) {
+          return RpcUtils.getStatus(
+              TSStatusCode.PATH_NOT_EXIST, "The specified file does not exist 
in " + path);
+        }
+        File modsFile = new File(path + ModificationFile.FILE_SUFFIX);
+        hasModsFiles |= modsFile.exists();
+
+        consistentSettleInfoStatusPair = 
calculateConsistentInfo(currentTsFile);
+        ConsistentSettleInfo currentInfo = consistentSettleInfoStatusPair.left;
+        if (this.consistentSettleInfo == null) {
+          this.consistentSettleInfo = currentInfo;
+        }
+        validationResult = consistentSettleInfoStatusPair.right;
+        if (!isSuccess(validationResult)) {

Review Comment:
   This judgement can be moved before line `134`



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