Unverified Commit 09aa5c0b authored by gaoyan's avatar gaoyan Committed by GitHub

Optimized the Kubernetes submission logic (#2484)

* Optimize the process * fix some bugs * Optimized the Kubernetes submission logic * close client after submit finish * fix some bug * fix some bug
parent 6d623f38
...@@ -101,7 +101,7 @@ public class ProcessAspect { ...@@ -101,7 +101,7 @@ public class ProcessAspect {
ProcessStepType processStepType = processStep.type(); ProcessStepType processStepType = processStep.type();
ProcessStepEntity step = contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME), parentStep); ProcessStepEntity step = contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME), parentStep);
MDC.put(PROCESS_STEP, step.getKey()); MDC.put(PROCESS_STEP, step.getKey());
contextHolder.appendLog(processName, step.getKey(), "Start Process Step:" + step.getType()); contextHolder.appendLog(processName, step.getKey(), "Start Process Step:" + step.getType(), true);
try { try {
result = joinPoint.proceed(); result = joinPoint.proceed();
......
...@@ -70,7 +70,7 @@ public class ConsoleContextHolder { ...@@ -70,7 +70,7 @@ public class ConsoleContextHolder {
/** /**
* Get a list of all processes * Get a list of all processes
* */ */
public List<ProcessEntity> list() { public List<ProcessEntity> list() {
return new ArrayList<>(logPross.values()); return new ArrayList<>(logPross.values());
} }
...@@ -97,12 +97,14 @@ public class ConsoleContextHolder { ...@@ -97,12 +97,14 @@ public class ConsoleContextHolder {
* @param log messages * @param log messages
* @throws BusException Throws an exception if the process does not exist * @throws BusException Throws an exception if the process does not exist
*/ */
public void appendLog(String processName, String stepPid, String log) { public void appendLog(String processName, String stepPid, String log, boolean recordGlobal) {
if (!logPross.containsKey(processName)) { if (!logPross.containsKey(processName)) {
throw new BusException(StrFormatter.format("process {} does not exist", processName)); throw new BusException(StrFormatter.format("process {} does not exist", processName));
} }
ProcessEntity process = logPross.get(processName); ProcessEntity process = logPross.get(processName);
if (recordGlobal) {
process.appendLog(log); process.appendLog(log);
}
if (stepPid != null) { if (stepPid != null) {
ProcessStepEntity stepNode = getStepNode(stepPid, getStepsMap(processName)); ProcessStepEntity stepNode = getStepNode(stepPid, getStepsMap(processName));
stepNode.appendLog(log); stepNode.appendLog(log);
...@@ -136,7 +138,7 @@ public class ConsoleContextHolder { ...@@ -136,7 +138,7 @@ public class ConsoleContextHolder {
.children(new CopyOnWriteArrayList<>()) .children(new CopyOnWriteArrayList<>())
.build(); .build();
logPross.put(processName, entity); logPross.put(processName, entity);
appendLog(processName, null, "Start Process:" + processName); appendLog(processName, null, "Start Process:" + processName, true);
} }
/** /**
...@@ -180,7 +182,6 @@ public class ConsoleContextHolder { ...@@ -180,7 +182,6 @@ public class ConsoleContextHolder {
* @param processName process name * @param processName process name
* @param status Process status * @param status Process status
* @param e exception object, optional * @param e exception object, optional
*
*/ */
public void finishedProcess(String processName, ProcessStatus status, Throwable e) { public void finishedProcess(String processName, ProcessStatus status, Throwable e) {
if (!logPross.containsKey(processName)) { if (!logPross.containsKey(processName)) {
...@@ -192,14 +193,14 @@ public class ConsoleContextHolder { ...@@ -192,14 +193,14 @@ public class ConsoleContextHolder {
process.setTime( process.setTime(
Duration.between(process.getStartTime(), process.getEndTime()).toMillis()); Duration.between(process.getStartTime(), process.getEndTime()).toMillis());
if (e != null) { if (e != null) {
appendLog(processName, null, LogUtil.getError(e)); appendLog(processName, null, LogUtil.getError(e.getCause()), true);
} }
String filePath = String.format("%s/tmp/log/%s.json", System.getProperty("user.dir"), process.getTitle()); String filePath = String.format("%s/tmp/log/%s.json", System.getProperty("user.dir"), process.getTitle());
if (FileUtil.exist(filePath)) { if (FileUtil.exist(filePath)) {
Assert.isTrue(FileUtil.del(filePath)); Assert.isTrue(FileUtil.del(filePath));
} }
FileUtil.writeUtf8String(JSONObject.toJSONString(process), filePath); FileUtil.writeUtf8String(JSONObject.toJSONString(process), filePath);
appendLog(processName, null, StrFormatter.format("Process {} exit with status:{}", processName, status)); appendLog(processName, null, StrFormatter.format("Process {} exit with status:{}", processName, status), true);
logPross.remove(processName); logPross.remove(processName);
} }
...@@ -211,7 +212,7 @@ public class ConsoleContextHolder { ...@@ -211,7 +212,7 @@ public class ConsoleContextHolder {
* @param status Process step status * @param status Process step status
* @param e exception object, optional * @param e exception object, optional
*/ */
public void finishedStep(String processName, ProcessStepEntity step, ProcessStatus status, Throwable e) { public void finishedStep(String processName, ProcessStepEntity step, ProcessStatus status, Exception e) {
if (!logPross.containsKey(processName)) { if (!logPross.containsKey(processName)) {
return; return;
} }
...@@ -219,12 +220,13 @@ public class ConsoleContextHolder { ...@@ -219,12 +220,13 @@ public class ConsoleContextHolder {
step.setEndTime(LocalDateTime.now()); step.setEndTime(LocalDateTime.now());
step.setTime(Duration.between(step.getStartTime(), step.getEndTime()).toMillis()); step.setTime(Duration.between(step.getStartTime(), step.getEndTime()).toMillis());
if (e != null) { if (e != null) {
appendLog(processName, step.getKey(), LogUtil.getError(e)); appendLog(processName, step.getKey(), LogUtil.getError(e.getCause()), false);
} }
appendLog( appendLog(
processName, processName,
step.getKey(), step.getKey(),
StrFormatter.format("Process Step {} exit with status:{}", step.getType(), status)); StrFormatter.format("Process Step {} exit with status:{}", step.getType(), status),
true);
} }
private ProcessStepEntity getStepNode(String stepPid, CopyOnWriteArrayList<ProcessStepEntity> stepsMap) { private ProcessStepEntity getStepNode(String stepPid, CopyOnWriteArrayList<ProcessStepEntity> stepsMap) {
...@@ -243,7 +245,7 @@ public class ConsoleContextHolder { ...@@ -243,7 +245,7 @@ public class ConsoleContextHolder {
/** /**
* 递归查找节点 * 递归查找节点
* */ */
private ProcessStepEntity findStepNode(String stepPid, CopyOnWriteArrayList<ProcessStepEntity> stepsMap) { private ProcessStepEntity findStepNode(String stepPid, CopyOnWriteArrayList<ProcessStepEntity> stepsMap) {
for (ProcessStepEntity processStepEntity : stepsMap) { for (ProcessStepEntity processStepEntity : stepsMap) {
if (processStepEntity.getKey().equals(stepPid)) { if (processStepEntity.getKey().equals(stepPid)) {
......
...@@ -228,9 +228,10 @@ public class JobRefreshHandler { ...@@ -228,9 +228,10 @@ public class JobRefreshHandler {
Gateway gateway = Gateway.build(gatewayConfig); Gateway gateway = Gateway.build(gatewayConfig);
return gateway.getJobStatusById(appId); return gateway.getJobStatusById(appId);
} catch (NotSupportGetStatusException ignored) { } catch (NotSupportGetStatusException ignored) {
// if the gateway does not support get status, then use the api to get job status
// ignore to do something here
} }
} }
JobDataDto jobDataDto = jobInfoDetail.getJobDataDto(); JobDataDto jobDataDto = jobInfoDetail.getJobDataDto();
String status = jobDataDto.getJob().getState(); String status = jobDataDto.getJob().getState();
return JobStatus.get(status); return JobStatus.get(status);
......
...@@ -251,7 +251,6 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen ...@@ -251,7 +251,6 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen
} }
@Override @Override
@ProcessStep(type = ProcessStepType.SUBMIT_TASK)
public JobResult submitTask(Integer id, String savePointPath) throws Exception { public JobResult submitTask(Integer id, String savePointPath) throws Exception {
TaskDTO taskDTO = this.getTaskInfoById(id); TaskDTO taskDTO = this.getTaskInfoById(id);
...@@ -262,16 +261,11 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen ...@@ -262,16 +261,11 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen
// 注解自调用会失效,这里通过获取对象方法绕过此限制 // 注解自调用会失效,这里通过获取对象方法绕过此限制
TaskServiceImpl taskServiceBean = applicationContext.getBean(TaskServiceImpl.class); TaskServiceImpl taskServiceBean = applicationContext.getBean(TaskServiceImpl.class);
JobResult jobResult = taskServiceBean.executeJob(taskDTO); JobResult jobResult = taskServiceBean.executeJob(taskDTO);
if (Job.JobStatus.SUCCESS == jobResult.getStatus()) {
log.info("Job Submit success"); log.info("Job Submit success");
Task task = new Task(id, jobResult.getJobInstanceId()); Task task = new Task(id, jobResult.getJobInstanceId());
if (!this.updateById(task)) { if (!this.updateById(task)) {
throw new BusException(Status.TASK_UPDATE_FAILED.getMessage()); throw new BusException(Status.TASK_UPDATE_FAILED.getMessage());
} }
} else {
log.error("Job Submit failed, error: " + jobResult.getError());
}
return jobResult; return jobResult;
} }
...@@ -426,6 +420,7 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen ...@@ -426,6 +420,7 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen
Integer tenantId = baseMapper.getTenantByTaskId(id); Integer tenantId = baseMapper.getTenantByTaskId(id);
Asserts.checkNull(tenantId, Status.TASK_NOT_EXIST.getMessage()); Asserts.checkNull(tenantId, Status.TASK_NOT_EXIST.getMessage());
TenantContextHolder.set(tenantId); TenantContextHolder.set(tenantId);
log.info("Init task tenan finished..");
} }
@Override @Override
......
...@@ -69,7 +69,7 @@ public class LogSseAppender extends AbstractAppender { ...@@ -69,7 +69,7 @@ public class LogSseAppender extends AbstractAppender {
String processName = contextData.getValue(ProcessAspect.PROCESS_NAME); String processName = contextData.getValue(ProcessAspect.PROCESS_NAME);
String processStepPid = contextData.getValue(ProcessAspect.PROCESS_STEP); String processStepPid = contextData.getValue(ProcessAspect.PROCESS_STEP);
String log = getStringLayout().toSerializable(event); String log = getStringLayout().toSerializable(event);
ConsoleContextHolder.getInstances().appendLog(processName, processStepPid, log); ConsoleContextHolder.getInstances().appendLog(processName, processStepPid, log, true);
} }
} }
......
...@@ -102,6 +102,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -102,6 +102,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
...@@ -604,12 +605,13 @@ public class JobManager { ...@@ -604,12 +605,13 @@ public class JobManager {
success(); success();
} }
} catch (Exception e) { } catch (Exception e) {
String error = LogUtil.getError("Exception in executing FlinkSQL:\n" + addLineNumber(currentSql), e); String error = StrFormatter.format(
"Exception in executing FlinkSQL:\n{}\n{}", addLineNumber(currentSql), e.getMessage());
job.setEndTime(LocalDateTime.now()); job.setEndTime(LocalDateTime.now());
job.setStatus(Job.JobStatus.FAILED); job.setStatus(Job.JobStatus.FAILED);
job.setError(error); job.setError(error);
failed(); failed();
throw e; throw new Exception(error, e);
} finally { } finally {
close(); close();
} }
......
...@@ -25,6 +25,7 @@ import org.dinky.gateway.config.GatewayConfig; ...@@ -25,6 +25,7 @@ import org.dinky.gateway.config.GatewayConfig;
import org.dinky.gateway.enums.ActionType; import org.dinky.gateway.enums.ActionType;
import org.dinky.gateway.enums.GatewayType; import org.dinky.gateway.enums.GatewayType;
import org.dinky.gateway.exception.GatewayException; import org.dinky.gateway.exception.GatewayException;
import org.dinky.gateway.exception.NotSupportGetStatusException;
import org.dinky.gateway.model.JobInfo; import org.dinky.gateway.model.JobInfo;
import org.dinky.gateway.result.GatewayResult; import org.dinky.gateway.result.GatewayResult;
import org.dinky.gateway.result.SavePointResult; import org.dinky.gateway.result.SavePointResult;
...@@ -172,7 +173,7 @@ public abstract class AbstractGateway implements Gateway { ...@@ -172,7 +173,7 @@ public abstract class AbstractGateway implements Gateway {
@Override @Override
public JobStatus getJobStatusById(String id) { public JobStatus getJobStatusById(String id) {
return JobStatus.UNKNOWN; throw new NotSupportGetStatusException(StrFormatter.format("{} is not support get status.", getType()));
} }
@Override @Override
......
...@@ -47,6 +47,8 @@ import cn.hutool.core.io.FileUtil; ...@@ -47,6 +47,8 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrFormatter; import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
/** /**
* KubernetesGateway * KubernetesGateway
...@@ -55,6 +57,7 @@ import cn.hutool.core.util.ReflectUtil; ...@@ -55,6 +57,7 @@ import cn.hutool.core.util.ReflectUtil;
public abstract class KubernetesGateway extends AbstractGateway { public abstract class KubernetesGateway extends AbstractGateway {
protected FlinkKubeClient client; protected FlinkKubeClient client;
protected KubernetesClient kubernetesClient;
public KubernetesGateway() {} public KubernetesGateway() {}
...@@ -108,6 +111,7 @@ public abstract class KubernetesGateway extends AbstractGateway { ...@@ -108,6 +111,7 @@ public abstract class KubernetesGateway extends AbstractGateway {
private void initKubeClient() { private void initKubeClient() {
client = FlinkKubeClientFactory.getInstance().fromConfiguration(configuration, "client"); client = FlinkKubeClientFactory.getInstance().fromConfiguration(configuration, "client");
kubernetesClient = new DefaultKubernetesClient();
} }
public SavePointResult savepointCluster(String savePoint) { public SavePointResult savepointCluster(String savePoint) {
......
...@@ -30,7 +30,7 @@ public interface GatewayResult { ...@@ -30,7 +30,7 @@ public interface GatewayResult {
String getId(); String getId();
void setId(String id); GatewayResult setId(String id);
String getWebURL(); String getWebURL();
......
...@@ -26,12 +26,14 @@ import java.util.List; ...@@ -26,12 +26,14 @@ import java.util.List;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
/** /**
* KubernetesResult * KubernetesResult
* *
* @since 2021/12/26 15:06 * @since 2021/12/26 15:06
*/ */
@Getter
@ApiModel(value = "KubernetesResult", description = "Result of Kubernetes operation") @ApiModel(value = "KubernetesResult", description = "Result of Kubernetes operation")
public class KubernetesResult extends AbstractGatewayResult { public class KubernetesResult extends AbstractGatewayResult {
...@@ -60,28 +62,20 @@ public class KubernetesResult extends AbstractGatewayResult { ...@@ -60,28 +62,20 @@ public class KubernetesResult extends AbstractGatewayResult {
super(type, startTime); super(type, startTime);
} }
public KubernetesResult(
String clusterId, LocalDateTime startTime, LocalDateTime endTime, boolean isSuccess, String exceptionMsg) {
super(startTime, endTime, isSuccess, exceptionMsg);
this.clusterId = clusterId;
}
@Override @Override
public String getId() { public String getId() {
return clusterId; return clusterId;
} }
@Override @Override
public void setId(String id) { public KubernetesResult setId(String id) {
this.clusterId = id; this.clusterId = id;
return this;
} }
public void setWebURL(String webURL) { public KubernetesResult setWebURL(String webURL) {
this.webURL = webURL; this.webURL = webURL;
} return this;
public String getWebURL() {
return webURL;
} }
@Override @Override
...@@ -89,8 +83,9 @@ public class KubernetesResult extends AbstractGatewayResult { ...@@ -89,8 +83,9 @@ public class KubernetesResult extends AbstractGatewayResult {
return jids; return jids;
} }
public void setJids(List<String> jids) { public KubernetesResult setJids(List<String> jids) {
this.jids = jids; this.jids = jids;
return this;
} }
public static KubernetesResult build(GatewayType type) { public static KubernetesResult build(GatewayType type) {
......
...@@ -69,8 +69,9 @@ public class SavePointResult extends AbstractGatewayResult { ...@@ -69,8 +69,9 @@ public class SavePointResult extends AbstractGatewayResult {
} }
@Override @Override
public void setId(String id) { public GatewayResult setId(String id) {
this.appId = id; this.appId = id;
return this;
} }
@Override @Override
......
...@@ -71,8 +71,9 @@ public class YarnResult extends AbstractGatewayResult { ...@@ -71,8 +71,9 @@ public class YarnResult extends AbstractGatewayResult {
} }
@Override @Override
public void setId(String id) { public GatewayResult setId(String id) {
this.appId = id; this.appId = id;
return this;
} }
public String getWebURL() { public String getWebURL() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment