Sxnan commented on code in PR #23919: URL: https://github.com/apache/flink/pull/23919#discussion_r1426426138
########## flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/io/RecordAttributesCombiner.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.streaming.runtime.io.PushingAsyncDataInput.DataOutput; +import org.apache.flink.streaming.runtime.streamrecord.RecordAttributes; +import org.apache.flink.streaming.runtime.streamrecord.RecordAttributesBuilder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; + +/** RecordAttributesValve combine RecordAttributes from different input channels. */ +public class RecordAttributesCombiner { + + private static final Logger LOG = LoggerFactory.getLogger(RecordAttributesCombiner.class); + + private final int numInputChannels; + private final RecordAttributes[] allChannelRecordAttributes; + private int nonBacklogChannelsCnt = 0; + private RecordAttributes lastOutputAttributes = null; + + public RecordAttributesCombiner(int numInputChannels) { + this.numInputChannels = numInputChannels; + this.allChannelRecordAttributes = new RecordAttributes[numInputChannels]; + } + + public void inputRecordAttributes( + RecordAttributes recordAttributes, int channelIdx, DataOutput<?> output) + throws Exception { + LOG.debug("RecordAttributes: {} from channel idx: {}", recordAttributes, channelIdx); + RecordAttributes lastChannelRecordAttributes = allChannelRecordAttributes[channelIdx]; + allChannelRecordAttributes[channelIdx] = recordAttributes; + + // skip if the input RecordAttributes of the input channel is the same as the last. + if (recordAttributes.equals(lastChannelRecordAttributes)) { + return; + } + + final RecordAttributesBuilder builder = + new RecordAttributesBuilder(Collections.emptyList()); + + builder.setBacklog(combineIsBacklog(lastChannelRecordAttributes, recordAttributes)); + + final RecordAttributes outputAttribute = builder.build(); + if (!outputAttribute.equals(lastOutputAttributes)) { + output.emitRecordAttributes(outputAttribute); + lastOutputAttributes = outputAttribute; + } + } + + /** If any of the input channels is backlog, the combined RecordAttributes is backlog. */ + private boolean combineIsBacklog( + RecordAttributes lastRecordAttributes, RecordAttributes recordAttributes) { + if (lastRecordAttributes == null + || lastRecordAttributes.isBacklog() != recordAttributes.isBacklog()) { + if (lastRecordAttributes != null && recordAttributes.isBacklog()) { + nonBacklogChannelsCnt -= 1; + } + if (!recordAttributes.isBacklog()) { + nonBacklogChannelsCnt += 1; + } + } + + return nonBacklogChannelsCnt < numInputChannels; + } Review Comment: Thanks for the review! I updated the `RecordAttributesCombiner` to address the initialization issue. The isBacklog of each input channel has three states, namely undefined, isBacklog=true, and isBacklog=false. We decide the isBacklog of the input as the following: - if any input channel isBacklog=true, then the input is isBacklog=true - otherwise, if any input channel is undefined, the isBacklog status of the input is unchanged - otherwise (all the channel is defined and isBacklog=false), the input is isBacklog=false Currently, all the operators are initialized with non-backlog mode. I agree that, ideally, we should determine the initial status before receiving the first `RecordAttributes` so that we don't have to initialize the operator in non-backlog mode and immediately switch to backlog mode before processing any records. However, It turns out that it is non-trivial and I don't think it should block this PR. Thus, I prefer to keep the current PR simple and address the problem in the future. WDYT? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
