|
|
@@ -0,0 +1,82 @@
|
|
|
+package cn.reghao.tnb.file.app.util;
|
|
|
+
|
|
|
+import cn.reghao.tnb.file.app.config.DfsConfig;
|
|
|
+import cn.reghao.tnb.file.app.config.DfsProperties;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.FileStore;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2022-05-23 18:21:22
|
|
|
+ */
|
|
|
+public class LocalStores {
|
|
|
+ private static final Map<String, LocalStore> storeMap = new HashMap<>();
|
|
|
+ private static final Map<String, List<String>> subDirs = new HashMap<>();
|
|
|
+
|
|
|
+ public static void init(DfsProperties dfsProperties) throws IOException {
|
|
|
+ Map<String, String> blockIdMap = getBlockIdMap();
|
|
|
+ Map<String, String> checker = new HashMap<>();
|
|
|
+ for (DfsConfig dfsConfig : dfsProperties.getDfsConfigs()) {
|
|
|
+ String baseDir = dfsConfig.getBaseDir();
|
|
|
+ FileStore fileStore = Files.getFileStore(Path.of(baseDir));
|
|
|
+ String fs = fileStore.name();
|
|
|
+ String previous = checker.putIfAbsent(fs, baseDir);
|
|
|
+ if (previous != null) {
|
|
|
+ String msg = String.format("%s 和 %s 同属于分区 %s, 每个 baseDir 应该分属于不同的分区", baseDir, previous, fs);
|
|
|
+ throw new IOException(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String blockId = blockIdMap.get(fs);
|
|
|
+ if (blockId == null) {
|
|
|
+ throw new IOException(String.format("%s 没有对应的 blockId, 请检查 /dev/disk/by-uuid", fs));
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalStore localStore = new LocalStore(baseDir, fileStore, blockId, 0.9);
|
|
|
+ storeMap.put(baseDir, localStore);
|
|
|
+ subDirs.computeIfAbsent(baseDir, k -> new ArrayList<>());
|
|
|
+ createSubDirs(baseDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, String> getBlockIdMap() throws IOException {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ File dir = new File("/dev/disk/by-uuid");
|
|
|
+ for (File symbolicFile : Objects.requireNonNull(dir.listFiles())) {
|
|
|
+ String blkId = symbolicFile.getName();
|
|
|
+ String str = Files.readSymbolicLink(Paths.get(symbolicFile.getPath())).toString();
|
|
|
+ String fs = "/dev/" + str.replace("../../", "");
|
|
|
+ map.putIfAbsent(fs, blkId);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void createSubDirs(String baseDir) throws IOException {
|
|
|
+ int total = 128;
|
|
|
+ for (int i = 0; i < total; i++) {
|
|
|
+ for (int j = 0; j < total; j++) {
|
|
|
+ String dirPath = String.format("%s/%s/%s", baseDir, i, j);
|
|
|
+ File dir = new File(dirPath);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ FileUtils.forceMkdir(dir);
|
|
|
+ }
|
|
|
+ subDirs.get(baseDir).add(dirPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMaxStore(long size) {
|
|
|
+ String storePath = storeMap.keySet().iterator().next();
|
|
|
+ return storePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> getSubDirs(String store) {
|
|
|
+ return subDirs.get(store);
|
|
|
+ }
|
|
|
+}
|