com.dangdang.ddframe.job.cloud.state.failover.FailoverService.java Source code

Java tutorial

Introduction

Here is the source code for com.dangdang.ddframe.job.cloud.state.failover.FailoverService.java

Source

/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.dangdang.ddframe.job.cloud.state.failover;

import com.dangdang.ddframe.job.cloud.config.CloudJobConfiguration;
import com.dangdang.ddframe.job.cloud.config.ConfigurationService;
import com.dangdang.ddframe.job.cloud.context.ExecutionType;
import com.dangdang.ddframe.job.cloud.context.JobContext;
import com.dangdang.ddframe.job.cloud.context.TaskContext;
import com.dangdang.ddframe.job.cloud.state.running.RunningService;
import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * ?.
 *
 * @author zhangliang
 */
public class FailoverService {

    private final CoordinatorRegistryCenter regCenter;

    private final ConfigurationService configService;

    private final RunningService runningService;

    public FailoverService(final CoordinatorRegistryCenter regCenter) {
        this.regCenter = regCenter;
        configService = new ConfigurationService(regCenter);
        runningService = new RunningService(regCenter);
    }

    /**
     * .
     *
     * @param taskContext ?
     */
    public void add(final TaskContext taskContext) {
        String failoverTaskNodePath = FailoverNode.getFailoverTaskNodePath(taskContext.getMetaInfo().toString());
        if (!regCenter.isExisted(failoverTaskNodePath)
                && !runningService.isTaskRunning(taskContext.getMetaInfo())) {
            regCenter.persist(failoverTaskNodePath, taskContext.getId());
        }
    }

    /**
     * ?.
     *
     * @return ?
     */
    public Collection<JobContext> getAllEligibleJobContexts() {
        if (!regCenter.isExisted(FailoverNode.ROOT)) {
            return Collections.emptyList();
        }
        List<String> jobNames = regCenter.getChildrenKeys(FailoverNode.ROOT);
        Collection<JobContext> result = new ArrayList<>(jobNames.size());
        Set<HashCode> assignedTasks = new HashSet<>(jobNames.size() * 10, 1);
        for (String each : jobNames) {
            List<String> taskMetaInfoList = regCenter.getChildrenKeys(FailoverNode.getFailoverJobNodePath(each));
            if (taskMetaInfoList.isEmpty()) {
                regCenter.remove(FailoverNode.getFailoverJobNodePath(each));
                continue;
            }
            Optional<CloudJobConfiguration> jobConfig = configService.load(each);
            if (!jobConfig.isPresent()) {
                regCenter.remove(FailoverNode.getFailoverJobNodePath(each));
                continue;
            }
            List<Integer> assignedShardingItems = getAssignedShardingItems(each, taskMetaInfoList, assignedTasks);
            if (!assignedShardingItems.isEmpty()) {
                result.add(new JobContext(jobConfig.get(), assignedShardingItems, ExecutionType.FAILOVER));
            }
        }
        return result;
    }

    private List<Integer> getAssignedShardingItems(final String jobName, final List<String> taskMetaInfoList,
            final Set<HashCode> assignedTasks) {
        List<Integer> result = new ArrayList<>(taskMetaInfoList.size());
        for (String each : taskMetaInfoList) {
            TaskContext.MetaInfo metaInfo = TaskContext.MetaInfo.from(each);
            if (assignedTasks
                    .add(Hashing.md5().newHasher().putString(jobName, Charsets.UTF_8)
                            .putInt(metaInfo.getShardingItem()).hash())
                    && !runningService.isTaskRunning(metaInfo)) {
                result.add(metaInfo.getShardingItem());
            }
        }
        return result;
    }

    /**
     * .
     * 
     * @param metaInfoList ??
     */
    public void remove(final Collection<TaskContext.MetaInfo> metaInfoList) {
        for (TaskContext.MetaInfo each : metaInfoList) {
            regCenter.remove(FailoverNode.getFailoverTaskNodePath(each.toString()));
        }
    }
}