This is an automated email from the ASF dual-hosted git repository.
jiangtian pushed a commit to branch deletion_expr
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/deletion_expr by this push:
new 0254f961fb5 add ModFileManager
0254f961fb5 is described below
commit 0254f961fb5731881030450e03e5b2fdfc94ea03
Author: Tian Jiang <[email protected]>
AuthorDate: Thu Sep 19 16:17:17 2024 +0800
add ModFileManager
---
.../dataregion/modification/ModFileManager.java | 101 +++++++++++++++++++++
.../dataregion/modification/ModificationFile.java | 25 ++++-
2 files changed, 124 insertions(+), 2 deletions(-)
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModFileManager.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModFileManager.java
new file mode 100644
index 00000000000..e88014ae74a
--- /dev/null
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModFileManager.java
@@ -0,0 +1,101 @@
+/*
+ * 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.storageengine.dataregion.modification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.iotdb.commons.utils.FileUtils;
+import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileID;
+import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A ModFileManager manages the ModificationFiles of a Time Partition.
+ */
+@SuppressWarnings({"resource",
"SynchronizationOnLocalVariableOrMethodParameter"})
+public class ModFileManager {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(ModFileManager.class);
+ // levelNum -> modFileNum -> modFile
+ private final Map<Long, TreeMap<Long, ModificationFile>> allLevelModsFileMap
= new ConcurrentHashMap<>();
+
+ public void recoverModFile(String modFilePath, TsFileResource resource) {
+ File file = new File(modFilePath);
+ String name = file.getName();
+ long[] levelNumAndModNum = ModificationFile.parseFileName(name);
+
+ ModificationFile modificationFile =
allLevelModsFileMap.computeIfAbsent(levelNumAndModNum[0],
+ k -> new TreeMap<>()).computeIfAbsent(levelNumAndModNum[1], k -> new
ModificationFile(file, resource));
+ modificationFile.addReference(resource);
+ }
+
+ private long maxModNum(long levelNum) {
+ TreeMap<Long, ModificationFile> levelModFileMap =
allLevelModsFileMap.computeIfAbsent(
+ levelNum, k -> new TreeMap<>());
+ if (levelModFileMap.isEmpty()) {
+ return -1;
+ } else {
+ return levelModFileMap.lastKey();
+ }
+ }
+
+ public ModificationFile allocateNew(TsFileResource resource) {
+ TsFileID tsFileID = resource.getTsFileID();
+ long levelNum = tsFileID.getInnerCompactionCount();
+ long nextModNum = maxModNum(levelNum) + 1;
+ File file = new File(resource.getTsFile().getParentFile(),
ModificationFile.composeFileName(levelNum, nextModNum));
+ TreeMap<Long, ModificationFile> levelModsFileMap =
this.allLevelModsFileMap.computeIfAbsent(
+ levelNum,
+ k -> new TreeMap<>());
+ synchronized (levelModsFileMap) {
+ return levelModsFileMap.computeIfAbsent(nextModNum, k -> new
ModificationFile(file, resource));
+ }
+ }
+
+ public void cleanModFile() {
+ for (TreeMap<Long, ModificationFile> levelModFileMap :
allLevelModsFileMap.values()) {
+ List<Long> modFilesToRemove = new ArrayList<>();
+ synchronized (levelModFileMap) {
+ levelModFileMap.forEach((modNum, modFile) -> {
+ if (!modFile.hasReference()) {
+ modFilesToRemove.add(modNum);
+ }
+ });
+ }
+
+ synchronized (levelModFileMap) {
+ for (Long l : modFilesToRemove) {
+ ModificationFile remove = levelModFileMap.remove(l);
+ try {
+ remove.close();
+ FileUtils.deleteFileOrDirectory(remove.getFile());
+ } catch (Exception e) {
+ LOGGER.warn("Failed to close mod file {}", remove.getFile(), e);
+ }
+ }
+ }
+ }
+ }
+}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModificationFile.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModificationFile.java
index ba941305cec..6ee348047fd 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModificationFile.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/modification/ModificationFile.java
@@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory;
public class ModificationFile implements AutoCloseable {
+ public static final String FILE_SUFFIX = ".mods2";
private static final Logger LOGGER =
LoggerFactory.getLogger(ModificationFile.class);
private final File file;
@@ -49,8 +50,8 @@ public class ModificationFile implements AutoCloseable {
private final Set<TsFileResource> tsFileRefs = new
ConcurrentSkipListSet<>(Comparator.comparing(
TsFileResource::getTsFilePath));
- public ModificationFile(String filePath, TsFileResource firstResource)
throws IOException {
- this.file = new File(filePath);
+ public ModificationFile(File file, TsFileResource firstResource) {
+ this.file = file;
tsFileRefs.add(firstResource);
}
@@ -75,6 +76,10 @@ public class ModificationFile implements AutoCloseable {
channel = null;
}
+ public File getFile() {
+ return file;
+ }
+
/**
* Add a TsFile to the reference set only if the set is not empty.
* @param tsFile TsFile to be added
@@ -109,6 +114,22 @@ public class ModificationFile implements AutoCloseable {
}
}
+ public boolean hasReference() {
+ return !tsFileRefs.isEmpty();
+ }
+
+ public static String composeFileName(long levelNum, long modFileNum) {
+ return levelNum + "-" + modFileNum + "." + FILE_SUFFIX;
+ }
+
+ public static long[] parseFileName(String name) {
+ name = name.substring(0, name.lastIndexOf(ModificationFile.FILE_SUFFIX));
+ String[] split = name.split("-");
+ long levelNum = Long.parseLong(split[0]);
+ long modNum = Long.parseLong(split[1]);
+ return new long[] {levelNum, modNum};
+ }
+
public class ModIterator implements Iterator<ModEntry>, AutoCloseable {
private InputStream inputStream;
private ModEntry nextEntry;