Search in sources :

Example 16 with ManagerContralFactory

use of com.bonree.brfs.schedulers.ManagerContralFactory in project BRFS by zhangnianli.

the class QuartzOperationStateWithZKTask method execute.

@Override
public void execute(JobExecutionContext context) {
    int currentIndex = -1;
    JobDataMap data = null;
    String serverId = null;
    String taskTypeName = null;
    String taskName = null;
    boolean isSuccess = true;
    try {
        data = context.getJobDetail().getJobDataMap();
        int repeatCount = data.getInt(JobDataMapConstract.TASK_REPEAT_RUN_COUNT);
        taskName = data.getString(JobDataMapConstract.TASK_NAME);
        serverId = data.getString(JobDataMapConstract.SERVER_ID);
        int taskType = data.getInt(JobDataMapConstract.TASK_TYPE);
        taskTypeName = TaskType.valueOf(taskType).name();
        // 设置当前任务执行
        if (!data.containsKey(JobDataMapConstract.CURRENT_INDEX)) {
            data.put(JobDataMapConstract.CURRENT_INDEX, repeatCount + "");
            TaskStateLifeContral.updateTaskRunState(serverId, taskName, taskTypeName);
            LOG.info("task {}-{} run", taskTypeName, taskName);
        }
        currentIndex = data.getInt(JobDataMapConstract.CURRENT_INDEX);
        LOG.debug("taskType [{}],taskname [{}],batch id[{}], data :[{}]", taskTypeName, taskName, currentIndex, data.getString(currentIndex + ""));
        operation(context);
    } catch (Exception e) {
        context.put("ExceptionMessage", e.getMessage());
        caughtException(context);
        isSuccess = false;
        LOG.error("task {}-{} happen exception:{}", taskTypeName, taskName, e);
        EmailPool emailPool = EmailPool.getInstance();
        MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
        builder.setModel(this.getClass().getSimpleName() + " execute 模块服务发生问题");
        builder.setException(e);
        ManagerContralFactory mcf = ManagerContralFactory.getInstance();
        builder.setMessage(mcf.getGroupName() + "(" + mcf.getServerId() + ")服务 执行任务时发生问题");
        builder.setVariable(data.getWrappedMap());
        emailPool.sendEmail(builder);
    } finally {
        if (data == null) {
            return;
        }
        LOG.debug("operation batch id {}", currentIndex);
        try {
            // 更新任务状态
            TaskResultModel resultTask = new TaskResultModel();
            resultTask.setSuccess(isSuccess);
            TaskStateLifeContral.updateMapTaskMessage(context, resultTask);
            // 更新要操作的批次
            if (currentIndex > 1) {
                data.put(JobDataMapConstract.CURRENT_INDEX, (currentIndex - 1) + "");
            // 最后一次更新任务信息
            } else if (currentIndex == 1) {
                String result = data.getString(JobDataMapConstract.TASK_RESULT);
                TaskResultModel tResult = new TaskResultModel();
                if (!BrStringUtils.isEmpty(result)) {
                    tResult = JsonUtils.toObjectQuietly(result, TaskResultModel.class);
                }
                TaskStateLifeContral.updateTaskStatusByCompelete(serverId, taskName, taskTypeName, tResult);
                data.put(JobDataMapConstract.CURRENT_INDEX, (currentIndex - 1) + "");
                LOG.info("task {}-{}:{} end!!", taskTypeName, taskName, tResult.isSuccess());
            }
        } catch (Exception e) {
            LOG.error("execute error", e);
            EmailPool emailPool = EmailPool.getInstance();
            MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
            builder.setModel(this.getClass().getSimpleName() + "模块服务发生问题");
            builder.setException(e);
            builder.setMessage("更新任务发生错误");
            builder.setVariable(data.getWrappedMap());
            emailPool.sendEmail(builder);
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) EmailPool(com.bonree.brfs.email.EmailPool) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) TaskResultModel(com.bonree.brfs.schedulers.task.model.TaskResultModel) UnableToInterruptJobException(org.quartz.UnableToInterruptJobException)

Example 17 with ManagerContralFactory

use of com.bonree.brfs.schedulers.ManagerContralFactory in project BRFS by zhangnianli.

the class CopyRecovery method recoveryDirs.

/**
 * 概述:修复目录
 * @param content
 * @param zkHosts
 * @param baseRoutesPath
 * @return
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static TaskResultModel recoveryDirs(String content, String zkHosts, String baseRoutesPath, String dataPath) {
    TaskResultModel result = new TaskResultModel();
    BatchAtomModel batch = converStringToBatch(content);
    if (batch == null) {
        result.setSuccess(false);
        LOG.debug("batch is empty");
        return result;
    }
    List<AtomTaskModel> atoms = batch.getAtoms();
    if (atoms == null || atoms.isEmpty()) {
        result.setSuccess(true);
        LOG.debug(" files is empty");
        return result;
    }
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    ServerIDManager sim = mcf.getSim();
    ServiceManager sm = mcf.getSm();
    StorageRegionManager snm = mcf.getSnm();
    CuratorClient curatorClient = mcf.getClient();
    StorageRegion sn;
    SecondIDParser parser;
    String snName;
    int snId;
    AtomTaskResultModel atomR;
    List<String> errors;
    for (AtomTaskModel atom : atoms) {
        atomR = new AtomTaskResultModel();
        atomR.setFiles(atom.getFiles());
        atomR.setSn(atom.getStorageName());
        snName = atom.getStorageName();
        sn = snm.findStorageRegionByName(snName);
        if (sn == null) {
            atomR.setSuccess(false);
            result.setSuccess(false);
            result.add(atomR);
            LOG.debug("sn == null snName :{}", snName);
            continue;
        }
        snId = sn.getId();
        parser = new SecondIDParser(curatorClient, snId, baseRoutesPath);
        parser.updateRoute();
        errors = recoveryFiles(sm, sim, parser, sn, atom, dataPath);
        if (errors == null || errors.isEmpty()) {
            result.add(atomR);
            LOG.debug("result is empty snName:{}", snName);
            continue;
        }
        atomR.addAll(errors);
        atomR.setSuccess(false);
        result.setSuccess(false);
    }
    return result;
}
Also used : SecondIDParser(com.bonree.brfs.rebalance.route.SecondIDParser) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) ServerIDManager(com.bonree.brfs.server.identification.ServerIDManager) TaskResultModel(com.bonree.brfs.schedulers.task.model.TaskResultModel) AtomTaskResultModel(com.bonree.brfs.schedulers.task.model.AtomTaskResultModel) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion) ServiceManager(com.bonree.brfs.common.service.ServiceManager) BatchAtomModel(com.bonree.brfs.schedulers.task.model.BatchAtomModel) AtomTaskResultModel(com.bonree.brfs.schedulers.task.model.AtomTaskResultModel) StorageRegionManager(com.bonree.brfs.duplication.storageregion.StorageRegionManager) AtomTaskModel(com.bonree.brfs.schedulers.task.model.AtomTaskModel)

Example 18 with ManagerContralFactory

use of com.bonree.brfs.schedulers.ManagerContralFactory in project BRFS by zhangnianli.

the class TaskStateLifeContral method updateTaskRunState.

/**
 * 概述:将服务状态修改为RUN
 * @param serverId
 * @param taskname
 * @param taskType
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static void updateTaskRunState(String serverId, String taskname, String taskType) {
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    MetaTaskManagerInterface release = mcf.getTm();
    int taskStat = release.queryTaskState(taskname, taskType);
    // 修改服务几点状态,若不为RUN则修改为RUN
    TaskServerNodeModel serverNode = release.getTaskServerContentNodeInfo(taskType, taskname, serverId);
    if (serverNode == null) {
        serverNode = new TaskServerNodeModel();
    }
    LOG.debug("TaskMessage Run  sTask :{}", JsonUtils.toJsonStringQuietly(serverNode));
    serverNode.setTaskStartTime(TimeUtils.formatTimeStamp(System.currentTimeMillis(), TimeUtils.TIME_MILES_FORMATE));
    serverNode.setTaskState(TaskState.RUN.code());
    release.updateServerTaskContentNode(serverId, taskname, taskType, serverNode);
    LOG.debug("> run server task :{} - {} - {} - {}", taskType, taskname, serverId, TaskState.valueOf(serverNode.getTaskState()).name());
    // 查询任务节点状态,若不为RUN则获取分布式锁,修改为RUN
    if (taskStat != TaskState.RUN.code()) {
        TaskModel task = release.getTaskContentNodeInfo(taskType, taskname);
        if (task == null) {
            task = new TaskModel();
        }
        task.setTaskState(TaskState.RUN.code());
        release.updateTaskContentNode(task, taskType, taskname);
        LOG.debug("run task :{} - {} - {}", taskType, taskname, TaskState.valueOf(task.getTaskState()).name());
    }
}
Also used : TaskServerNodeModel(com.bonree.brfs.schedulers.task.model.TaskServerNodeModel) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel) AtomTaskModel(com.bonree.brfs.schedulers.task.model.AtomTaskModel)

Aggregations

ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)18 MetaTaskManagerInterface (com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface)9 JobDataMap (org.quartz.JobDataMap)9 StorageRegion (com.bonree.brfs.duplication.storageregion.StorageRegion)8 StorageRegionManager (com.bonree.brfs.duplication.storageregion.StorageRegionManager)7 EmailPool (com.bonree.brfs.email.EmailPool)7 TaskModel (com.bonree.brfs.schedulers.task.model.TaskModel)6 ServiceManager (com.bonree.brfs.common.service.ServiceManager)5 TaskType (com.bonree.brfs.common.task.TaskType)4 CuratorClient (com.bonree.brfs.common.zookeeper.curator.CuratorClient)4 AtomTaskModel (com.bonree.brfs.schedulers.task.model.AtomTaskModel)4 UnableToInterruptJobException (org.quartz.UnableToInterruptJobException)4 TaskResultModel (com.bonree.brfs.schedulers.task.model.TaskResultModel)3 ServerIDManager (com.bonree.brfs.server.identification.ServerIDManager)3 Map (java.util.Map)3 Service (com.bonree.brfs.common.service.Service)2 TaskState (com.bonree.brfs.common.task.TaskState)2 SecondIDParser (com.bonree.brfs.rebalance.route.SecondIDParser)2 RunnableTaskInterface (com.bonree.brfs.schedulers.task.manager.RunnableTaskInterface)2 TaskServerNodeModel (com.bonree.brfs.schedulers.task.model.TaskServerNodeModel)2