Merge pull request #2849 from lin-mt/develop

fix:#2839
This commit is contained in:
杨翊 SionYang 2020-06-10 10:35:43 +08:00 committed by GitHub
commit ecf65eec01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,11 +50,13 @@ import java.util.concurrent.ConcurrentMap;
@Component @Component
public class RaftStore { 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 { 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 { 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()) { if (!metaFile.exists() && !metaFile.getParentFile().mkdirs() && !metaFile.createNewFile()) {
throw new IllegalStateException("failed to create meta file: " + metaFile.getAbsolutePath()); 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()); 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; continue;
} }
@ -112,12 +114,16 @@ public class RaftStore {
return null; 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; ByteBuffer buffer;
FileChannel fc = null; try (FileChannel fc = new FileInputStream(file).getChannel()) {
try {
fc = new FileInputStream(file).getChannel();
buffer = ByteBuffer.allocate((int) file.length()); buffer = ByteBuffer.allocate((int) file.length());
fc.read(buffer); fc.read(buffer);
@ -192,24 +198,25 @@ public class RaftStore {
} catch (Exception e) { } catch (Exception e) {
Loggers.RAFT.warn("waning: failed to deserialize key: {}", file.getName()); Loggers.RAFT.warn("waning: failed to deserialize key: {}", file.getName());
throw e; 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 { public synchronized void write(final Datum datum) throws Exception {
String namespaceId = KeyBuilder.getNamespace(datum.key); String namespaceId = KeyBuilder.getNamespace(datum.key);
File cacheFile; File cacheFile = new File(cacheFileName(namespaceId, datum));
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));
}
if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) { if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) {
MetricsMonitor.getDiskException().increment(); MetricsMonitor.getDiskException().increment();
@ -241,7 +248,7 @@ public class RaftStore {
String oldFormatKey = String oldFormatKey =
datum.key.replace(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER, StringUtils.EMPTY); 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()) { if (cacheFile.exists() && !cacheFile.delete()) {
Loggers.RAFT.error("[RAFT-DELETE] failed to delete old format datum: {}, value: {}", Loggers.RAFT.error("[RAFT-DELETE] failed to delete old format datum: {}, value: {}",
datum.key, datum.value); datum.key, datum.value);
@ -252,7 +259,7 @@ public class RaftStore {
} }
private File[] listCaches() throws Exception { private File[] listCaches() throws Exception {
File cacheDir = new File(this.cacheDir); File cacheDir = new File(CACHE_DIR);
if (!cacheDir.exists() && !cacheDir.mkdirs()) { if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new IllegalStateException("cloud not make out directory: " + cacheDir.getName()); throw new IllegalStateException("cloud not make out directory: " + cacheDir.getName());
} }
@ -267,7 +274,7 @@ public class RaftStore {
if (StringUtils.isNotBlank(namespaceId)) { 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()) { if (cacheFile.exists() && !cacheFile.delete()) {
Loggers.RAFT.error("[RAFT-DELETE] failed to delete datum: {}, value: {}", datum.key, datum.value); Loggers.RAFT.error("[RAFT-DELETE] failed to delete datum: {}, value: {}", datum.key, datum.value);
throw new IllegalStateException("failed to delete datum: " + datum.key); throw new IllegalStateException("failed to delete datum: " + datum.key);
@ -276,7 +283,7 @@ public class RaftStore {
} }
public void updateTerm(long term) throws Exception { 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()) { if (!file.exists() && !file.getParentFile().mkdirs() && !file.createNewFile()) {
throw new IllegalStateException("failed to create meta file"); throw new IllegalStateException("failed to create meta file");
} }
@ -288,11 +295,11 @@ public class RaftStore {
} }
} }
private static String encodeFileName(String fileName) { private static String encodeDatumKey(String datumKey) {
return fileName.replace(':', '#'); return datumKey.replace(':', '#');
} }
private static String decodeFileName(String fileName) { private static String decodeDatumKey(String datumKey) {
return fileName.replace("#", ":"); return datumKey.replace("#", ":");
} }
} }