|
|
@ -37,7 +37,6 @@ import org.apache.dolphinscheduler.remote.config.NettyClientConfig; |
|
|
|
import org.apache.dolphinscheduler.remote.exceptions.RemotingException; |
|
|
|
import org.apache.dolphinscheduler.remote.exceptions.RemotingException; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable; |
|
|
|
import javax.annotation.Nullable; |
|
|
@ -46,43 +45,23 @@ import lombok.NonNull; |
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@Service |
|
|
|
* log client |
|
|
|
public class LogClient implements AutoCloseable { |
|
|
|
*/ |
|
|
|
|
|
|
|
public class LogClientService implements AutoCloseable { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LogClientService.class); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LogClient.class); |
|
|
|
|
|
|
|
|
|
|
|
private final NettyClientConfig clientConfig; |
|
|
|
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
|
|
|
|
|
|
|
|
|
|
|
private final NettyRemotingClient client; |
|
|
|
private final NettyRemotingClient client; |
|
|
|
|
|
|
|
|
|
|
|
private volatile boolean isRunning; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* request time out |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static final long LOG_REQUEST_TIMEOUT = 10 * 1000L; |
|
|
|
private static final long LOG_REQUEST_TIMEOUT = 10 * 1000L; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public LogClient() { |
|
|
|
* construct client |
|
|
|
NettyClientConfig nettyClientConfig = new NettyClientConfig(); |
|
|
|
*/ |
|
|
|
this.client = new NettyRemotingClient(nettyClientConfig); |
|
|
|
public LogClientService() { |
|
|
|
logger.info("Initialized LogClientService with config: {}", nettyClientConfig); |
|
|
|
this.clientConfig = new NettyClientConfig(); |
|
|
|
|
|
|
|
this.clientConfig.setWorkerThreads(4); |
|
|
|
|
|
|
|
this.client = new NettyRemotingClient(clientConfig); |
|
|
|
|
|
|
|
this.isRunning = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* close |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void close() { |
|
|
|
|
|
|
|
this.client.close(); |
|
|
|
|
|
|
|
this.isRunning = false; |
|
|
|
|
|
|
|
logger.info("logger client closed"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -96,25 +75,30 @@ public class LogClientService implements AutoCloseable { |
|
|
|
* @return log content |
|
|
|
* @return log content |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public String rollViewLog(String host, int port, String path, int skipLineNum, int limit) { |
|
|
|
public String rollViewLog(String host, int port, String path, int skipLineNum, int limit) { |
|
|
|
logger.info("roll view log, host : {}, port : {}, path {}, skipLineNum {} ,limit {}", host, port, path, |
|
|
|
logger.info("Roll view log from host : {}, port : {}, path {}, skipLineNum {} ,limit {}", host, port, path, |
|
|
|
skipLineNum, limit); |
|
|
|
skipLineNum, limit); |
|
|
|
RollViewLogRequestCommand request = new RollViewLogRequestCommand(path, skipLineNum, limit); |
|
|
|
RollViewLogRequestCommand request = new RollViewLogRequestCommand(path, skipLineNum, limit); |
|
|
|
String result = ""; |
|
|
|
|
|
|
|
final Host address = new Host(host, port); |
|
|
|
final Host address = new Host(host, port); |
|
|
|
try { |
|
|
|
try { |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
Command response = client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
if (response != null) { |
|
|
|
if (response != null) { |
|
|
|
RollViewLogResponseCommand rollReviewLog = JSONUtils.parseObject( |
|
|
|
RollViewLogResponseCommand rollReviewLog = |
|
|
|
response.getBody(), RollViewLogResponseCommand.class); |
|
|
|
JSONUtils.parseObject(response.getBody(), RollViewLogResponseCommand.class); |
|
|
|
return rollReviewLog.getMsg(); |
|
|
|
return rollReviewLog.getMsg(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return "Roll view log response is null"; |
|
|
|
|
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
|
|
|
|
logger.error( |
|
|
|
|
|
|
|
"Roll view log from host : {}, port : {}, path {}, skipLineNum {} ,limit {} error, the current thread has been interrupted", |
|
|
|
|
|
|
|
host, port, path, skipLineNum, limit, ex); |
|
|
|
|
|
|
|
return "Roll view log error: " + ex.getMessage(); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("roll view log error", e); |
|
|
|
logger.error("Roll view log from host : {}, port : {}, path {}, skipLineNum {} ,limit {} error", host, port, |
|
|
|
} finally { |
|
|
|
path, skipLineNum, limit, e); |
|
|
|
this.client.closeChannel(address); |
|
|
|
return "Roll view log error: " + e.getMessage(); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -126,28 +110,31 @@ public class LogClientService implements AutoCloseable { |
|
|
|
* @return log content |
|
|
|
* @return log content |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public String viewLog(String host, int port, String path) { |
|
|
|
public String viewLog(String host, int port, String path) { |
|
|
|
logger.info("view log path {}", path); |
|
|
|
logger.info("View log from host: {}, port: {}, logPath: {}", host, port, path); |
|
|
|
ViewLogRequestCommand request = new ViewLogRequestCommand(path); |
|
|
|
ViewLogRequestCommand request = new ViewLogRequestCommand(path); |
|
|
|
String result = ""; |
|
|
|
|
|
|
|
final Host address = new Host(host, port); |
|
|
|
final Host address = new Host(host, port); |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (NetUtils.getHost().equals(host)) { |
|
|
|
if (NetUtils.getHost().equals(host)) { |
|
|
|
result = LoggerUtils.readWholeFileContent(request.getPath()); |
|
|
|
return LoggerUtils.readWholeFileContent(request.getPath()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
if (response != null) { |
|
|
|
if (response != null) { |
|
|
|
ViewLogResponseCommand viewLog = JSONUtils.parseObject( |
|
|
|
ViewLogResponseCommand viewLog = |
|
|
|
response.getBody(), ViewLogResponseCommand.class); |
|
|
|
JSONUtils.parseObject(response.getBody(), ViewLogResponseCommand.class); |
|
|
|
result = viewLog.getMsg(); |
|
|
|
return viewLog.getMsg(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return "View log response is null"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
|
|
|
|
logger.error("View log from host: {}, port: {}, logPath: {} error, the current thread has been interrupted", |
|
|
|
|
|
|
|
host, port, path, ex); |
|
|
|
|
|
|
|
return "View log error: " + ex.getMessage(); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("view log error", e); |
|
|
|
logger.error("View log from host: {}, port: {}, logPath: {} error", host, port, path, e); |
|
|
|
} finally { |
|
|
|
return "View log error: " + e.getMessage(); |
|
|
|
this.client.closeChannel(address); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -159,23 +146,28 @@ public class LogClientService implements AutoCloseable { |
|
|
|
* @return log content bytes |
|
|
|
* @return log content bytes |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public byte[] getLogBytes(String host, int port, String path) { |
|
|
|
public byte[] getLogBytes(String host, int port, String path) { |
|
|
|
logger.info("log path {}", path); |
|
|
|
logger.info("Get log bytes from host: {}, port: {}, logPath {}", host, port, path); |
|
|
|
GetLogBytesRequestCommand request = new GetLogBytesRequestCommand(path); |
|
|
|
GetLogBytesRequestCommand request = new GetLogBytesRequestCommand(path); |
|
|
|
final Host address = new Host(host, port); |
|
|
|
final Host address = new Host(host, port); |
|
|
|
try { |
|
|
|
try { |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
if (response != null) { |
|
|
|
if (response != null) { |
|
|
|
GetLogBytesResponseCommand getLog = JSONUtils.parseObject( |
|
|
|
GetLogBytesResponseCommand getLog = |
|
|
|
response.getBody(), GetLogBytesResponseCommand.class); |
|
|
|
JSONUtils.parseObject(response.getBody(), GetLogBytesResponseCommand.class); |
|
|
|
return getLog.getData() == null ? new byte[0] : getLog.getData(); |
|
|
|
return getLog.getData() == null ? EMPTY_BYTE_ARRAY : getLog.getData(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return EMPTY_BYTE_ARRAY; |
|
|
|
|
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
|
|
|
|
logger.error( |
|
|
|
|
|
|
|
"Get logSize from host: {}, port: {}, logPath: {} error, the current thread has been interrupted", |
|
|
|
|
|
|
|
host, port, path, ex); |
|
|
|
|
|
|
|
return EMPTY_BYTE_ARRAY; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("get log size error", e); |
|
|
|
logger.error("Get logSize from host: {}, port: {}, logPath: {} error", host, port, path, e); |
|
|
|
} finally { |
|
|
|
return EMPTY_BYTE_ARRAY; |
|
|
|
this.client.closeChannel(address); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return new byte[0]; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -187,24 +179,28 @@ public class LogClientService implements AutoCloseable { |
|
|
|
* @return remove task status |
|
|
|
* @return remove task status |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public Boolean removeTaskLog(String host, int port, String path) { |
|
|
|
public Boolean removeTaskLog(String host, int port, String path) { |
|
|
|
logger.info("log path {}", path); |
|
|
|
logger.info("Remove task log from host: {}, port: {}, logPath {}", host, port, path); |
|
|
|
RemoveTaskLogRequestCommand request = new RemoveTaskLogRequestCommand(path); |
|
|
|
RemoveTaskLogRequestCommand request = new RemoveTaskLogRequestCommand(path); |
|
|
|
Boolean result = false; |
|
|
|
|
|
|
|
final Host address = new Host(host, port); |
|
|
|
final Host address = new Host(host, port); |
|
|
|
try { |
|
|
|
try { |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command command = request.convert2Command(); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
Command response = this.client.sendSync(address, command, LOG_REQUEST_TIMEOUT); |
|
|
|
if (response != null) { |
|
|
|
if (response != null) { |
|
|
|
RemoveTaskLogResponseCommand taskLogResponse = JSONUtils.parseObject( |
|
|
|
RemoveTaskLogResponseCommand taskLogResponse = |
|
|
|
response.getBody(), RemoveTaskLogResponseCommand.class); |
|
|
|
JSONUtils.parseObject(response.getBody(), RemoveTaskLogResponseCommand.class); |
|
|
|
return taskLogResponse.getStatus(); |
|
|
|
return taskLogResponse.getStatus(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
|
|
|
|
logger.error( |
|
|
|
|
|
|
|
"Remove task log from host: {}, port: {} logPath: {} error, the current thread has been interrupted", |
|
|
|
|
|
|
|
host, port, path, ex); |
|
|
|
|
|
|
|
return false; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("remove task log error", e); |
|
|
|
logger.error("Remove task log from host: {}, port: {} logPath: {} error", host, port, path, e); |
|
|
|
} finally { |
|
|
|
return false; |
|
|
|
this.client.closeChannel(address); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public @Nullable List<String> getAppIds(@NonNull String host, int port, |
|
|
|
public @Nullable List<String> getAppIds(@NonNull String host, int port, |
|
|
@ -212,9 +208,8 @@ public class LogClientService implements AutoCloseable { |
|
|
|
logger.info("Begin to get appIds from worker: {}:{} taskLogPath: {}", host, port, taskLogFilePath); |
|
|
|
logger.info("Begin to get appIds from worker: {}:{} taskLogPath: {}", host, port, taskLogFilePath); |
|
|
|
final Host workerAddress = new Host(host, port); |
|
|
|
final Host workerAddress = new Host(host, port); |
|
|
|
List<String> appIds = null; |
|
|
|
List<String> appIds = null; |
|
|
|
try { |
|
|
|
|
|
|
|
if (NetUtils.getHost().equals(host)) { |
|
|
|
if (NetUtils.getHost().equals(host)) { |
|
|
|
appIds = new ArrayList<>(LogUtils.getAppIdsFromLogFile(taskLogFilePath)); |
|
|
|
appIds = LogUtils.getAppIdsFromLogFile(taskLogFilePath); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
final Command command = new GetAppIdRequestCommand(taskLogFilePath).convert2Command(); |
|
|
|
final Command command = new GetAppIdRequestCommand(taskLogFilePath).convert2Command(); |
|
|
|
Command response = this.client.sendSync(workerAddress, command, LOG_REQUEST_TIMEOUT); |
|
|
|
Command response = this.client.sendSync(workerAddress, command, LOG_REQUEST_TIMEOUT); |
|
|
@ -224,14 +219,14 @@ public class LogClientService implements AutoCloseable { |
|
|
|
appIds = responseCommand.getAppIds(); |
|
|
|
appIds = responseCommand.getAppIds(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} finally { |
|
|
|
|
|
|
|
client.closeChannel(workerAddress); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
logger.info("Get appIds: {} from worker: {}:{} taskLogPath: {}", appIds, host, port, taskLogFilePath); |
|
|
|
logger.info("Get appIds: {} from worker: {}:{} taskLogPath: {}", appIds, host, port, taskLogFilePath); |
|
|
|
return appIds; |
|
|
|
return appIds; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean isRunning() { |
|
|
|
@Override |
|
|
|
return isRunning; |
|
|
|
public void close() { |
|
|
|
|
|
|
|
this.client.close(); |
|
|
|
|
|
|
|
logger.info("LogClientService closed"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |