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

com.alibaba.dts.client.service.ClientServiceImpl Maven / Gradle / Ivy

package com.alibaba.dts.client.service;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.dts.client.executor.job.context.ClientContextImpl;
import com.alibaba.dts.client.store.access.TaskSnapshotAccess;
import com.alibaba.dts.common.constants.Constants;
import com.alibaba.dts.common.context.InvocationContext;
import com.alibaba.dts.common.domain.ExecutableTask;
import com.alibaba.dts.common.domain.ExecuteLogger;
import com.alibaba.dts.common.domain.ExecutionInfo;
import com.alibaba.dts.common.domain.JobInstanceInfo;
import com.alibaba.dts.common.domain.remoting.RemoteMachine;
import com.alibaba.dts.common.domain.result.Result;
import com.alibaba.dts.common.domain.result.ResultCode;
import com.alibaba.dts.common.domain.store.ExecutionCounter;
import com.alibaba.dts.common.domain.store.TaskSnapshot;
import com.alibaba.dts.common.domain.store.assemble.ExecuteLog;
import com.alibaba.dts.common.logger.SchedulerXLoggerFactory;
import com.alibaba.dts.common.logger.innerlog.Logger;
import com.alibaba.dts.common.service.ClientService;
import com.alibaba.dts.common.util.EagleEyeUtil;

/**
 * 客户端通用基础服务
 *
 * @author tianyao.myc
 */
public class ClientServiceImpl implements ClientService, Constants {

    private static final Logger logger = SchedulerXLoggerFactory.getLogger(ClientServiceImpl.class);

    private final ClientContextImpl clientContext;

    public ClientServiceImpl(final ClientContextImpl clientContext) {
        this.clientContext = clientContext;
    }

    /**
     * 心跳检查
     */
    @Override
    public Result heartBeatCheck() {
        //		RemoteMachine remoteMachine = InvocationContext.acquireRemoteMachine();
        //		AtomicLong counter = null;
        //		try {
        //			counter = this.clientContext.getClientRemoting().getHeartBeatCounter4Increase(remoteMachine
        // .getRemoteAddress());
        //			counter.incrementAndGet();
        //		} catch (Throwable e) {
        //			logger.error("[ClientServiceImpl]: heartBeatCheck error, server:" + remoteMachine.getRemoteAddress
        // (), e);
        //			return new Result("something wrong with " + clientConfig.getLocalAddress(), ResultCode
        // .FAILURE);
        //		}
        return new Result("I am alive !", ResultCode.SUCCESS);
    }

    /**
     * 心跳检查任务状态
     */
    @Override
    public Result heartBeatCheckJobInstance(int jobType, long jobId, long jobInstanceId) {
        return this.clientContext.getExecutor().heartBeatCheckJobInstance(jobType, jobId, jobInstanceId);
    }

    /**
     * 执行简单触发任务
     */
    @Override
    public Result executeTask(ExecutableTask executableTask) {
        RemoteMachine remoteMachine = InvocationContext.acquireRemoteMachine();
        if (remoteMachine.getRpcRequest() != null) {
            EagleEyeUtil.buildContextFromRpcRequest(remoteMachine, "ClientService", "executeTask");
        }
        Result result = this.clientContext.getExecutor().executeTask(executableTask);
        if (remoteMachine.getRpcRequest() != null) {
            EagleEyeUtil.endRpc();
        }
        return result;
    }

    @Override
    public Result activeTask(ExecutableTask executableTask) {
        return this.clientContext.getExecutor().activeTask(executableTask);
    }

    /**
     * 停止任务
     */
    @Override
    public Result stopTask(int jobType, long jobId, long jobInstanceId) {
        logger.info("stop job, jobId=" + jobId + ", jobInstanceId=" + jobInstanceId + ", jobType=" + jobType);
        return this.clientContext.getExecutor().stopTask(jobType, jobId, jobInstanceId);
    }

    /**
     * 强制停止任务
     */
    @Override
    public Result forceStopTask(ExecutableTask executableTask) {
        return this.clientContext.getExecutor().forceStopTask(executableTask);
    }

    /**
     * 推任务
     */
    @Override
    public Result push(int jobType, long jobId, long jobInstanceId, TaskSnapshot taskSnapshot) {
        return this.clientContext.getExecutor().push(jobType, jobId, jobInstanceId, taskSnapshot);
    }

    @Override
    public Result releaseCompleteTask(ExecutableTask executableTask) {
        return this.clientContext.getExecutor().releaseCompleteTask(executableTask);
    }

    @Override
    public Result getExceptionInfo(JobInstanceInfo jobInstance) {
        ExecuteLogger executeLogger = new ExecuteLogger();

        if (jobInstance == null) {
            return new Result(null, ResultCode.FAILURE);
        }

        JobRunningException jobRunningException = JobRunningStateManager.getManageHandler().getJobRunningException(
                jobInstance.getJobInstanceId());

        if (jobRunningException != null) {
            executeLogger.setContents(jobRunningException.getThrowableStrRep());
            executeLogger.setHappenTime(jobRunningException.getUpdateTime());
        } else {
            return new Result(null, ResultCode.FAILURE);
        }

        return new Result(executeLogger, ResultCode.SUCCESS);
    }

    @Override
    public Result queryGridTasksCountByJobInstanceId(long jobInstanceId, int status) {
        TaskSnapshotAccess taskSnapshotAccess = clientContext.getStore().getTaskSnapshotDao();
        Result result = null;
        try {
            int count = taskSnapshotAccess.queryTaskCountByJobInstanceId(jobInstanceId, status);
            result = new Result(count, ResultCode.SUCCESS);
            return result;
        } catch (Throwable e) {
            logger.error("queryGridTasksCountByJobInstanceId failed, jobInstanceId=" + jobInstanceId, e);
            result = new Result(ResultCode.FAILURE);
            return result;
        }
    }

    @Override
    public Result>> getExecutionCounters(
            long jobInstanceId) {
        return new Result>>(
                clientContext.getExecutor().getExecutionCounters(jobInstanceId));
    }

    @Override
    public Result isJobInstanceFinished(long jobInstanceId) {
        ExecutionInfo executionInfo = null;

        try {
            boolean canFinish = clientContext.getExecutor().isJobInstanceFinished(jobInstanceId);
            ConcurrentHashMap> executionCounterByReceiveNode = new ConcurrentHashMap>();
            boolean canDelete = canFinish;
            if (canFinish) {
                executionCounterByReceiveNode = clientContext.getExecutor().getExecutionCounters(jobInstanceId);
//                canDelete = clientContext.getExecutor().isJobInstanceCanBeDeleted(jobInstanceId);
            }
            executionInfo = new ExecutionInfo(canFinish, canDelete, executionCounterByReceiveNode);
        } catch (Throwable throwable) {
            logger.error("isTasksFinished error, jobInstanceId=" + jobInstanceId, throwable);
        }

        return new Result(executionInfo);
    }

    @Override
    public Result isJobInstanceCanBeDeleted(long jobInstanceId) {
        ExecutionInfo executionInfo = null;

        try {
            boolean canDelete = clientContext.getExecutor().isJobInstanceCanBeDeleted(jobInstanceId);
            ConcurrentHashMap> executionCounterByReceiveNode
                    = clientContext.getExecutor().getExecutionCounters(jobInstanceId);
            executionInfo = new ExecutionInfo(true, canDelete, executionCounterByReceiveNode);
        } catch (Throwable throwable) {
            logger.error("isTasksFinished error, jobInstanceId=" + jobInstanceId, throwable);
        }

        return new Result(executionInfo);
    }

    @Override
    public Result doGridJobCleanTask(long jobInstanceId) {
        return clientContext.getExecutor().doGridJobCleanTask(jobInstanceId);
    }

    @Override
    public Result getLog(long jobId, long jobInstanceId, String fireTime, int streamType) {
        Result result = new Result();
        ExecuteLog executeLog = new ExecuteLog();
        try {
            List lines = clientContext.getExecutor().getLog(jobId, jobInstanceId, fireTime, streamType);
            executeLog.setLines(lines);
            result.setResultCode(ResultCode.SUCCESS);
            result.setData(executeLog);
        } catch (IOException e) {
            result.setResultCode(ResultCode.FAILURE);
            executeLog.setErrMsg(e.getMessage());
            result.setData(executeLog);
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy