sunhaibotb commented on a change in pull request #8124: [FLINK-11877] Implement the runtime handling of the InputSelectable interface URL: https://github.com/apache/flink/pull/8124#discussion_r291893541
########## File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/io/StreamTwoInputSelectableProcessor.java ########## @@ -0,0 +1,408 @@ +/* + * 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.streaming.runtime.io; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.SimpleCounter; +import org.apache.flink.runtime.io.disk.iomanager.IOManager; +import org.apache.flink.runtime.io.network.partition.consumer.InputGate; +import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup; +import org.apache.flink.streaming.api.operators.InputSelectable; +import org.apache.flink.streaming.api.operators.InputSelection; +import org.apache.flink.streaming.api.operators.TwoInputStreamOperator; +import org.apache.flink.streaming.api.watermark.Watermark; +import org.apache.flink.streaming.runtime.metrics.WatermarkGauge; +import org.apache.flink.streaming.runtime.streamrecord.StreamElement; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.streaming.runtime.streamstatus.StatusWatermarkValve; +import org.apache.flink.streaming.runtime.streamstatus.StreamStatus; +import org.apache.flink.streaming.runtime.streamstatus.StreamStatusMaintainer; +import org.apache.flink.util.ExceptionUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Input reader for {@link org.apache.flink.streaming.runtime.tasks.TwoInputSelectableStreamTask} + * in the case that the operator is InputSelectable. + * + * @param <IN1> The type of the records that arrive on the first input + * @param <IN2> The type of the records that arrive on the second input + */ +@Internal +public class StreamTwoInputSelectableProcessor<IN1, IN2> { + + private static final Logger LOG = LoggerFactory.getLogger(StreamTwoInputSelectableProcessor.class); + + private static final CompletableFuture<?> UNAVAILABLE = new CompletableFuture<>(); + + private final TwoInputStreamOperator<IN1, IN2, ?> streamOperator; + private final InputSelectable inputSelector; + + private final Object lock; + + private final StreamTaskInput input1; + private final StreamTaskInput input2; + + /** + * Valves that control how watermarks and stream statuses from the 2 inputs are forwarded. + */ + private final StatusWatermarkValve statusWatermarkValve1; + private final StatusWatermarkValve statusWatermarkValve2; + + /** + * Stream status for the two inputs. We need to keep track for determining when + * to forward stream status changes downstream. + */ + private StreamStatus firstStatus; + private StreamStatus secondStatus; + + private int availableInputsMask; + + private int lastReadInputIndex; + + private InputSelection inputSelection; + + private Counter numRecordsIn; + + private boolean initialized; + + public StreamTwoInputSelectableProcessor( + Collection<InputGate> inputGates1, + Collection<InputGate> inputGates2, + TypeSerializer<IN1> inputSerializer1, + TypeSerializer<IN2> inputSerializer2, + Object lock, + IOManager ioManager, + StreamStatusMaintainer streamStatusMaintainer, + TwoInputStreamOperator<IN1, IN2, ?> streamOperator, + WatermarkGauge input1WatermarkGauge, + WatermarkGauge input2WatermarkGauge) { + + checkState(streamOperator instanceof InputSelectable); + + this.streamOperator = checkNotNull(streamOperator); + this.inputSelector = (InputSelectable) streamOperator; + + this.lock = checkNotNull(lock); + + InputGate unionedInputGate1 = InputGateUtil.createInputGate(inputGates1.toArray(new InputGate[0])); + InputGate unionedInputGate2 = InputGateUtil.createInputGate(inputGates2.toArray(new InputGate[0])); + + // create a Input instance for each input + this.input1 = new StreamTaskNetworkInput(new BarrierDiscarder(unionedInputGate1), inputSerializer1, ioManager, 0); + this.input2 = new StreamTaskNetworkInput(new BarrierDiscarder(unionedInputGate2), inputSerializer2, ioManager, 1); + + this.statusWatermarkValve1 = new StatusWatermarkValve( + unionedInputGate1.getNumberOfInputChannels(), + new ForwardingValveOutputHandler(streamOperator, lock, streamStatusMaintainer, input1WatermarkGauge, 0)); + this.statusWatermarkValve2 = new StatusWatermarkValve( + unionedInputGate2.getNumberOfInputChannels(), + new ForwardingValveOutputHandler(streamOperator, lock, streamStatusMaintainer, input2WatermarkGauge, 1)); + + this.firstStatus = StreamStatus.ACTIVE; + this.secondStatus = StreamStatus.ACTIVE; + + this.availableInputsMask = (int) new InputSelection.Builder().select(1).select(2).build().getInputMask(); + + this.lastReadInputIndex = 1; // always try to read from the first input + + this.initialized = false; + + } + + public boolean processInput() throws Exception { + if (!initialized) { + initialize(); Review comment: I'll change the method name `initialize` to the appropriate one and add a comment. ---------------------------------------------------------------- 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 With regards, Apache Git Services