|
|
@@ -1,15 +1,11 @@
|
|
|
package cn.reghao.oss.store.util.store;
|
|
|
|
|
|
-import cn.reghao.oss.store.config.OssProperties;
|
|
|
-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.util.*;
|
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -17,36 +13,50 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
*/
|
|
|
public class LocalStores {
|
|
|
private static final Map<String, LocalStore> storeMap = new HashMap<>();
|
|
|
- private static final Map<String, Map<String, StoreDir>> subDirs = new HashMap<>();
|
|
|
+ private static final Map<String, Map<String, StoreDir>> storeDirMap = new HashMap<>();
|
|
|
|
|
|
- public static void init(OssProperties ossProperties) throws IOException {
|
|
|
- for (String baseDir : ossProperties.getMountedDirs()) {
|
|
|
- FileStore fileStore = Files.getFileStore(Path.of(baseDir));
|
|
|
- String fs = fileStore.name();
|
|
|
- String mountedOn = fileStore.toString().replace(fs, "").replace(" ()", "");
|
|
|
+ public static void init(List<String> diskDirs) throws IOException {
|
|
|
+ Set<String> set = new HashSet<>();
|
|
|
+ for (String diskDir : diskDirs) {
|
|
|
+ FileStore fileStore = Files.getFileStore(Path.of(diskDir));
|
|
|
+ String logicalDisk = fileStore.name();
|
|
|
+ if (set.add(logicalDisk)) {
|
|
|
+ long total = fileStore.getTotalSpace();
|
|
|
+ long available = fileStore.getUsableSpace();
|
|
|
+ LocalStore localStore = new LocalStore(diskDir, total, available, 0.9);
|
|
|
+ storeMap.put(diskDir, localStore);
|
|
|
+ storeDirMap.computeIfAbsent(diskDir, k -> new HashMap<>());
|
|
|
+ createSubDirs(diskDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- LocalStore localStore = new LocalStore(baseDir, fileStore, 0.9);
|
|
|
- storeMap.put(mountedOn, localStore);
|
|
|
- subDirs.computeIfAbsent(mountedOn, k -> new HashMap<>());
|
|
|
- createSubDirs(mountedOn, baseDir);
|
|
|
+ public static void initStoreDirs(String diskDir, Map<String, Integer> map) {
|
|
|
+ Map<String, StoreDir> map1 = storeDirMap.get(diskDir);
|
|
|
+ if (map1 == null) {
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ map.forEach((path, total) -> {
|
|
|
+ StoreDir storeDir = map1.get(path);
|
|
|
+ if (storeDir != null) {
|
|
|
+ storeDir.setTotal(total);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- private static void createSubDirs(String blockId, String baseDir) throws IOException {
|
|
|
+ private static void createSubDirs(String diskDir) throws IOException {
|
|
|
int total = 128;
|
|
|
for (int i = 0; i < total; i++) {
|
|
|
for (int j = 0; j < total; j++) {
|
|
|
- String absoluteDirPath = String.format("%s%s/%s/", baseDir, i, j);
|
|
|
- File dir = new File(absoluteDirPath);
|
|
|
- int filesCount = 0;
|
|
|
- if (!dir.exists()) {
|
|
|
- FileUtils.forceMkdir(dir);
|
|
|
- } else {
|
|
|
- // 统计每个目录下文件的数量
|
|
|
- filesCount = Objects.requireNonNull(dir.list()).length;
|
|
|
+ String baseDir = String.format("%s%s/%s/", diskDir, i, j);
|
|
|
+ File dir = new File(baseDir);
|
|
|
+ if (!dir.exists() && !dir.mkdirs()) {
|
|
|
+ String msg = String.format("create %s failed", dir);
|
|
|
+ throw new IOException(msg);
|
|
|
}
|
|
|
- StoreDir storeDir = new StoreDir(blockId, new AtomicInteger(filesCount), absoluteDirPath);
|
|
|
- subDirs.get(blockId).put(absoluteDirPath, storeDir);
|
|
|
+
|
|
|
+ storeDirMap.get(diskDir).put(baseDir, new StoreDir(baseDir));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -55,25 +65,25 @@ public class LocalStores {
|
|
|
public static LocalStore getMaxStore(long size) {
|
|
|
Map<String, Long> map = new HashMap<>();
|
|
|
for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
|
|
|
- String blockId = entry.getKey();
|
|
|
+ String diskDir = entry.getKey();
|
|
|
LocalStore localStore = entry.getValue();
|
|
|
- long availSize = localStore.getAvail() - size;
|
|
|
- map.put(blockId, availSize);
|
|
|
+ long currentAvailable = localStore.getAvailable() - size;
|
|
|
+ map.put(diskDir, currentAvailable);
|
|
|
}
|
|
|
|
|
|
- List<String> baseDirs = new ArrayList<>();
|
|
|
- // mountedDirs 中的元素升序排列
|
|
|
+ List<String> diskDirs = new ArrayList<>();
|
|
|
+ // diskDirs 中的元素升序排列
|
|
|
map.entrySet().stream()
|
|
|
.sorted(Map.Entry.comparingByValue())
|
|
|
- .forEachOrdered(b -> baseDirs.add(b.getKey()));
|
|
|
+ .forEachOrdered(b -> diskDirs.add(b.getKey()));
|
|
|
|
|
|
- String baseDir = baseDirs.get(baseDirs.size()-1);
|
|
|
- LocalStore localStore = storeMap.get(baseDir);
|
|
|
- localStore.setAvail(map.get(baseDir));
|
|
|
+ String maxDisk = diskDirs.get(diskDirs.size()-1);
|
|
|
+ LocalStore localStore = storeMap.get(maxDisk);
|
|
|
+ localStore.setCurrentAvailable(map.get(maxDisk));
|
|
|
return localStore;
|
|
|
}
|
|
|
|
|
|
- public static List<StoreDir> getSubDirs(String blockId) {
|
|
|
- return new ArrayList<>(subDirs.get(blockId).values());
|
|
|
+ public static List<StoreDir> getSubDirs(String diskDir) {
|
|
|
+ return new ArrayList<>(storeDirMap.get(diskDir).values());
|
|
|
}
|
|
|
}
|