Skip to content

Commit

Permalink
[ISSUE alibaba#3160]fix compatibility issue when reading cache files …
Browse files Browse the repository at this point in the history
…with suffix ".datum" (alibaba#3163)

* fix: alibaba#3160

* change the name definition
  • Loading branch information
Maijh97 authored Jun 24, 2020
1 parent ac0eabf commit aac1c6f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public static boolean matchServiceMetaKey(String key, String namespaceId, String
}

public static boolean matchSwitchKey(String key) {
return key.endsWith(UtilsAndCommons.SWITCH_DOMAIN_NAME);
return key.endsWith(UtilsAndCommons.SWITCH_DOMAIN_NAME) ||
key.endsWith(UtilsAndCommons.SWITCH_DOMAIN_NAME + UtilsAndCommons.RAFT_CACHE_FILE_SUFFIX);
}

public static boolean matchServiceName(String key, String namespaceId, String serviceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -45,6 +44,10 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentMap;

import static com.alibaba.nacos.naming.misc.UtilsAndCommons.DATA_BASE_DIR;
import static com.alibaba.nacos.naming.misc.UtilsAndCommons.RAFT_CACHE_FILE_PREFIX;
import static com.alibaba.nacos.naming.misc.UtilsAndCommons.RAFT_CACHE_FILE_SUFFIX;

/**
* Raft store.
*
Expand All @@ -55,11 +58,9 @@ public class RaftStore {

private final Properties meta = new Properties();

private static final String META_FILE_NAME = UtilsAndCommons.DATA_BASE_DIR + File.separator + "meta.properties";

private static final String CACHE_DIR = UtilsAndCommons.DATA_BASE_DIR + File.separator + "data";
private static final String META_FILE_NAME = DATA_BASE_DIR + File.separator + "meta.properties";

private static final String CACHE_FILE_SUFFIX = ".datum";
private static final String CACHE_DIR = DATA_BASE_DIR + File.separator + "data";

/**
* Load datum from cache file.
Expand Down Expand Up @@ -127,7 +128,8 @@ public synchronized Datum load(String key) throws Exception {
Loggers.RAFT.warn("warning: encountered directory in cache dir: {}", cache.getAbsolutePath());
}

if (!StringUtils.equals(cache.getName(), encodeDatumKey(key) + CACHE_FILE_SUFFIX)) {
if (!StringUtils.equals(cache.getName(), encodeDatumKey(key)) &&
!StringUtils.equals(cache.getName(), encodeDatumKey(key) + RAFT_CACHE_FILE_SUFFIX)) {
continue;
}

Expand All @@ -139,7 +141,7 @@ public synchronized Datum load(String key) throws Exception {
}

private boolean isDatumCacheFile(String fileName) {
return fileName.endsWith(CACHE_FILE_SUFFIX);
return fileName.endsWith(RAFT_CACHE_FILE_SUFFIX) || fileName.startsWith(RAFT_CACHE_FILE_PREFIX);
}

private synchronized Datum readDatum(File file, String namespaceId) throws IOException {
Expand All @@ -156,12 +158,14 @@ private synchronized Datum readDatum(File file, String namespaceId) throws IOExc
return null;
}

if (KeyBuilder.matchSwitchKey(file.getName())) {
final String fileName = file.getName();

if (KeyBuilder.matchSwitchKey(fileName)) {
return JacksonUtils.toObj(json, new TypeReference<Datum<SwitchDomain>>() {
});
}

if (KeyBuilder.matchServiceMetaKey(file.getName())) {
if (KeyBuilder.matchServiceMetaKey(fileName)) {

Datum<Service> serviceDatum;

Expand All @@ -188,7 +192,7 @@ private synchronized Datum readDatum(File file, String namespaceId) throws IOExc
return serviceDatum;
}

if (KeyBuilder.matchInstanceListKey(file.getName())) {
if (KeyBuilder.matchInstanceListKey(fileName)) {

Datum<Instances> instancesDatum;

Expand Down Expand Up @@ -235,10 +239,17 @@ private String cacheFileName(String namespaceId, Datum datum) {
} else {
fileName = CACHE_DIR + File.separator + encodeDatumKey(datum.key);
}
fileName += CACHE_FILE_SUFFIX;
return fileName;
}

private File cacheFile(String cacheFileName) {
File cacheFile = new File(cacheFileName);
if (cacheFile.exists()) {
return cacheFile;
}
return new File(cacheFileName + RAFT_CACHE_FILE_SUFFIX);
}

/**
* Write datum to cache file.
*
Expand All @@ -249,7 +260,7 @@ public synchronized void write(final Datum datum) throws Exception {

String namespaceId = KeyBuilder.getNamespace(datum.key);

File cacheFile = new File(cacheFileName(namespaceId, datum));
File cacheFile = cacheFile(cacheFileName(namespaceId, datum));

if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) {
MetricsMonitor.getDiskException().increment();
Expand Down Expand Up @@ -281,7 +292,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(cacheFileName(namespaceId, datum));
cacheFile = cacheFile(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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
public void init() {

try {
consistencyService.listen(UtilsAndCommons.getSwitchDomainKey(), this);
consistencyService.listen(KeyBuilder.getSwitchDomainKey(), this);
} catch (NacosException e) {
Loggers.SRV_LOG.error("listen switch service failed.", e);
}
Expand All @@ -79,7 +79,7 @@ public void update(String entry, String value, boolean debug) throws Exception {
lock.lock();
try {

Datum datum = consistencyService.get(UtilsAndCommons.getSwitchDomainKey());
Datum datum = consistencyService.get(KeyBuilder.getSwitchDomainKey());
SwitchDomain switchDomain;

if (datum != null && datum.value != null) {
Expand Down Expand Up @@ -291,7 +291,7 @@ public void update(String entry, String value, boolean debug) throws Exception {
if (debug) {
update(switchDomain);
} else {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
consistencyService.put(KeyBuilder.getSwitchDomainKey(), switchDomain);
}

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public class UtilsAndCommons {
public static final String DATA_BASE_DIR =
ApplicationUtils.getNacosHome() + File.separator + "data" + File.separator + "naming";

public static final String RAFT_CACHE_FILE_SUFFIX = ".datum";

public static final String RAFT_CACHE_FILE_PREFIX = "com.alibaba.nacos.naming";

public static final String NUMBER_PATTERN = "^\\d+$";

public static final ScheduledExecutorService SERVICE_SYNCHRONIZATION_EXECUTOR;
Expand Down Expand Up @@ -181,10 +185,6 @@ public class UtilsAndCommons {

}

public static String getSwitchDomainKey() {
return UtilsAndCommons.DOMAINS_DATA_ID_PRE + UtilsAndCommons.SWITCH_DOMAIN_NAME;
}

/**
* Parse meta data from string.
*
Expand Down

0 comments on commit aac1c6f

Please sign in to comment.