xintongsong commented on a change in pull request #8841: 
[FLINK-12765][coordinator] Bookkeeping of available resources of allocated 
slots in SlotPool
URL: https://github.com/apache/flink/pull/8841#discussion_r297467266
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/clusterframework/types/ResourceProfile.java
 ##########
 @@ -260,6 +265,102 @@ public int compareTo(@Nonnull ResourceProfile other) {
                return cmp;
        }
 
+       /**
+        * Calculates the sum of two resource profiles.
+        *
+        * @param other The other resource profile to add.
+        * @return The merged resource profile.
+        */
+       @Nonnull
+       public ResourceProfile merge(@Nonnull ResourceProfile other) {
+               if (equals(ANY) || other.equals(ANY)) {
+                       return ANY;
+               }
+
+               if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) {
+                       return UNKNOWN;
+               }
+
+               Map<String, Resource> resultExtendedResource = new 
HashMap<>(extendedResources);
+
+               other.extendedResources.forEach((String name, Resource 
resource) -> {
+                       resultExtendedResource.compute(name, (ignored, 
oldResource) ->
+                                       oldResource == null ? resource : 
oldResource.merge(resource));
+               });
+
+               return new ResourceProfile(
+                       addDoublesConsideringOverflow(cpuCores, other.cpuCores),
+                       addIntegersConsideringOverflow(heapMemoryInMB, 
other.heapMemoryInMB),
+                       addIntegersConsideringOverflow(directMemoryInMB, 
other.directMemoryInMB),
+                       addIntegersConsideringOverflow(nativeMemoryInMB, 
other.nativeMemoryInMB),
+                       addIntegersConsideringOverflow(networkMemoryInMB, 
other.networkMemoryInMB),
+                       resultExtendedResource);
+       }
+
+       /**
+        * Subtracts another piece of resource profile from this one.
+        *
+        * @param other The other resource profile to subtract.
+        * @return The subtracted resource profile.
+        */
+       public ResourceProfile subtract(ResourceProfile other) {
+               if (equals(ANY) || other.equals(ANY)) {
+                       return ANY;
+               }
+
+               if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) {
+                       return UNKNOWN;
+               }
+
+               checkArgument(isMatching(other), "Try to subtract an unmatched 
resource profile from this one.");
+
+               Map<String, Resource> resultExtendedResource = new 
HashMap<>(extendedResources);
+
+               other.extendedResources.forEach((String name, Resource 
resource) -> {
+                       resultExtendedResource.compute(name, (ignored, 
oldResource) -> {
+                               Resource resultResource = 
oldResource.subtract(resource);
+                               return resultResource.getValue() == 0 ? null : 
resultResource;
+                       });
+               });
+
+               return new ResourceProfile(
+                       subtractDoublesConsideringInf(cpuCores, other.cpuCores),
+                       subtractIntegersConsideringInf(heapMemoryInMB, 
other.heapMemoryInMB),
+                       subtractIntegersConsideringInf(directMemoryInMB, 
other.directMemoryInMB),
+                       subtractIntegersConsideringInf(nativeMemoryInMB, 
other.nativeMemoryInMB),
+                       subtractIntegersConsideringInf(networkMemoryInMB, 
other.networkMemoryInMB),
+                       resultExtendedResource
+               );
+       }
+
+       private double addDoublesConsideringOverflow(double first, double 
second) {
+               double result = first + second;
+
+               if (result == Double.POSITIVE_INFINITY) {
+                       return Double.MAX_VALUE;
+               }
+
+               return result;
+       }
+
+       private int addIntegersConsideringOverflow(int first, int second) {
+               int result = first + second;
+
+               if (result < 0) {
+                       return Integer.MAX_VALUE;
 
 Review comment:
   If we assume both integers to be added are non-negative, we should make this 
assumption explicit. Maybe change the method name to 
addNonNegtiveIntegersConsideringOverflow?

----------------------------------------------------------------
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