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

com.alibaba.dts.client.executor.longtime.LongTimePool Maven / Gradle / Ivy

package com.alibaba.dts.client.executor.longtime;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.dts.client.executor.job.context.ClientContextImpl;
import com.alibaba.dts.client.executor.job.processor.LongTimeJobProcessor;
import com.alibaba.dts.client.executor.longtime.unit.ExecutorUnit;
import com.alibaba.dts.common.constants.Constants;
import com.alibaba.dts.common.domain.ExecutableTask;
import com.alibaba.dts.common.domain.result.Result;
import com.alibaba.dts.common.domain.result.ResultCode;
import com.alibaba.dts.common.domain.store.TaskSnapshot;
import com.alibaba.dts.common.logger.SchedulerXLoggerFactory;
import com.alibaba.dts.common.logger.innerlog.Logger;

import org.springframework.util.CollectionUtils;

/**
 * LongTime job执行池
 *
 */
public class LongTimePool implements Constants {

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

	/** 任务执行单元映射表 */
	private ConcurrentHashMap> executorUnitTable =
			new ConcurrentHashMap>();

	private final ClientContextImpl clientContext;

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

	public boolean stopService(){
		boolean result = true;
		Iterator iterator = executorUnitTable.entrySet().iterator();

		long jobCount = 0;
		long processorCount = 0;

		while(iterator.hasNext()) {

			long jobid=0;
			jobCount++;
			try {

				Map.Entry entry = (Map.Entry)iterator.next();

				jobid = (Long)entry.getKey();

				ConcurrentHashMap executorUnitMap = (ConcurrentHashMap)entry.getValue();

				if(CollectionUtils.isEmpty(executorUnitMap)) {
					continue;
				}

				Iterator executorUnitIterator = executorUnitMap.entrySet().iterator();

				while(executorUnitIterator.hasNext()) {
					processorCount++;
					Map.Entry processorEntity = (Map.Entry)executorUnitIterator.next();
					ExecutorUnit processor = (ExecutorUnit)processorEntity.getValue();
					processor.stopTask();
				}

			} catch (Throwable e) {
				result = false;
				logger.error("[SimplePool]: stopService error, jobid:" + jobid, e);
			}

			logger.info("[SimplePool]: stopService, jobCount:"+jobCount +
					", processorCount:" + processorCount);
		}

		executorUnitTable.clear();
		return result;
	}

	@SuppressWarnings("unchecked")
	public synchronized boolean executeTask(ExecutableTask executableTask) {
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(executableTask.getJob().getId());
		if(null == executorUnitMap) {
			if (!isImplProcessor(executableTask)) return false;
			executorUnitMap = new ConcurrentHashMap();
			ConcurrentHashMap existExecutorUnitMap =
			this.executorUnitTable.putIfAbsent(executableTask.getJob().getId(), executorUnitMap);
			if (existExecutorUnitMap!=null){
				executorUnitMap = existExecutorUnitMap;
			}
		}

		//控制job单实例运行
		if (executorUnitMap.size()>1){
           for (ExecutorUnit executorUnit:executorUnitMap.values()){
			   if (executorUnit.getExecutableTask().getJobInstanceSnapshot().getId()!= executableTask.getJobInstanceSnapshot().getId()){
				   stopTask(executorUnit.getExecutableTask().getJob().getId(),executorUnit.getExecutableTask().getJobInstanceSnapshot().getId());
				   logger.warn("[LongTimePool]: executeTask init stop previous instance:"
						   + ", jobId:" + executableTask.getJob().getId()
						   + ", instanceId:" + executableTask.getJobInstanceSnapshot().getId());
			   }
		   }
		}

		ExecutorUnit executorUnit = executorUnitMap.get(executableTask.getJobInstanceSnapshot().getId());
		if(null == executorUnit) {
			if (!isImplProcessor(executableTask)) return false;
			executorUnit = new ExecutorUnit(clientContext,this, executableTask);
			ExecutorUnit existExecutorUnit =
			executorUnitMap.putIfAbsent(executableTask.getJobInstanceSnapshot().getId(), executorUnit);
			if (existExecutorUnit!=null){
				executorUnit = null;
			}else{
				try {
					executorUnit.init();
					logger.info("[LongTimePool]: executeTask init success"
							+ ", instanceId:" + executableTask.getJobInstanceSnapshot().getId());
				} catch (Throwable e) {
					logger.error("[LongTimePool]: executeTask init error"
							+ ", instanceId:" + executableTask.getJobInstanceSnapshot().getId(), e);
					return false;
				}
			}
		}else{
			logger.info("[LongTimePool]: instanceId exists,restartPull"
					+ ", instanceId:" + executableTask.getJobInstanceSnapshot().getId());
			executorUnit.restartPull();
		}

		return true;
	}

	public ExecutorUnit getExecutorUnit(long jobid, long instanceid){
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobid);
		if(null == executorUnitMap) {
			return null;
		}
		ExecutorUnit executorUnit = executorUnitMap.get(instanceid);
		return executorUnit;
	}

	public boolean isImplProcessor(ExecutableTask executableTask){

		LongTimeJobProcessor longTimeJobProcessor = null;
		try {
			longTimeJobProcessor = this.clientContext.getJobProcessorFactory().createAndGetLongTimeJobProcessor(executableTask.getJob(), false);
		} catch (Throwable e) {
			logger.error("[LongTimePool]: LongTimeJobProcessor is not implement."
					+ ", jobProcessor:" + executableTask.getJob().getJobProcessor(), e);
			return false;
		}
		if (longTimeJobProcessor==null) return false;

		return true;
	}

	@Deprecated
	public boolean activeTask(ExecutableTask executableTask) {
		return false;
	}


	public boolean releaseCompleteTask(ExecutableTask executableTask) {
		boolean result=true;
		try{

			ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(executableTask.getJob().getId());
			if(null == executorUnitMap) {
				return false;
			}
			ExecutorUnit executorUnit = executorUnitMap.get(executableTask.getJobInstanceSnapshot().getId());
			if(null == executorUnit) {
				return false;
			}else{
				executorUnit.releaseCompleteTask();
				logger.info("[LongTimePool]: activeTask releaseCompleteTask success"
						+ ", instanceId:" + executableTask.getJobInstanceSnapshot().getId());
			}

		}catch (Throwable e){
			result = false;
			logger.error("[LongTimePool]: releaseCompleteTask error"
					+ ", instanceId:" + executableTask.getJobInstanceSnapshot().getId(), e);
		}
		return result;
	}



	public boolean stopTask(long jobId, long jobInstanceId) {
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobId);
		if(null == executorUnitMap || executorUnitMap.isEmpty()) {
			return true;
		}
		ExecutorUnit executorUnit = executorUnitMap.get(jobInstanceId);
		if(null == executorUnit) {
			return true;
		}
		
		/** 停止任务执行单元 */
		executorUnit.stopTask();
		
		/** 删除任务执行单元 */
		try {
			executorUnitMap.remove(jobInstanceId);
			logger.info("[LongTimePool]: stopTask remove executorUnitMap success"
					+ ", instanceId:" + jobInstanceId);
		} catch (Throwable e) {
			logger.error("[LongTimePool]: stopTask remove executorUnitMap error"
					+ ", jobId:" + jobId 
					+ ", jobInstanceId:" + jobInstanceId, e);
			return false;
		}
		return true;
	}

	public boolean forceStopTask(long jobId, long jobInstanceId) {
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobId);
		if(null == executorUnitMap || executorUnitMap.isEmpty()) {
			return true;
		}
		ExecutorUnit executorUnit = executorUnitMap.get(jobInstanceId);
		if(null == executorUnit) {
			return true;
		}
		
		/** 强制停止任务执行单元 */
		executorUnit.forceStopTask();
		
		/** 删除任务执行单元 */
		try {
			executorUnitMap.remove(jobInstanceId);
			logger.info("[LongTimePool]: forceStopTask remove executorUnitMap success"
					+ ", instanceId:" + jobInstanceId);
		} catch (Throwable e) {
			logger.error("[LongTimePool]: forceStopTask remove executorUnitMap error"
					+ ", jobId:" + jobId 
					+ ", jobInstanceId:" + jobInstanceId, e);
			return false;
		}
		return true;
	}

	public Result heartBeatCheckJobInstance(long jobId, long jobInstanceId) {
		Result result = new Result();
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobId);
		if(null == executorUnitMap || executorUnitMap.isEmpty()) {
			result.setResultCode(ResultCode.HEART_BEAT_CHECK_EXIT);
			return result;
		}
		ExecutorUnit executorUnit = executorUnitMap.get(jobInstanceId);
		if(null == executorUnit) {
			result.setResultCode(ResultCode.HEART_BEAT_CHECK_EXIT);
			return result;
		}
		result.setResultCode(ResultCode.HEART_BEAT_CHECK_SUCCESS);
		logger.info("[LongTimePool]: heartBeatCheckJobInstance"
				+ ", jobId:" + jobId
				+ ", jobInstanceId:" + jobInstanceId
				+ ", result:" + result.getResultCode().getInformation());
		return result;
	}

	public int runInstanceCount(long jobId, long jobInstanceId){

		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobId);
		if(null == executorUnitMap || executorUnitMap.isEmpty()) {
			return 0;
		}
		ExecutorUnit executorUnit = executorUnitMap.get(jobInstanceId);
		if(null == executorUnit) {
			return 0;
		}

		return 1;
	}
	

	public Result push(long jobId, long jobInstanceId, TaskSnapshot taskSnapshot) {
		
		ConcurrentHashMap executorUnitMap = this.executorUnitTable.get(jobId);
		if(null == executorUnitMap || executorUnitMap.isEmpty()) {
			return new Result(false, ResultCode.PUSH_UNIT_MAP_IS_EMPTY_ERROR);
		}
		
		ExecutorUnit executorUnit = executorUnitMap.get(jobInstanceId);
		if(null == executorUnit) {
			return new Result(false, ResultCode.PUSH_UNIT_IS_NULL_ERROR);
		}
		boolean result = false;
		try{
			//将任务放入队列
			result = executorUnit.offer(taskSnapshot);
			logger.info("[LongTimePool]: push success"
					+ ", result:" + result
					+ ", jobId:" + jobId
					+ ", jobInstanceId:" + jobInstanceId
			);
		}catch (Throwable e){
			logger.error("[LongTimePool]: push error"
					+ ", jobId:" + jobId
					+ ", jobInstanceId:" + jobInstanceId, e);
		}

		
		return new Result(result, result ? ResultCode.SUCCESS : ResultCode.FAILURE);
	}

	// 判断是否有常驻任务运行
	public boolean hasLongTimeJob() {
		boolean result = false;
		try {
			Iterator iterator = executorUnitTable.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry entry = (Map.Entry)iterator.next();
				ConcurrentHashMap executorUnitMap = (ConcurrentHashMap)entry.getValue();
				// 有对应的实例信息
				if(!CollectionUtils.isEmpty(executorUnitMap)) {
					result = true;
					break;
				}
			}
		} catch (Exception e) {
			logger.error("[LongTimePool]: hasLongTimeJob error.", e);
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy