KarmaGYZ commented on code in PR #23635: URL: https://github.com/apache/flink/pull/23635#discussion_r1398939301
########## flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategy.java: ########## @@ -0,0 +1,361 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.scheduler.strategy.SchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * This strategy tries to get a balanced tasks scheduling. Execution vertices, which are belong to + * the same SlotSharingGroup, tend to be put evenly in each ExecutionSlotSharingGroup. Co-location + * constraints will be respected. + */ +class TaskBalancedPreferredSlotSharingStrategy extends AbstractSlotSharingStrategy { + + public static final Logger LOG = + LoggerFactory.getLogger(TaskBalancedPreferredSlotSharingStrategy.class); + + TaskBalancedPreferredSlotSharingStrategy( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + super(topology, slotSharingGroups, coLocationGroups); + } + + @Override + protected Map<ExecutionVertexID, ExecutionSlotSharingGroup> computeExecutionSlotSharingGroups( + SchedulingTopology schedulingTopology) { + return new TaskBalancedExecutionSlotSharingGroupBuilder( + schedulingTopology, this.logicalSlotSharingGroups, this.coLocationGroups) + .build(); + } + + static class Factory implements SlotSharingStrategy.Factory { + + public TaskBalancedPreferredSlotSharingStrategy create( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + + return new TaskBalancedPreferredSlotSharingStrategy( + topology, slotSharingGroups, coLocationGroups); + } + } + + /** SlotSharingGroupBuilder class for balanced scheduling strategy. */ + private static class TaskBalancedExecutionSlotSharingGroupBuilder { + + private final SchedulingTopology topology; + + private final Map<JobVertexID, SlotSharingGroup> slotSharingGroupMap; + + /** Record the {@link ExecutionSlotSharingGroup}s for {@link SlotSharingGroup}s. */ + private final Map<SlotSharingGroup, List<ExecutionSlotSharingGroup>> + paralleledExecutionSlotSharingGroupsMap; + + /** + * Record the next round-robin {@link ExecutionSlotSharingGroup} index for {@link + * SlotSharingGroup}s. + */ + private final Map<SlotSharingGroup, Integer> slotSharingGroupIndexMap; + + private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> + executionSlotSharingGroupMap; + + private final Map<JobVertexID, CoLocationGroup> coLocationGroupMap; + + private final Map<CoLocationConstraint, ExecutionSlotSharingGroup> + constraintToExecutionSlotSharingGroupMap; + + private TaskBalancedExecutionSlotSharingGroupBuilder( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + this.topology = checkNotNull(topology); + + this.coLocationGroupMap = new HashMap<>(); + for (CoLocationGroup coLocationGroup : coLocationGroups) { + for (JobVertexID jobVertexId : coLocationGroup.getVertexIds()) { + coLocationGroupMap.put(jobVertexId, coLocationGroup); + } + } + + this.constraintToExecutionSlotSharingGroupMap = new HashMap<>(); + this.paralleledExecutionSlotSharingGroupsMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupIndexMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupMap = new HashMap<>(); + this.executionSlotSharingGroupMap = new HashMap<>(); + + for (SlotSharingGroup slotSharingGroup : slotSharingGroups) { + for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) { + slotSharingGroupMap.put(jobVertexId, slotSharingGroup); + } + } + } + + private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() { + + final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = + getExecutionVertices(topology); + + initParalleledExecutionSlotSharingGroupsMap(allVertices); + + // Loop on job vertices + for (Map.Entry<JobVertexID, List<SchedulingExecutionVertex>> executionVertexInfos : + allVertices.entrySet()) { + + JobVertexID jobVertexID = executionVertexInfos.getKey(); + List<SchedulingExecutionVertex> executionVertices = executionVertexInfos.getValue(); + final int jobVertexParallel = executionVertices.size(); + final SlotSharingGroup slotSharingGroup = slotSharingGroupMap.get(jobVertexID); + final int index = getSlotRoundRobinIndex(jobVertexParallel, slotSharingGroup); Review Comment: ```suggestion final int startIndex = getSlotRoundRobinIndex(jobVertexParallel, slotSharingGroup); ``` ########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/MergingSharedSlotProfileRetrieverTest.java: ########## @@ -67,9 +68,10 @@ public void testGetEmptySlotProfile() throws ExecutionException, InterruptedExce () -> Collections.emptySet()) .createFromBulk(Collections.emptySet()); + SlotSharingGroup slotSharingGroup = new SlotSharingGroup(); SlotProfile slotProfile = sharedSlotProfileRetriever.getSlotProfile( - new ExecutionSlotSharingGroup(), ResourceProfile.ZERO); + new ExecutionSlotSharingGroup(slotSharingGroup), ResourceProfile.ZERO); Review Comment: ```suggestion new ExecutionSlotSharingGroup(new SlotSharingGroup()), ResourceProfile.ZERO); ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategy.java: ########## @@ -0,0 +1,361 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.scheduler.strategy.SchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * This strategy tries to get a balanced tasks scheduling. Execution vertices, which are belong to + * the same SlotSharingGroup, tend to be put evenly in each ExecutionSlotSharingGroup. Co-location + * constraints will be respected. + */ +class TaskBalancedPreferredSlotSharingStrategy extends AbstractSlotSharingStrategy { + + public static final Logger LOG = + LoggerFactory.getLogger(TaskBalancedPreferredSlotSharingStrategy.class); + + TaskBalancedPreferredSlotSharingStrategy( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + super(topology, slotSharingGroups, coLocationGroups); + } + + @Override + protected Map<ExecutionVertexID, ExecutionSlotSharingGroup> computeExecutionSlotSharingGroups( + SchedulingTopology schedulingTopology) { + return new TaskBalancedExecutionSlotSharingGroupBuilder( + schedulingTopology, this.logicalSlotSharingGroups, this.coLocationGroups) + .build(); + } + + static class Factory implements SlotSharingStrategy.Factory { + + public TaskBalancedPreferredSlotSharingStrategy create( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + + return new TaskBalancedPreferredSlotSharingStrategy( + topology, slotSharingGroups, coLocationGroups); + } + } + + /** SlotSharingGroupBuilder class for balanced scheduling strategy. */ + private static class TaskBalancedExecutionSlotSharingGroupBuilder { + + private final SchedulingTopology topology; + + private final Map<JobVertexID, SlotSharingGroup> slotSharingGroupMap; + + /** Record the {@link ExecutionSlotSharingGroup}s for {@link SlotSharingGroup}s. */ + private final Map<SlotSharingGroup, List<ExecutionSlotSharingGroup>> + paralleledExecutionSlotSharingGroupsMap; + + /** + * Record the next round-robin {@link ExecutionSlotSharingGroup} index for {@link + * SlotSharingGroup}s. + */ + private final Map<SlotSharingGroup, Integer> slotSharingGroupIndexMap; + + private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> + executionSlotSharingGroupMap; + + private final Map<JobVertexID, CoLocationGroup> coLocationGroupMap; + + private final Map<CoLocationConstraint, ExecutionSlotSharingGroup> + constraintToExecutionSlotSharingGroupMap; + + private TaskBalancedExecutionSlotSharingGroupBuilder( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + this.topology = checkNotNull(topology); + + this.coLocationGroupMap = new HashMap<>(); + for (CoLocationGroup coLocationGroup : coLocationGroups) { + for (JobVertexID jobVertexId : coLocationGroup.getVertexIds()) { + coLocationGroupMap.put(jobVertexId, coLocationGroup); + } + } + + this.constraintToExecutionSlotSharingGroupMap = new HashMap<>(); + this.paralleledExecutionSlotSharingGroupsMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupIndexMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupMap = new HashMap<>(); + this.executionSlotSharingGroupMap = new HashMap<>(); + + for (SlotSharingGroup slotSharingGroup : slotSharingGroups) { + for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) { + slotSharingGroupMap.put(jobVertexId, slotSharingGroup); + } + } + } + + private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() { + + final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = + getExecutionVertices(topology); + + initParalleledExecutionSlotSharingGroupsMap(allVertices); + + // Loop on job vertices + for (Map.Entry<JobVertexID, List<SchedulingExecutionVertex>> executionVertexInfos : + allVertices.entrySet()) { + + JobVertexID jobVertexID = executionVertexInfos.getKey(); + List<SchedulingExecutionVertex> executionVertices = executionVertexInfos.getValue(); + final int jobVertexParallel = executionVertices.size(); + final SlotSharingGroup slotSharingGroup = slotSharingGroupMap.get(jobVertexID); + final int index = getSlotRoundRobinIndex(jobVertexParallel, slotSharingGroup); + + if (!coLocationGroupMap.containsKey(jobVertexID)) { + // For vertices without CoLocationConstraint. + allocateNonCoLocatedVertices(slotSharingGroup, executionVertices, index); + } else { + // For vertices with CoLocationConstraint. + allocateCoLocatedVertices(slotSharingGroup, executionVertices, index); + } + } + return executionSlotSharingGroupMap; + } + + private void initParalleledExecutionSlotSharingGroupsMap( + final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices) { + + allVertices.entrySet().stream() + .map( + jobVertexExecutionVertices -> + Tuple2.of( + slotSharingGroupMap.get( + jobVertexExecutionVertices.getKey()), + jobVertexExecutionVertices.getValue().size())) + .collect( + Collectors.groupingBy( + tuple -> tuple.f0, + Collectors.summarizingInt(tuple -> tuple.f1))) + .forEach( + (slotSharingGroup, statistics) -> { + int slotNum = statistics.getMax(); + paralleledExecutionSlotSharingGroupsMap.put( + slotSharingGroup, + createExecutionSlotSharingGroups( + slotSharingGroup, slotNum)); + }); + } + + private List<ExecutionSlotSharingGroup> createExecutionSlotSharingGroups( + SlotSharingGroup slotSharingGroup, int slotNum) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + new ArrayList<>(slotNum); + for (int i = 0; i < slotNum; i++) { + final ExecutionSlotSharingGroup executionSlotSharingGroup = + new ExecutionSlotSharingGroup(slotSharingGroup); + executionSlotSharingGroups.add(i, executionSlotSharingGroup); + LOG.debug( + "Create {}th executionSlotSharingGroup {}.", i, executionSlotSharingGroup); + } + return executionSlotSharingGroups; + } + + private void allocateCoLocatedVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final int slotNum = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup).size(); + final int jobVertexParallel = executionVertices.size(); + if (slotNum == jobVertexParallel) { + // For CoLocations with max parallelism of the slot sharing group. + allocateCoLocatedMaxParallelVertices(slotSharingGroup, executionVertices, index); + } else { + // For CoLocations with non-max parallelism of the slot sharing group. + allocateCoLocatedNonMaxParallelVertices(slotSharingGroup, executionVertices); + } + } + + private void allocateCoLocatedMaxParallelVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final CoLocationConstraint coLocationConstraint = + getCoLocationConstraint(executionVertex); + ExecutionSlotSharingGroup executionSlotSharingGroup = + constraintToExecutionSlotSharingGroupMap.get(coLocationConstraint); + if (Objects.isNull(executionSlotSharingGroup)) { + executionSlotSharingGroup = executionSlotSharingGroups.get(index); + } + addVertexToExecutionSlotSharingGroup(executionSlotSharingGroup, executionVertex); + index = ++index % executionSlotSharingGroups.size(); + } + updateConstraintToExecutionSlotSharingGroupMap(executionVertices); + } + + private void allocateCoLocatedNonMaxParallelVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices) { + List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + int index; + // For CoLocations with non-max parallelism of the slot sharing group. + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final CoLocationConstraint coLocationConstraint = + getCoLocationConstraint(executionVertex); + ExecutionSlotSharingGroup executionSlotSharingGroup = + constraintToExecutionSlotSharingGroupMap.get(coLocationConstraint); + if (Objects.isNull(executionSlotSharingGroup)) { + index = getFittestSlotIndex(executionSlotSharingGroups, executionVertex); + executionSlotSharingGroup = executionSlotSharingGroups.get(index); + } + addVertexToExecutionSlotSharingGroup(executionSlotSharingGroup, executionVertex); + } + updateConstraintToExecutionSlotSharingGroupMap(executionVertices); + index = getFittestSlotIndex(executionSlotSharingGroups, null); + final int jobVertexParallel = executionVertices.size(); + updateSlotRoundRobinIndexIfNeeded(jobVertexParallel, slotSharingGroup, index); + } + + private void allocateNonCoLocatedVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + for (SchedulingExecutionVertex executionVertex : executionVertices) { + addVertexToExecutionSlotSharingGroup( + executionSlotSharingGroups.get(index), executionVertex); + index = ++index % executionSlotSharingGroups.size(); + } + updateSlotRoundRobinIndexIfNeeded(executionVertices.size(), slotSharingGroup, index); + } + + private void addVertexToExecutionSlotSharingGroup( + ExecutionSlotSharingGroup executionSlotSharingGroup, + SchedulingExecutionVertex executionVertex) { + final ExecutionVertexID executionVertexId = executionVertex.getId(); + executionSlotSharingGroup.addVertex(executionVertexId); + executionSlotSharingGroupMap.put(executionVertexId, executionSlotSharingGroup); + } + + private void updateConstraintToExecutionSlotSharingGroupMap( + final List<SchedulingExecutionVertex> executionVertices) { + + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final ExecutionVertexID executionVertexId = executionVertex.getId(); + final CoLocationGroup coLocationGroup = + coLocationGroupMap.get(executionVertexId.getJobVertexId()); + if (Objects.nonNull(coLocationGroup)) { + final CoLocationConstraint constraint = + getCoLocationConstraint(executionVertex); + + constraintToExecutionSlotSharingGroupMap.put( + constraint, executionSlotSharingGroupMap.get(executionVertexId)); + } + } + } + + private CoLocationConstraint getCoLocationConstraint(SchedulingExecutionVertex sev) { + final JobVertexID jobVertexID = sev.getId().getJobVertexId(); + final int subtaskIndex = sev.getId().getSubtaskIndex(); + return coLocationGroupMap.get(jobVertexID).getLocationConstraint(subtaskIndex); + } + + private int getSlotRoundRobinIndex( + final int jobVertexParallelism, SlotSharingGroup slotSharingGroup) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + final boolean maxParallel = jobVertexParallelism == executionSlotSharingGroups.size(); + return maxParallel ? 0 : slotSharingGroupIndexMap.getOrDefault(slotSharingGroup, 0); + } + + private int getFittestSlotIndex( Review Comment: ```suggestion private int getLeastUtilizeSlotIndex( ``` ########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategyTest.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupImpl; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingTopology; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; +import org.apache.flink.shaded.guava31.com.google.common.collect.Sets; + +import org.assertj.core.data.Offset; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link TaskBalancedPreferredSlotSharingStrategy}. */ +class TaskBalancedPreferredSlotSharingStrategyTest { + + @Test + void testCoLocationConstraintIsRespected() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + List<Tuple2<JobVertexID, List<TestingSchedulingExecutionVertex>>> jobVertexInfos = + new ArrayList<>(); + SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + CoLocationGroup coLocationGroup1 = new CoLocationGroupImpl(); + CoLocationGroup coLocationGroup2 = new CoLocationGroupImpl(); + List<TestingJobVertexInfo> mockedJobVertices = + getMockedJobVertices(slotSharingGroup1, coLocationGroup1, coLocationGroup2); + setupCase(mockedJobVertices, topology, jobVertexInfos); + + final SlotSharingStrategy strategy = + new TaskBalancedPreferredSlotSharingStrategy( + topology, + Sets.newHashSet(slotSharingGroup1), + Sets.newHashSet(coLocationGroup1, coLocationGroup2)); + List<TestingSchedulingExecutionVertex> executionVertices1 = jobVertexInfos.get(1).f1; + List<TestingSchedulingExecutionVertex> executionVertices2 = jobVertexInfos.get(2).f1; + TestingSchedulingExecutionVertex executionVertexOfJv0 = jobVertexInfos.get(0).f1.get(0); + ExecutionSlotSharingGroup executionSlotSharingGroupOfExecutionVertexOfJv0 = + strategy.getExecutionSlotSharingGroup(executionVertexOfJv0.getId()); + + assertThat(executionVertices1).hasSameSizeAs(executionVertices2); + for (int i = 0; i < executionVertices1.size(); i++) { + ExecutionSlotSharingGroup executionSlotSharingGroup = + strategy.getExecutionSlotSharingGroup(executionVertices1.get(i).getId()); + assertThat(executionSlotSharingGroup) + .isNotEqualTo(executionSlotSharingGroupOfExecutionVertexOfJv0); + assertThat(executionSlotSharingGroup) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices2.get(i).getId())); + } + + List<TestingSchedulingExecutionVertex> executionVertices3 = jobVertexInfos.get(3).f1; + List<TestingSchedulingExecutionVertex> executionVertices4 = jobVertexInfos.get(4).f1; Review Comment: assertThat(executionVertices3).hasSameSizeAs(executionVertices4); ########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategyTest.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupImpl; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingTopology; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; +import org.apache.flink.shaded.guava31.com.google.common.collect.Sets; + +import org.assertj.core.data.Offset; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link TaskBalancedPreferredSlotSharingStrategy}. */ +class TaskBalancedPreferredSlotSharingStrategyTest { + + @Test + void testCoLocationConstraintIsRespected() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + List<Tuple2<JobVertexID, List<TestingSchedulingExecutionVertex>>> jobVertexInfos = + new ArrayList<>(); + SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + CoLocationGroup coLocationGroup1 = new CoLocationGroupImpl(); + CoLocationGroup coLocationGroup2 = new CoLocationGroupImpl(); + List<TestingJobVertexInfo> mockedJobVertices = + getMockedJobVertices(slotSharingGroup1, coLocationGroup1, coLocationGroup2); + setupCase(mockedJobVertices, topology, jobVertexInfos); + + final SlotSharingStrategy strategy = + new TaskBalancedPreferredSlotSharingStrategy( + topology, + Sets.newHashSet(slotSharingGroup1), + Sets.newHashSet(coLocationGroup1, coLocationGroup2)); + List<TestingSchedulingExecutionVertex> executionVertices1 = jobVertexInfos.get(1).f1; + List<TestingSchedulingExecutionVertex> executionVertices2 = jobVertexInfos.get(2).f1; + TestingSchedulingExecutionVertex executionVertexOfJv0 = jobVertexInfos.get(0).f1.get(0); + ExecutionSlotSharingGroup executionSlotSharingGroupOfExecutionVertexOfJv0 = + strategy.getExecutionSlotSharingGroup(executionVertexOfJv0.getId()); + + assertThat(executionVertices1).hasSameSizeAs(executionVertices2); + for (int i = 0; i < executionVertices1.size(); i++) { + ExecutionSlotSharingGroup executionSlotSharingGroup = + strategy.getExecutionSlotSharingGroup(executionVertices1.get(i).getId()); + assertThat(executionSlotSharingGroup) + .isNotEqualTo(executionSlotSharingGroupOfExecutionVertexOfJv0); + assertThat(executionSlotSharingGroup) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices2.get(i).getId())); + } + + List<TestingSchedulingExecutionVertex> executionVertices3 = jobVertexInfos.get(3).f1; + List<TestingSchedulingExecutionVertex> executionVertices4 = jobVertexInfos.get(4).f1; + for (int i = 0; i < executionVertices1.size(); i++) { + assertThat(strategy.getExecutionSlotSharingGroup(executionVertices3.get(i).getId())) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices4.get(i).getId())); + } + } + + @Nonnull + private static ArrayList<TestingJobVertexInfo> getMockedJobVertices( Review Comment: If it is only used in one test, no need to split them. ########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategyTest.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupImpl; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingTopology; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; +import org.apache.flink.shaded.guava31.com.google.common.collect.Sets; + +import org.assertj.core.data.Offset; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link TaskBalancedPreferredSlotSharingStrategy}. */ +class TaskBalancedPreferredSlotSharingStrategyTest { + + @Test + void testCoLocationConstraintIsRespected() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + List<Tuple2<JobVertexID, List<TestingSchedulingExecutionVertex>>> jobVertexInfos = + new ArrayList<>(); + SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + CoLocationGroup coLocationGroup1 = new CoLocationGroupImpl(); + CoLocationGroup coLocationGroup2 = new CoLocationGroupImpl(); + List<TestingJobVertexInfo> mockedJobVertices = + getMockedJobVertices(slotSharingGroup1, coLocationGroup1, coLocationGroup2); + setupCase(mockedJobVertices, topology, jobVertexInfos); + + final SlotSharingStrategy strategy = + new TaskBalancedPreferredSlotSharingStrategy( + topology, + Sets.newHashSet(slotSharingGroup1), + Sets.newHashSet(coLocationGroup1, coLocationGroup2)); + List<TestingSchedulingExecutionVertex> executionVertices1 = jobVertexInfos.get(1).f1; + List<TestingSchedulingExecutionVertex> executionVertices2 = jobVertexInfos.get(2).f1; + TestingSchedulingExecutionVertex executionVertexOfJv0 = jobVertexInfos.get(0).f1.get(0); + ExecutionSlotSharingGroup executionSlotSharingGroupOfExecutionVertexOfJv0 = + strategy.getExecutionSlotSharingGroup(executionVertexOfJv0.getId()); + + assertThat(executionVertices1).hasSameSizeAs(executionVertices2); + for (int i = 0; i < executionVertices1.size(); i++) { + ExecutionSlotSharingGroup executionSlotSharingGroup = + strategy.getExecutionSlotSharingGroup(executionVertices1.get(i).getId()); + assertThat(executionSlotSharingGroup) + .isNotEqualTo(executionSlotSharingGroupOfExecutionVertexOfJv0); + assertThat(executionSlotSharingGroup) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices2.get(i).getId())); + } + + List<TestingSchedulingExecutionVertex> executionVertices3 = jobVertexInfos.get(3).f1; + List<TestingSchedulingExecutionVertex> executionVertices4 = jobVertexInfos.get(4).f1; + for (int i = 0; i < executionVertices1.size(); i++) { Review Comment: ```suggestion for (int i = 0; i < executionVertices3.size(); i++) { ``` ########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategyTest.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupImpl; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.TestingSchedulingTopology; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; +import org.apache.flink.shaded.guava31.com.google.common.collect.Sets; + +import org.assertj.core.data.Offset; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link TaskBalancedPreferredSlotSharingStrategy}. */ +class TaskBalancedPreferredSlotSharingStrategyTest { + + @Test + void testCoLocationConstraintIsRespected() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + List<Tuple2<JobVertexID, List<TestingSchedulingExecutionVertex>>> jobVertexInfos = + new ArrayList<>(); + SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + CoLocationGroup coLocationGroup1 = new CoLocationGroupImpl(); + CoLocationGroup coLocationGroup2 = new CoLocationGroupImpl(); + List<TestingJobVertexInfo> mockedJobVertices = + getMockedJobVertices(slotSharingGroup1, coLocationGroup1, coLocationGroup2); + setupCase(mockedJobVertices, topology, jobVertexInfos); + + final SlotSharingStrategy strategy = + new TaskBalancedPreferredSlotSharingStrategy( + topology, + Sets.newHashSet(slotSharingGroup1), + Sets.newHashSet(coLocationGroup1, coLocationGroup2)); + List<TestingSchedulingExecutionVertex> executionVertices1 = jobVertexInfos.get(1).f1; + List<TestingSchedulingExecutionVertex> executionVertices2 = jobVertexInfos.get(2).f1; + TestingSchedulingExecutionVertex executionVertexOfJv0 = jobVertexInfos.get(0).f1.get(0); + ExecutionSlotSharingGroup executionSlotSharingGroupOfExecutionVertexOfJv0 = + strategy.getExecutionSlotSharingGroup(executionVertexOfJv0.getId()); + + assertThat(executionVertices1).hasSameSizeAs(executionVertices2); + for (int i = 0; i < executionVertices1.size(); i++) { + ExecutionSlotSharingGroup executionSlotSharingGroup = + strategy.getExecutionSlotSharingGroup(executionVertices1.get(i).getId()); + assertThat(executionSlotSharingGroup) + .isNotEqualTo(executionSlotSharingGroupOfExecutionVertexOfJv0); + assertThat(executionSlotSharingGroup) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices2.get(i).getId())); + } + + List<TestingSchedulingExecutionVertex> executionVertices3 = jobVertexInfos.get(3).f1; + List<TestingSchedulingExecutionVertex> executionVertices4 = jobVertexInfos.get(4).f1; + for (int i = 0; i < executionVertices1.size(); i++) { + assertThat(strategy.getExecutionSlotSharingGroup(executionVertices3.get(i).getId())) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices4.get(i).getId())); + } + } + + @Nonnull + private static ArrayList<TestingJobVertexInfo> getMockedJobVertices( + SlotSharingGroup slotSharingGroup, + CoLocationGroup coLocationGroup1, + CoLocationGroup coLocationGroup2) { + return Lists.newArrayList( + new TestingJobVertexInfo(1, slotSharingGroup, null), + new TestingJobVertexInfo(2, slotSharingGroup, coLocationGroup1), + new TestingJobVertexInfo(2, slotSharingGroup, coLocationGroup1), + new TestingJobVertexInfo(3, slotSharingGroup, coLocationGroup2), + new TestingJobVertexInfo(3, slotSharingGroup, coLocationGroup2)); + } + + @Test + void testVerticesInDifferentSlotSharingGroups() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + List<Tuple2<JobVertexID, List<TestingSchedulingExecutionVertex>>> jobVertexInfos = + new ArrayList<>(); + SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + SlotSharingGroup slotSharingGroup2 = new SlotSharingGroup(); + List<TestingJobVertexInfo> testingJobVertexInfos = + Lists.newArrayList( + new TestingJobVertexInfo(1, slotSharingGroup1, null), + new TestingJobVertexInfo(2, slotSharingGroup1, null), + new TestingJobVertexInfo(3, slotSharingGroup1, null), + new TestingJobVertexInfo(1, slotSharingGroup2, null), + new TestingJobVertexInfo(2, slotSharingGroup2, null), + new TestingJobVertexInfo(2, slotSharingGroup2, null)); + + setupCase(testingJobVertexInfos, topology, jobVertexInfos); + + final SlotSharingStrategy strategy = + new TaskBalancedPreferredSlotSharingStrategy( + topology, + Sets.newHashSet(slotSharingGroup1, slotSharingGroup2), + Collections.emptySet()); + assertThat(strategy.getExecutionSlotSharingGroups()).hasSize(5); + checkBalanceAtSlotsLevelWithoutCoLocation(strategy); + + List<TestingSchedulingExecutionVertex> executionVertices4 = jobVertexInfos.get(4).f1; + List<TestingSchedulingExecutionVertex> executionVertices5 = jobVertexInfos.get(5).f1; + assertThat(executionVertices4).hasSameSizeAs(executionVertices5); + // Check for JVs whose parallelism is the max in the same slot sharing group. + for (int i = 0; i < executionVertices4.size(); i++) { + TestingSchedulingExecutionVertex executionVertex4 = executionVertices4.get(i); + assertThat(strategy.getExecutionSlotSharingGroup(executionVertex4.getId())) + .isEqualTo( + strategy.getExecutionSlotSharingGroup( + executionVertices5.get(i).getId())); + } + } + + private static void checkBalanceAtSlotsLevelWithoutCoLocation(SlotSharingStrategy strategy) { + strategy.getExecutionSlotSharingGroups().stream() + .collect(Collectors.groupingBy(ExecutionSlotSharingGroup::getSlotSharingGroup)) + .forEach( + (slotSharingGroup, executionSlotSharingGroups) -> { + Optional<Integer> max = + executionSlotSharingGroups.stream() + .map( + executionSlotSharingGroup -> + executionSlotSharingGroup + .getExecutionVertexIds() + .size()) + .max(Comparator.comparing(i -> i)); + Optional<Integer> min = + executionSlotSharingGroups.stream() + .map( + executionSlotSharingGroup -> + executionSlotSharingGroup + .getExecutionVertexIds() + .size()) + .min(Comparator.comparing(i -> i)); + assertThat(max.get()).isCloseTo(min.get(), Offset.offset(1)); + }); + } + + @Test + void testSetSlotSharingGroupResource() { + TestingSchedulingTopology topology = new TestingSchedulingTopology(); + final JobVertexID jobVertexId1 = new JobVertexID(); + final JobVertexID jobVertexId2 = new JobVertexID(); + final TestingSchedulingExecutionVertex ev10 = topology.newExecutionVertex(jobVertexId1, 0); + final TestingSchedulingExecutionVertex ev11 = topology.newExecutionVertex(jobVertexId1, 1); + final TestingSchedulingExecutionVertex ev20 = topology.newExecutionVertex(jobVertexId2, 0); + + final SlotSharingGroup slotSharingGroup1 = new SlotSharingGroup(); + final ResourceProfile resourceProfile1 = ResourceProfile.fromResources(1, 10); + slotSharingGroup1.addVertexToGroup(jobVertexId1); + slotSharingGroup1.setResourceProfile(resourceProfile1); + + final SlotSharingGroup slotSharingGroup2 = new SlotSharingGroup(); + final ResourceProfile resourceProfile2 = ResourceProfile.fromResources(2, 20); + slotSharingGroup2.addVertexToGroup(jobVertexId2); + slotSharingGroup2.setResourceProfile(resourceProfile2); + + final Set<SlotSharingGroup> slotSharingGroups = + Sets.newHashSet(slotSharingGroup1, slotSharingGroup2); + + final SlotSharingStrategy strategy = + new LocalInputPreferredSlotSharingStrategy( Review Comment: ```suggestion new TaskBalancedPreferredSlotSharingStrategy( ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/TaskBalancedPreferredSlotSharingStrategy.java: ########## @@ -0,0 +1,361 @@ +/* + * 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.java.tuple.Tuple2; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.scheduler.strategy.SchedulingExecutionVertex; +import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * This strategy tries to get a balanced tasks scheduling. Execution vertices, which are belong to + * the same SlotSharingGroup, tend to be put evenly in each ExecutionSlotSharingGroup. Co-location + * constraints will be respected. + */ +class TaskBalancedPreferredSlotSharingStrategy extends AbstractSlotSharingStrategy { + + public static final Logger LOG = + LoggerFactory.getLogger(TaskBalancedPreferredSlotSharingStrategy.class); + + TaskBalancedPreferredSlotSharingStrategy( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + super(topology, slotSharingGroups, coLocationGroups); + } + + @Override + protected Map<ExecutionVertexID, ExecutionSlotSharingGroup> computeExecutionSlotSharingGroups( + SchedulingTopology schedulingTopology) { + return new TaskBalancedExecutionSlotSharingGroupBuilder( + schedulingTopology, this.logicalSlotSharingGroups, this.coLocationGroups) + .build(); + } + + static class Factory implements SlotSharingStrategy.Factory { + + public TaskBalancedPreferredSlotSharingStrategy create( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + + return new TaskBalancedPreferredSlotSharingStrategy( + topology, slotSharingGroups, coLocationGroups); + } + } + + /** SlotSharingGroupBuilder class for balanced scheduling strategy. */ + private static class TaskBalancedExecutionSlotSharingGroupBuilder { + + private final SchedulingTopology topology; + + private final Map<JobVertexID, SlotSharingGroup> slotSharingGroupMap; + + /** Record the {@link ExecutionSlotSharingGroup}s for {@link SlotSharingGroup}s. */ + private final Map<SlotSharingGroup, List<ExecutionSlotSharingGroup>> + paralleledExecutionSlotSharingGroupsMap; + + /** + * Record the next round-robin {@link ExecutionSlotSharingGroup} index for {@link + * SlotSharingGroup}s. + */ + private final Map<SlotSharingGroup, Integer> slotSharingGroupIndexMap; + + private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> + executionSlotSharingGroupMap; + + private final Map<JobVertexID, CoLocationGroup> coLocationGroupMap; + + private final Map<CoLocationConstraint, ExecutionSlotSharingGroup> + constraintToExecutionSlotSharingGroupMap; + + private TaskBalancedExecutionSlotSharingGroupBuilder( + final SchedulingTopology topology, + final Set<SlotSharingGroup> slotSharingGroups, + final Set<CoLocationGroup> coLocationGroups) { + this.topology = checkNotNull(topology); + + this.coLocationGroupMap = new HashMap<>(); + for (CoLocationGroup coLocationGroup : coLocationGroups) { + for (JobVertexID jobVertexId : coLocationGroup.getVertexIds()) { + coLocationGroupMap.put(jobVertexId, coLocationGroup); + } + } + + this.constraintToExecutionSlotSharingGroupMap = new HashMap<>(); + this.paralleledExecutionSlotSharingGroupsMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupIndexMap = new HashMap<>(slotSharingGroups.size()); + this.slotSharingGroupMap = new HashMap<>(); + this.executionSlotSharingGroupMap = new HashMap<>(); + + for (SlotSharingGroup slotSharingGroup : slotSharingGroups) { + for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) { + slotSharingGroupMap.put(jobVertexId, slotSharingGroup); + } + } + } + + private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() { + + final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = + getExecutionVertices(topology); + + initParalleledExecutionSlotSharingGroupsMap(allVertices); + + // Loop on job vertices + for (Map.Entry<JobVertexID, List<SchedulingExecutionVertex>> executionVertexInfos : + allVertices.entrySet()) { + + JobVertexID jobVertexID = executionVertexInfos.getKey(); + List<SchedulingExecutionVertex> executionVertices = executionVertexInfos.getValue(); + final int jobVertexParallel = executionVertices.size(); + final SlotSharingGroup slotSharingGroup = slotSharingGroupMap.get(jobVertexID); + final int index = getSlotRoundRobinIndex(jobVertexParallel, slotSharingGroup); + + if (!coLocationGroupMap.containsKey(jobVertexID)) { + // For vertices without CoLocationConstraint. + allocateNonCoLocatedVertices(slotSharingGroup, executionVertices, index); + } else { + // For vertices with CoLocationConstraint. + allocateCoLocatedVertices(slotSharingGroup, executionVertices, index); + } + } + return executionSlotSharingGroupMap; + } + + private void initParalleledExecutionSlotSharingGroupsMap( + final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices) { + + allVertices.entrySet().stream() + .map( + jobVertexExecutionVertices -> + Tuple2.of( + slotSharingGroupMap.get( + jobVertexExecutionVertices.getKey()), + jobVertexExecutionVertices.getValue().size())) + .collect( + Collectors.groupingBy( + tuple -> tuple.f0, + Collectors.summarizingInt(tuple -> tuple.f1))) + .forEach( + (slotSharingGroup, statistics) -> { + int slotNum = statistics.getMax(); + paralleledExecutionSlotSharingGroupsMap.put( + slotSharingGroup, + createExecutionSlotSharingGroups( + slotSharingGroup, slotNum)); + }); + } + + private List<ExecutionSlotSharingGroup> createExecutionSlotSharingGroups( + SlotSharingGroup slotSharingGroup, int slotNum) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + new ArrayList<>(slotNum); + for (int i = 0; i < slotNum; i++) { + final ExecutionSlotSharingGroup executionSlotSharingGroup = + new ExecutionSlotSharingGroup(slotSharingGroup); + executionSlotSharingGroups.add(i, executionSlotSharingGroup); + LOG.debug( + "Create {}th executionSlotSharingGroup {}.", i, executionSlotSharingGroup); + } + return executionSlotSharingGroups; + } + + private void allocateCoLocatedVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final int slotNum = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup).size(); + final int jobVertexParallel = executionVertices.size(); + if (slotNum == jobVertexParallel) { + // For CoLocations with max parallelism of the slot sharing group. + allocateCoLocatedMaxParallelVertices(slotSharingGroup, executionVertices, index); + } else { + // For CoLocations with non-max parallelism of the slot sharing group. + allocateCoLocatedNonMaxParallelVertices(slotSharingGroup, executionVertices); + } + } + + private void allocateCoLocatedMaxParallelVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final CoLocationConstraint coLocationConstraint = + getCoLocationConstraint(executionVertex); + ExecutionSlotSharingGroup executionSlotSharingGroup = + constraintToExecutionSlotSharingGroupMap.get(coLocationConstraint); + if (Objects.isNull(executionSlotSharingGroup)) { + executionSlotSharingGroup = executionSlotSharingGroups.get(index); + } + addVertexToExecutionSlotSharingGroup(executionSlotSharingGroup, executionVertex); + index = ++index % executionSlotSharingGroups.size(); + } + updateConstraintToExecutionSlotSharingGroupMap(executionVertices); + } + + private void allocateCoLocatedNonMaxParallelVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices) { + List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + int index; + // For CoLocations with non-max parallelism of the slot sharing group. + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final CoLocationConstraint coLocationConstraint = + getCoLocationConstraint(executionVertex); + ExecutionSlotSharingGroup executionSlotSharingGroup = + constraintToExecutionSlotSharingGroupMap.get(coLocationConstraint); + if (Objects.isNull(executionSlotSharingGroup)) { + index = getFittestSlotIndex(executionSlotSharingGroups, executionVertex); + executionSlotSharingGroup = executionSlotSharingGroups.get(index); + } + addVertexToExecutionSlotSharingGroup(executionSlotSharingGroup, executionVertex); + } + updateConstraintToExecutionSlotSharingGroupMap(executionVertices); + index = getFittestSlotIndex(executionSlotSharingGroups, null); + final int jobVertexParallel = executionVertices.size(); + updateSlotRoundRobinIndexIfNeeded(jobVertexParallel, slotSharingGroup, index); + } + + private void allocateNonCoLocatedVertices( + SlotSharingGroup slotSharingGroup, + List<SchedulingExecutionVertex> executionVertices, + int index) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + for (SchedulingExecutionVertex executionVertex : executionVertices) { + addVertexToExecutionSlotSharingGroup( + executionSlotSharingGroups.get(index), executionVertex); + index = ++index % executionSlotSharingGroups.size(); + } + updateSlotRoundRobinIndexIfNeeded(executionVertices.size(), slotSharingGroup, index); + } + + private void addVertexToExecutionSlotSharingGroup( + ExecutionSlotSharingGroup executionSlotSharingGroup, + SchedulingExecutionVertex executionVertex) { + final ExecutionVertexID executionVertexId = executionVertex.getId(); + executionSlotSharingGroup.addVertex(executionVertexId); + executionSlotSharingGroupMap.put(executionVertexId, executionSlotSharingGroup); + } + + private void updateConstraintToExecutionSlotSharingGroupMap( + final List<SchedulingExecutionVertex> executionVertices) { + + for (SchedulingExecutionVertex executionVertex : executionVertices) { + final ExecutionVertexID executionVertexId = executionVertex.getId(); + final CoLocationGroup coLocationGroup = + coLocationGroupMap.get(executionVertexId.getJobVertexId()); + if (Objects.nonNull(coLocationGroup)) { + final CoLocationConstraint constraint = + getCoLocationConstraint(executionVertex); + + constraintToExecutionSlotSharingGroupMap.put( + constraint, executionSlotSharingGroupMap.get(executionVertexId)); + } + } + } + + private CoLocationConstraint getCoLocationConstraint(SchedulingExecutionVertex sev) { + final JobVertexID jobVertexID = sev.getId().getJobVertexId(); + final int subtaskIndex = sev.getId().getSubtaskIndex(); + return coLocationGroupMap.get(jobVertexID).getLocationConstraint(subtaskIndex); + } + + private int getSlotRoundRobinIndex( + final int jobVertexParallelism, SlotSharingGroup slotSharingGroup) { + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups = + paralleledExecutionSlotSharingGroupsMap.get(slotSharingGroup); + final boolean maxParallel = jobVertexParallelism == executionSlotSharingGroups.size(); + return maxParallel ? 0 : slotSharingGroupIndexMap.getOrDefault(slotSharingGroup, 0); + } + + private int getFittestSlotIndex( + final List<ExecutionSlotSharingGroup> executionSlotSharingGroups, + final @Nullable SchedulingExecutionVertex executionVertex) { + int indexWithLeastExecutionVertices = 0; + int leastExecutionVertices = Integer.MAX_VALUE; + for (int index = 0; index < executionSlotSharingGroups.size(); index++) { + final ExecutionSlotSharingGroup executionSlotSharingGroup = + executionSlotSharingGroups.get(index); + final int executionVertices = + executionSlotSharingGroup.getExecutionVertexIds().size(); + if (leastExecutionVertices > executionVertices + && allocatable(executionSlotSharingGroup, executionVertex)) { Review Comment: ```suggestion && (Objects.isNull(executionVertex) || allocatable(executionSlotSharingGroup, executionVertex))) { ``` -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org