godfreyhe commented on a change in pull request #13760: URL: https://github.com/apache/flink/pull/13760#discussion_r511539068
########## File path: flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/operators/multipleinput/input/InputSelectionHandler.java ########## @@ -0,0 +1,98 @@ +/* + * 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 org.apache.flink.table.runtime.operators.multipleinput.input; + +import org.apache.flink.streaming.api.operators.InputSelection; +import org.apache.flink.streaming.api.operators.MultipleInputStreamOperator; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * This handler is mainly used for selecting the next available input index + * according to read priority in {@link MultipleInputStreamOperator}. + * + * <p>Input read order: the input with high priority (the value of read order is lower) + * will be read first, the inputs with same priorities will be read fairly. + */ +public class InputSelectionHandler { + private final List<InputSpec> inputSpecs; + private final int numberOfInput; + /** + * All inputs ids sorted by priority. + */ + private final List<List<Integer>> sortedAvailableInputs; + private InputSelection inputSelection; + + public InputSelectionHandler(List<InputSpec> inputSpecs) { + this.inputSpecs = inputSpecs; + this.numberOfInput = inputSpecs.size(); + this.sortedAvailableInputs = buildSortedAvailableInputs(); + // read the highest priority inputs first + this.inputSelection = buildInputSelection(sortedAvailableInputs.get(0)); + } + + public InputSelection getInputSelection() { + return inputSelection; + } + + public void endInput(int inputId) { + List<Integer> inputIds = sortedAvailableInputs.get(0); + if (!inputIds.remove(Integer.valueOf(inputId))) { + throw new RuntimeException("This should not happen."); + } + if (inputIds.isEmpty()) { + // remove the finished input + sortedAvailableInputs.remove(0); + + if (sortedAvailableInputs.isEmpty()) { + // all input are finished + inputIds = null; + } else { + // read next one + inputIds = sortedAvailableInputs.get(0); + } + inputSelection = buildInputSelection(inputIds); Review comment: `buildInputSelection` already handles the null case ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org