#421 Fixed
This commit is contained in:
parent
7916583541
commit
7115834a98
@ -17,6 +17,8 @@ package com.alibaba.nacos.api.naming.pojo;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -191,6 +193,15 @@ public class ServiceInfo {
|
|||||||
return getKey(name, clusters, env, isAllIPs());
|
return getKey(name, clusters, env, isAllIPs());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
public String getKeyEncoded() {
|
||||||
|
try {
|
||||||
|
return getKey(URLEncoder.encode(name, "UTF-8"), clusters, env, isAllIPs());
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
return getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@JSONField(serialize = false)
|
@JSONField(serialize = false)
|
||||||
public static String getKey(String name, String clusters, String unit) {
|
public static String getKey(String name, String clusters, String unit) {
|
||||||
return getKey(name, clusters, unit, false);
|
return getKey(name, clusters, unit, false);
|
||||||
|
@ -25,6 +25,7 @@ import com.alibaba.nacos.client.naming.utils.StringUtils;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -41,7 +42,9 @@ public class DiskCache {
|
|||||||
try {
|
try {
|
||||||
makeSureCacheDirExists(dir);
|
makeSureCacheDirExists(dir);
|
||||||
|
|
||||||
File file = new File(dir, dom.getKey());
|
|
||||||
|
|
||||||
|
File file = new File(dir, dom.getKeyEncoded());
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
// add another !file.exists() to avoid conflicted creating-new-file from multi-instances
|
// add another !file.exists() to avoid conflicted creating-new-file from multi-instances
|
||||||
if (!file.createNewFile() && !file.exists()) {
|
if (!file.createNewFile() && !file.exists()) {
|
||||||
@ -87,9 +90,11 @@ public class DiskCache {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(file.getName().endsWith(ServiceInfo.SPLITER + "meta") || file.getName().endsWith(
|
String fileName = URLDecoder.decode(file.getName(), "UTF-8");
|
||||||
|
|
||||||
|
if (!(fileName.endsWith(ServiceInfo.SPLITER + "meta") || fileName.endsWith(
|
||||||
ServiceInfo.SPLITER + "special-url"))) {
|
ServiceInfo.SPLITER + "special-url"))) {
|
||||||
ServiceInfo dom = new ServiceInfo(file.getName());
|
ServiceInfo dom = new ServiceInfo(fileName);
|
||||||
List<Instance> ips = new ArrayList<Instance>();
|
List<Instance> ips = new ArrayList<Instance>();
|
||||||
dom.setHosts(ips);
|
dom.setHosts(ips);
|
||||||
|
|
||||||
|
@ -107,7 +107,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(), key)) {
|
if (!StringUtils.equals(decodeFileName(cache.getName()), key)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ public class RaftStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized static void write(final Datum datum) throws Exception {
|
public synchronized static void write(final Datum datum) throws Exception {
|
||||||
File cacheFile = new File(CACHE_DIR + File.separator + datum.key);
|
File cacheFile = new File(CACHE_DIR + File.separator + encodeFileName(datum.key));
|
||||||
if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) {
|
if (!cacheFile.exists() && !cacheFile.getParentFile().mkdirs() && !cacheFile.createNewFile()) {
|
||||||
throw new IllegalStateException("can not make cache file: " + cacheFile.getName());
|
throw new IllegalStateException("can not make cache file: " + cacheFile.getName());
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ public class RaftStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void delete(Datum datum) {
|
public static void delete(Datum datum) {
|
||||||
File cacheFile = new File(CACHE_DIR + File.separator + datum.key);
|
File cacheFile = new File(CACHE_DIR + File.separator + encodeFileName(datum.key));
|
||||||
if (!cacheFile.delete()) {
|
if (!cacheFile.delete()) {
|
||||||
Loggers.RAFT.error("RAFT-DELETE", "failed to delete datum: " + datum.key + ", value: " + datum.value);
|
Loggers.RAFT.error("RAFT-DELETE", "failed to delete datum: " + datum.key + ", value: " + datum.value);
|
||||||
throw new IllegalStateException("failed to delete datum: " + datum.key);
|
throw new IllegalStateException("failed to delete datum: " + datum.key);
|
||||||
@ -188,4 +188,12 @@ public class RaftStore {
|
|||||||
meta.store(outStream, null);
|
meta.store(outStream, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String encodeFileName(String fileName) {
|
||||||
|
return fileName.replace(':', '#');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String decodeFileName(String fileName) {
|
||||||
|
return fileName.replace("#", ":");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user