All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.schedulerx.worker.master.handler.CommonUpdateInstanceStatusHandler Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.alibaba.schedulerx.worker.master.handler;

import org.apache.commons.lang.StringUtils;

import com.alibaba.schedulerx.common.domain.InstanceStatus;
import com.alibaba.schedulerx.common.domain.JobInstanceInfo;
import com.alibaba.schedulerx.common.util.IdUtil;
import com.alibaba.schedulerx.protocol.Worker.WorkerReportJobInstanceStatusRequest;
import com.alibaba.schedulerx.worker.SchedulerxWorker;
import com.alibaba.schedulerx.worker.log.LogFactory;
import com.alibaba.schedulerx.worker.log.Logger;
import com.alibaba.schedulerx.worker.logcollector.ClientLoggerMessage;
import com.alibaba.schedulerx.worker.logcollector.LogCollector;
import com.alibaba.schedulerx.worker.logcollector.LogCollectorFactory;
import com.alibaba.schedulerx.worker.master.StandaloneTaskMaster;
import com.alibaba.schedulerx.worker.master.TaskMaster;
import com.alibaba.schedulerx.worker.processor.ProcessResult;

/**
 *
 * @author zhaibian
 * @version $Id: CommonUpdateInstanceStatusHandler.java, v 0.1 2019年02月28日 19:38 zhaibian Exp $
 */
public class CommonUpdateInstanceStatusHandler extends UpdateInstanceStatusHandler {

    private LogCollector logCollector = LogCollectorFactory.get();
    private static final Logger LOGGER = LogFactory.getLogger(CommonUpdateInstanceStatusHandler.class);

    CommonUpdateInstanceStatusHandler(TaskMaster taskMaster, JobInstanceInfo jobInstanceInfo) {
        super(taskMaster, jobInstanceInfo);
    }

    @Override
    public void handle(long serialNum, InstanceStatus instanceStatus, String result) throws Exception {
        String uniqueId = IdUtil.getUniqueIdWithoutTask(jobInstanceInfo.getJobId(), jobInstanceInfo.getJobInstanceId());
        if (!taskMaster.getInstanceStatus().equals(instanceStatus)) {
            taskMaster.setInstanceStatus(instanceStatus);
            if (instanceStatus.isFinish()) {
                 ProcessResult postResult = taskMaster.postFinish(jobInstanceInfo.getJobInstanceId());
                 if (postResult != null) {
                     if (instanceStatus.equals(InstanceStatus.SUCCESS) && postResult.getStatus().equals(InstanceStatus.FAILED)) {
                         instanceStatus = InstanceStatus.FAILED;
                     }
                     if (postResult.getResult() != null && !StringUtils.contains(result, "Worker master shutdown")) {
                         result = postResult.getResult();
                     }
                 }

                if (!"killed from server".equals(result)) {
                    // 对服务端强制停止操作不做状态反馈
                    //report job instance status with at-least-once-delivery
                    WorkerReportJobInstanceStatusRequest.Builder builder = WorkerReportJobInstanceStatusRequest
                            .newBuilder()
                            .setJobId(jobInstanceInfo.getJobId())
                            .setJobInstanceId(jobInstanceInfo.getJobInstanceId())
                            .setStatus(instanceStatus.getValue())
                            .setGroupId(jobInstanceInfo.getGroupId());

                    if (result != null) {
                        builder.setResult(result);
                    }

                    String progress = taskMaster.getJobInstanceProgress();
                    if (!StringUtils.isEmpty(progress)) {
                        builder.setProgress(progress);
                    }
                    String traceId = jobInstanceInfo.getTraceId();
                    if (!StringUtils.isEmpty(traceId)) {
                        builder.setTraceId(traceId);
                    }

                    SchedulerxWorker.AtLeastDeliveryRoutingActor.tell(builder.build(), null);
                    LOGGER.info("report jobInstance={}, status={} to AtLeastDeliveryRoutingActor", jobInstanceInfo.getJobInstanceId(),
                            instanceStatus);
                }

                //destory containers and taskMaster
                taskMaster.destroyContainerPool();
                masterPool.get(jobInstanceInfo.getJobInstanceId()).stop();
                masterPool.remove(jobInstanceInfo.getJobInstanceId());
                logCollector.collect(jobInstanceInfo.getAppGroupId(), uniqueId, ClientLoggerMessage.appendMessage(ClientLoggerMessage.INSTANCE_FINISH,
                        instanceStatus.getDescription()), jobInstanceInfo.getGroupId());
                LOGGER.info("uniqueId: {} is finished, remove from MasterPool.", uniqueId);
            }
        }

        String progress = taskMaster.getJobInstanceProgress();
        if(taskMaster instanceof StandaloneTaskMaster && !instanceStatus.isFinish()
                && (!StringUtils.isEmpty(progress) || !StringUtils.isEmpty(jobInstanceInfo.getTraceId()))) {
            //report job instance status with at-least-once-delivery
            WorkerReportJobInstanceStatusRequest.Builder builder = WorkerReportJobInstanceStatusRequest
                    .newBuilder()
                    .setJobId(jobInstanceInfo.getJobId())
                    .setJobInstanceId(jobInstanceInfo.getJobInstanceId())
                    .setStatus(instanceStatus.getValue())
                    .setGroupId(jobInstanceInfo.getGroupId());
            if (result != null) {
                builder.setResult(result);
            }
            String traceId = jobInstanceInfo.getTraceId();
            if (!StringUtils.isEmpty(traceId)) {
                builder.setTraceId(traceId);
            }
            if (!StringUtils.isEmpty(progress)) {
                builder.setProgress(progress);
            }
            SchedulerxWorker.AtLeastDeliveryRoutingActor.tell(builder.build(), null);
            LOGGER.info("report jobInstance={}, status={} to AtLeastDeliveryRoutingActor", jobInstanceInfo.getJobInstanceId(),
                    instanceStatus);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy