This is an automated email from the ASF dual-hosted git repository.

wilfreds pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-core.git


The following commit(s) were added to refs/heads/master by this push:
     new bdecf775 [YUNIKORN-1403] Improve overflow handling (#555)
bdecf775 is described below

commit bdecf775880d79bc1544a8a83780154c06716c77
Author: Simon <[email protected]>
AuthorDate: Mon Jun 26 14:12:14 2023 +1000

    [YUNIKORN-1403] Improve overflow handling (#555)
    
    Add unit tests for calculation overflow and improve overflow handling in
    the resource absolute usage calculations.
    
    Closes: #555
    
    Signed-off-by: Wilfred Spiegelenburg <[email protected]>
---
 pkg/common/resources/resources.go      | 18 ++++--------------
 pkg/common/resources/resources_test.go |  4 ++--
 2 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/pkg/common/resources/resources.go 
b/pkg/common/resources/resources.go
index f0da8d76..5d246649 100644
--- a/pkg/common/resources/resources.go
+++ b/pkg/common/resources/resources.go
@@ -952,23 +952,13 @@ func CalculateAbsUsedCapacity(capacity, used *Resource) 
*Resource {
                                        zap.Int64("capacity", 
int64(availableResource)),
                                        zap.Int64("usage", int64(usedResource)))
                        }
-                       div := float64(usedResource) / 
float64(availableResource)
-                       absResValue = int64(div * 100)
-                       // protect against positive integer overflow
-                       if absResValue < 0 && div > 0 {
-                               log.Log(log.Resources).Warn("Absolute resource 
value result positive overflow",
+                       div := float64(usedResource) * 100 / 
float64(availableResource)
+                       absResValue = int64(div)
+                       if ((usedResource >= 0) == (availableResource > 0)) == 
(absResValue < 0) || (div > math.MaxInt64) || (div < math.MinInt64) {
+                               log.Log(log.Resources).Warn("Absolute resource 
value result wrapped or overflow",
                                        zap.String("resource", resourceName),
                                        zap.Int64("capacity", 
int64(availableResource)),
                                        zap.Int64("usage", int64(usedResource)))
-                               absResValue = math.MaxInt64
-                       }
-                       // protect against negative integer overflow
-                       if absResValue > 0 && div < 0 {
-                               log.Log(log.Resources).Warn("Absolute resource 
value result negative overflow",
-                                       zap.String("resource", resourceName),
-                                       zap.Int64("capacity", 
int64(availableResource)),
-                                       zap.Int64("usage", int64(usedResource)))
-                               absResValue = math.MinInt64
                        }
                } else {
                        if missingResources.Len() != 0 {
diff --git a/pkg/common/resources/resources_test.go 
b/pkg/common/resources/resources_test.go
index a086cb3f..9ae1429e 100644
--- a/pkg/common/resources/resources_test.go
+++ b/pkg/common/resources/resources_test.go
@@ -1507,7 +1507,7 @@ func TestCalculateAbsUsedCapacity(t *testing.T) {
                "positive overflow": {
                        capacity: 
NewResourceFromMap(map[string]Quantity{"memory": 10}),
                        used:     
NewResourceFromMap(map[string]Quantity{"memory": math.MaxInt64}),
-                       expected: 
NewResourceFromMap(map[string]Quantity{"memory": math.MaxInt64}),
+                       expected: 
NewResourceFromMap(map[string]Quantity{"memory": math.MinInt64}),
                },
                "negative overflow": {
                        capacity: 
NewResourceFromMap(map[string]Quantity{"memory": 10}),
@@ -1517,7 +1517,7 @@ func TestCalculateAbsUsedCapacity(t *testing.T) {
                "zero resource, non zero used": {
                        capacity: zeroResource,
                        used:     usageSet,
-                       expected: 
NewResourceFromMap(map[string]Quantity{"memory": math.MaxInt64, "vcores": 
math.MaxInt64}),
+                       expected: 
NewResourceFromMap(map[string]Quantity{"memory": math.MinInt64, "vcores": 
math.MinInt64}),
                },
        }
        for _, test := range tests {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to