azagrebin commented on a change in pull request #9996: [FLINK-14532][coordination] Split PartitionTracker URL: https://github.com/apache/flink/pull/9996#discussion_r340513720
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/AbstractPartitionTracker.java ########## @@ -0,0 +1,109 @@ +/* + * 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.runtime.io.network.partition; + +import org.apache.flink.runtime.taskexecutor.partition.PartitionTable; +import org.apache.flink.util.Preconditions; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Base partition tracker implementation, providing underlying data-structures for storing partitions, their associated + * keys and meta-information. + */ +public abstract class AbstractPartitionTracker<K, M> implements PartitionTracker<K, M> { + + private final PartitionTable<K> partitionTable = new PartitionTable<>(); + private final Map<ResultPartitionID, PartitionInfo<K, M>> partitionInfos = new HashMap<>(); + + void startTrackingPartition(K key, ResultPartitionID resultPartitionId, M metaInfo) { + partitionInfos.put(resultPartitionId, new PartitionInfo<>(key, metaInfo)); + partitionTable.startTrackingPartitions(key, Collections.singletonList(resultPartitionId)); + } + + @Override + public Collection<PartitionTrackerEntry<K, M>> stopTrackingPartitionsFor(K key) { + Preconditions.checkNotNull(key); + + // this is a bit icky since we make 2 calls to pT#stopTrackingPartitions + final Collection<ResultPartitionID> resultPartitionIds = partitionTable.stopTrackingPartitions(key); + + return resultPartitionIds.stream() + .map(this::internalStopTrackingPartition) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } + + @Override + public void stopTrackingPartitions(Collection<ResultPartitionID> resultPartitionIds) { + Preconditions.checkNotNull(resultPartitionIds); + + resultPartitionIds.forEach(this::internalStopTrackingPartition); Review comment: could also return `Collection<PartitionTrackerEntry<K, M>>` as `stopTrackingPartitionsFor` and then reused in `stopTrackingAndReleasePartitions` to deduplicate the stream transformation. `internalStopTrackingPartition` can also become `private` ---------------------------------------------------------------- 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