Skip to content

Commit 9f900d2

Browse files
vinayakphegdepbacsko
authored andcommitted
[YUNIKORN-2089] Moved usedResource type and tests to their own files (apache#713)
Closes: apache#713 Signed-off-by: Peter Bacsko <[email protected]>
1 parent 0e99462 commit 9f900d2

File tree

4 files changed

+324
-61
lines changed

4 files changed

+324
-61
lines changed

pkg/common/resources/resources.go

-58
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"sort"
2626
"strconv"
2727
"strings"
28-
"sync"
29-
"time"
3028

3129
"go.uber.org/zap"
3230

@@ -46,58 +44,6 @@ func (q Quantity) string() string {
4644
return strconv.FormatInt(int64(q), 10)
4745
}
4846

49-
// Util struct to keep track of application resource usage
50-
type TrackedResource struct {
51-
// Two level map for aggregated resource usage
52-
// With instance type being the top level key, the mapped value is a map:
53-
// resource type (CPU, memory etc) -> the aggregated used time (in seconds) of the resource type
54-
//
55-
TrackedResourceMap map[string]map[string]int64
56-
57-
sync.RWMutex
58-
}
59-
60-
func (ur *TrackedResource) Clone() *TrackedResource {
61-
if ur == nil {
62-
return nil
63-
}
64-
ret := NewTrackedResource()
65-
ur.RLock()
66-
defer ur.RUnlock()
67-
for k, v := range ur.TrackedResourceMap {
68-
dest := make(map[string]int64)
69-
for key, element := range v {
70-
dest[key] = element
71-
}
72-
ret.TrackedResourceMap[k] = dest
73-
}
74-
return ret
75-
}
76-
77-
// Aggregate the resource usage to UsedResourceMap[instType]
78-
// The time the given resource used is the delta between the resource createTime and currentTime
79-
func (ur *TrackedResource) AggregateTrackedResource(instType string,
80-
resource *Resource, bindTime time.Time) {
81-
ur.Lock()
82-
defer ur.Unlock()
83-
84-
releaseTime := time.Now()
85-
timeDiff := int64(releaseTime.Sub(bindTime).Seconds())
86-
aggregatedResourceTime, ok := ur.TrackedResourceMap[instType]
87-
if !ok {
88-
aggregatedResourceTime = map[string]int64{}
89-
}
90-
for key, element := range resource.Resources {
91-
curUsage, ok := aggregatedResourceTime[key]
92-
if !ok {
93-
curUsage = 0
94-
}
95-
curUsage += int64(element) * timeDiff // resource size times timeDiff
96-
aggregatedResourceTime[key] = curUsage
97-
}
98-
ur.TrackedResourceMap[instType] = aggregatedResourceTime
99-
}
100-
10147
// Never update value of Zero
10248
var Zero = NewResource()
10349

@@ -157,10 +103,6 @@ func NewResourceFromConf(configMap map[string]string) (*Resource, error) {
157103
return res, nil
158104
}
159105

160-
func NewTrackedResource() *TrackedResource {
161-
return &TrackedResource{TrackedResourceMap: make(map[string]map[string]int64)}
162-
}
163-
164106
func (r *Resource) String() string {
165107
if r == nil {
166108
return "nil resource"
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package resources
20+
21+
import (
22+
"fmt"
23+
"strings"
24+
"sync"
25+
"time"
26+
)
27+
28+
// TrackedResource is a utility struct to keep track of application resource usage.
29+
type TrackedResource struct {
30+
// TrackedResourceMap is a two-level map for aggregated resource usage.
31+
// The top-level key is the instance type, and the value is a map:
32+
// resource type (CPU, memory, etc.) -> aggregated used time (in seconds) of the resource type.
33+
TrackedResourceMap map[string]map[string]int64
34+
35+
sync.RWMutex
36+
}
37+
38+
// NewTrackedResource creates a new instance of TrackedResource.
39+
func NewTrackedResource() *TrackedResource {
40+
return &TrackedResource{TrackedResourceMap: make(map[string]map[string]int64)}
41+
}
42+
43+
// NewTrackedResourceFromMap creates NewTrackedResource from the given map.
44+
// Using for Testing purpose only.
45+
func NewTrackedResourceFromMap(m map[string]map[string]int64) *TrackedResource {
46+
if m == nil {
47+
return NewTrackedResource()
48+
}
49+
return &TrackedResource{TrackedResourceMap: m}
50+
}
51+
52+
func (tr *TrackedResource) String() string {
53+
tr.RLock()
54+
defer tr.RUnlock()
55+
56+
var resourceUsage []string
57+
for instanceType, resourceTypeMap := range tr.TrackedResourceMap {
58+
for resourceType, usageTime := range resourceTypeMap {
59+
resourceUsage = append(resourceUsage, fmt.Sprintf("%s:%s=%d", instanceType, resourceType, usageTime))
60+
}
61+
}
62+
63+
return fmt.Sprintf("TrackedResource{%s}", strings.Join(resourceUsage, ","))
64+
}
65+
66+
// Clone creates a deep copy of TrackedResource.
67+
func (tr *TrackedResource) Clone() *TrackedResource {
68+
if tr == nil {
69+
return nil
70+
}
71+
ret := NewTrackedResource()
72+
tr.RLock()
73+
defer tr.RUnlock()
74+
for k, v := range tr.TrackedResourceMap {
75+
dest := make(map[string]int64)
76+
for key, element := range v {
77+
dest[key] = element
78+
}
79+
ret.TrackedResourceMap[k] = dest
80+
}
81+
return ret
82+
}
83+
84+
// AggregateTrackedResource aggregates resource usage to TrackedResourceMap[instType].
85+
// The time the given resource used is the delta between the resource createTime and currentTime.
86+
func (tr *TrackedResource) AggregateTrackedResource(instType string,
87+
resource *Resource, bindTime time.Time) {
88+
if resource == nil {
89+
return
90+
}
91+
tr.Lock()
92+
defer tr.Unlock()
93+
94+
releaseTime := time.Now()
95+
timeDiff := int64(releaseTime.Sub(bindTime).Seconds())
96+
aggregatedResourceTime, ok := tr.TrackedResourceMap[instType]
97+
if !ok {
98+
aggregatedResourceTime = map[string]int64{}
99+
}
100+
for key, element := range resource.Resources {
101+
curUsage, ok := aggregatedResourceTime[key]
102+
if !ok {
103+
curUsage = 0
104+
}
105+
curUsage += int64(element) * timeDiff // resource size times timeDiff
106+
aggregatedResourceTime[key] = curUsage
107+
}
108+
tr.TrackedResourceMap[instType] = aggregatedResourceTime
109+
}

0 commit comments

Comments
 (0)