This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathqueues.go
248 lines (214 loc) · 7.77 KB
/
queues.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package executions
import (
"context"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
runtimeInterfaces "github.com/lyft/flyteadmin/pkg/runtime/interfaces"
)
type project = string
type domain = string
type workflowName = string
type tag = string
type singleQueueConfiguration struct {
PrimaryQueue string
DynamicQueue string
}
type queueConfigSet = map[singleQueueConfiguration]bool
type queues = []singleQueueConfiguration
type queueConfig = map[tag]queues
// Catch-all queues for a project
type defaultProjectQueueAssignment = map[project]singleQueueConfiguration
/**
Catch-all queues for a project + domain combo
project: {
domain: queue
}
*/
type defaultProjectDomainQueueAssignment = map[project]map[domain]singleQueueConfiguration
/**
Expanded workflowConfig structure:
project: {
domain: {
workflow: queue
}
}
*/
// Stores an execution queue (when it exists) that matches all tags specified by a workflow config
type workflowQueueAssignment = map[project]map[domain]map[workflowName]singleQueueConfiguration
type QueueAllocator interface {
GetQueue(ctx context.Context, identifier core.Identifier) singleQueueConfiguration
}
type queueAllocatorImpl struct {
queueConfigMap queueConfig
defaultQueue singleQueueConfiguration
defaultProjectQueueAssignmentMap defaultProjectQueueAssignment
defaultProjectDomainQueueAssignmentMap defaultProjectDomainQueueAssignment
workflowQueueAssignmentMap workflowQueueAssignment
config runtimeInterfaces.Configuration
}
// Returns an arbitrary map entry's key from the input map. Used when a workflow can be run on multiple queues.
func getAnyMapKey(input queueConfigSet) singleQueueConfiguration {
for key := range input {
return key
}
// Nothing can be returned.
logger.Error(context.Background(), "can't find any map key for empty map")
return singleQueueConfiguration{}
}
func (q *queueAllocatorImpl) refreshExecutionQueues(executionQueues []runtimeInterfaces.ExecutionQueue) {
logger.Debug(context.Background(), "refreshing execution queues")
var queueConfigMap = make(queueConfig)
for _, queue := range executionQueues {
for _, tag := range queue.Attributes {
queuesForTag, ok := queueConfigMap[tag]
if !ok {
queuesForTag = make(queues, 0, 1)
}
queueConfigMap[tag] = append(queuesForTag, singleQueueConfiguration{
PrimaryQueue: queue.Primary,
DynamicQueue: queue.Dynamic,
})
}
}
q.queueConfigMap = queueConfigMap
}
func (q *queueAllocatorImpl) findQueueCandidates(
config runtimeInterfaces.WorkflowConfig) queueConfigSet {
// go through and find queues that match *all* specified tags
queueCandidates := make(queueConfigSet)
for _, queue := range q.queueConfigMap[config.Tags[0]] {
queueCandidates[queue] = true
}
for i := 1; i < len(config.Tags); i++ {
filteredQueueCandidates := make(queueConfigSet)
for _, queue := range q.queueConfigMap[config.Tags[i]] {
if ok := queueCandidates[queue]; ok {
filteredQueueCandidates[queue] = true
}
}
if len(filteredQueueCandidates) == 0 {
break
}
queueCandidates = filteredQueueCandidates
}
return queueCandidates
}
func (q *queueAllocatorImpl) refreshWorkflowQueueMap(workflowConfigs []runtimeInterfaces.WorkflowConfig) {
logger.Debug(context.Background(), "refreshing workflow configs")
var workflowQueueMap = make(workflowQueueAssignment)
var projectQueueMap = make(defaultProjectQueueAssignment)
var projectDomainQueueMap = make(defaultProjectDomainQueueAssignment)
for _, config := range workflowConfigs {
var queue singleQueueConfiguration
// go through and find queues that match *all* specified tags
queueCandidates := q.findQueueCandidates(config)
if len(queueCandidates) > 0 {
queue = getAnyMapKey(queueCandidates)
}
if config.Project == "" {
// This is a default queue assignment
q.defaultQueue = queue
continue
}
// Now assign the queue to the most-specific configuration that is possible.
projectSubMap, ok := workflowQueueMap[config.Project]
if !ok {
projectSubMap = make(map[domain]map[workflowName]singleQueueConfiguration)
}
// This queue applies to *all* workflows in this project
if config.Domain == "" {
projectQueueMap[config.Project] = queue
continue
}
defaultProjectDomainMap, ok := projectDomainQueueMap[config.Project]
if !ok {
defaultProjectDomainMap = make(map[domain]singleQueueConfiguration)
projectDomainQueueMap[config.Project] = defaultProjectDomainMap
}
// This queue applies to *all* workflows in this project + domain combo
if config.WorkflowName == "" {
defaultProjectDomainMap[config.Domain] = queue
continue
}
// This queue applies to individual workflows with this project + domain + workflowName combo
domainSubMap, ok := projectSubMap[config.Domain]
if !ok {
domainSubMap = make(map[workflowName]singleQueueConfiguration)
}
domainSubMap[config.WorkflowName] = queue
projectSubMap[config.Domain] = domainSubMap
workflowQueueMap[config.Project] = projectSubMap
}
q.defaultProjectQueueAssignmentMap = projectQueueMap
q.defaultProjectDomainQueueAssignmentMap = projectDomainQueueMap
q.workflowQueueAssignmentMap = workflowQueueMap
}
// Returns a queue specifically matching identifier project, domain, and name
// Barring a match for that, a queue matching a combination of project + domain will be returned.
// And if there is no existing match for that, a queue matching the project will be returned if it exists.
func (q *queueAllocatorImpl) getQueueForIdentifier(identifier core.Identifier) *singleQueueConfiguration {
projectSubMap, ok := q.workflowQueueAssignmentMap[identifier.Project]
if !ok {
return nil
}
domainSubMap, ok := projectSubMap[identifier.Domain]
if !ok {
return nil
}
queue, ok := domainSubMap[identifier.Name]
if !ok {
return nil
}
return &queue
}
func (q *queueAllocatorImpl) getQueueForProjectAndDomain(identifier core.Identifier) *singleQueueConfiguration {
domainSubMap, ok := q.defaultProjectDomainQueueAssignmentMap[identifier.Project]
if !ok {
return nil
}
defaultDomainQueue, ok := domainSubMap[identifier.Domain]
if !ok {
return nil
}
return &defaultDomainQueue
}
func (q *queueAllocatorImpl) getQueueForProject(identifier core.Identifier) *singleQueueConfiguration {
queue, ok := q.defaultProjectQueueAssignmentMap[identifier.Project]
if !ok {
return nil
}
return &queue
}
func (q *queueAllocatorImpl) GetQueue(ctx context.Context, identifier core.Identifier) singleQueueConfiguration {
// NOTE: If refreshing the execution queues & workflow configs on every call to GetQueue becomes too slow we should
// investigate caching the computed queue assignments.
executionQueues := q.config.QueueConfiguration().GetExecutionQueues()
q.refreshExecutionQueues(executionQueues)
workflowConfigs := q.config.QueueConfiguration().GetWorkflowConfigs()
q.refreshWorkflowQueueMap(workflowConfigs)
logger.Debugf(ctx,
"Evaluating execution queue for [%+v] with available queues [%+v] and available workflow configs [%+v]",
identifier, executionQueues, workflowConfigs)
queue := q.getQueueForIdentifier(identifier)
if queue != nil {
logger.Debugf(ctx, "Found queue for identifier [%+v]: %v", identifier, queue)
return *queue
}
queue = q.getQueueForProjectAndDomain(identifier)
if queue != nil {
logger.Debugf(ctx, "Found queue for project+domain [%s/%s]: %v", identifier.Project, identifier.Domain, queue)
return *queue
}
queue = q.getQueueForProject(identifier)
if queue != nil {
logger.Debugf(ctx, "Found queue for project [%s]: %v", identifier.Project, queue)
return *queue
}
return q.defaultQueue
}
func NewQueueAllocator(config runtimeInterfaces.Configuration) QueueAllocator {
queueAllocator := queueAllocatorImpl{
config: config,
}
return &queueAllocator
}