|
|
@@ -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());
|
|
|
- }
|
|
|
}
|