-
Notifications
You must be signed in to change notification settings - Fork 212
/
user_tracker.go
197 lines (172 loc) · 6.56 KB
/
user_tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
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 ugm
import (
"strings"
"go.uber.org/zap"
"github.com/apache/yunikorn-core/pkg/common"
"github.com/apache/yunikorn-core/pkg/common/configs"
"github.com/apache/yunikorn-core/pkg/common/resources"
"github.com/apache/yunikorn-core/pkg/locking"
"github.com/apache/yunikorn-core/pkg/log"
"github.com/apache/yunikorn-core/pkg/webservice/dao"
)
type UserTracker struct {
userName string // Name of the user for which usage is being tracked upon
// Holds group tracker object for every application user runs.
// Group is not fixed for user unlike other systems and would be selected based on queue limit config processing flow and may vary for different applications.
// Hence, group tracker object may vary for same user running different applications linked through this map with key as application id
// and group tracker object as value.
appGroupTrackers map[string]*GroupTracker
queueTracker *QueueTracker // Holds the actual resource usage of queue path where application runs
events *ugmEvents
locking.RWMutex
}
func newUserTracker(userName string, ugmEvents *ugmEvents) *UserTracker {
queueTracker := newRootQueueTracker(user)
userTracker := &UserTracker{
userName: userName,
appGroupTrackers: make(map[string]*GroupTracker),
queueTracker: queueTracker,
events: ugmEvents,
}
return userTracker
}
func (ut *UserTracker) increaseTrackedResource(queuePath string, applicationID string, usage *resources.Resource) bool {
ut.Lock()
defer ut.Unlock()
hierarchy := strings.Split(queuePath, configs.DOT)
ut.events.sendIncResourceUsageForUser(ut.userName, queuePath, usage)
increased := ut.queueTracker.increaseTrackedResource(hierarchy, applicationID, user, usage)
if increased {
gt := ut.appGroupTrackers[applicationID]
log.Log(log.SchedUGM).Debug("Increasing resource usage for group",
zap.String("group", gt.getName()),
zap.String("queue path", queuePath),
zap.String("application", applicationID),
zap.Stringer("resource", usage))
increasedGroupUsage := gt.increaseTrackedResource(queuePath, applicationID, usage, ut.userName)
if !increasedGroupUsage {
_, decreased := ut.queueTracker.decreaseTrackedResource(hierarchy, applicationID, usage, false)
if !decreased {
log.Log(log.SchedUGM).Error("User resource usage rollback has failed",
zap.String("queue path", queuePath),
zap.String("application", applicationID),
zap.String("user", ut.userName))
}
}
return increasedGroupUsage
}
return increased
}
func (ut *UserTracker) decreaseTrackedResource(queuePath string, applicationID string, usage *resources.Resource, removeApp bool) (bool, bool) {
ut.Lock()
defer ut.Unlock()
ut.events.sendDecResourceUsageForUser(ut.userName, queuePath, usage)
if removeApp {
tracker := ut.appGroupTrackers[applicationID]
if tracker != nil {
appGroup := tracker.groupName
ut.events.sendAppGroupUnlinked(appGroup, applicationID)
}
delete(ut.appGroupTrackers, applicationID)
}
return ut.queueTracker.decreaseTrackedResource(strings.Split(queuePath, configs.DOT), applicationID, usage, removeApp)
}
func (ut *UserTracker) hasGroupForApp(applicationID string) bool {
ut.RLock()
defer ut.RUnlock()
_, ok := ut.appGroupTrackers[applicationID]
return ok
}
func (ut *UserTracker) setGroupForApp(applicationID string, groupTrack *GroupTracker) {
ut.Lock()
defer ut.Unlock()
if groupTrack != nil {
ut.events.sendAppGroupLinked(groupTrack.groupName, applicationID)
}
ut.appGroupTrackers[applicationID] = groupTrack
}
func (ut *UserTracker) getGroupForApp(applicationID string) string {
ut.RLock()
defer ut.RUnlock()
if ut.appGroupTrackers[applicationID] != nil {
return ut.appGroupTrackers[applicationID].groupName
}
return common.Empty
}
func (ut *UserTracker) getTrackedApplications() map[string]*GroupTracker {
ut.RLock()
defer ut.RUnlock()
return ut.appGroupTrackers
}
func (ut *UserTracker) setLimits(queuePath string, resource *resources.Resource, maxApps uint64, useWildCard bool, doWildCardCheck bool) {
ut.Lock()
defer ut.Unlock()
ut.events.sendLimitSetForUser(ut.userName, queuePath)
ut.queueTracker.setLimit(strings.Split(queuePath, configs.DOT), resource, maxApps, useWildCard, user, doWildCardCheck)
}
func (ut *UserTracker) clearLimits(queuePath string, doWildCardCheck bool) {
ut.Lock()
defer ut.Unlock()
ut.events.sendLimitRemoveForUser(ut.userName, queuePath)
ut.queueTracker.setLimit(strings.Split(queuePath, configs.DOT), nil, 0, false, user, doWildCardCheck)
}
func (ut *UserTracker) headroom(hierarchy []string) *resources.Resource {
ut.Lock()
defer ut.Unlock()
return ut.queueTracker.headroom(hierarchy, user)
}
func (ut *UserTracker) GetUserResourceUsageDAOInfo() *dao.UserResourceUsageDAOInfo {
ut.RLock()
defer ut.RUnlock()
userResourceUsage := &dao.UserResourceUsageDAOInfo{
Groups: make(map[string]string),
}
userResourceUsage.UserName = ut.userName
for app, gt := range ut.appGroupTrackers {
if gt != nil {
userResourceUsage.Groups[app] = gt.groupName
}
}
userResourceUsage.Queues = ut.queueTracker.getResourceUsageDAOInfo(common.Empty)
return userResourceUsage
}
func (ut *UserTracker) IsQueuePathTrackedCompletely(hierarchy []string) bool {
ut.RLock()
defer ut.RUnlock()
return ut.queueTracker.IsQueuePathTrackedCompletely(hierarchy)
}
func (ut *UserTracker) IsUnlinkRequired(hierarchy []string) bool {
ut.RLock()
defer ut.RUnlock()
return ut.queueTracker.IsUnlinkRequired(hierarchy)
}
func (ut *UserTracker) UnlinkQT(hierarchy []string) bool {
ut.Lock()
defer ut.Unlock()
return ut.queueTracker.UnlinkQT(hierarchy)
}
func (ut *UserTracker) canBeRemoved() bool {
ut.RLock()
defer ut.RUnlock()
return ut.queueTracker.canBeRemoved()
}
func (ut *UserTracker) canRunApp(hierarchy []string, applicationID string) bool {
ut.Lock()
defer ut.Unlock()
return ut.queueTracker.canRunApp(hierarchy, applicationID, user)
}