JingGe commented on a change in pull request #18428: URL: https://github.com/apache/flink/pull/18428#discussion_r793777351
########## File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/operators/sink/committables/CommittableCollector.java ########## @@ -0,0 +1,235 @@ +/* + * 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.operators.sink.committables; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.functions.RuntimeContext; +import org.apache.flink.api.connector.sink2.Sink.InitContext; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.streaming.api.connector.sink2.CommittableMessage; +import org.apache.flink.streaming.api.connector.sink2.CommittableSummary; +import org.apache.flink.streaming.api.connector.sink2.CommittableWithLineage; + +import javax.annotation.Nullable; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * This class is responsible to book-keep the committing progress across checkpoints and subtasks. + * It handles the emission of committables and the {@link CommittableSummary}. + * + * @param <CommT> type of committable + */ +@Internal +public class CommittableCollector<CommT> { + private static final long EOI = Long.MAX_VALUE; + private final NavigableMap<Long, CheckpointCommittablesImpl<CommT>> checkpointCommittables; + private final int subtaskId; + private final int numberOfSubtasks; + + CommittableCollector(int subtaskId, int numberOfSubtasks) { + this.subtaskId = subtaskId; + this.numberOfSubtasks = numberOfSubtasks; + this.checkpointCommittables = new TreeMap<>(); + } + + /** For deserialization. */ + CommittableCollector(Map<Long, CheckpointCommittablesImpl<CommT>> checkpointCommittables) { + this(checkpointCommittables, 0, 1); + } + + /** For deep-copy. */ + CommittableCollector( + Map<Long, CheckpointCommittablesImpl<CommT>> checkpointCommittables, + int subtaskId, + int numberOfSubtasks) { + this.checkpointCommittables = new TreeMap<>(checkNotNull(checkpointCommittables)); + this.subtaskId = subtaskId; + this.numberOfSubtasks = numberOfSubtasks; + } + + /** + * Creates a {@link CommittableCollector} based on the current runtime information. This method + * should be used for to instantiate a collector for all Sink V2. + * + * @param context holding runtime of information + * @param <CommT> type of the committable + * @return {@link CommittableCollector} + */ + public static <CommT> CommittableCollector<CommT> of(RuntimeContext context) { + return new CommittableCollector<>( + context.getIndexOfThisSubtask(), context.getNumberOfParallelSubtasks()); + } + + /** + * Creates a {@link CommittableCollector} for a list of committables. This method is mainly used + * to create a collector from the state of Sink V1. + * + * @param r list of committables + * @param <CommT> type of committables + * @return {@link CommittableCollector} + */ + static <CommT> CommittableCollector<CommT> ofLegacy(List<CommT> r) { + CommittableCollector<CommT> committableCollector = new CommittableCollector<>(0, 1); + // add a checkpoint with the lowest checkpoint id, this will be merged into the next + // checkpoint data, subtask id is arbitrary + CommittableSummary<CommT> summary = + new CommittableSummary<>( + 0, 1, InitContext.INITIAL_CHECKPOINT_ID, r.size(), r.size(), 0); + committableCollector.addSummary(summary); + SubtaskCommittables<CommT> subtask = + committableCollector + .getCheckpointCommittables(summary) + .getSubtaskCommittables(summary.getSubtaskId()); + r.forEach(subtask::add); + return committableCollector; + } + + /** + * Adds a {@link CommittableMessage} to the collector to hold it until emission. + * + * @param message either {@link CommittableSummary} or {@link CommittableWithLineage} + */ + public void addMessage(CommittableMessage<CommT> message) { Review comment: sure, it is not a big deal. The long name will improve the code readability at the place where the method is called, since code like `addMessage(message)` does not contain enough info. But once the developer get familiar with the code, it is not an issue. -- 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]
