Skip to content

Commit

Permalink
[FLINK-30233] Introduce AllFinishedInputConsumableDecider.
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed Dec 22, 2022
1 parent 90473d8 commit bf1cb5f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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:https://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 java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
* {@link AllFinishedInputConsumableDecider} is a special {@link InputConsumableDecider}. The input
* is considered to be consumable only when all producer partitions are finished.
*/
public class AllFinishedInputConsumableDecider implements InputConsumableDecider {

@Override
public boolean isInputConsumable(
SchedulingExecutionVertex executionVertex,
Set<ExecutionVertexID> verticesToDeploy,
Map<ConsumedPartitionGroup, Boolean> consumableStatusCache) {
for (ConsumedPartitionGroup consumedPartitionGroup :
executionVertex.getConsumedPartitionGroups()) {

if (!consumableStatusCache.computeIfAbsent(
consumedPartitionGroup, this::isConsumedPartitionGroupConsumable)) {
return false;
}
}
return true;
}

private boolean isConsumedPartitionGroupConsumable(
final ConsumedPartitionGroup consumedPartitionGroup) {
return consumedPartitionGroup.getNumberOfUnfinishedPartitions() == 0;
}

/** Factory for {@link AllFinishedInputConsumableDecider}. */
public static class Factory implements InputConsumableDecider.Factory {

public static final Factory INSTANCE = new Factory();

private Factory() {}

@Override
public InputConsumableDecider createInstance(
SchedulingTopology schedulingTopology,
Function<ExecutionVertexID, Boolean> scheduledVertexRetriever) {
return new AllFinishedInputConsumableDecider();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.apache.flink.runtime.scheduler.strategy;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.runtime.io.network.partition.ResultPartitionType;
import org.apache.flink.runtime.jobgraph.IntermediateDataSetID;
import org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID;
Expand Down Expand Up @@ -120,7 +119,6 @@ public int partitionFinished() {
return unfinishedPartitions.decrementAndGet();
}

@VisibleForTesting
public int getNumberOfUnfinishedPartitions() {
return unfinishedPartitions.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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:https://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.runtime.io.network.partition.ResultPartitionType;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link AllFinishedInputConsumableDecider}. */
class AllFinishedInputConsumableDeciderTest {
@Test
void testNotFinishedBlockingInput() {
final TestingSchedulingTopology topology = new TestingSchedulingTopology();

final List<TestingSchedulingExecutionVertex> producers =
topology.addExecutionVertices().withParallelism(2).finish();

final List<TestingSchedulingExecutionVertex> consumer =
topology.addExecutionVertices().withParallelism(2).finish();

topology.connectAllToAll(producers, consumer)
.withResultPartitionState(ResultPartitionState.CREATED)
.withResultPartitionType(ResultPartitionType.BLOCKING)
.finish();

AllFinishedInputConsumableDecider inputConsumableDecider =
createAllFinishedInputConsumableDecider();

assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(0), Collections.emptySet(), new HashMap<>()))
.isFalse();
assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(1), Collections.emptySet(), new HashMap<>()))
.isFalse();
}

@Test
void testAllFinishedBlockingInput() {
final TestingSchedulingTopology topology = new TestingSchedulingTopology();

final List<TestingSchedulingExecutionVertex> producers =
topology.addExecutionVertices().withParallelism(2).finish();

final List<TestingSchedulingExecutionVertex> consumer =
topology.addExecutionVertices().withParallelism(2).finish();

topology.connectAllToAll(producers, consumer)
.withResultPartitionState(ResultPartitionState.ALL_DATA_PRODUCED)
.withResultPartitionType(ResultPartitionType.BLOCKING)
.finish();

AllFinishedInputConsumableDecider inputConsumableDecider =
createAllFinishedInputConsumableDecider();

assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(0), Collections.emptySet(), new HashMap<>()))
.isTrue();
assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(1), Collections.emptySet(), new HashMap<>()))
.isTrue();
}

@Test
void testNotFinishedHybridInput() {
final TestingSchedulingTopology topology = new TestingSchedulingTopology();

final List<TestingSchedulingExecutionVertex> producers =
topology.addExecutionVertices().withParallelism(2).finish();

final List<TestingSchedulingExecutionVertex> consumer =
topology.addExecutionVertices().withParallelism(2).finish();

topology.connectAllToAll(producers, consumer)
.withResultPartitionState(ResultPartitionState.CREATED)
.withResultPartitionType(ResultPartitionType.HYBRID_FULL)
.finish();

AllFinishedInputConsumableDecider inputConsumableDecider =
createAllFinishedInputConsumableDecider();

assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(0), Collections.emptySet(), new HashMap<>()))
.isFalse();
assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(1), Collections.emptySet(), new HashMap<>()))
.isFalse();
}

@Test
void testAllFinishedHybridInput() {
final TestingSchedulingTopology topology = new TestingSchedulingTopology();

final List<TestingSchedulingExecutionVertex> producers =
topology.addExecutionVertices().withParallelism(2).finish();

final List<TestingSchedulingExecutionVertex> consumer =
topology.addExecutionVertices().withParallelism(2).finish();

topology.connectAllToAll(producers, consumer)
.withResultPartitionState(ResultPartitionState.ALL_DATA_PRODUCED)
.withResultPartitionType(ResultPartitionType.HYBRID_FULL)
.finish();

AllFinishedInputConsumableDecider inputConsumableDecider =
createAllFinishedInputConsumableDecider();

assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(0), Collections.emptySet(), new HashMap<>()))
.isTrue();
assertThat(
inputConsumableDecider.isInputConsumable(
consumer.get(1), Collections.emptySet(), new HashMap<>()))
.isTrue();
}

private static AllFinishedInputConsumableDecider createAllFinishedInputConsumableDecider() {
return new AllFinishedInputConsumableDecider();
}
}

0 comments on commit bf1cb5f

Please sign in to comment.