zhijiangW commented on a change in pull request #8804: [FLINK-12883][WIP][runtime] Add elaborated partition release logic URL: https://github.com/apache/flink/pull/8804#discussion_r297095452
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/executiongraph/RegionPartitionReleaseStrategyTest.java ########## @@ -0,0 +1,149 @@ +/* + * 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.executiongraph; + +import org.apache.flink.runtime.executiongraph.failover.flip1.partitionrelease.PipelinedRegion; +import org.apache.flink.runtime.executiongraph.failover.flip1.partitionrelease.RegionPartitionReleaseStrategy; +import org.apache.flink.runtime.io.network.partition.ResultPartitionType; +import org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingResultPartition; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingTopology; +import org.apache.flink.util.TestLogger; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link RegionPartitionReleaseStrategy}. + */ +public class RegionPartitionReleaseStrategyTest extends TestLogger { + + private TestingSchedulingTopology testingSchedulingTopology; + + @Before + public void setUp() throws Exception { + testingSchedulingTopology = new TestingSchedulingTopology(); + } + + @Test + public void releasePartitionsIfDownstreamRegionIsFinished() { + final List<TestingSchedulingExecutionVertex> producers = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingExecutionVertex> consumers = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingResultPartition> resultPartitions = testingSchedulingTopology.connectPointwise(producers, consumers).finish(); + + final ExecutionVertexID onlyProducerVertexId = producers.get(0).getId(); + final ExecutionVertexID onlyConsumerVertexId = consumers.get(0).getId(); + final IntermediateResultPartitionID onlyResultPartitionId = resultPartitions.get(0).getId(); + + final Set<PipelinedRegion> pipelinedRegions = pipelinedRegionsSet( + PipelinedRegion.from(onlyProducerVertexId), + PipelinedRegion.from(onlyConsumerVertexId)); + + final RegionPartitionReleaseStrategy regionPartitionReleaseStrategy = new RegionPartitionReleaseStrategy(testingSchedulingTopology, pipelinedRegions); + + final List<IntermediateResultPartitionID> partitionsToRelease = regionPartitionReleaseStrategy.vertexFinished(onlyConsumerVertexId); + assertThat(partitionsToRelease, contains(onlyResultPartitionId)); + } + + @Test + public void releasePartitionsIfDownstreamRegionWithMultipleOperatorsIsFinished() { + final List<TestingSchedulingExecutionVertex> sourceVertices = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingExecutionVertex> intermediateVertices = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingExecutionVertex> sinkVertices = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingResultPartition> sourceResultPartitions = testingSchedulingTopology.connectAllToAll(sourceVertices, intermediateVertices).finish(); + testingSchedulingTopology.connectAllToAll(intermediateVertices, sinkVertices).withResultPartitionType(ResultPartitionType.PIPELINED).finish(); + + final ExecutionVertexID onlySourceVertexId = sourceVertices.get(0).getId(); + final ExecutionVertexID onlyIntermediateVertexId = intermediateVertices.get(0).getId(); + final ExecutionVertexID onlySinkVertexId = sinkVertices.get(0).getId(); + final IntermediateResultPartitionID onlySourceResultPartitionId = sourceResultPartitions.get(0).getId(); + + final Set<PipelinedRegion> pipelinedRegions = pipelinedRegionsSet( + PipelinedRegion.from(onlySourceVertexId), + PipelinedRegion.from(onlyIntermediateVertexId, onlySinkVertexId)); + + final RegionPartitionReleaseStrategy regionPartitionReleaseStrategy = new RegionPartitionReleaseStrategy(testingSchedulingTopology, pipelinedRegions); + + regionPartitionReleaseStrategy.vertexFinished(onlyIntermediateVertexId); + final List<IntermediateResultPartitionID> partitionsToRelease = regionPartitionReleaseStrategy.vertexFinished(onlySinkVertexId); + assertThat(partitionsToRelease, contains(onlySourceResultPartitionId)); + } + + @Test + public void notReleasePartitionsIfDownstreamRegionIsNotFinished() { + final List<TestingSchedulingExecutionVertex> producers = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingExecutionVertex> consumers = testingSchedulingTopology.addExecutionVertices().withParallelism(2).finish(); + testingSchedulingTopology.connectAllToAll(producers, consumers).finish(); + + final ExecutionVertexID onlyProducerVertexId = producers.get(0).getId(); + final ExecutionVertexID consumerVertex1 = consumers.get(0).getId(); + final ExecutionVertexID consumerVertex2 = consumers.get(1).getId(); + + final Set<PipelinedRegion> pipelinedRegions = pipelinedRegionsSet( + PipelinedRegion.from(onlyProducerVertexId), + PipelinedRegion.from(consumerVertex1, consumerVertex2)); + + final RegionPartitionReleaseStrategy regionPartitionReleaseStrategy = new RegionPartitionReleaseStrategy(testingSchedulingTopology, pipelinedRegions); + + final List<IntermediateResultPartitionID> partitionsToRelease = regionPartitionReleaseStrategy.vertexFinished(consumerVertex1); + assertThat(partitionsToRelease, is(empty())); + } + + @Test + public void toggleVertexFinishedUnfinished() { + final List<TestingSchedulingExecutionVertex> producers = testingSchedulingTopology.addExecutionVertices().finish(); + final List<TestingSchedulingExecutionVertex> consumers = testingSchedulingTopology.addExecutionVertices().withParallelism(2).finish(); + testingSchedulingTopology.connectAllToAll(producers, consumers).finish(); + + final ExecutionVertexID onlyProducerVertexId = producers.get(0).getId(); + final ExecutionVertexID consumerVertex1 = consumers.get(0).getId(); + final ExecutionVertexID consumerVertex2 = consumers.get(1).getId(); + + final Set<PipelinedRegion> pipelinedRegions = pipelinedRegionsSet( + PipelinedRegion.from(onlyProducerVertexId), + PipelinedRegion.from(consumerVertex1, consumerVertex2)); + + final RegionPartitionReleaseStrategy regionPartitionReleaseStrategy = new RegionPartitionReleaseStrategy(testingSchedulingTopology, pipelinedRegions); + + regionPartitionReleaseStrategy.vertexFinished(consumerVertex1); + regionPartitionReleaseStrategy.vertexFinished(consumerVertex2); + regionPartitionReleaseStrategy.vertexUnfinished(consumerVertex2); + + final List<IntermediateResultPartitionID> partitionsToRelease = regionPartitionReleaseStrategy.vertexFinished(consumerVertex1); + assertThat(partitionsToRelease, is(empty())); + } + + private static Set<PipelinedRegion> pipelinedRegionsSet(final PipelinedRegion... pipelinedRegions) { Review comment: might be further deduplication via `pipelinedRegionsSet(ExecutionVertexID producerIds, ExecutionVertexID... consumerIds)` ---------------------------------------------------------------- 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