Skip to content

Commit

Permalink
[ISSUE alibaba#11967] Can not aquire the specific config
Browse files Browse the repository at this point in the history
  • Loading branch information
syshenyao authored and yao.shen@hstong.com committed Apr 25, 2024
1 parent da3e88b commit 43fe2b2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.service.dump.disk;

import com.alibaba.nacos.api.utils.StringUtils;
import com.alibaba.nacos.common.pathencoder.PathEncoderManager;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -65,6 +66,10 @@ public void saveToDisk(String dataId, String group, String tenant, String conten
* Returns the path of the server cache file.
*/
private static File targetFile(String dataId, String group, String tenant) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), BASE_DIR);
Expand All @@ -81,6 +86,10 @@ private static File targetFile(String dataId, String group, String tenant) {
* Returns the path of cache file in server.
*/
private static File targetBetaFile(String dataId, String group, String tenant) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), BETA_DIR);
Expand All @@ -97,6 +106,10 @@ private static File targetBetaFile(String dataId, String group, String tenant) {
* Returns the path of the tag cache file in server.
*/
private static File targetTagFile(String dataId, String group, String tenant, String tag) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), TAG_DIR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.alibaba.nacos.config.server.service.dump.disk;

import junit.framework.TestCase;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ConfigRawDiskServiceTest extends TestCase {

/**
* 测试获取beta文件路径.
*/
public void testTargetFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetFile", String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
// 获取最后三段路径
String lastSegment = path.getFileName().toString();
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
assertEquals("aaaa%A3%dsaknkf", thirdLastSegment);
assertEquals("aaaa%A2%dsaknkf", secondLastSegment);
assertEquals("aaaa%A1%dsaknkf", lastSegment);
}

/**
* 测试获取beta文件路径.
*/
public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetBetaFile", String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
// 获取最后三段路径
String lastSegment = path.getFileName().toString();
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
assertEquals("aaaa%A3%dsaknkf", thirdLastSegment);
assertEquals("aaaa%A2%dsaknkf", secondLastSegment);
assertEquals("aaaa%A1%dsaknkf", lastSegment);

}

/**
* 测试获取tag文件路径.
* @throws NoSuchMethodException 方法不存在异常
* @throws IllegalAccessException 非法访问异常
* @throws InvocationTargetException 目标异常
*/
public void testTargetTagFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetTagFile", String.class, String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf", "aaaadsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
Path greatGrandParent = grandParent.getParent();
// 获取最后四段路径
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
String fourthLastSegment = greatGrandParent.getFileName().toString();
assertEquals("aaaa%A3%dsaknkf", fourthLastSegment);
assertEquals("aaaa%A2%dsaknkf", thirdLastSegment);
assertEquals("aaaa%A1%dsaknkf", secondLastSegment);
String lastSegment = path.getFileName().toString();
assertEquals("aaaadsaknkf", lastSegment);
}
}

0 comments on commit 43fe2b2

Please sign in to comment.