/
scheduler.go
337 lines (279 loc) · 8.38 KB
/
scheduler.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Package scheduler provides a registry to be used as a postprocessor for the routes
// that use a LIFO filter.
package scheduler
import (
"fmt"
"sync"
"time"
"github.com/aryszka/jobqueue"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/metrics"
"github.com/zalando/skipper/routing"
)
// note: Config must stay comparable because it is used to detect changes in route specific LIFO config
const (
// Key used during routing to pass lifo values from the filters to the proxy.
LIFOKey = "lifo"
)
// Config can be used to provide configuration of the registry.
type Config struct {
// MaxConcurrency defines how many jobs are allowed to run concurrently.
// Defaults to 1.
MaxConcurrency int
// MaxStackSize defines how many jobs may be waiting in the stack.
// Defaults to infinite.
MaxQueueSize int
// Timeout defines how long a job can be waiting in the stack.
// Defaults to infinite.
Timeout time.Duration
// CloseTimeout sets a maximum duration for how long the queue can wait
// for the active and queued jobs to finish. Defaults to infinite.
CloseTimeout time.Duration
}
// QueueStatus reports the current status of a queue. It can be used for metrics.
type QueueStatus struct {
// ActiveRequests represents the number of the requests currently being handled.
ActiveRequests int
// QueuedRequests represents the number of requests waiting to be handled.
QueuedRequests int
// Closed indicates that the queue was closed.
Closed bool
}
// Queue objects implement a LIFO queue for handling requests, with a maximum allowed
// concurrency and queue size. Currently, they can be used from the lifo and lifoGroup
// filters in the filters/scheduler package only.
type Queue struct {
queue *jobqueue.Stack
config Config
activeRequestsMetricsKey string
queuedRequestsMetricsKey string
}
// Options provides options for the registry.
type Options struct {
// MetricsUpdateTimeout defines the frequence of how often the LIFO metrics
// are updated when they are enabled. Defaults to 1s.
MetricsUpdateTimeout time.Duration
// EnableRouteLIFOMetrics enables collecting metrics about the LIFO queues.
EnableRouteLIFOMetrics bool
// Metrics must be provided to the registry in order to collect the LIFO metrics.
Metrics metrics.Metrics
}
// Registry maintains a set of LIFO queues. It is used to preserve LIFO queue instances
// across multiple generations of the routing. It implements the routing.PostProcessor
// interface, it is enough to just pass in to routing.Routing when initializing it.
//
// When the EnableRouteLIFOMetrics is set, then the registry starts a background goroutine
// for regularly take snapshots of the active lifo queues and update the corresponding
// metrics. This goroutine is started when the first lifo filter is detected and returns
// when the registry is closed. Individual metrics objects (keys) are used for each
// lifo filter, and one for each lifo group defined by the lifoGroup filter.
//
type Registry struct {
options Options
queues *sync.Map
measuring bool
quit chan struct{}
}
// LIFOFilter is the interface that needs to be implemented by the filters that
// use a LIFO queue maintained by the registry.
type LIFOFilter interface {
// SetQueue will be used by the registry to pass in the right queue to
// the filter.
SetQueue(*Queue)
// GetQueue is currently used only by tests.
GetQueue() *Queue
// Config will be called by the registry once during processing the
// routing to get the right queue settings from the filter.
Config() Config
}
// GroupedLIFOFilter is an extension of the LIFOFilter interface for filters
// that use a shared queue.
type GroupedLIFOFilter interface {
LIFOFilter
// Group returns the name of the group.
Group() string
// HasConfig indicates that the current filter provides the queue
// queue settings for the group.
HasConfig() bool
}
// Wait blocks until a request can be processed or needs to be rejected.
// When it can be processed, calling done indicates that it has finished.
// It is mandatory to call done() the request was processed. When the
// request needs to be rejected, an error will be returned.
func (q *Queue) Wait() (done func(), err error) {
return q.queue.Wait()
}
// Status returns the current status of a queue.
func (q *Queue) Status() QueueStatus {
st := q.queue.Status()
return QueueStatus{
ActiveRequests: st.ActiveJobs,
QueuedRequests: st.QueuedJobs,
Closed: st.Closed,
}
}
// Config returns the configuration that the queue was created with.
func (q *Queue) Config() Config {
return q.config
}
func (q *Queue) reconfigure() {
q.queue.Reconfigure(jobqueue.Options{
MaxConcurrency: q.config.MaxConcurrency,
MaxStackSize: q.config.MaxQueueSize,
Timeout: q.config.Timeout,
})
}
func (q *Queue) close() {
q.queue.Close()
}
// RegistryWith (Options) creates a registry with the provided options.
func RegistryWith(o Options) *Registry {
if o.MetricsUpdateTimeout <= 0 {
o.MetricsUpdateTimeout = time.Second
}
return &Registry{
options: o,
queues: new(sync.Map),
quit: make(chan struct{}),
}
}
// NewRegistry creates a registry with the default options.
func NewRegistry() *Registry {
return RegistryWith(Options{})
}
func (r *Registry) newQueue(name string, c Config) *Queue {
q := &Queue{
config: c,
// renaming Stack -> Queue in the jobqueue project will follow
queue: jobqueue.With(jobqueue.Options{
MaxConcurrency: c.MaxConcurrency,
MaxStackSize: c.MaxQueueSize,
Timeout: c.Timeout,
}),
}
if r.options.EnableRouteLIFOMetrics {
if name == "" {
name = "unknown"
}
q.activeRequestsMetricsKey = fmt.Sprintf("lifo.%s.active", name)
q.queuedRequestsMetricsKey = fmt.Sprintf("lifo.%s.queued", name)
r.measure()
}
return q
}
// Do implements routing.PostProcessor and sets the queue for the scheduler filters.
//
// It preserves the existing queue when available.
func (r *Registry) Do(routes []*routing.Route) []*routing.Route {
rr := make([]*routing.Route, len(routes))
existingKeys := make(map[string]bool)
groups := make(map[string][]GroupedLIFOFilter)
for i, ri := range routes {
rr[i] = ri
var lifoCount int
for _, fi := range ri.Filters {
if glf, ok := fi.Filter.(GroupedLIFOFilter); ok {
groupName := glf.Group()
groups[groupName] = append(groups[groupName], glf)
continue
}
lf, ok := fi.Filter.(LIFOFilter)
if !ok {
continue
}
lifoCount++
var q *Queue
key := fmt.Sprintf("lifo::%s", ri.Id)
existingKeys[key] = true
c := lf.Config()
qi, ok := r.queues.Load(key)
if ok {
q = qi.(*Queue)
if q.config != c {
q.config = c
q.reconfigure()
}
} else {
q = r.newQueue(ri.Id, c)
r.queues.Store(key, q)
}
lf.SetQueue(q)
}
if lifoCount > 1 {
log.Warnf("Found multiple lifo filters in route: %s", ri.Id)
}
}
for name, group := range groups {
var (
c Config
foundConfig bool
)
for _, glf := range group {
if !glf.HasConfig() {
continue
}
if foundConfig && glf.Config() != c {
log.Warnf("Found mismatching configuration for the LIFO group: %s", name)
continue
}
c = glf.Config()
foundConfig = true
}
var q *Queue
key := fmt.Sprintf("group-lifo::%s", name)
existingKeys[key] = true
qi, ok := r.queues.Load(key)
if ok {
q = qi.(*Queue)
if q.config != c {
q.config = c
q.reconfigure()
}
} else {
q = r.newQueue(name, c)
r.queues.Store(key, q)
}
for _, glf := range group {
glf.SetQueue(q)
}
}
r.queues.Range(func(key, qi interface{}) bool {
if !existingKeys[key.(string)] {
qi.(*Queue).close()
r.queues.Delete(key)
}
return true
})
return rr
}
func (r *Registry) measure() {
if r.options.Metrics == nil || r.measuring {
return
}
r.measuring = true
go func() {
for {
r.queues.Range(func(_, value interface{}) bool {
q := value.(*Queue)
s := q.Status()
r.options.Metrics.UpdateGauge(q.activeRequestsMetricsKey, float64(s.ActiveRequests))
r.options.Metrics.UpdateGauge(q.queuedRequestsMetricsKey, float64(s.QueuedRequests))
return true
})
select {
case <-time.After(r.options.MetricsUpdateTimeout):
case <-r.quit:
return
}
}
}()
}
// Close closes the registry, including gracefull tearing down the stored
// queues.
func (r *Registry) Close() {
r.queues.Range(func(_, value interface{}) bool {
value.(*Queue).close()
return true
})
close(r.quit)
}