|
@@ -10,20 +10,17 @@ import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
|
* @date 2022-05-23 18:21:22
|
|
* @date 2022-05-23 18:21:22
|
|
|
*/
|
|
*/
|
|
|
public class LocalStores {
|
|
public class LocalStores {
|
|
|
- private static String domain;
|
|
|
|
|
- @Deprecated
|
|
|
|
|
private static final Map<String, LocalStore> storeMap = new HashMap<>();
|
|
private static final Map<String, LocalStore> storeMap = new HashMap<>();
|
|
|
- private static final Map<String, LocalStore> storeMap1 = new HashMap<>();
|
|
|
|
|
- private static final Map<String, List<String>> subDirs = new HashMap<>();
|
|
|
|
|
|
|
+ private static final Map<String, Map<String, StoreDir>> subDirs = new HashMap<>();
|
|
|
|
|
|
|
|
public static void init(DfsProperties dfsProperties) throws IOException {
|
|
public static void init(DfsProperties dfsProperties) throws IOException {
|
|
|
- domain = dfsProperties.getDomain();
|
|
|
|
|
Map<String, String> blockIdMap = getBlockIdMap();
|
|
Map<String, String> blockIdMap = getBlockIdMap();
|
|
|
Map<String, String> checker = new HashMap<>();
|
|
Map<String, String> checker = new HashMap<>();
|
|
|
for (String baseDir : dfsProperties.getBaseDirs()) {
|
|
for (String baseDir : dfsProperties.getBaseDirs()) {
|
|
@@ -41,57 +38,12 @@ public class LocalStores {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
LocalStore localStore = new LocalStore(baseDir, fileStore, blockId, 0.9);
|
|
LocalStore localStore = new LocalStore(baseDir, fileStore, blockId, 0.9);
|
|
|
- storeMap.put(baseDir, localStore);
|
|
|
|
|
- storeMap1.put(blockId, localStore);
|
|
|
|
|
- subDirs.computeIfAbsent(baseDir, k -> new ArrayList<>());
|
|
|
|
|
- createSubDirs(baseDir);
|
|
|
|
|
|
|
+ storeMap.put(blockId, localStore);
|
|
|
|
|
+ subDirs.computeIfAbsent(blockId, k -> new HashMap<>());
|
|
|
|
|
+ createSubDirs(blockId, baseDir);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String getDomain() {
|
|
|
|
|
- return domain;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // TODO 临时在 VideoFileServiceImpl 中使用
|
|
|
|
|
- @Deprecated
|
|
|
|
|
- public static String getBlockId(String path) {
|
|
|
|
|
- List<String> blockIds = new ArrayList<>();
|
|
|
|
|
- for (Map.Entry<String, LocalStore> entry : storeMap1.entrySet()) {
|
|
|
|
|
- String blockId = entry.getKey();
|
|
|
|
|
- LocalStore localStore = entry.getValue();
|
|
|
|
|
- String baseDir = localStore.getBaseDir();
|
|
|
|
|
- File file = new File(baseDir + path);
|
|
|
|
|
- if (file.exists()) {
|
|
|
|
|
- blockIds.add(blockId);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return blockIds.get(0);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String getBlockId1(String filePath) throws Exception {
|
|
|
|
|
- for (Map.Entry<String, LocalStore> entry : storeMap1.entrySet()) {
|
|
|
|
|
- String blockId = entry.getKey();
|
|
|
|
|
- LocalStore localStore = entry.getValue();
|
|
|
|
|
- String baseDir = localStore.getBaseDir();
|
|
|
|
|
- if (filePath.startsWith(baseDir)) {
|
|
|
|
|
- return blockId;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- throw new Exception(String.format("没有找到 %s 对应的 blockId", filePath));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String getRelativePath(String filePath) throws Exception {
|
|
|
|
|
- for (Map.Entry<String, LocalStore> entry : storeMap1.entrySet()) {
|
|
|
|
|
- LocalStore localStore = entry.getValue();
|
|
|
|
|
- String baseDir = localStore.getBaseDir();
|
|
|
|
|
- if (filePath.startsWith(baseDir)) {
|
|
|
|
|
- return filePath.replace(baseDir, "");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- throw new Exception(String.format("没有找到 %s 对应的 block", filePath));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private static Map<String, String> getBlockIdMap() throws IOException {
|
|
private static Map<String, String> getBlockIdMap() throws IOException {
|
|
|
Map<String, String> map = new HashMap<>();
|
|
Map<String, String> map = new HashMap<>();
|
|
|
File dir = new File("/dev/disk/by-uuid");
|
|
File dir = new File("/dev/disk/by-uuid");
|
|
@@ -104,36 +56,63 @@ public class LocalStores {
|
|
|
return map;
|
|
return map;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static void createSubDirs(String baseDir) throws IOException {
|
|
|
|
|
|
|
+ private static void createSubDirs(String blockId, String baseDir) throws IOException {
|
|
|
int total = 128;
|
|
int total = 128;
|
|
|
for (int i = 0; i < total; i++) {
|
|
for (int i = 0; i < total; i++) {
|
|
|
for (int j = 0; j < total; j++) {
|
|
for (int j = 0; j < total; j++) {
|
|
|
- String dirPath = String.format("%s%s/%s/", baseDir, i, j);
|
|
|
|
|
- File dir = new File(dirPath);
|
|
|
|
|
|
|
+ String absoluteDirPath = String.format("%s%s/%s/", baseDir, i, j);
|
|
|
|
|
+ File dir = new File(absoluteDirPath);
|
|
|
|
|
+ int filesCount = 0;
|
|
|
if (!dir.exists()) {
|
|
if (!dir.exists()) {
|
|
|
FileUtils.forceMkdir(dir);
|
|
FileUtils.forceMkdir(dir);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ filesCount = Objects.requireNonNull(dir.list()).length;
|
|
|
}
|
|
}
|
|
|
- subDirs.get(baseDir).add(dirPath);
|
|
|
|
|
|
|
+ StoreDir storeDir = new StoreDir(blockId, new AtomicInteger(filesCount), absoluteDirPath);
|
|
|
|
|
+ subDirs.get(blockId).put(absoluteDirPath, storeDir);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ 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));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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) {
|
|
public static String getMountedOn(String blockId) {
|
|
|
- return storeMap1.get(blockId).getMountedOn();
|
|
|
|
|
|
|
+ return storeMap.get(blockId).getMountedOn();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static String getBaseDir(String blockId) {
|
|
public static String getBaseDir(String blockId) {
|
|
|
- return storeMap1.get(blockId).getBaseDir();
|
|
|
|
|
|
|
+ return storeMap.get(blockId).getBaseDir();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// TODO 优化算法, 处理异常
|
|
// TODO 优化算法, 处理异常
|
|
|
public static LocalStore getMaxStore(long size) {
|
|
public static LocalStore getMaxStore(long size) {
|
|
|
Map<String, Long> map = new HashMap<>();
|
|
Map<String, Long> map = new HashMap<>();
|
|
|
for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
|
|
for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
|
|
|
- String baseDir = entry.getKey();
|
|
|
|
|
|
|
+ String blockId = entry.getKey();
|
|
|
LocalStore localStore = entry.getValue();
|
|
LocalStore localStore = entry.getValue();
|
|
|
- long availSize = localStore.getAvailable() - size;
|
|
|
|
|
- map.put(baseDir, availSize);
|
|
|
|
|
|
|
+ long availSize = localStore.getAvail() - size;
|
|
|
|
|
+ map.put(blockId, availSize);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
List<String> baseDirs = new ArrayList<>();
|
|
List<String> baseDirs = new ArrayList<>();
|
|
@@ -144,11 +123,16 @@ public class LocalStores {
|
|
|
|
|
|
|
|
String baseDir = baseDirs.get(baseDirs.size()-1);
|
|
String baseDir = baseDirs.get(baseDirs.size()-1);
|
|
|
LocalStore localStore = storeMap.get(baseDir);
|
|
LocalStore localStore = storeMap.get(baseDir);
|
|
|
- localStore.setAvailable(map.get(baseDir));
|
|
|
|
|
|
|
+ localStore.setAvail(map.get(baseDir));
|
|
|
return localStore;
|
|
return localStore;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static List<StoreDir> getSubDirs(String blockId) {
|
|
|
|
|
+ return new ArrayList<>(subDirs.get(blockId).values());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public static List<String> getSubDirs(String store) {
|
|
|
|
|
- return subDirs.get(store);
|
|
|
|
|
|
|
+ public static StoreDir getStoreDir(String absolutePath) throws Exception {
|
|
|
|
|
+ String blockId = getBlockId(absolutePath);
|
|
|
|
|
+ return subDirs.get(blockId).get(new File(absolutePath).getParent() + File.separator);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|