commit
ecf65eec01
@ -50,11 +50,13 @@ import java.util.concurrent.ConcurrentMap;
|
||||
@Component
|
||||
public class RaftStore {
|
||||
|
||||
private Properties meta = new Properties();
|
||||
private final Properties meta = new Properties();
|
||||
|
||||
private String metaFileName = UtilsAndCommons.DATA_BASE_DIR + File.separator + "meta.properties";
|
||||
private static final String META_FILE_NAME = UtilsAndCommons.DATA_BASE_DIR + File.separator + "meta.properties";
|
||||
|
||||
private String cacheDir = UtilsAndCommons.DATA_BASE_DIR + File.separator + "data";
|
||||
private static final String CACHE_DIR = UtilsAndCommons.DATA_BASE_DIR + File.separator + "data";
|
||||
|
||||
private static final String CACHE_FILE_SUFFIX = ".datum";
|
||||
|
||||
public synchronized void loadDatums(RaftCore.Notifier notifier, ConcurrentMap<String, Datum> datums) throws Exception {
|
||||
|
||||
@ -81,7 +83,7 @@ public class RaftStore {
|
||||
}
|
||||
|
||||
public synchronized Properties loadMeta() throws Exception {
|
||||
File metaFile = new File(metaFileName);
|
||||
File metaFile = new File(META_FILE_NAME);
|
||||
if (!metaFile.exists() && !metaFile.getParentFile().mkdirs() && !metaFile.createNewFile()) {
|
||||
throw new IllegalStateException("failed to create meta file: " + metaFile.getAbsolutePath());
|
||||
}
|
||||
@ -100,7 +102,7 @@ public class RaftStore {
|
||||
Loggers.RAFT.warn("warning: encountered directory in cache dir: {}", cache.getAbsolutePath());
|
||||
}
|
||||
|
||||
if (!StringUtils.equals(cache.getName(), encodeFileName(key))) {
|
||||
if (!StringUtils.equals(cache.getName(), encodeDatumKey(key) + CACHE_FILE_SUFFIX)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -112,12 +114,16 @@ public class RaftStore {
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized Datum readDatum(File file, String namespaceId) throws IOException {
|
||||
private boolean isDatumCacheFile(String fileName) {
|
||||
return fileName.endsWith(CACHE_FILE_SUFFIX);
|
||||
}
|
||||
|
||||
public synchronized Datum readDatum(File file, String namespaceId) throws IOException {
|
||||
if (!isDatumCacheFile(file.getName())) {
|
||||
return null;
|
||||
}
|
||||
ByteBuffer buffer;
|
||||
FileChannel fc = null;
|
||||
try {
|
||||
fc = new FileInputStream(file).getChannel();
|
||||
try (FileChannel fc = new FileInputStream(file).getChannel()) {
|
||||
buffer = ByteBuffer.allocate((int) file.length());
|
||||
fc.read(buffer);
|
||||
|
||||
@ -192,24 +198,25 @@ public class RaftStore {
|
||||
} catch (Exception e) {
|
||||
Loggers.RAFT.warn("waning: failed to deserialize key: {}", file.getName());
|
||||
throw e;
|
||||
} finally {
|
||||
if (fc != null) {
|
||||
fc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String cacheFileName(String namespaceId, Datum datum) {
|
||||
String fileName;
|
||||
if (StringUtils.isNotBlank(namespaceId)) {
|
||||
fileName = CACHE_DIR + File.separator + namespaceId + File.separator + encodeDatumKey(datum.key);
|
||||
} else {
|
||||
fileName = CACHE_DIR + File.separator + encodeDatumKey(datum.key);
|
||||
}
|
||||
fileName += CACHE_FILE_SUFFIX;
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public synchronized void write(final Datum datum) throws Exception {
|
||||
|
||||
String namespaceId = KeyBuilder.getNamespace(datum.key);
|
||||
|
||||
File cacheFile;
|
||||
|
||||
if (StringUtils.isNotBlank(namespaceId)) {
|
||||
cacheFile = new File(cacheDir + File.separator + namespaceId + File.separator + encodeFileName(datum.key));
|
||||
} else {
|
||||
cacheFile = new File(cacheDir + File.separator + encodeFileName(datum.key));
|
||||
}
|
||||
File cacheFile = new File(cacheFileName(namespaceId, datum));
|
||||
|
||||
if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) {
|
||||
MetricsMonitor.getDiskException().increment();
|
||||
@ -241,7 +248,7 @@ public class RaftStore {
|
||||
String oldFormatKey =
|
||||
datum.key.replace(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER, StringUtils.EMPTY);
|
||||
|
||||
cacheFile = new File(cacheDir + File.separator + namespaceId + File.separator + encodeFileName(oldFormatKey));
|
||||
cacheFile = new File(cacheFileName(namespaceId, datum));
|
||||
if (cacheFile.exists() && !cacheFile.delete()) {
|
||||
Loggers.RAFT.error("[RAFT-DELETE] failed to delete old format datum: {}, value: {}",
|
||||
datum.key, datum.value);
|
||||
@ -252,7 +259,7 @@ public class RaftStore {
|
||||
}
|
||||
|
||||
private File[] listCaches() throws Exception {
|
||||
File cacheDir = new File(this.cacheDir);
|
||||
File cacheDir = new File(CACHE_DIR);
|
||||
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
|
||||
throw new IllegalStateException("cloud not make out directory: " + cacheDir.getName());
|
||||
}
|
||||
@ -267,7 +274,7 @@ public class RaftStore {
|
||||
|
||||
if (StringUtils.isNotBlank(namespaceId)) {
|
||||
|
||||
File cacheFile = new File(cacheDir + File.separator + namespaceId + File.separator + encodeFileName(datum.key));
|
||||
File cacheFile = new File(cacheFileName(namespaceId, datum));
|
||||
if (cacheFile.exists() && !cacheFile.delete()) {
|
||||
Loggers.RAFT.error("[RAFT-DELETE] failed to delete datum: {}, value: {}", datum.key, datum.value);
|
||||
throw new IllegalStateException("failed to delete datum: " + datum.key);
|
||||
@ -276,7 +283,7 @@ public class RaftStore {
|
||||
}
|
||||
|
||||
public void updateTerm(long term) throws Exception {
|
||||
File file = new File(metaFileName);
|
||||
File file = new File(META_FILE_NAME);
|
||||
if (!file.exists() && !file.getParentFile().mkdirs() && !file.createNewFile()) {
|
||||
throw new IllegalStateException("failed to create meta file");
|
||||
}
|
||||
@ -288,11 +295,11 @@ public class RaftStore {
|
||||
}
|
||||
}
|
||||
|
||||
private static String encodeFileName(String fileName) {
|
||||
return fileName.replace(':', '#');
|
||||
private static String encodeDatumKey(String datumKey) {
|
||||
return datumKey.replace(':', '#');
|
||||
}
|
||||
|
||||
private static String decodeFileName(String fileName) {
|
||||
return fileName.replace("#", ":");
|
||||
private static String decodeDatumKey(String datumKey) {
|
||||
return datumKey.replace("#", ":");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user