未验证 提交 c4516fd1 编写于 作者: W Wenjun Ruan 提交者: GitHub

Use LineNumBer to read line (#150)

上级 34217ffe
......@@ -43,8 +43,10 @@ import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
......@@ -180,7 +182,7 @@ public class LoggerRequestProcessor implements NettyRequestProcessor {
return new byte[0];
}
private RollViewLogResponseCommand readPartFileContent(RollViewLogRequestCommand rollViewLogRequest) {
protected RollViewLogResponseCommand readPartFileContent(RollViewLogRequestCommand rollViewLogRequest) {
String rollViewLogPath = rollViewLogRequest.getPath();
if (!checkPathSecurity(rollViewLogPath)) {
......@@ -195,13 +197,16 @@ public class LoggerRequestProcessor implements NettyRequestProcessor {
int skipLine = rollViewLogRequest.getSkipLineNum();
int limit = rollViewLogRequest.getLimit();
try (Stream<String> stream = Files.lines(Paths.get(rollViewLogPath))) {
try (
Stream<String> stream = Files.lines(Paths.get(rollViewLogPath));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(rollViewLogPath))) {
List<String> lines = stream.skip(skipLine).limit(limit).collect(Collectors.toList());
long totalLineNumber = stream.count();
lineNumberReader.skip(Long.MAX_VALUE);
return RollViewLogResponseCommand.builder()
.currentLineNumber(skipLine + lines.size())
.currentTotalLineNumber(totalLineNumber)
.currentTotalLineNumber(lineNumberReader.getLineNumber())
.log(String.join("\r\n", lines))
.build();
} catch (IOException e) {
......
......@@ -17,12 +17,15 @@
package org.apache.dolphinscheduler.server.log;
import io.netty.channel.Channel;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.LoggerUtils;
import org.apache.dolphinscheduler.remote.command.Command;
import org.apache.dolphinscheduler.remote.command.CommandType;
import org.apache.dolphinscheduler.remote.command.log.RollViewLogRequestCommand;
import org.apache.dolphinscheduler.remote.command.log.RollViewLogResponseCommand;
import org.apache.dolphinscheduler.remote.command.log.ViewLogRequestCommand;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
......@@ -30,8 +33,6 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import io.netty.channel.Channel;
@RunWith(PowerMockRunner.class)
@PrepareForTest({LoggerUtils.class})
public class LoggerRequestProcessorTest {
......@@ -106,4 +107,34 @@ public class LoggerRequestProcessorTest {
LoggerRequestProcessor loggerRequestProcessor = new LoggerRequestProcessor();
loggerRequestProcessor.process(channel, command);
}
@Test
public void testReadPartFileContent() {
String logPath = LoggerRequestProcessorTest.class.getResource("/mock.log").getPath();
System.setProperty("DOLPHINSCHEDULER_WORKER_HOME", logPath);
LoggerRequestProcessor loggerRequestProcessor = new LoggerRequestProcessor();
RollViewLogRequestCommand requestCommand = RollViewLogRequestCommand.builder()
.path(logPath)
.skipLineNum(0)
.limit(1)
.build();
RollViewLogResponseCommand responseCommand = loggerRequestProcessor.readPartFileContent(requestCommand);
Assert.assertEquals(8, responseCommand.getCurrentTotalLineNumber());
Assert.assertEquals(1, responseCommand.getCurrentLineNumber());
Assert.assertEquals("line1", responseCommand.getLog());
Assert.assertEquals(RollViewLogResponseCommand.Status.SUCCESS, responseCommand.getResponseStatus());
RollViewLogRequestCommand requestCommandSkipNumber = RollViewLogRequestCommand.builder()
.path(logPath)
.skipLineNum(1)
.limit(1)
.build();
RollViewLogResponseCommand requestCommandSkipNumberResponse =
loggerRequestProcessor.readPartFileContent(requestCommandSkipNumber);
Assert.assertEquals(8, requestCommandSkipNumberResponse.getCurrentTotalLineNumber());
Assert.assertEquals(2, requestCommandSkipNumberResponse.getCurrentLineNumber());
Assert.assertEquals("line2", requestCommandSkipNumberResponse.getLog());
Assert.assertEquals(RollViewLogResponseCommand.Status.SUCCESS,
requestCommandSkipNumberResponse.getResponseStatus());
}
}
line1
line2
line3
line4
line5
line6
line7
line8
line9
\ No newline at end of file
......@@ -42,7 +42,6 @@ public class RollViewLogResponseCommand implements Serializable {
private long currentTotalLineNumber;
public static RollViewLogResponseCommand error(Status status) {
RollViewLogResponseCommand rollViewLogResponseCommand = new RollViewLogResponseCommand();
rollViewLogResponseCommand.setResponseStatus(status);
......@@ -58,6 +57,7 @@ public class RollViewLogResponseCommand implements Serializable {
}
public enum Status {
SUCCESS("success"),
LOG_PATH_IS_NOT_SECURITY("Log file path is not at a security directory"),
LOG_FILE_NOT_FOUND("Log file doesn't exist"),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册