Skip to content

Commit

Permalink
optimize config file read (#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiyue1102 authored Feb 4, 2021
1 parent 0449fbc commit 4f0ec0a
Showing 1 changed file with 18 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.core.remote.RequestHandler;
import com.alibaba.nacos.core.remote.control.TpsControl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import static com.alibaba.nacos.api.common.Constants.LINE_BREAK;
import static com.alibaba.nacos.api.common.Constants.ENCODE;
import static com.alibaba.nacos.config.server.utils.LogUtil.PULL_LOG;
import static com.alibaba.nacos.config.server.utils.RequestUtil.CLIENT_APPNAME_HEADER;

Expand All @@ -74,8 +71,7 @@ public ConfigQueryRequestHandler(PersistService persistService) {
@Override
@TpsControl(pointName = "ConfigQuery", parsers = {ConfigQueryGroupKeyParser.class, ConfigQueryGroupParser.class})
@Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class)
public ConfigQueryResponse handle(ConfigQueryRequest request, RequestMeta meta)
throws NacosException {
public ConfigQueryResponse handle(ConfigQueryRequest request, RequestMeta meta) throws NacosException {

try {
ConfigQueryResponse context = getContext(request, meta, request.isNotify());
Expand All @@ -99,7 +95,7 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re

final String groupKey = GroupKey2
.getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(), configQueryRequest.getTenant());

String autoTag = configQueryRequest.getHeader(com.alibaba.nacos.api.common.Constants.VIPSERVER_TAG);

String requestIpApp = meta.getLabels().get(CLIENT_APPNAME_HEADER);
Expand Down Expand Up @@ -208,7 +204,7 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re
}
}
}

response.setMd5(md5);

if (PropertyUtil.isDirectRead()) {
Expand All @@ -218,10 +214,16 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re

} else {
//read from file
String content = readFileContent(file);
response.setContent(content);
response.setLastModified(lastModified);
response.setResultCode(ResponseCode.SUCCESS.getCode());
String content = null;
try {
content = readFileContent(file);
response.setContent(content);
response.setLastModified(lastModified);
response.setResultCode(ResponseCode.SUCCESS.getCode());
} catch (IOException e) {
response.setErrorInfo(ResponseCode.FAIL.getCode(), e.getMessage());
return response;
}

}

Expand Down Expand Up @@ -262,34 +264,9 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re
* @param file file to read.
* @return content.
*/
public static String readFileContent(File file) {
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), Charset.forName(Constants.ENCODE));

reader = new BufferedReader(isr);
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr).append(LINE_BREAK);
}
if (sbf.indexOf(LINE_BREAK) > 0) {
sbf.setLength(sbf.length() - 1);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return sbf.toString();
public static String readFileContent(File file) throws IOException {
return FileUtils.readFileToString(file, ENCODE);

}

private static void releaseConfigReadLock(String groupKey) {
Expand Down

0 comments on commit 4f0ec0a

Please sign in to comment.