You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/07/29 07:18:28 UTC

[GitHub] [flink] zhuzhurk opened a new pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

zhuzhurk opened a new pull request #13009:
URL: https://github.com/apache/flink/pull/13009


   
   ## What is the purpose of the change
   
   This PR introduces ExecutionSlotSharingGroup, SlotSharingStrategy and implements LocalInputPreferredSlotSharingStrategy.
   
   The default SlotSharingStrategy would be LocalInputPreferredSlotSharingStrategy. It will try to reduce remote data exchanges. Subtasks, which are connected and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
   
   ## Brief change log
   
     - Introduce ExecutionSlotSharingGroup and SlotSharingStrategy interface
     - Implement LocalInputPreferredSlotSharingStrategy
   
   
   ## Verifying this change
   
     - *Added unit tests for LocalInputPreferredSlotSharingStrategy*
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (yes / **no**)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / **no**)
     - The serializers: (yes / **no** / don't know)
     - The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (**yes** / no / don't know)
     - The S3 file system connector: (yes / **no** / don't know)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / **no**)
     - If yes, how is the feature documented? (**not applicable** / docs / JavaDocs / not documented)
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r465137414



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();

Review comment:
       thanks for confirmation




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 044578bc01da8b57e29a483ee2560a0e8f61148a Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971) 
   * d7133401edf323e5be894758398098e70cf70506 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464556118



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);
+		}
+
+		private boolean isGroupAvailableForVertex(
+				final ExecutionSlotSharingGroup executionSlotSharingGroup,
+				final ExecutionVertexID executionVertexId) {
+
+			final Set<JobVertexID> assignedVertices = assignedJobVerticesForGroups.get(executionSlotSharingGroup);
+			return assignedVertices == null || !assignedVertices.contains(executionVertexId.getJobVertexId());
+		}
+
+		private void addVertexToExecutionSlotSharingGroup(
+				final SchedulingExecutionVertex vertex,
+				final ExecutionSlotSharingGroup group) {
+
+			group.addVertex(vertex.getId());
+			executionSlotSharingGroupMap.put(vertex.getId(), group);
+			assignedJobVerticesForGroups.computeIfAbsent(group, k -> new HashSet<>()).add(vertex.getId().getJobVertexId());
+		}
+
+		private void findAvailableOrCreateNewExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				// create a new slotSharingGroupId if the vertex is not in a slot sharing group

Review comment:
       Done by introducing `getSlotSharingGroupId(...)` which checks the slot sharing group to be non-null.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     }, {
       "hash" : "557e982407c488191ade7cc66807662796b3a306",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5148",
       "triggerID" : "557e982407c488191ade7cc66807662796b3a306",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 557e982407c488191ade7cc66807662796b3a306 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5148) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r462855642



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/SlotSharingStrategy.java
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.Set;
+
+/**
+ * Strategy which determines {@link ExecutionSlotSharingGroup} for each execution vertex.
+ */
+interface SlotSharingStrategy {
+
+	ExecutionSlotSharingGroup getExecutionSlotSharingGroup(
+		ExecutionVertexID executionVertexId);
+
+	Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups();
+
+	interface Factory {

Review comment:
       ```suggestion
       @FunctionalInterface
   	interface Factory {
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmanager/scheduler/CoLocationConstraintDesc.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.jobmanager.scheduler;
+
+import org.apache.flink.util.AbstractID;
+
+import java.util.Objects;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A read-only and light weight version of {@link CoLocationConstraint}.
+ */
+public class CoLocationConstraintDesc {
+
+	private final AbstractID coLocationGroupId;
+
+	private final int constraintIndex;
+
+	public CoLocationConstraintDesc(final AbstractID coLocationGroupId, final int constraintIndex) {

Review comment:
       ```suggestion
   	CoLocationConstraintDesc(final AbstractID coLocationGroupId, final int constraintIndex) {
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmanager/scheduler/CoLocationGroupDesc.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.jobmanager.scheduler;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.util.AbstractID;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A read-only and light weight version of {@link CoLocationGroup}.
+ */
+public class CoLocationGroupDesc {
+
+	private final AbstractID id;
+
+	private final List<JobVertexID> vertices;
+
+	private CoLocationGroupDesc(final AbstractID id, final List<JobVertexID> vertices) {
+		this.id = checkNotNull(id);
+		this.vertices = checkNotNull(vertices);
+	}
+
+	public AbstractID getId() {
+		return id;
+	}
+
+	public List<JobVertexID> getVertices() {
+		return vertices;

Review comment:
       ```suggestion
   		return Collections.unmodifiableList(vertices);
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:

Review comment:
       If we do not want to do the full complicated connected graph analysis (harder to maintain), we could a lighter optimisation now or later. After the suggested traversal, we could do one more traversal where we try to move producers closer to their consumers if the producers are not already bounded by other co-located consumers or possibly by a co-location constraint. Parent producers of the producers (if parent ones have less parallelism) should not bound them for the move if the second traversal is bottom->up.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);

Review comment:
       Do you know whether we have some kind of check whether the co-location constraint does not contradict to the slot sharing group? like 2 executions must be co-located according to the co-location constraint but they are in different logical slot sharing groups (e.g. forced by user in API)?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group

Review comment:
       ```suggestion
   			// loop on job vertices so that an execution vertex will not be added into a group
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();

Review comment:
       Is `allVertices` `LinkedHashMap` because we assume that topology gives the vertexes in the topological order to assign vertexes to their producers' groups ?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);
+		}
+
+		private boolean isGroupAvailableForVertex(
+				final ExecutionSlotSharingGroup executionSlotSharingGroup,
+				final ExecutionVertexID executionVertexId) {
+
+			final Set<JobVertexID> assignedVertices = assignedJobVerticesForGroups.get(executionSlotSharingGroup);
+			return assignedVertices == null || !assignedVertices.contains(executionVertexId.getJobVertexId());
+		}
+
+		private void addVertexToExecutionSlotSharingGroup(
+				final SchedulingExecutionVertex vertex,
+				final ExecutionSlotSharingGroup group) {
+
+			group.addVertex(vertex.getId());
+			executionSlotSharingGroupMap.put(vertex.getId(), group);
+			assignedJobVerticesForGroups.computeIfAbsent(group, k -> new HashSet<>()).add(vertex.getId().getJobVertexId());
+		}
+
+		private void findAvailableOrCreateNewExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				// create a new slotSharingGroupId if the vertex is not in a slot sharing group

Review comment:
       Can this happen at this point?
   
   If I understand correctly, if a vertex does not belong to any group then it belongs to the default group according to user docs. So then the question is whether the default group already has a `slotSharingGroupId` at this point:
   - if yes, then this should not happen
   - if no, then do we risk to create a new `slotSharingGroupId` for each `executionVertex` from the default group?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);

Review comment:
       ```suggestion
   			return Objects.equals(slotSharingGroupId1, slotSharingGroupId2);
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     }, {
       "hash" : "557e982407c488191ade7cc66807662796b3a306",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5148",
       "triggerID" : "557e982407c488191ade7cc66807662796b3a306",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * d7133401edf323e5be894758398098e70cf70506 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138) 
   * 557e982407c488191ade7cc66807662796b3a306 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5148) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665106853






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464542910



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);
+		}
+
+		private boolean isGroupAvailableForVertex(
+				final ExecutionSlotSharingGroup executionSlotSharingGroup,
+				final ExecutionVertexID executionVertexId) {
+
+			final Set<JobVertexID> assignedVertices = assignedJobVerticesForGroups.get(executionSlotSharingGroup);
+			return assignedVertices == null || !assignedVertices.contains(executionVertexId.getJobVertexId());
+		}
+
+		private void addVertexToExecutionSlotSharingGroup(
+				final SchedulingExecutionVertex vertex,
+				final ExecutionSlotSharingGroup group) {
+
+			group.addVertex(vertex.getId());
+			executionSlotSharingGroupMap.put(vertex.getId(), group);
+			assignedJobVerticesForGroups.computeIfAbsent(group, k -> new HashSet<>()).add(vertex.getId().getJobVertexId());
+		}
+
+		private void findAvailableOrCreateNewExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				// create a new slotSharingGroupId if the vertex is not in a slot sharing group

Review comment:
       This should not happen in production.
   If the slot sharing group name of an operator is set to be `null` in DataStream, its `slotSharingGroup` will be decided automatically, either in its producers' slot sharing group or in the `default` slot sharing group, and no chance to be `null` in production. See `StreamGraphGenerator#determineSlotSharingGroup()`.
   
   However, in current runtime framework, `slotSharingGroup` is still treated as nullable in many places. I had once opened a ticket to propose to simply it https://issues.apache.org/jira/browse/FLINK-14870.
   
   In this case, we can simplify it by having a sanity check to ensure that a vertex can never have a `null` `slotSharingGroup`.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     }, {
       "hash" : "557e982407c488191ade7cc66807662796b3a306",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "557e982407c488191ade7cc66807662796b3a306",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * d7133401edf323e5be894758398098e70cf70506 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138) 
   * 557e982407c488191ade7cc66807662796b3a306 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r465143204



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);
+		}
+
+		private boolean isGroupAvailableForVertex(
+				final ExecutionSlotSharingGroup executionSlotSharingGroup,
+				final ExecutionVertexID executionVertexId) {
+
+			final Set<JobVertexID> assignedVertices = assignedJobVerticesForGroups.get(executionSlotSharingGroup);
+			return assignedVertices == null || !assignedVertices.contains(executionVertexId.getJobVertexId());
+		}
+
+		private void addVertexToExecutionSlotSharingGroup(
+				final SchedulingExecutionVertex vertex,
+				final ExecutionSlotSharingGroup group) {
+
+			group.addVertex(vertex.getId());
+			executionSlotSharingGroupMap.put(vertex.getId(), group);
+			assignedJobVerticesForGroups.computeIfAbsent(group, k -> new HashSet<>()).add(vertex.getId().getJobVertexId());
+		}
+
+		private void findAvailableOrCreateNewExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				// create a new slotSharingGroupId if the vertex is not in a slot sharing group

Review comment:
       Alright, makes sense




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464531434



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);

Review comment:
       There are such checks in `StreamingJobGraphGenerator#setCoLocation()` for DataStream/SQL jobs and `JobVertex#setStrictlyCoLocatedWith()` for DataSet jobs.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r463477687



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:

Review comment:
       Result of our offline discussion with @tillrohrmann:
   The described suboptimal case is not a strict regression because the existing `SlotSharingManager` can already produce this suboptimal case. The `SlotSharingStrategy` is already designed to be pluggable. Therefore, we can focus on the overall new bulk allocation implementation and optimize the simplest `SlotSharingStrategy` approach, suggested in this PR, later.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk merged pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk merged pull request #13009:
URL: https://github.com/apache/flink/pull/13009


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464533058



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);

Review comment:
       It's a bit different since I had wanted it to return false if either `slotSharingGroup` is `null`.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 044578bc01da8b57e29a483ee2560a0e8f61148a Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971) 
   * d7133401edf323e5be894758398098e70cf70506 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r465143555



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);

Review comment:
       alright, makes sense




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13009:
URL: https://github.com/apache/flink/pull/13009#issuecomment-665109698


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=4971",
       "triggerID" : "044578bc01da8b57e29a483ee2560a0e8f61148a",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d7133401edf323e5be894758398098e70cf70506",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138",
       "triggerID" : "d7133401edf323e5be894758398098e70cf70506",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * d7133401edf323e5be894758398098e70cf70506 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=5138) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r461676413



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:

Review comment:
       One known defect is the grouping of execution vertices from brother job vertices are possible suboptimal because it does not take connections to consumers vertices into account.
   
   Example: A(parallelism=4) --forward--> C(parallelism=4), B(parallelism=2) --rescale--> C
   Execution edges are: A1->C1, A2->C2, A3->C3, A4->C4; B1->C1, B1->C2, B2->C3,B2->C4
   Optimal grouping: {A1,B1,C1}{A2,C2}{A3,B2,C3}{A4,C4}     ->   there would be 2 remote edges
   Current grouping result: {A1,B1,C1} {A2,B2,C2}  {A3,C3}  {A4,C4}     ->   there would be 3 remote edges




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464528968



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();

Review comment:
       Yes. `BaseTopology#getVertices()` is defined to return vertices in topological order. And we need `LinkedHashMap` to retain this order.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] azagrebin commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
azagrebin commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r465137625



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);

Review comment:
       alright, thanks for pointing out




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464556912



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group

Review comment:
       done.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmanager/scheduler/CoLocationGroupDesc.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.jobmanager.scheduler;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.util.AbstractID;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A read-only and light weight version of {@link CoLocationGroup}.
+ */
+public class CoLocationGroupDesc {
+
+	private final AbstractID id;
+
+	private final List<JobVertexID> vertices;
+
+	private CoLocationGroupDesc(final AbstractID id, final List<JobVertexID> vertices) {
+		this.id = checkNotNull(id);
+		this.vertices = checkNotNull(vertices);
+	}
+
+	public AbstractID getId() {
+		return id;
+	}
+
+	public List<JobVertexID> getVertices() {
+		return vertices;

Review comment:
       done.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmanager/scheduler/CoLocationConstraintDesc.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.jobmanager.scheduler;
+
+import org.apache.flink.util.AbstractID;
+
+import java.util.Objects;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A read-only and light weight version of {@link CoLocationConstraint}.
+ */
+public class CoLocationConstraintDesc {
+
+	private final AbstractID coLocationGroupId;
+
+	private final int constraintIndex;
+
+	public CoLocationConstraintDesc(final AbstractID coLocationGroupId, final int constraintIndex) {

Review comment:
       done.

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/SlotSharingStrategy.java
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.Set;
+
+/**
+ * Strategy which determines {@link ExecutionSlotSharingGroup} for each execution vertex.
+ */
+interface SlotSharingStrategy {
+
+	ExecutionSlotSharingGroup getExecutionSlotSharingGroup(
+		ExecutionVertexID executionVertexId);
+
+	Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups();
+
+	interface Factory {

Review comment:
       done.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464556766



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);

Review comment:
       But given that we now assumes that `slotSharingGroup` cannot be `null`. We can do this simplification.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] zhuzhurk commented on a change in pull request #13009: [FLINK-18690][runtime] Implement LocalInputPreferredSlotSharingStrategy

Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #13009:
URL: https://github.com/apache/flink/pull/13009#discussion_r464533058



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/LocalInputPreferredSlotSharingStrategy.java
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraintDesc;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroupDesc;
+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.SchedulingResultPartition;
+import org.apache.flink.runtime.scheduler.strategy.SchedulingTopology;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * This strategy tries to reduce remote data exchanges. Execution vertices, which are connected
+ * and belong to the same SlotSharingGroup, tend to be put in the same ExecutionSlotSharingGroup.
+ * Co-location constraints will be respected.
+ */
+class LocalInputPreferredSlotSharingStrategy implements SlotSharingStrategy {
+
+	private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+	LocalInputPreferredSlotSharingStrategy(
+			final SchedulingTopology topology,
+			final Set<SlotSharingGroup> logicalSlotSharingGroups,
+			final Set<CoLocationGroupDesc> coLocationGroups) {
+
+		this.executionSlotSharingGroupMap = new ExecutionSlotSharingGroupBuilder(
+			topology,
+			logicalSlotSharingGroups,
+			coLocationGroups).build();
+	}
+
+	@Override
+	public ExecutionSlotSharingGroup getExecutionSlotSharingGroup(final ExecutionVertexID executionVertexId) {
+		return executionSlotSharingGroupMap.get(executionVertexId);
+	}
+
+	@Override
+	public Set<ExecutionSlotSharingGroup> getExecutionSlotSharingGroups() {
+		return new HashSet<>(executionSlotSharingGroupMap.values());
+	}
+
+	static class Factory implements SlotSharingStrategy.Factory {
+
+		public LocalInputPreferredSlotSharingStrategy create(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			return new LocalInputPreferredSlotSharingStrategy(topology, logicalSlotSharingGroups, coLocationGroups);
+		}
+	}
+
+	private static class ExecutionSlotSharingGroupBuilder {
+		private final SchedulingTopology topology;
+
+		private final Map<JobVertexID, SlotSharingGroupId> slotSharingGroupMap;
+
+		private final Map<JobVertexID, CoLocationGroupDesc> coLocationGroupMap;
+
+		private final Map<ExecutionVertexID, ExecutionSlotSharingGroup> executionSlotSharingGroupMap;
+
+		final Map<CoLocationConstraintDesc, ExecutionSlotSharingGroup> constraintToExecutionSlotSharingGroupMap;
+
+		final Map<SlotSharingGroupId, List<ExecutionSlotSharingGroup>> executionSlotSharingGroups;
+
+		private final Map<ExecutionSlotSharingGroup, Set<JobVertexID>> assignedJobVerticesForGroups;
+
+		private ExecutionSlotSharingGroupBuilder(
+				final SchedulingTopology topology,
+				final Set<SlotSharingGroup> logicalSlotSharingGroups,
+				final Set<CoLocationGroupDesc> coLocationGroups) {
+
+			this.topology = checkNotNull(topology);
+
+			this.slotSharingGroupMap = new HashMap<>();
+			for (SlotSharingGroup slotSharingGroup : logicalSlotSharingGroups) {
+				for (JobVertexID jobVertexId : slotSharingGroup.getJobVertexIds()) {
+					slotSharingGroupMap.put(jobVertexId, slotSharingGroup.getSlotSharingGroupId());
+				}
+			}
+
+			this.coLocationGroupMap = new HashMap<>();
+			for (CoLocationGroupDesc coLocationGroup : coLocationGroups) {
+				for (JobVertexID jobVertexId : coLocationGroup.getVertices()) {
+					coLocationGroupMap.put(jobVertexId, coLocationGroup);
+				}
+			}
+
+			executionSlotSharingGroupMap = new HashMap<>();
+			constraintToExecutionSlotSharingGroupMap = new HashMap<>();
+			executionSlotSharingGroups = new HashMap<>();
+			assignedJobVerticesForGroups = new IdentityHashMap<>();
+		}
+
+		/**
+		 * Build ExecutionSlotSharingGroups for all vertices in the topology.
+		 * The ExecutionSlotSharingGroup of a vertex is determined in order below:
+		 *
+		 * <p>1. try finding an existing group of the corresponding co-location constraint.
+		 *
+		 * <p>2. try finding an available group of its producer vertex if the producer is in the same slot sharing group.
+		 *
+		 * <p>3. try finding any available group.
+		 *
+		 * <p>4. create a new group.
+		 */
+		private Map<ExecutionVertexID, ExecutionSlotSharingGroup> build() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> allVertices = getExecutionVertices();
+
+			// loop on job vertices so that an execution vertex will not be add into a group
+			// if that group better fits another execution vertex
+			for (List<SchedulingExecutionVertex> executionVertices : allVertices.values()) {
+				final List<SchedulingExecutionVertex> remaining = tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+					executionVertices);
+
+				findAvailableOrCreateNewExecutionSlotSharingGroupFor(remaining);
+
+				updateConstraintToExecutionSlotSharingGroupMap(executionVertices);
+			}
+
+			return executionSlotSharingGroupMap;
+		}
+
+		private LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> getExecutionVertices() {
+			final LinkedHashMap<JobVertexID, List<SchedulingExecutionVertex>> vertices = new LinkedHashMap<>();
+			for (SchedulingExecutionVertex executionVertex : topology.getVertices()) {
+				final List<SchedulingExecutionVertex> executionVertexGroup = vertices.computeIfAbsent(
+					executionVertex.getId().getJobVertexId(),
+					k -> new ArrayList<>());
+				executionVertexGroup.add(executionVertex);
+			}
+			return vertices;
+		}
+
+		private List<SchedulingExecutionVertex> tryFindOptimalAvailableExecutionSlotSharingGroupFor(
+				final List<SchedulingExecutionVertex> executionVertices) {
+
+			final List<SchedulingExecutionVertex> remaining = new ArrayList<>();
+			for (SchedulingExecutionVertex executionVertex : executionVertices) {
+				ExecutionSlotSharingGroup group = tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(executionVertex);
+
+				if (group == null) {
+					group = tryFindAvailableProducerExecutionSlotSharingGroupFor(executionVertex);
+				}
+
+				if (group == null) {
+					remaining.add(executionVertex);
+				} else {
+					addVertexToExecutionSlotSharingGroup(executionVertex, group);
+				}
+			}
+
+			return remaining;
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableCoLocatedExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+			final CoLocationGroupDesc coLocationGroup = coLocationGroupMap.get(executionVertexId.getJobVertexId());
+			if (coLocationGroup != null) {
+				final CoLocationConstraintDesc constraint = coLocationGroup.getLocationConstraint(
+					executionVertexId.getSubtaskIndex());
+
+				return constraintToExecutionSlotSharingGroupMap.get(constraint);
+			} else {
+				return null;
+			}
+		}
+
+		private ExecutionSlotSharingGroup tryFindAvailableProducerExecutionSlotSharingGroupFor(
+				final SchedulingExecutionVertex executionVertex) {
+
+			final ExecutionVertexID executionVertexId = executionVertex.getId();
+
+			for (SchedulingResultPartition partition : executionVertex.getConsumedResults()) {
+				final ExecutionVertexID producerVertexId = partition.getProducer().getId();
+				if (!inSameLogicalSlotSharingGroup(producerVertexId, executionVertexId)) {
+					continue;
+				}
+
+				final ExecutionSlotSharingGroup producerGroup = executionSlotSharingGroupMap.get(producerVertexId);
+
+				checkState(producerGroup != null);
+				if (isGroupAvailableForVertex(producerGroup, executionVertexId)) {
+					return producerGroup;
+				}
+			}
+
+			return null;
+		}
+
+		private boolean inSameLogicalSlotSharingGroup(
+				final ExecutionVertexID executionVertexId1,
+				final ExecutionVertexID executionVertexId2) {
+
+			final SlotSharingGroupId slotSharingGroupId1 = slotSharingGroupMap.get(executionVertexId1.getJobVertexId());
+			final SlotSharingGroupId slotSharingGroupId2 = slotSharingGroupMap.get(executionVertexId2.getJobVertexId());
+
+			return slotSharingGroupId1 != null && slotSharingGroupId1.equals(slotSharingGroupId2);

Review comment:
       It's a bit different since I had wanted it to return false if the slotSharingGroup for any of them is `null`.




----------------------------------------------------------------
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:
users@infra.apache.org