/
lifo.go
326 lines (279 loc) · 8.26 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
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 {
key string
config scheduler.Config
stack *scheduler.Stack
}
lifoGroupFilter struct {
key string
config scheduler.Config
stack *scheduler.Stack
}
)
const (
LIFOName = "lifo"
LIFOGroupName = "lifoGroup"
defaultMaxConcurreny = 100
defaultMaxStackSize = 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 stack based
// queue for handling requests instead of the fifo queue. The first
// parameter is MaxConcurrency the second MaxStackSize 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, MaxStackSize 100, Timeout 10s.
//
// The total maximum number of requests has to be computed by adding
// MaxConcurrency and MaxStackSize: total max = MaxConcurrency + MaxStackSize
//
// Min values are 1 for MaxConcurrency and MaxStackSize, 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.MaxStackSize = defaultMaxStackSize
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.MaxStackSize = 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 (s *lifoGroupSpec) Name() string { return LIFOGroupName }
// CreateFilter creates a lifoGroupFilter, that will use a stack based
// queue for handling requests instead of the fifo queue. The first
// parameter is the Name, the second MaxConcurrency, the third
// MaxStackSize 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, MaxStackSize 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 MaxStackSize: total max = MaxConcurrency + MaxStackSize
//
// Min values are 1 for MaxConcurrency and MaxStackSize, and 1ms for
// Timeout. All configration that is below will be set to these min
// values.
func (s *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.key = v
default:
return nil, filters.ErrInvalidFilterParameters
}
// set defaults
cfg := scheduler.Config{
MaxConcurrency: defaultMaxConcurreny,
MaxStackSize: defaultMaxStackSize,
Timeout: defaultTimeout,
}
l.config = cfg
if len(args) > 1 {
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.MaxStackSize = 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.Registry) scheduler.Config {
return l.config
}
// SetStack binds the stack to the current filter context
func (l *lifoFilter) SetStack(s *scheduler.Stack) {
l.stack = s
}
// GetStack is only used in tests
func (l *lifoFilter) GetStack() *scheduler.Stack {
return l.stack
}
// Key returns the scheduler string
func (l *lifoFilter) Key() string {
return l.key
}
// SetKey sets the scheduler string, which should be called from the
// PostProcessor
func (l *lifoFilter) SetKey(k string) {
l.key = k
}
// Request is the filter.Filter interface implementation. Request will
// increase the number of inflight requests and respond to the caller,
// if the bounded stack returns an error. Status code by Error:
//
// - 503 if jobqueue.ErrStackFull
// - 502 if jobqueue.ErrTimeout
func (l *lifoFilter) Request(ctx filters.FilterContext) {
request(l.GetStack(), 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)
}
// Config returns the scheduler configuration for the given filter
func (l *lifoGroupFilter) Config(r *scheduler.Registry) scheduler.Config {
return r.Config(l.key)
}
// SetStack binds the stack to the current filter context
func (l *lifoGroupFilter) SetStack(s *scheduler.Stack) {
l.stack = s
}
// GetStack is only used in tests
func (l *lifoGroupFilter) GetStack() *scheduler.Stack {
return l.stack
}
// Key returns the scheduler group string
func (l *lifoGroupFilter) Key() string {
return l.key
}
// SetKey is a noop to implement the LIFOFilter interface
func (*lifoGroupFilter) SetKey(string) {}
// Request is the filter.Filter interface implementation. Request will
// increase the number of inflight requests and respond to the caller,
// if the bounded stack returns an error. Status code by Error:
//
// - 503 if jobqueue.ErrStackFull
// - 502 if jobqueue.ErrTimeout
func (l *lifoGroupFilter) Request(ctx filters.FilterContext) {
request(l.GetStack(), 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(s *scheduler.Stack, key string, ctx filters.FilterContext) {
if s == nil {
log.Warningf("Unexpected scheduler.Stack is nil for key %s", key)
return
}
done, err := s.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 stack to process StackFull: %v for host %s", err, ctx.Request().Host)
ctx.Serve(&http.Response{StatusCode: http.StatusServiceUnavailable, Status: "Stack Full - https://opensource.zalando.com/skipper/operation/operation/#scheduler"})
case jobqueue.ErrTimeout:
log.Errorf("Failed to get an entry on to the stack to process Timeout: %v for host %s", err, ctx.Request().Host)
ctx.Serve(&http.Response{StatusCode: http.StatusBadGateway, Status: "Stack 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]
}