zentol commented on a change in pull request #10200: [FLINK-14062][runtime] 
Calculate managed memory fraction based on slot sharing groups
URL: https://github.com/apache/flink/pull/10200#discussion_r346883588
 
 

 ##########
 File path: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/graph/StreamingJobGraphGenerator.java
 ##########
 @@ -684,6 +689,96 @@ private void setCoLocation() {
                }
        }
 
+       private void setManagedMemoryFraction() {
+               // all slot sharing groups in this job
+               final Set<SlotSharingGroup> slotSharingGroups = 
Collections.newSetFromMap(new IdentityHashMap<>());
+               // maps a job vertex ID to its head operator ID
+               final Map<JobVertexID, Integer> vertexHeadOperators = new 
HashMap<>();
+               // maps a job vertex ID to IDs of all operators in the vertex
+               final Map<JobVertexID, Set<Integer>> vertexOperators = new 
HashMap<>();
+
+               for (Entry<Integer, JobVertex> entry : jobVertices.entrySet()) {
+                       final int headOperatorId = entry.getKey();
+                       final JobVertex jobVertex = entry.getValue();
+
+                       final SlotSharingGroup jobVertexSlotSharingGroup = 
jobVertex.getSlotSharingGroup();
+                       if (jobVertexSlotSharingGroup != null) {
+                               
slotSharingGroups.add(jobVertexSlotSharingGroup);
+                       }
+
+                       vertexHeadOperators.put(jobVertex.getID(), 
headOperatorId);
+
+                       final Set<Integer> operatorIds = new HashSet<>();
+                       operatorIds.add(headOperatorId);
+                       
operatorIds.addAll(chainedConfigs.getOrDefault(headOperatorId, new 
HashMap<>()).keySet());
+                       vertexOperators.put(jobVertex.getID(), operatorIds);
+               }
+
+               for (SlotSharingGroup slotSharingGroup : slotSharingGroups) {
+                       
setManagedMemoryFractionForSlotSharingGroup(slotSharingGroup, 
vertexHeadOperators, vertexOperators);
+               }
+       }
+
+       private void setManagedMemoryFractionForSlotSharingGroup(
+                       final SlotSharingGroup slotSharingGroup,
+                       final Map<JobVertexID, Integer> vertexHeadOperators,
+                       final Map<JobVertexID, Set<Integer>> vertexOperators) {
+
+               int groupOperatorCount = 0;
+               for (JobVertexID jobVertexID : 
slotSharingGroup.getJobVertexIds()) {
+                       final Set<Integer> operatorIds = 
vertexOperators.get(jobVertexID);
+                       groupOperatorCount += operatorIds.size();
+               }
+
+               for (JobVertexID jobVertexID : 
slotSharingGroup.getJobVertexIds()) {
+                       for (int operatorNodeId : 
vertexOperators.get(jobVertexID)) {
+                               final StreamConfig operatorConfig = 
vertexConfigs.get(operatorNodeId);
+                               final ResourceSpec operatorResourceSpec = 
streamGraph.getStreamNode(operatorNodeId).getMinResources();
+                               setManagedMemoryFractionForOperator(
+                                       operatorResourceSpec,
+                                       slotSharingGroup.getResourceSpec(),
+                                       groupOperatorCount,
+                                       operatorConfig);
+                       }
+
+                       // need to refresh the chained task configs
+                       final int headOperatorNodeId = 
vertexHeadOperators.get(jobVertexID);
+                       final StreamConfig vertexConfig = 
vertexConfigs.get(headOperatorNodeId);
+                       
vertexConfig.setTransitiveChainedTaskConfigs(chainedConfigs.get(headOperatorNodeId));
+               }
+       }
+
+       private void setManagedMemoryFractionForOperator(
+                       final ResourceSpec operatorResourceSpec,
+                       final ResourceSpec groupResourceSpec,
+                       final int groupOperatorCount,
+                       final StreamConfig operatorConfig) {
+
+               final double managedMemoryFractionOnHeap;
+               final double managedMemoryFractionOffHeap;
+
+               if (groupResourceSpec.equals(ResourceSpec.UNKNOWN)) {
+                       checkArgument(groupOperatorCount > 0, "A slot sharing 
group must contain at least 1 operator");
+
+                       managedMemoryFractionOnHeap = 1.0 / groupOperatorCount;
+                       managedMemoryFractionOffHeap = 1.0 / groupOperatorCount;
+               } else {
+                       final long groupOnHeapManagedMemoryBytes = 
groupResourceSpec.getOnHeapManagedMemory().getBytes();
+                       final long groupOffHeapManagedMemoryBytes = 
groupResourceSpec.getOffHeapManagedMemory().getBytes();
+
+                       managedMemoryFractionOnHeap = 
groupOnHeapManagedMemoryBytes > 0 ?
+                               (double) 
operatorResourceSpec.getOnHeapManagedMemory().getBytes() / 
groupOnHeapManagedMemoryBytes :
+                               0.0;
+
+                       managedMemoryFractionOffHeap = 
groupOffHeapManagedMemoryBytes > 0 ?
+                               (double) 
operatorResourceSpec.getOffHeapManagedMemory().getBytes() / 
groupOffHeapManagedMemoryBytes :
+                               0.0;
+               }
+
+               
operatorConfig.setManagedMemoryFractionOnHeap(managedMemoryFractionOnHeap);
 
 Review comment:
   Unrelated to this PR, but these methods should check that the given value is 
neither negative nor greater than 1.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to