Skip to content

Commit

Permalink
Merge pull request #2849 from lin-mt/develop
Browse files Browse the repository at this point in the history
fix:#2839
  • Loading branch information
KomachiSion authored Jun 10, 2020
2 parents 510ffb5 + a7bc539 commit ecf65ee
Showing 1 changed file with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@
@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 {

Expand All @@ -81,7 +83,7 @@ public synchronized void loadDatums(RaftCore.Notifier notifier, ConcurrentMap<St
}

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());
}
Expand All @@ -100,7 +102,7 @@ public synchronized Datum load(String key) throws Exception {
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;
}

Expand All @@ -112,12 +114,16 @@ public synchronized Datum load(String key) throws Exception {
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);

Expand Down Expand Up @@ -192,24 +198,25 @@ public synchronized Datum readDatum(File file, String namespaceId) throws IOExce
} 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();
Expand Down Expand Up @@ -241,7 +248,7 @@ public synchronized void write(final Datum datum) throws Exception {
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);
Expand All @@ -252,7 +259,7 @@ public synchronized void write(final Datum datum) throws Exception {
}

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());
}
Expand All @@ -267,7 +274,7 @@ public void delete(Datum datum) {

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);
Expand All @@ -276,7 +283,7 @@ public void delete(Datum datum) {
}

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");
}
Expand All @@ -288,11 +295,11 @@ public void updateTerm(long term) throws Exception {
}
}

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("#", ":");
}
}

0 comments on commit ecf65ee

Please sign in to comment.