Ver Fonte

update dfs-store

reghao há 2 anos atrás
pai
commit
bfa30d4803

+ 5 - 1
dfs-store/src/main/java/cn/reghao/dfs/store/util/FileLifecycle.java → dfs-store/src/main/java/cn/reghao/dfs/store/config/spring/FileLifecycle.java

@@ -1,7 +1,8 @@
-package cn.reghao.dfs.store.util;
+package cn.reghao.dfs.store.config.spring;
 
 import cn.reghao.dfs.store.config.OssProperties;
 import cn.reghao.dfs.store.util.store.LocalStores;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
@@ -11,6 +12,7 @@ import org.springframework.stereotype.Component;
  * @author reghao
  * @date 2022-03-23 09:22:01
  */
+@Slf4j
 @Component
 public class FileLifecycle implements ApplicationRunner, DisposableBean {
     private final OssProperties ossProperties;
@@ -21,7 +23,9 @@ public class FileLifecycle implements ApplicationRunner, DisposableBean {
 
     @Override
     public void run(ApplicationArguments args) throws Exception {
+        log.info("加载本地磁盘数据...");
         LocalStores.init(ossProperties);
+        log.info("本地磁盘数据加载完成...");
     }
 
     @Override

+ 2 - 10
dfs-store/src/main/java/cn/reghao/dfs/store/util/store/LocalStore.java

@@ -15,7 +15,6 @@ import java.util.concurrent.atomic.AtomicLong;
 @Getter
 public class LocalStore {
     private final String baseDir;
-    //private final String blockId;
     private final String fs;
     private final String fsType;
     private final String mountedOn;
@@ -23,9 +22,8 @@ public class LocalStore {
     private final AtomicLong available;
     private final long max;
 
-    public LocalStore(String baseDir, FileStore fileStore, String blockId, double maxPercent) throws IOException {
+    public LocalStore(String baseDir, FileStore fileStore, double maxPercent) throws IOException {
         this.baseDir = baseDir;
-        //this.blockId = blockId;
         this.fs = fileStore.name();
         this.fsType = fileStore.type();
         this.mountedOn = fileStore.toString().replace(fs, "").replace(" ()", "");
@@ -38,10 +36,6 @@ public class LocalStore {
         this.max = result.longValue();
     }
 
-    public void incrAvail(long avail) {
-        available.addAndGet(avail);
-    }
-
     public void setAvail(long avail) {
         available.getAndSet(avail);
     }
@@ -53,7 +47,6 @@ public class LocalStore {
     @Override
     public int hashCode() {
         int result = 17;
-        //result = result * 31 + blockId.hashCode();
         result = result * 31 + fs.hashCode();
         result = result * 31 + fsType.hashCode();
         result = result * 31 + mountedOn.hashCode();
@@ -68,8 +61,7 @@ public class LocalStore {
 
         if (other instanceof LocalStore) {
             LocalStore o = (LocalStore) other;
-            return /*o.blockId.equals(blockId) &&*/ o.fs.equals(fs)
-                    && o.fsType.equals(fsType) && o.mountedOn.equals(mountedOn);
+            return o.fs.equals(fs) && o.fsType.equals(fsType) && o.mountedOn.equals(mountedOn);
         } else {
             return false;
         }

+ 3 - 80
dfs-store/src/main/java/cn/reghao/dfs/store/util/store/LocalStores.java

@@ -8,10 +8,8 @@ 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.*;
 import java.util.concurrent.atomic.AtomicInteger;
-import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -22,42 +20,18 @@ public class LocalStores {
     private static final Map<String, Map<String, StoreDir>> subDirs = new HashMap<>();
 
     public static void init(OssProperties ossProperties) throws IOException {
-        //Map<String, String> blockIdMap = getBlockIdMap();
-        Map<String, String> checker = new HashMap<>();
         for (String baseDir : ossProperties.getMountedDirs()) {
             FileStore fileStore = Files.getFileStore(Path.of(baseDir));
             String fs = fileStore.name();
             String mountedOn = fileStore.toString().replace(fs, "").replace(" ()", "");
-            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);
+            LocalStore localStore = new LocalStore(baseDir, fileStore, 0.9);
             storeMap.put(mountedOn, localStore);
             subDirs.computeIfAbsent(mountedOn, k -> new HashMap<>());
             createSubDirs(mountedOn, 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 blockId, String baseDir) throws IOException {
         int total = 128;
         for (int i = 0; i < total; i++) {
@@ -68,6 +42,7 @@ public class LocalStores {
                 if (!dir.exists()) {
                     FileUtils.forceMkdir(dir);
                 } else {
+                    // 统计每个目录下文件的数量
                     filesCount = Objects.requireNonNull(dir.list()).length;
                 }
                 StoreDir storeDir = new StoreDir(blockId, new AtomicInteger(filesCount), absoluteDirPath);
@@ -76,38 +51,6 @@ public class LocalStores {
         }
     }
 
-    public static String getBlockId(String absolutePath) throws Exception {
-        for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
-            String blockId = entry.getKey();
-            LocalStore localStore = entry.getValue();
-            String baseDir = localStore.getBaseDir();
-            if (absolutePath.startsWith(baseDir)) {
-                return blockId;
-            }
-        }
-        throw new Exception(String.format("没有找到 %s 对应的 blockId", absolutePath));
-    }
-
-    @Deprecated
-    public static String getRelativePath(String absolutePath) throws Exception {
-        for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
-            LocalStore localStore = entry.getValue();
-            String baseDir = localStore.getBaseDir();
-            if (absolutePath.startsWith(baseDir)) {
-                return absolutePath.replace(baseDir, "");
-            }
-        }
-        throw new Exception(String.format("没有找到 %s 对应的 block", absolutePath));
-    }
-
-    public static String getMountedOn(String blockId) {
-        return storeMap.get(blockId).getMountedOn();
-    }
-
-    public static String getBaseDir(String blockId) {
-        return storeMap.get(blockId).getBaseDir();
-    }
-
     // TODO 优化算法, 处理异常
     public static LocalStore getMaxStore(long size) {
         Map<String, Long> map = new HashMap<>();
@@ -119,7 +62,7 @@ public class LocalStores {
         }
 
         List<String> baseDirs = new ArrayList<>();
-        // baseDirs 中的元素升序排列
+        // mountedDirs 中的元素升序排列
         map.entrySet().stream()
                 .sorted(Map.Entry.comparingByValue())
                 .forEachOrdered(b -> baseDirs.add(b.getKey()));
@@ -133,24 +76,4 @@ public class LocalStores {
     public static List<StoreDir> getSubDirs(String blockId) {
         return new ArrayList<>(subDirs.get(blockId).values());
     }
-
-    public static StoreDir getStoreDir(String absolutePath) throws Exception {
-        String blockId = getBlockId(absolutePath);
-        return subDirs.get(blockId).get(new File(absolutePath).getParent() + File.separator);
-    }
-
-    public static List<LocalStore> getLocalStores() {
-        return new ArrayList<>(storeMap.values());
-    }
-
-    public static List<StoreDir> getStoreDir1(String blockId) {
-        return new ArrayList<>(subDirs.get(blockId).values());
-    }
-
-    public static List<StoreDir> getStoreDirs() {
-        return storeMap.keySet().stream()
-                .map(blockId -> subDirs.get(blockId).values())
-                .flatMap(Collection::stream)
-                .collect(Collectors.toList());
-    }
 }