/*
 * 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.scheduler.strategy;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/** Group of consumed {@link IntermediateResultPartitionID}s. */
public class ConsumedPartitionGroup implements Iterable<IntermediateResultPartitionID> {

    private final List<IntermediateResultPartitionID> resultPartitions;

    private final AtomicInteger unfinishedPartitions;

    private ConsumedPartitionGroup(List<IntermediateResultPartitionID> resultPartitions) {
        this.resultPartitions = resultPartitions;
        this.unfinishedPartitions = new AtomicInteger(resultPartitions.size());
    }

    public static ConsumedPartitionGroup fromMultiplePartitions(
            List<IntermediateResultPartitionID> resultPartitions) {
        return new ConsumedPartitionGroup(resultPartitions);
    }

    public static ConsumedPartitionGroup fromSinglePartition(
            IntermediateResultPartitionID resultPartition) {
        return new ConsumedPartitionGroup(Collections.singletonList(resultPartition));
    }

    @Override
    public Iterator<IntermediateResultPartitionID> iterator() {
        return resultPartitions.iterator();
    }

    public int size() {
        return resultPartitions.size();
    }

    public boolean isEmpty() {
        return resultPartitions.isEmpty();
    }

    public IntermediateResultPartitionID getFirst() {
        return iterator().next();
    }

    public int partitionUnfinished() {
        return unfinishedPartitions.incrementAndGet();
    }

    public int partitionFinished() {
        return unfinishedPartitions.decrementAndGet();
    }

    @VisibleForTesting
    public int getNumberOfUnfinishedPartitions() {
        return unfinishedPartitions.get();
    }

    public boolean areAllPartitionsFinished() {
        return unfinishedPartitions.get() == 0;
    }
}

@@ -907,16 +907,80 @@
 esting;%0A
+import org.apache.flink.runtime.jobgraph.IntermediateDataSetID;%0A
 import o
@@ -1043,16 +1043,16 @@
 tionID;%0A
-
 %0Aimport 
@@ -1175,16 +1175,82 @@
 teger;%0A%0A
+import static org.apache.flink.util.Preconditions.checkArgument;%0A%0A
 /** Grou
@@ -1516,32 +1516,96 @@
 hedPartitions;%0A%0A
+    private final IntermediateDataSetID intermediateDataSetID;%0A%0A
     private Cons
@@ -1671,32 +1671,641 @@
 ltPartitions) %7B%0A
+        checkArgument(%0A                resultPartitions.size() %3E 0,%0A                %22The size of result partitions in the ConsumedPartitionGroup should be larger than 0.%22);%0A        this.intermediateDataSetID = resultPartitions.get(0).getIntermediateDataSetID();%0A%0A        // Sanity check: all the partitions in one ConsumedPartitionGroup should have the same%0A        // IntermediateDataSetID%0A        for (IntermediateResultPartitionID resultPartition : resultPartitions) %7B%0A            checkArgument(%0A                    resultPartition.getIntermediateDataSetID().equals(this.intermediateDataSetID));%0A        %7D%0A
         this.res
@@ -2338,16 +2338,17 @@
 itions;%0A
+%0A
         
@@ -3175,24 +3175,24 @@
 etFirst() %7B%0A
-
         retu
@@ -3212,32 +3212,224 @@
 .next();%0A    %7D%0A%0A
+    /** Get the ID of IntermediateDataSet this ConsumedPartitionGroup belongs to. */%0A    public IntermediateDataSetID getIntermediateDataSetID() %7B%0A        return intermediateDataSetID;%0A    %7D%0A%0A
     public int p

Add ConsumedPartitionGroup#getIntermediateDataSetID