-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbles.go
541 lines (492 loc) · 12.6 KB
/
bubbles.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Package bubbles is an Elasticsearch bulk insert client.
//
// It connects to an Elasticsearch cluster via the bulk API
// (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html).
// Actions are batched into bulk requests. Actions which resulted
// in an error are retried individually.
package bubbles
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/alicebob/json"
"github.com/realzeitmedia/bubbles/loges"
)
const (
// DefaultMaxDocumentsPerBatch is the number of documents a batch needs to
// have before it is send. This is per connection.
DefaultMaxDocumentsPerBatch = 1000
// DefaultFlushTimeout is the maximum time we batch something before we try
// to send it to a server.
DefaultFlushTimeout = 10 * time.Second
// DefaultServerTimeout is the time we give ElasticSearch to respond.
DefaultServerTimeout = 1 * time.Minute
// DefaultConnCount is the number of connections per hosts.
DefaultConnCount = 2
// tuneTimeoutRatio determines when we start backing off to keep the request
// duration under control.
tuneTimeoutRatio = 6
serverErrorWait = 50 * time.Millisecond
serverErrorWaitMax = 10 * time.Second
defaultElasticSearchPort = "9200"
)
var (
errInvalidResponse = errors.New("invalid response")
)
// Bubbles is the main struct to control a queue of Actions going to the
// ElasticSearch servers.
type Bubbles struct {
q chan Action
retryQ chan Action
quit chan struct{}
clientWg sync.WaitGroup
docWg sync.WaitGroup
maxDocumentCount int
connCount int
flushTimeout time.Duration
serverTimeout time.Duration
c loges.Counter
e loges.Errer
}
// Opt is any option to New().
type Opt func(*Bubbles)
// OptConnCount is an option to New() to specify the number of connections per
// host. The default is DefaultConnCount.
func OptConnCount(n int) Opt {
return func(b *Bubbles) {
b.connCount = n
}
}
// OptFlush is an option to New() to specify the flush timeout of a batch. The
// default is DefaultFlushTimeout.
func OptFlush(d time.Duration) Opt {
return func(b *Bubbles) {
b.flushTimeout = d
}
}
// OptServerTimeout is an option to New() to specify the timeout of a single
// batch POST to ElasticSearch. All actions in a bulk which is timed out will
// be retried. The default is DefaultServerTimeout.
func OptServerTimeout(d time.Duration) Opt {
return func(b *Bubbles) {
b.serverTimeout = d
}
}
// OptMaxDocs is an option to New() to specify maximum number of documents in a
// single batch. The default is DefaultMaxDocumentsPerBatch.
func OptMaxDocs(n int) Opt {
return func(b *Bubbles) {
b.maxDocumentCount = n
}
}
// OptCounter is an option to New() to specify something that counts documents.
func OptCounter(c loges.Counter) Opt {
return func(b *Bubbles) {
b.c = c
}
}
// OptErrer is an option to New() to specify an error handler. The default
// handler uses the log module.
func OptErrer(e loges.Errer) Opt {
return func(b *Bubbles) {
b.e = e
}
}
// New makes a new ElasticSearch bulk inserter. It needs a list with 'ip' or
// 'ip:port' addresses, options are added via the Opt* functions.
func New(addrs []string, opts ...Opt) *Bubbles {
b := Bubbles{
q: make(chan Action),
quit: make(chan struct{}),
maxDocumentCount: DefaultMaxDocumentsPerBatch,
connCount: DefaultConnCount,
flushTimeout: DefaultFlushTimeout,
serverTimeout: DefaultServerTimeout,
c: loges.DefaultCounter{},
e: loges.DefaultErrer{},
}
for _, o := range opts {
o(&b)
}
b.retryQ = make(chan Action, len(addrs)*b.connCount*b.maxDocumentCount)
cl := &http.Client{
Timeout: b.serverTimeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return errors.New("no redirect")
},
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: b.serverTimeout,
KeepAlive: 30 * time.Second,
}).Dial,
MaxIdleConnsPerHost: b.connCount,
DisableCompression: false,
},
}
// Start a go routine per connection per host
for _, a := range addrs {
addr := withPort(a, defaultElasticSearchPort)
for i := 0; i < b.connCount; i++ {
b.clientWg.Add(1)
go func(a string) {
client(&b, cl, a)
b.clientWg.Done()
}(addr)
}
}
return &b
}
// Enqueue returns the queue to add Actions in a routine. It will block if all bulk
// processors are busy.
func (b *Bubbles) Enqueue(a Action) {
b.q <- a
b.docWg.Add(1)
}
// EnqueueSave returns the queue to add Actions in a routine. It will block if
// all bulk processors are busy, or until the quit channel is closed.
func (b *Bubbles) EnqueueSave(a Action, quit <-chan struct{}) bool {
select {
case b.q <- a:
b.docWg.Add(1)
return true
case <-quit:
return false
}
}
// Wait until all queues are empty. Useful for a graceful shutdown.
func (b *Bubbles) Wait(t time.Duration) {
d := make(chan struct{})
go func() {
b.docWg.Wait()
close(d)
}()
select {
case <-d:
case <-time.After(t):
}
}
// Stop shuts down all ElasticSearch clients. It'll return all Action entries
// which were not yet processed, or were up for a retry.
func (b *Bubbles) Stop() []Action {
close(b.quit)
// There is no explicit timeout, we rely on b.serverTimeout to shut down
// everything.
b.clientWg.Wait()
// Collect and return elements which are in flight.
close(b.retryQ)
close(b.q)
pending := make([]Action, 0, len(b.q)+len(b.retryQ))
for a := range b.q {
pending = append(pending, a)
}
for a := range b.retryQ {
pending = append(pending, a)
}
return pending
}
type backoff struct {
level uint8
max uint8
maxBatchSize int
}
func newBackoff(maxBatchSize int) *backoff {
b := &backoff{
maxBatchSize: maxBatchSize,
}
// Max out when both components do.
for ; (b.wait() < serverErrorWaitMax || b.size() > 1) && b.level < 32; b.level++ {
}
b.max = b.level
b.level = 0
return b
}
// wait calculates the delay based on the current backoff level.
func (b *backoff) wait() time.Duration {
if b.level == 0 {
return 0 * time.Second
}
w := (1 << (b.level - 1)) * serverErrorWait
if w >= serverErrorWaitMax {
return serverErrorWaitMax
}
return w
}
// batchSize calculates the batchsize based on the current backoff level.
func (b *backoff) size() int {
s := b.maxBatchSize / (1 << b.level)
if s <= 1 {
return 1
}
return s
}
// inc increases the backoff level.
func (b *backoff) inc() {
if b.level < b.max {
b.level++
}
}
// dec decreases the backoff level.
func (b *backoff) dec() {
if b.level > 0 {
b.level--
}
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
// client talks to ElasticSearch. This runs in a go routine in a loop and deals
// with a single ElasticSearch address.
func client(b *Bubbles, cl *http.Client, addr string) {
var (
url = fmt.Sprintf("http://%s/_bulk", addr)
backoffTrouble = newBackoff(b.maxDocumentCount)
backoffTune = newBackoff(b.maxDocumentCount)
tuneTimeout = b.serverTimeout / tuneTimeoutRatio
scratch = &bytes.Buffer{}
)
for {
select {
case <-b.quit:
return
case <-time.After(backoffTrouble.wait()):
}
tuneMax := backoffTune.size()
trouble, batchTime, sent := runBatch(
b,
cl,
url,
min(backoffTrouble.size(), tuneMax),
scratch,
)
for i := 0; i < sent; i++ {
b.docWg.Done()
}
if trouble {
backoffTrouble.inc()
b.c.Trouble()
} else {
backoffTrouble.dec()
}
if batchTime > tuneTimeout {
backoffTune.inc()
} else if batchTime <= tuneTimeout/2 && sent == tuneMax {
backoffTune.dec()
}
b.c.BatchTime(batchTime)
}
}
// runBatch gathers and deals with a batch of actions. It returns
// whether there was trouble, how long the actual request took, and
// how many items were sent successfully.
func runBatch(
b *Bubbles,
cl *http.Client,
url string,
batchSize int,
scratch *bytes.Buffer,
) (bool, time.Duration, int) {
actions := make([]Action, 0, b.maxDocumentCount)
// First use all retry actions.
retry:
for len(actions) < batchSize {
select {
case a := <-b.retryQ:
actions = append(actions, a)
default:
// no more retry actions queued
break retry
}
}
var t <-chan time.Time
gather:
for len(actions) < batchSize {
if t == nil && len(actions) > 0 {
// Set timeout on the first element we read
t = time.After(b.flushTimeout)
}
select {
case <-b.quit:
for _, a := range actions {
b.retryQ <- a
}
b.c.Actions(0, len(actions), 0)
return false, 0, 0
case <-t:
// this case is not enabled until we've got an action
break gather
case a := <-b.retryQ:
actions = append(actions, a)
case a := <-b.q:
actions = append(actions, a)
}
}
t0 := time.Now()
res, err := postActions(b.c, cl, url, actions, b.quit, scratch)
dt := time.Since(t0)
if err != nil {
// A server error. Retry these actions later.
if !strings.Contains(err.Error(), "net/http: request canceled") {
b.e.Error(err)
}
for _, a := range actions {
b.retryQ <- a
}
b.c.Actions(0, len(actions), 0)
return true, dt, 0
}
// Server has accepted the request an sich, but there can be errors in the
// individual actions.
if !res.Errors {
// Simple case, no errors present.
b.c.Actions(len(actions), 0, 0)
return false, dt, len(actions)
}
// Invalid response from ElasticSearch.
if len(actions) != len(res.Items) {
b.e.Error(errInvalidResponse)
for _, a := range actions {
b.retryQ <- a
}
b.c.Actions(0, len(actions), 0)
return true, dt, 0
}
// Figure out which actions have errors.
var (
retries = 0
errors = 0
)
for i, e := range res.Items {
a := actions[i]
el, ok := e[string(a.Type)]
if !ok {
// cheap fallback. 'Index' sometimes throws 'Create' errors.
el, ok = e[string(Create)]
}
if !ok {
// Unexpected reply from ElasticSearch.
b.e.Error(errInvalidResponse)
b.retryQ <- a
retries++
continue
}
c := el.Status
switch {
case c >= 200 && c < 300:
// Document accepted by ElasticSearch.
case c == 429 || (c >= 500 && c < 600):
// Server error. Retry it.
// We get a 429 when the bulk queue is full, which we just retry as
// well.
b.e.Warning(ActionError{
Action: a,
StatusCode: c,
Server: url,
Elasticsearch: el.Error,
})
b.retryQ <- a
retries++
case c >= 400 && c < 500:
// Some error. Nothing we can do with it.
b.e.Error(ActionError{
Action: a,
StatusCode: c,
Server: url,
Elasticsearch: el.Error,
})
errors++
default:
// No idea.
b.e.Error(fmt.Errorf("unwelcome response %d: %s", c, el.Error))
errors++
}
}
sent := len(actions) - errors - retries
b.c.Actions(sent, retries, errors)
return retries > 0, dt, sent
}
// ESError is a raw Elasticsearch error
type ESError struct {
Type string `json:"type"`
Reason string `json:"reason"`
CausedBy struct {
Type string `json:"type"`
Reason string `json:"reason"`
} `json:"caused_by"`
}
type bulkRes struct {
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Version int `json:"_version"`
Status int `json:"status"`
Error ESError `json:"error"`
} `json:"items"`
}
func postActions(
c loges.Counter,
cl *http.Client,
url string,
actions []Action,
quit <-chan struct{},
buf *bytes.Buffer,
) (*bulkRes, error) {
buf.Reset()
for _, a := range actions {
buf.WriteString(a.Buf())
}
c.SendTotal(buf.Len())
// This doesn't Chunk.
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Cancel = quit
resp, err := cl.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("status: %s", resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var bulk bulkRes
if err := json.Decode(string(b), &bulk); err != nil {
return nil, fmt.Errorf("err %s in %q", err, string(b))
}
return &bulk, nil
}
// ActionError wraps an Action we won't retry. It implements the error interface.
type ActionError struct {
Action Action
StatusCode int
Server string
Elasticsearch ESError
}
func (e ActionError) Error() string {
return fmt.Sprintf("%s %s status %d: %s %s", e.Server, e.Action.Type, e.StatusCode, e.Elasticsearch.Type, e.Elasticsearch.Reason)
}
// withPort adds a default port to an address string.
func withPort(a, port string) string {
if _, _, err := net.SplitHostPort(a); err != nil {
// no port found.
return net.JoinHostPort(a, port)
}
return a
}