zhuzhurk commented on a change in pull request #10462: [FLINK-15031][runtime] 
Calculate required shuffle memory before allocating slots if resources are 
specified
URL: https://github.com/apache/flink/pull/10462#discussion_r355130172
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/ResourceRequirementsRetrieverTest.java
 ##########
 @@ -0,0 +1,207 @@
+/*
+ * 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;
+
+import org.apache.flink.api.common.operators.ResourceSpec;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
+import org.apache.flink.runtime.executiongraph.ExecutionGraph;
+import org.apache.flink.runtime.executiongraph.ExecutionJobVertex;
+import org.apache.flink.runtime.executiongraph.TestingExecutionGraphBuilder;
+import org.apache.flink.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionType;
+import org.apache.flink.runtime.jobgraph.DistributionPattern;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup;
+import org.apache.flink.runtime.shuffle.PartitionDescriptor;
+import org.apache.flink.runtime.shuffle.ProducerDescriptor;
+import org.apache.flink.runtime.shuffle.ShuffleDescriptor;
+import org.apache.flink.runtime.shuffle.ShuffleMaster;
+import org.apache.flink.runtime.shuffle.TaskInputsOutputsDescriptor;
+import org.apache.flink.runtime.testtasks.NoOpInvokable;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link ResourceRequirementsRetriever}.
+ */
+public class ResourceRequirementsRetrieverTest {
+
+       private static final int OFFSET = 1000;
+
+       private static final ResourceSpec DEFAULT_RESOURCES = 
ResourceSpec.newBuilder(1.0, 100).build();
+
+       private static final TestShuffleMaster SHUFFLE_MASTER = new 
TestShuffleMaster();
+
+       private JobGraph jobGraph;
+
+       private ExecutionGraph executionGraph;
+
+       private List<SlotSharingGroup> slotSharingGroups;
+
+       private ResourceRequirementsRetriever resourceRequirementsRetriever;
+
+       @Test
+       public void testJobVertexResourceRequirements() throws Exception {
+               setUp(DEFAULT_RESOURCES);
+
+               final List<JobVertex> jobVertices = 
jobGraph.getVerticesSortedTopologicallyFromSources();
+               final ExecutionJobVertex source = 
executionGraph.getJobVertex(jobVertices.get(0).getID());
+               final ExecutionJobVertex map = 
executionGraph.getJobVertex(jobVertices.get(1).getID());
+               final ExecutionJobVertex sink = 
executionGraph.getJobVertex(jobVertices.get(2).getID());
+
+               assertEquals(
+                       ResourceProfile.fromResourceSpec(DEFAULT_RESOURCES, new 
MemorySize(0 * OFFSET + 2)),
+                       
resourceRequirementsRetriever.getJobVertexResourceRequirement(source.getJobVertexId()));
+
+               assertEquals(
+                       ResourceProfile.fromResourceSpec(DEFAULT_RESOURCES, new 
MemorySize(1 * OFFSET + 6)),
+                       
resourceRequirementsRetriever.getJobVertexResourceRequirement(map.getJobVertexId()));
+
+               assertEquals(
+                       ResourceProfile.fromResourceSpec(DEFAULT_RESOURCES, new 
MemorySize(5 * OFFSET + 0)),
+                       
resourceRequirementsRetriever.getJobVertexResourceRequirement(sink.getJobVertexId()));
+       }
+
+       @Test
+       public void testGroupResourceRequirements() throws Exception {
+               setUp(DEFAULT_RESOURCES);
+
+               final SlotSharingGroupId groupId1 = 
slotSharingGroups.get(0).getSlotSharingGroupId();
+               final SlotSharingGroupId groupId2 = 
slotSharingGroups.get(1).getSlotSharingGroupId();
+
+               assertEquals(
+                       ResourceProfile.fromResourceSpec(
+                               DEFAULT_RESOURCES.merge(DEFAULT_RESOURCES),
+                               new MemorySize((0 + 1) * OFFSET + (2 + 6))),
+                       
resourceRequirementsRetriever.getSlotSharingGroupResourceRequirement(groupId1));
+
+               assertEquals(
+                       ResourceProfile.fromResourceSpec(DEFAULT_RESOURCES, new 
MemorySize(5 * OFFSET + 0)),
+                       
resourceRequirementsRetriever.getSlotSharingGroupResourceRequirement(groupId2));
+       }
+
+       @Test
+       public void testJobVertexResourceRequirementWithUnknownResources() 
throws Exception {
+               setUp(ResourceSpec.UNKNOWN);
+
+               for (JobVertex jobVertex : 
jobGraph.getVerticesSortedTopologicallyFromSources()) {
+                       assertEquals(
+                               ResourceProfile.UNKNOWN,
+                               
resourceRequirementsRetriever.getJobVertexResourceRequirement(jobVertex.getID()));
+               }
+       }
+
+       @Test
+       public void testGroupResourceRequirementWithUnknownResources() throws 
Exception {
+               setUp(ResourceSpec.UNKNOWN);
+
+               for (SlotSharingGroup slotSharingGroup : slotSharingGroups) {
+                       assertEquals(
+                               ResourceProfile.UNKNOWN,
+                               
resourceRequirementsRetriever.getSlotSharingGroupResourceRequirement(slotSharingGroup.getSlotSharingGroupId()));
+               }
+       }
+
+       private void setUp(final ResourceSpec resources) throws Exception {
+               slotSharingGroups = Arrays.asList(new SlotSharingGroup(), new 
SlotSharingGroup());
+
+               jobGraph = createJobGraph(slotSharingGroups, resources);
+
+               executionGraph = 
TestingExecutionGraphBuilder.newBuilder().setJobGraph(jobGraph).build();
+
+               resourceRequirementsRetriever = new 
ResourceRequirementsRetriever(executionGraph.getAllVertices(), SHUFFLE_MASTER);
+       }
+
+       /**
+        * Create a job graph for testing.
+        * The parallelism is 4/5/6 for source/map/sink.
+        * source and map are in the same slot sharing group. sink is in an 
individual group.
+        * map is POINTWISE connected to source. sink is ALL_TO_ALL connected 
to map.
+        */
+       private static JobGraph createJobGraph(final List<SlotSharingGroup> 
slotSharingGroups, final ResourceSpec resources) {
+               final JobVertex source = new JobVertex("source");
+               source.setInvokableClass(NoOpInvokable.class);
+               source.setParallelism(4);
+               source.setSlotSharingGroup(slotSharingGroups.get(0));
+               source.setResources(resources, resources);
+
+               final JobVertex map = new JobVertex("map");
+               map.setInvokableClass(NoOpInvokable.class);
+               map.setParallelism(5);
+               map.setSlotSharingGroup(slotSharingGroups.get(0));
+               map.setResources(resources, resources);
+
+               final JobVertex sink = new JobVertex("sink");
+               sink.setInvokableClass(NoOpInvokable.class);
+               sink.setParallelism(6);
+               sink.setSlotSharingGroup(slotSharingGroups.get(1));
+               sink.setResources(resources, resources);
+
+               map.connectNewDataSetAsInput(source, 
DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);
+               sink.connectNewDataSetAsInput(map, 
DistributionPattern.ALL_TO_ALL, ResultPartitionType.PIPELINED);
+
+               return new JobGraph(source, map, sink);
+       }
+
+       /**
+        * Test implementation of {@link ShuffleMaster}.
+        * The shuffle memory size for a task is {@code totalChannels * OFFSET 
+ totalResultSubpartitions}。
+        */
+       private static class TestShuffleMaster implements 
ShuffleMaster<ShuffleDescriptor> {
+
+               @Override
+               public CompletableFuture<ShuffleDescriptor> 
registerPartitionWithProducer(
+                               final PartitionDescriptor partitionDescriptor,
+                               final ProducerDescriptor producerDescriptor) {
+
+                       return null;
+               }
+
+               @Override
+               public void releasePartitionExternally(final ShuffleDescriptor 
shuffleDescriptor) {
+               }
+
+               @Override
+               public MemorySize getShuffleMemoryForTask(final 
TaskInputsOutputsDescriptor taskInputsOutputsDescriptor) {
+                       final long size = 
getInputChannelsOfLastTaskInputsOutputsDescriptor(taskInputsOutputsDescriptor) 
* OFFSET
+                               + 
getResultSubpartitionsOfLastTaskInputsOutputsDescriptor(taskInputsOutputsDescriptor);
+                       return new MemorySize(size);
+               }
 
 Review comment:
   We still need to implement a `ShuffleMaster` to create 
`ResourceRequirementsRetriever`.
   It should be not too bad since there are only 2 other interfaces.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to