Search in sources :

Example 6 with TaskModel

use of com.bonree.brfs.schedulers.task.model.TaskModel in project BRFS by zhangnianli.

the class OperationTaskJob method operation.

@Override
public void operation(JobExecutionContext context) {
    JobDataMap data = context.getJobDetail().getJobDataMap();
    String dataPath = data.getString(JobDataMapConstract.DATA_PATH);
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    MetaTaskManagerInterface release = mcf.getTm();
    if (release == null) {
        throw new NullPointerException("MetaTaskManager is empty !!!");
    }
    List<TaskType> switchList = mcf.getTaskOn();
    if (switchList == null || switchList.isEmpty()) {
        LOG.warn("switch task is empty !!!");
        return;
    }
    SchedulerManagerInterface schd = mcf.getStm();
    if (schd == null) {
        throw new NullPointerException("SchedulerManagerInterface is empty !!!");
    }
    RunnableTaskInterface runTask = mcf.getRt();
    if (runTask == null) {
        throw new NullPointerException("RunnableTaskInterface is empty !!!");
    }
    String typeName;
    String currentTaskName;
    TaskModel task;
    TaskRunPattern runPattern;
    int poolSize;
    int sumbitSize;
    String serverId = mcf.getServerId();
    SumbitTaskInterface sumbitTask;
    // 判断是否有恢复任务,有恢复任务则不进行创建
    boolean rebalanceFlag = WatchSomeThingJob.getState(WatchSomeThingJob.RECOVERY_STATUSE);
    for (TaskType taskType : switchList) {
        sumbitTask = null;
        try {
            if (TaskType.SYSTEM_COPY_CHECK.equals(taskType)) {
                continue;
            }
            typeName = taskType.name();
            poolSize = schd.getTaskPoolSize(typeName);
            sumbitSize = schd.getSumbitedTaskCount(typeName);
            // 判断任务是否可以执行
            boolean isRun = runTask.taskRunnable(taskType.code(), poolSize, sumbitSize);
            if (!isRun) {
                LOG.warn("resource is limit !!! skip {} !!!", typeName);
                continue;
            }
            int retryCount = 3;
            if (TaskType.SYSTEM_CHECK.equals(taskType) || TaskType.SYSTEM_MERGER.equals(taskType) || TaskType.SYSTEM_DELETE.equals(taskType)) {
                retryCount = 0;
            }
            Pair<String, TaskModel> taskPair = TaskStateLifeContral.getCurrentOperationTask(release, typeName, serverId, retryCount);
            if (taskPair == null) {
                LOG.warn("taskType :{} taskName: null is vaild ,skiping !!!", typeName);
                continue;
            }
            currentTaskName = taskPair.getFirst();
            task = TaskStateLifeContral.changeRunTaskModel(taskPair.getSecond(), dataPath);
            // 获取执行策略
            runPattern = runTask.taskRunnPattern(task);
            if (runPattern == null) {
                LOG.warn("TaskRunPattern is null will do it once");
                runPattern = new TaskRunPattern();
                runPattern.setRepeateCount(1);
                runPattern.setSleepTime(1000);
            }
            // 创建任务提交信息
            if (TaskType.SYSTEM_DELETE.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), SystemDeleteJob.class.getCanonicalName(), dataPath);
            }
            if (TaskType.SYSTEM_CHECK.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), SystemCheckJob.class.getCanonicalName(), dataPath);
            }
            if (TaskType.USER_DELETE.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), UserDeleteJob.class.getCanonicalName(), dataPath);
            }
            if (rebalanceFlag && TaskType.SYSTEM_CHECK.equals(taskType)) {
                LOG.warn("rebalance task running !! Skip {} sumbit", taskType.name());
                continue;
            }
            if (sumbitTask == null) {
                LOG.warn("sumbit type:{}, taskName :{}, taskcontent is null", typeName, currentTaskName);
                continue;
            }
            boolean isSumbit = schd.addTask(typeName, sumbitTask);
            LOG.info("sumbit type:{}, taskName :{}, state:{}", typeName, currentTaskName, isSumbit);
            if (!isSumbit) {
                LOG.warn("next cycle will sumbit against type : {}, taskName : {}", typeName, currentTaskName);
                continue;
            }
            // 更新任务状态
            // 更新任务执行的位置
            data.put(typeName, currentTaskName);
        } catch (Exception e) {
            LOG.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) SumbitTaskInterface(com.bonree.brfs.schedulers.task.meta.SumbitTaskInterface) TaskRunPattern(com.bonree.brfs.schedulers.task.model.TaskRunPattern) EmailPool(com.bonree.brfs.email.EmailPool) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) JsonException(com.bonree.brfs.common.utils.JsonUtils.JsonException) RunnableTaskInterface(com.bonree.brfs.schedulers.task.manager.RunnableTaskInterface) TaskType(com.bonree.brfs.common.task.TaskType) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) SchedulerManagerInterface(com.bonree.brfs.schedulers.task.manager.SchedulerManagerInterface) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Example 7 with TaskModel

use of com.bonree.brfs.schedulers.task.model.TaskModel in project BRFS by zhangnianli.

the class DefaultReleaseTask method changeTaskContentNodeStateByLock.

@Override
public boolean changeTaskContentNodeStateByLock(String serverId, String taskName, String taskType, int taskState) {
    try {
        if (BrStringUtils.isEmpty(serverId)) {
            return false;
        }
        if (BrStringUtils.isEmpty(taskName)) {
            return false;
        }
        if (BrStringUtils.isEmpty(taskType)) {
            return false;
        }
        String locks = this.taskLockPath + "/" + taskType + "/" + taskName;
        byte[] data = serverId.getBytes(StandardCharsets.UTF_8);
        if (!client.checkExists(locks)) {
            client.createEphemeral(locks, true, data);
        }
        byte[] tData = client.getData(locks);
        String zkStr = new String(tData, StandardCharsets.UTF_8);
        if (!serverId.equals(zkStr)) {
            return false;
        }
        TaskModel tmp = getTaskContentNodeInfo(taskType, taskName);
        if (tmp == null) {
            return false;
        }
        tmp.setTaskState(taskState);
        updateTaskContentNode(tmp, taskType, taskName);
        client.delete(locks, false);
        return true;
    } catch (Exception e) {
        LOG.error("change task state by lock error {}", e);
    }
    return false;
}
Also used : TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Example 8 with TaskModel

use of com.bonree.brfs.schedulers.task.model.TaskModel in project BRFS by zhangnianli.

the class DefaultReleaseTask method changeTaskContentNodeState.

@Override
public boolean changeTaskContentNodeState(String taskName, String taskType, int taskState) {
    try {
        if (BrStringUtils.isEmpty(taskName)) {
            return false;
        }
        if (BrStringUtils.isEmpty(taskType)) {
            return false;
        }
        TaskModel tmp = getTaskContentNodeInfo(taskType, taskName);
        if (tmp == null) {
            return false;
        }
        tmp.setTaskState(taskState);
        updateTaskContentNode(tmp, taskType, taskName);
        return true;
    } catch (Exception e) {
        LOG.error("update task content node error {}", e);
    }
    return false;
}
Also used : TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Example 9 with TaskModel

use of com.bonree.brfs.schedulers.task.model.TaskModel in project BRFS by zhangnianli.

the class DefaultReleaseTask method reviseTaskState.

/**
 * 概述:维护任务的状态
 * @param taskQueue
 * @param aliveServers
 * @param taskType
 * @param deleteIndex
 * @return
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
private int reviseTaskState(List<String> taskQueue, Collection<String> aliveServers, String taskType, int deleteIndex) {
    int count = 0;
    try {
        int size = taskQueue.size();
        StringBuilder taskPath;
        String taskName;
        String tmpPath;
        TaskModel taskContent;
        TaskServerNodeModel taskServer;
        List<String> cServers;
        for (int i = (size - 1); i >= deleteIndex; i--) {
            taskName = taskQueue.get(i);
            taskContent = getTaskContentNodeInfo(taskType, taskName);
            if (taskContent == null) {
                LOG.warn("{} {} is null", taskType, taskName);
                continue;
            }
            // 不为RUN与Exception的任务不进行检查
            int stat = taskContent.getTaskState();
            boolean exceptionFlag = TaskState.EXCEPTION.code() == stat;
            if (!(TaskState.RUN.code() == stat || exceptionFlag)) {
                continue;
            }
            // 获取任务下的子节点
            taskPath = new StringBuilder();
            taskPath.append(this.taskQueue).append("/").append(taskType).append("/").append(taskName);
            tmpPath = taskPath.toString();
            cServers = client.getChildren(tmpPath);
            // 服务为空,任务标记为异常
            if (cServers == null || cServers.isEmpty()) {
                count++;
                taskContent.setTaskState(TaskState.EXCEPTION.code());
                updateTaskContentNode(taskContent, taskType, taskName);
                continue;
            }
            boolean isException = false;
            for (String cServer : cServers) {
                // 存活的server不进行操作
                if (aliveServers.contains(cServer)) {
                    continue;
                }
                // 不存活的server,节点标记为Exception
                taskServer = getTaskServerContentNodeInfo(taskType, taskName, cServer);
                if (taskServer == null) {
                    LOG.warn("taskType :{}, taskName :{}, serverId :{} is not exists", taskType, taskName, cServer);
                    taskServer = new TaskServerNodeModel();
                    taskServer.setTaskState(TaskState.UNKNOW.code());
                }
                if (TaskState.FINISH.code() == taskServer.getTaskState()) {
                    continue;
                }
                isException = true;
                taskServer.setTaskState(TaskState.EXCEPTION.code());
                updateServerTaskContentNode(cServer, taskName, taskType, taskServer);
            }
            if (isException && !exceptionFlag) {
                count++;
                taskContent.setTaskState(TaskState.EXCEPTION.code());
                updateTaskContentNode(taskContent, taskType, taskName);
            }
        }
    } catch (Exception e) {
        LOG.error("revise task error {}", e);
    }
    return count;
}
Also used : TaskServerNodeModel(com.bonree.brfs.schedulers.task.model.TaskServerNodeModel) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Example 10 with TaskModel

use of com.bonree.brfs.schedulers.task.model.TaskModel in project BRFS by zhangnianli.

the class CreateSystemTaskJob method operation.

@Override
public void operation(JobExecutionContext context) {
    LOG.info("create system task working");
    // 判断是否有恢复任务,有恢复任务则不进行创建
    JobDataMap data = context.getJobDetail().getJobDataMap();
    long checkTtl = data.getLong(JobDataMapConstract.CHECK_TTL);
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    MetaTaskManagerInterface release = mcf.getTm();
    // 获取开启的任务名称
    List<TaskType> switchList = mcf.getTaskOn();
    if (switchList == null || switchList.isEmpty()) {
        LOG.warn("switch on task is empty !!!");
        return;
    }
    // 获取可用服务
    String groupName = mcf.getGroupName();
    ServiceManager sm = mcf.getSm();
    // 2.设置可用服务
    List<String> serverIds = CreateSystemTask.getServerIds(sm, groupName);
    if (serverIds == null || serverIds.isEmpty()) {
        LOG.warn("{} available server list is null", groupName);
        return;
    }
    // 3.获取storageName
    StorageRegionManager snm = mcf.getSnm();
    List<StorageRegion> snList = snm.getStorageRegionList();
    if (snList == null || snList.isEmpty()) {
        LOG.info("skip create system task !!! because storageName is null !!!");
        return;
    }
    TaskModel task;
    String taskName;
    TaskTypeModel tmodel;
    long ttl = 0;
    Pair<TaskModel, TaskTypeModel> result;
    List<String> srs = TaskStateLifeContral.getSRs(snm);
    for (TaskType taskType : switchList) {
        if (TaskType.SYSTEM_COPY_CHECK.equals(taskType) || TaskType.USER_DELETE.equals(taskType)) {
            continue;
        }
        TaskStateLifeContral.watchSR(release, srs, taskType.name());
        if (TaskType.SYSTEM_DELETE.equals(taskType)) {
            ttl = 0;
        } else if (TaskType.SYSTEM_CHECK.equals(taskType)) {
            ttl = checkTtl;
        }
        tmodel = release.getTaskTypeInfo(taskType.name());
        if (tmodel == null) {
            tmodel = new TaskTypeModel();
            tmodel.setSwitchFlag(true);
            LOG.warn("taskType{} is switch but metadata is null");
        }
        result = CreateSystemTask.createSystemTask(tmodel, taskType, snList, ttl);
        if (result == null) {
            LOG.warn("create sys task is empty {}", taskType.name());
            continue;
        }
        task = result.getFirst();
        taskName = CreateSystemTask.updateTask(release, task, serverIds, taskType);
        if (!BrStringUtils.isEmpty(taskName)) {
            LOG.info("create {} {} task successfull !!!", taskType.name(), taskName);
            release.setTaskTypeModel(taskType.name(), tmodel);
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) TaskTypeModel(com.bonree.brfs.schedulers.task.model.TaskTypeModel) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion) ServiceManager(com.bonree.brfs.common.service.ServiceManager) TaskType(com.bonree.brfs.common.task.TaskType) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) StorageRegionManager(com.bonree.brfs.duplication.storageregion.StorageRegionManager) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Aggregations

TaskModel (com.bonree.brfs.schedulers.task.model.TaskModel)21 AtomTaskModel (com.bonree.brfs.schedulers.task.model.AtomTaskModel)11 MetaTaskManagerInterface (com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface)7 ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)6 TaskServerNodeModel (com.bonree.brfs.schedulers.task.model.TaskServerNodeModel)5 StorageRegion (com.bonree.brfs.duplication.storageregion.StorageRegion)4 HashMap (java.util.HashMap)4 Pair (com.bonree.brfs.common.utils.Pair)3 TaskTypeModel (com.bonree.brfs.schedulers.task.model.TaskTypeModel)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 JobDataMap (org.quartz.JobDataMap)3 ServiceManager (com.bonree.brfs.common.service.ServiceManager)2 TaskType (com.bonree.brfs.common.task.TaskType)2 StorageRegionManager (com.bonree.brfs.duplication.storageregion.StorageRegionManager)2 TaskResultModel (com.bonree.brfs.schedulers.task.model.TaskResultModel)2 BRFSTimeFilter (com.bonree.brfs.common.files.impl.BRFSTimeFilter)1 Service (com.bonree.brfs.common.service.Service)1 TaskState (com.bonree.brfs.common.task.TaskState)1 JsonException (com.bonree.brfs.common.utils.JsonUtils.JsonException)1