/
lifo.go
332 lines (286 loc) · 8.39 KB
/
lifo.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
package scheduler
import (
"net/http"
"time"
"github.com/aryszka/jobqueue"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/scheduler"
)
type (
lifoSpec struct{}
lifoGroupSpec struct{}
lifoFilter struct {
config scheduler.Config
queue *scheduler.Queue
}
lifoGroupFilter struct {
name string
hasConfig bool
config scheduler.Config
queue *scheduler.Queue
}
)
const (
LIFOName = "lifo"
LIFOGroupName = "lifoGroup"
defaultMaxConcurreny = 100
defaultMaxQueueSize = 100
defaultTimeout = 10 * time.Second
)
func NewLIFO() filters.Spec {
return &lifoSpec{}
}
func NewLIFOGroup() filters.Spec {
return &lifoGroupSpec{}
}
func intArg(a interface{}) (int, error) {
switch v := a.(type) {
case int:
return v, nil
case float64:
return int(v), nil
default:
return 0, filters.ErrInvalidFilterParameters
}
}
func durationArg(a interface{}) (time.Duration, error) {
switch v := a.(type) {
case string:
return time.ParseDuration(v)
default:
return 0, filters.ErrInvalidFilterParameters
}
}
func (s *lifoSpec) Name() string { return LIFOName }
// CreateFilter creates a lifoFilter, that will use a queue based
// queue for handling requests instead of the fifo queue. The first
// parameter is MaxConcurrency the second MaxQueueSize and the third
// Timeout.
//
// The implementation is based on
// https://godoc.org/github.com/aryszka/jobqueue, which provides more
// detailed documentation.
//
// All parameters are optional and defaults to
// MaxConcurrency 100, MaxQueueSize 100, Timeout 10s.
//
// The total maximum number of requests has to be computed by adding
// MaxConcurrency and MaxQueueSize: total max = MaxConcurrency + MaxQueueSize
//
// Min values are 1 for MaxConcurrency and MaxQueueSize, and 1ms for
// Timeout. All configration that is below will be set to these min
// values.
func (s *lifoSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
var l lifoFilter
// set defaults
l.config.MaxConcurrency = defaultMaxConcurreny
l.config.MaxQueueSize = defaultMaxQueueSize
l.config.Timeout = defaultTimeout
if len(args) > 0 {
c, err := intArg(args[0])
if err != nil {
return nil, err
}
if c >= 1 {
l.config.MaxConcurrency = c
}
}
if len(args) > 1 {
c, err := intArg(args[1])
if err != nil {
return nil, err
}
if c >= 0 {
l.config.MaxQueueSize = c
}
}
if len(args) > 2 {
d, err := durationArg(args[2])
if err != nil {
return nil, err
}
if d >= 1*time.Millisecond {
l.config.Timeout = d
}
}
if len(args) > 3 {
return nil, filters.ErrInvalidFilterParameters
}
return &l, nil
}
func (*lifoGroupSpec) Name() string { return LIFOGroupName }
// CreateFilter creates a lifoGroupFilter, that will use a queue based
// queue for handling requests instead of the fifo queue. The first
// parameter is the Name, the second MaxConcurrency, the third
// MaxQueueSize and the fourth Timeout.
//
// The Name parameter is used to group the queue by one or
// multiple routes. All other parameters are optional and defaults to
// MaxConcurrency 100, MaxQueueSize 100, Timeout 10s. If the
// configuration for the same Name is different the behavior is
// undefined.
//
// The implementation is based on
// https://godoc.org/github.com/aryszka/jobqueue, which provides more
// detailed documentation.
//
// The total maximum number of requests has to be computed by adding
// MaxConcurrency and MaxQueueSize: total max = MaxConcurrency + MaxQueueSize
//
// Min values are 1 for MaxConcurrency and MaxQueueSize, and 1ms for
// Timeout. All configration that is below will be set to these min
// values.
//
// It is enough to set the concurrency, queue size and timeout parameters for
// one instance of the filter in the group, and only the group name for the
// rest. Setting these values for multiple instances is fine, too. While only
// one of them will be used as the source for the applied settings, if there
// is accidentally a difference between the settings in the same group, a
// warning will be logged.
//
func (*lifoGroupSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) < 1 || len(args) > 4 {
return nil, filters.ErrInvalidFilterParameters
}
l := &lifoGroupFilter{}
switch v := args[0].(type) {
case string:
l.name = v
default:
return nil, filters.ErrInvalidFilterParameters
}
// set defaults
cfg := scheduler.Config{
MaxConcurrency: defaultMaxConcurreny,
MaxQueueSize: defaultMaxQueueSize,
Timeout: defaultTimeout,
}
l.config = cfg
if len(args) > 1 {
l.hasConfig = true
c, err := intArg(args[1])
if err != nil {
return nil, err
}
if c >= 1 {
l.config.MaxConcurrency = c
}
}
if len(args) > 2 {
c, err := intArg(args[2])
if err != nil {
return nil, err
}
if c >= 1 {
l.config.MaxQueueSize = c
}
}
if len(args) > 3 {
d, err := durationArg(args[3])
if err != nil {
return nil, err
}
if d >= 1*time.Millisecond {
l.config.Timeout = d
}
}
return l, nil
}
// Config returns the scheduler configuration for the given filter
func (l *lifoFilter) Config() scheduler.Config {
return l.config
}
// SetQueue binds the queue to the current filter context
func (l *lifoFilter) SetQueue(q *scheduler.Queue) {
l.queue = q
}
// GetQueue is only used in tests.
func (l *lifoFilter) GetQueue() *scheduler.Queue {
return l.queue
}
// Request is the filter.Filter interface implementation. Request will
// increase the number of inflight requests and respond to the caller,
// if the bounded queue returns an error. Status code by Error:
//
// - 503 if jobqueue.ErrQueueFull
// - 502 if jobqueue.ErrTimeout
func (l *lifoFilter) Request(ctx filters.FilterContext) {
request(l.GetQueue(), scheduler.LIFOKey, ctx)
}
// Response is the filter.Filter interface implementation. Response
// will decrease the number of inflight requests.
func (l *lifoFilter) Response(ctx filters.FilterContext) {
response(scheduler.LIFOKey, ctx)
}
func (l *lifoGroupFilter) Group() string {
return l.name
}
func (l *lifoGroupFilter) HasConfig() bool {
return l.hasConfig
}
// Config returns the scheduler configuration for the given filter
func (l *lifoGroupFilter) Config() scheduler.Config {
return l.config
}
// SetQueue binds the queue to the current filter context
func (l *lifoGroupFilter) SetQueue(q *scheduler.Queue) {
l.queue = q
}
// GetQueue is only used in tests
func (l *lifoGroupFilter) GetQueue() *scheduler.Queue {
return l.queue
}
// Request is the filter.Filter interface implementation. Request will
// increase the number of inflight requests and respond to the caller,
// if the bounded queue returns an error. Status code by Error:
//
// - 503 if jobqueue.ErrStackFull
// - 502 if jobqueue.ErrTimeout
func (l *lifoGroupFilter) Request(ctx filters.FilterContext) {
request(l.GetQueue(), scheduler.LIFOKey, ctx)
}
// Response is the filter.Filter interface implementation. Response
// will decrease the number of inflight requests.
func (l *lifoGroupFilter) Response(ctx filters.FilterContext) {
response(scheduler.LIFOKey, ctx)
}
func request(q *scheduler.Queue, key string, ctx filters.FilterContext) {
if q == nil {
log.Warningf("Unexpected scheduler.Queue is nil for key %s", key)
return
}
done, err := q.Wait()
if err != nil {
// TODO: replace the log with metrics
switch err {
case jobqueue.ErrStackFull:
log.Errorf("Failed to get an entry on to the queue to process QueueFull: %v for host %s", err, ctx.Request().Host)
ctx.Serve(&http.Response{
StatusCode: http.StatusServiceUnavailable,
Status: "Queue Full - https://opensource.zalando.com/skipper/operation/operation/#scheduler",
})
case jobqueue.ErrTimeout:
log.Errorf("Failed to get an entry on to the queue to process Timeout: %v for host %s", err, ctx.Request().Host)
ctx.Serve(&http.Response{
StatusCode: http.StatusBadGateway,
Status: "Queue timeout - https://opensource.zalando.com/skipper/operation/operation/#scheduler",
})
default:
log.Errorf("Unknown error for route based LIFO: %v for host %s", err, ctx.Request().Host)
ctx.Serve(&http.Response{StatusCode: http.StatusInternalServerError})
}
return
}
pending, _ := ctx.StateBag()[key].([]func())
ctx.StateBag()[key] = append(pending, done)
}
func response(key string, ctx filters.FilterContext) {
pending, _ := ctx.StateBag()[key].([]func())
last := len(pending) - 1
if last < 0 {
return
}
pending[last]()
ctx.StateBag()[key] = pending[:last]
}