Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ package com.huawei.streaming.application; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.huawei.streaming.event.IEventType; import com.huawei.streaming.exception.ErrorCode; import com.huawei.streaming.exception.StreamingException; import com.huawei.streaming.operator.IRichOperator; import com.huawei.streaming.operator.OutputOperator; import com.huawei.streaming.operator.functionstream.SplitOp; /** * ?? * <??> * */ public class OperatorMng { private static final Logger LOG = LoggerFactory.getLogger(OperatorMng.class); //DFG???Storm? private List<IRichOperator> sortedFunctions; private Map<String, IRichOperator> inputs; private Map<String, IRichOperator> functions; private Map<String, IRichOperator> outputs; /** * <> */ public OperatorMng() { this.inputs = Maps.newLinkedHashMap(); this.outputs = Maps.newLinkedHashMap(); this.functions = Maps.newLinkedHashMap(); this.sortedFunctions = Lists.newArrayList(); } /** * ??????? * <??> * * @param opName ??? * @return ??? */ public boolean isNameValid(String opName) { LOG.debug("isNameValid enter, the opName is:{}.", opName); if (!inputs.containsKey(opName) && !functions.containsKey(opName)) { return true; } return false; } /** * ??? * * @param source ??? * @return ???? */ public boolean addInputStreamOperator(IRichOperator source) { if (isNameValid(source.getOperatorId())) { inputs.put(source.getOperatorId(), source); return true; } return false; } /** * ? * * @param output ? * @return ?? */ public boolean addOutputStreamOperator(IRichOperator output) { if (isNameValid(output.getOperatorId())) { outputs.put(output.getOperatorId(), output); return true; } return false; } /** * ? * * @param operator ? * @return ?? */ public boolean addFunctionStreamOperator(IRichOperator operator) { if (isNameValid(operator.getOperatorId())) { functions.put(operator.getOperatorId(), operator); return true; } return false; } /** * ?? * * @return ? */ public List<IRichOperator> getOutputOps() { List<IRichOperator> s = new ArrayList<IRichOperator>(); for (Entry<String, IRichOperator> m : outputs.entrySet()) { s.add(m.getValue()); } return s; } /** * ?? * <??> * * @return ??? */ public List<IRichOperator> getFunctionOps() { LOG.debug("GetFunctionOps enter."); List<IRichOperator> s = new ArrayList<IRichOperator>(); for (Entry<String, IRichOperator> m : functions.entrySet()) { s.add(m.getValue()); } return s; } /** * ??? * * @return ??? */ public List<IRichOperator> getSourceOps() { List<IRichOperator> s = new ArrayList<IRichOperator>(); for (Entry<String, IRichOperator> m : inputs.entrySet()) { s.add(m.getValue()); } return s; } /** * ?DAG * * ?? * * ?? * 1?? * ????(?0) * DAG?? * (??)T?????? * T?????????? * T??T[n-1]???????T? */ /** * ??? * <??> * * @return ?? * @throws StreamingException ?? */ public List<IRichOperator> genFunctionOpsOrder() throws StreamingException { LOG.info("GenFunctionOpsOrder enter."); validateOperators(); //???? List<String> deletedStreamName = Lists.newArrayList(); //Source???? for (Entry<String, IRichOperator> et : inputs.entrySet()) { deletedStreamName.add(et.getValue().getOutputStream()); } //Spout???NULL if (deletedStreamName.size() == 0) { LOG.debug("All the source operators have no output stream."); return null; } //????? Map<String, IRichOperator> functionsAndOutputs = functionAndOutputOperators(); Set<String> unSortedFunctions = Sets.newHashSet(); unSortedFunctions.addAll(functionsAndOutputs.keySet()); IRichOperator fun = null; boolean qualified = true; int presortedSize = 0; boolean stopFlag = false; //????? while (unSortedFunctions.size() > 0 && !stopFlag) { presortedSize = sortedFunctions.size(); Iterator<String> iterator = unSortedFunctions.iterator(); while (iterator.hasNext()) { String funname = iterator.next(); fun = functionsAndOutputs.get(funname); for (String inputname : fun.getInputStream()) { if (!deletedStreamName.contains(inputname)) { qualified = false; break; } } /* * ???????? * ??? * ?????? * ??????? */ if (qualified) { sortedFunctions.add(fun); if (!(fun instanceof OutputOperator)) { /* split? ?????? */ // if (fun instanceof SplitOp) { SplitOp sop = (SplitOp) fun; Map<String, IEventType> outputMap = sop.getOutputSchemaMap(); for (String outputStreamName : outputMap.keySet()) { deletedStreamName.add(outputStreamName); } } else { deletedStreamName.add(fun.getOutputStream()); } } iterator.remove(); } qualified = true; } /* * ????????????DAG?? */ if (sortedFunctions.size() == presortedSize) { stopFlag = true; } } //?DAG? stopWhileNoAdded(stopFlag); stopWhileNoAllAdded(functionsAndOutputs); return sortedFunctions; } private void stopWhileNoAllAdded(Map<String, IRichOperator> functionsAndOutputs) throws StreamingException { if (sortedFunctions.size() != functionsAndOutputs.size()) { StreamingException exception = new StreamingException(ErrorCode.PLATFORM_INVALID_TOPOLOGY); LOG.error(exception.getMessage(), exception); throw exception; } } private void stopWhileNoAdded(boolean stopFlag) throws StreamingException { if (stopFlag) { StreamingException exception = new StreamingException(ErrorCode.PLATFORM_INVALID_TOPOLOGY); LOG.error(exception.getMessage(), exception); throw exception; } } private void validateOperators() throws StreamingException { if (null == sortedFunctions || sortedFunctions.size() > 0) { StreamingException exception = new StreamingException(ErrorCode.PLATFORM_INVALID_TOPOLOGY); LOG.error(exception.getMessage(), exception); throw exception; } } private Map<String, IRichOperator> functionAndOutputOperators() { Map<String, IRichOperator> map = Maps.newHashMap(); map.putAll(functions); map.putAll(outputs); return map; } }