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

com.alibaba.dts.client.executor.grid.flowcontrol.FlowControlChain Maven / Gradle / Ivy

package com.alibaba.dts.client.executor.grid.flowcontrol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.dts.client.executor.job.context.ClientContextImpl;
import com.alibaba.dts.client.executor.job.context.JobContext;

/**
 * @author Ronan Zhan
 * @date 2016/12/22.
 */
public class FlowControlChain {

    private ClientContextImpl clientContext;

    private List flowControls = new ArrayList();

    private Map flowControlMap = new ConcurrentHashMap();

    public FlowControlChain(ClientContextImpl clientContext) {
        this.clientContext = clientContext;
    }

    public void init() {
        FlowControlStrategy flowControlStrategy = clientContext.getNodeConfig().getFlowControlStrategy();
        FlowControlParameterWatcher watcher = new FlowControlParameterWatcher(clientContext);
        watcher.watchH2Count();

        FlowControl h2Count = new H2CountOneFlowControl(clientContext);

        if (clientContext.getNodeConfig().getFlowControlStrategyMap() != null) {
            for (Entry jobProcessorFlowControlStrategy : clientContext.getNodeConfig()
                    .getFlowControlStrategyMap().entrySet()) {
                String jobProcessor = jobProcessorFlowControlStrategy.getKey();
                FlowControlStrategy flowControlStrategyTemp = jobProcessorFlowControlStrategy.getValue();
                if (flowControlStrategyTemp == FlowControlStrategy.H2_COUNT_ONE || flowControlStrategyTemp
                        == FlowControlStrategy.H2_COUNT) {
                    flowControlMap.put(jobProcessor, h2Count);
                }
            }
        }
        if (flowControlStrategy == FlowControlStrategy.NONE) {
            return;
        }
        if (flowControlStrategy == FlowControlStrategy.H2_COUNT_ONE || flowControlStrategy
                == FlowControlStrategy.H2_COUNT) {
            addLast(h2Count);
        }
    }

    public void control(JobContext jobContext) {
        String jobProcessor = jobContext.getJob().getJobProcessor();
        FlowControl flowControl = flowControlMap.get(jobProcessor);
        if (flowControl != null) {
            flowControl.pass(jobContext);
        } else {
            for (FlowControl flowControl2 : flowControls) {
                flowControl2.pass(jobContext);
            }
        }
    }

    public void addLast(FlowControl flowControl) {
        flowControls.add(flowControl);
    }

    public List getFlowControls() {
        return flowControls;
    }

    public void setFlowControls(List flowControls) {
        this.flowControls = flowControls;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy