-
Notifications
You must be signed in to change notification settings - Fork 623
/
Copy pathbulk_indexer.go
571 lines (500 loc) · 14 KB
/
bulk_indexer.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
package esutil
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
// BulkIndexer represents a parallel, asynchronous, efficient indexer for Elasticsearch.
//
type BulkIndexer interface {
// Add adds an item to the indexer. It returns an error when the item cannot be added.
// Use the OnSuccess and OnFailure callbacks to get the operation result for the item.
//
// You must call the Close() method after you're done adding items.
//
// It is safe for concurrent use. When it's called from goroutines,
// they must finish before the call to Close, eg. using sync.WaitGroup.
Add(context.Context, BulkIndexerItem) error
// Close waits until all added items are flushed and closes the indexer.
Close(context.Context) error
// Stats returns indexer statistics.
Stats() BulkIndexerStats
}
// BulkIndexerConfig represents configuration of the indexer.
//
type BulkIndexerConfig struct {
NumWorkers int // The number of workers. Defaults to runtime.NumCPU().
FlushBytes int // The flush threshold in bytes. Defaults to 5MB.
FlushInterval time.Duration // The flush threshold as duration. Defaults to 30sec.
Client *elasticsearch.Client // The Elasticsearch client.
Decoder BulkResponseJSONDecoder // A custom JSON decoder.
DebugLogger BulkIndexerDebugLogger // An optional logger for debugging.
OnError func(context.Context, error) // Called for indexer errors.
OnFlushStart func(context.Context) context.Context // Called when the flush starts.
OnFlushEnd func(context.Context) // Called when the flush ends.
// Parameters of the Bulk API.
Index string
ErrorTrace bool
FilterPath []string
Header http.Header
Human bool
Pipeline string
Pretty bool
Refresh string
Routing string
Source []string
SourceExcludes []string
SourceIncludes []string
Timeout time.Duration
WaitForActiveShards string
}
// BulkIndexerStats represents the indexer statistics.
//
type BulkIndexerStats struct {
NumAdded uint64
NumFlushed uint64
NumFailed uint64
NumIndexed uint64
NumCreated uint64
NumUpdated uint64
NumDeleted uint64
NumRequests uint64
}
// BulkIndexerItem represents an indexer item.
//
type BulkIndexerItem struct {
Index string
Action string
DocumentID string
Body io.Reader
RetryOnConflict *int
OnSuccess func(context.Context, BulkIndexerItem, BulkIndexerResponseItem) // Per item
OnFailure func(context.Context, BulkIndexerItem, BulkIndexerResponseItem, error) // Per item
}
// BulkIndexerResponse represents the Elasticsearch response.
//
type BulkIndexerResponse struct {
Took int `json:"took"`
HasErrors bool `json:"errors"`
Items []map[string]BulkIndexerResponseItem `json:"items,omitempty"`
}
// BulkIndexerResponseItem represents the Elasticsearch response item.
//
type BulkIndexerResponseItem struct {
Index string `json:"_index"`
DocumentID string `json:"_id"`
Version int64 `json:"_version"`
Result string `json:"result"`
Status int `json:"status"`
SeqNo int64 `json:"_seq_no"`
PrimTerm int64 `json:"_primary_term"`
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
} `json:"_shards"`
Error struct {
Type string `json:"type"`
Reason string `json:"reason"`
Cause struct {
Type string `json:"type"`
Reason string `json:"reason"`
} `json:"caused_by"`
} `json:"error,omitempty"`
}
// BulkResponseJSONDecoder defines the interface for custom JSON decoders.
//
type BulkResponseJSONDecoder interface {
UnmarshalFromReader(io.Reader, *BulkIndexerResponse) error
}
// BulkIndexerDebugLogger defines the interface for a debugging logger.
//
type BulkIndexerDebugLogger interface {
Printf(string, ...interface{})
}
type bulkIndexer struct {
wg sync.WaitGroup
queue chan BulkIndexerItem
workers []*worker
ticker *time.Ticker
done chan bool
stats *bulkIndexerStats
config BulkIndexerConfig
}
type bulkIndexerStats struct {
numAdded uint64
numFlushed uint64
numFailed uint64
numIndexed uint64
numCreated uint64
numUpdated uint64
numDeleted uint64
numRequests uint64
}
// NewBulkIndexer creates a new bulk indexer.
//
func NewBulkIndexer(cfg BulkIndexerConfig) (BulkIndexer, error) {
if cfg.Client == nil {
cfg.Client, _ = elasticsearch.NewDefaultClient()
}
if cfg.Decoder == nil {
cfg.Decoder = defaultJSONDecoder{}
}
if cfg.NumWorkers == 0 {
cfg.NumWorkers = runtime.NumCPU()
}
if cfg.FlushBytes == 0 {
cfg.FlushBytes = 5e+6
}
if cfg.FlushInterval == 0 {
cfg.FlushInterval = 30 * time.Second
}
bi := bulkIndexer{
config: cfg,
done: make(chan bool),
stats: &bulkIndexerStats{},
}
bi.init()
return &bi, nil
}
// Add adds an item to the indexer.
//
// Adding an item after a call to Close() will panic.
//
func (bi *bulkIndexer) Add(ctx context.Context, item BulkIndexerItem) error {
atomic.AddUint64(&bi.stats.numAdded, 1)
select {
case <-ctx.Done():
if bi.config.OnError != nil {
bi.config.OnError(ctx, ctx.Err())
}
return ctx.Err()
case bi.queue <- item:
}
return nil
}
// Close stops the periodic flush, closes the indexer queue channel,
// notifies the done channel and calls flush on all writers.
//
func (bi *bulkIndexer) Close(ctx context.Context) error {
bi.ticker.Stop()
close(bi.queue)
bi.done <- true
select {
case <-ctx.Done():
if bi.config.OnError != nil {
bi.config.OnError(ctx, ctx.Err())
}
return ctx.Err()
default:
bi.wg.Wait()
}
for _, w := range bi.workers {
w.mu.Lock()
if w.buf.Len() > 0 {
if err := w.flush(ctx); err != nil {
w.mu.Unlock()
if bi.config.OnError != nil {
bi.config.OnError(ctx, err)
}
continue
}
}
w.mu.Unlock()
}
return nil
}
// Stats returns indexer statistics.
//
func (bi *bulkIndexer) Stats() BulkIndexerStats {
return BulkIndexerStats{
NumAdded: atomic.LoadUint64(&bi.stats.numAdded),
NumFlushed: atomic.LoadUint64(&bi.stats.numFlushed),
NumFailed: atomic.LoadUint64(&bi.stats.numFailed),
NumIndexed: atomic.LoadUint64(&bi.stats.numIndexed),
NumCreated: atomic.LoadUint64(&bi.stats.numCreated),
NumUpdated: atomic.LoadUint64(&bi.stats.numUpdated),
NumDeleted: atomic.LoadUint64(&bi.stats.numDeleted),
NumRequests: atomic.LoadUint64(&bi.stats.numRequests),
}
}
// init initializes the bulk indexer.
//
func (bi *bulkIndexer) init() {
bi.queue = make(chan BulkIndexerItem, bi.config.NumWorkers)
for i := 1; i <= bi.config.NumWorkers; i++ {
w := worker{
id: i,
ch: bi.queue,
bi: bi,
buf: bytes.NewBuffer(make([]byte, 0, bi.config.FlushBytes)),
aux: make([]byte, 0, 512)}
w.run()
bi.workers = append(bi.workers, &w)
}
bi.wg.Add(bi.config.NumWorkers)
bi.ticker = time.NewTicker(bi.config.FlushInterval)
go func() {
ctx := context.Background()
for {
select {
case <-bi.done:
return
case <-bi.ticker.C:
if bi.config.DebugLogger != nil {
bi.config.DebugLogger.Printf("[indexer] Auto-flushing workers after %s\n", bi.config.FlushInterval)
}
for _, w := range bi.workers {
w.mu.Lock()
if w.buf.Len() > 0 {
if err := w.flush(ctx); err != nil {
w.mu.Unlock()
if bi.config.OnError != nil {
bi.config.OnError(ctx, err)
}
continue
}
}
w.mu.Unlock()
}
}
}
}()
}
// worker represents an indexer worker.
//
type worker struct {
id int
ch <-chan BulkIndexerItem
mu sync.Mutex
bi *bulkIndexer
buf *bytes.Buffer
aux []byte
items []BulkIndexerItem
}
// run launches the worker in a goroutine.
//
func (w *worker) run() {
go func() {
ctx := context.Background()
if w.bi.config.DebugLogger != nil {
w.bi.config.DebugLogger.Printf("[worker-%03d] Started\n", w.id)
}
defer w.bi.wg.Done()
for item := range w.ch {
w.mu.Lock()
if w.bi.config.DebugLogger != nil {
w.bi.config.DebugLogger.Printf("[worker-%03d] Received item [%s:%s]\n", w.id, item.Action, item.DocumentID)
}
if err := w.writeMeta(item); err != nil {
if item.OnFailure != nil {
item.OnFailure(ctx, item, BulkIndexerResponseItem{}, err)
}
atomic.AddUint64(&w.bi.stats.numFailed, 1)
w.mu.Unlock()
continue
}
if err := w.writeBody(&item); err != nil {
if item.OnFailure != nil {
item.OnFailure(ctx, item, BulkIndexerResponseItem{}, err)
}
atomic.AddUint64(&w.bi.stats.numFailed, 1)
w.mu.Unlock()
continue
}
w.items = append(w.items, item)
if w.buf.Len() >= w.bi.config.FlushBytes {
if err := w.flush(ctx); err != nil {
w.mu.Unlock()
if w.bi.config.OnError != nil {
w.bi.config.OnError(ctx, err)
}
continue
}
}
w.mu.Unlock()
}
}()
}
// writeMeta formats and writes the item metadata to the buffer; it must be called under a lock.
//
func (w *worker) writeMeta(item BulkIndexerItem) error {
w.buf.WriteRune('{')
w.aux = strconv.AppendQuote(w.aux, item.Action)
w.buf.Write(w.aux)
w.aux = w.aux[:0]
w.buf.WriteRune(':')
w.buf.WriteRune('{')
if item.DocumentID != "" {
w.buf.WriteString(`"_id":`)
w.aux = strconv.AppendQuote(w.aux, item.DocumentID)
w.buf.Write(w.aux)
w.aux = w.aux[:0]
}
if item.Index != "" {
if item.DocumentID != "" {
w.buf.WriteRune(',')
}
w.buf.WriteString(`"_index":`)
w.aux = strconv.AppendQuote(w.aux, item.Index)
w.buf.Write(w.aux)
w.aux = w.aux[:0]
}
w.buf.WriteRune('}')
w.buf.WriteRune('}')
w.buf.WriteRune('\n')
return nil
}
// writeBody writes the item body to the buffer; it must be called under a lock.
//
func (w *worker) writeBody(item *BulkIndexerItem) error {
if item.Body != nil {
var getBody func() io.Reader
if item.OnSuccess != nil || item.OnFailure != nil {
var buf bytes.Buffer
buf.ReadFrom(item.Body)
getBody = func() io.Reader {
r := buf
return ioutil.NopCloser(&r)
}
item.Body = getBody()
}
if _, err := w.buf.ReadFrom(item.Body); err != nil {
if w.bi.config.OnError != nil {
w.bi.config.OnError(context.Background(), err)
}
return err
}
w.buf.WriteRune('\n')
if getBody != nil && (item.OnSuccess != nil || item.OnFailure != nil) {
item.Body = getBody()
}
}
return nil
}
// flush writes out the worker buffer; it must be called under a lock.
//
func (w *worker) flush(ctx context.Context) error {
if w.bi.config.OnFlushStart != nil {
ctx = w.bi.config.OnFlushStart(ctx)
}
if w.bi.config.OnFlushEnd != nil {
defer func() { w.bi.config.OnFlushEnd(ctx) }()
}
if w.buf.Len() < 1 {
if w.bi.config.DebugLogger != nil {
w.bi.config.DebugLogger.Printf("[worker-%03d] Flush: Buffer empty\n", w.id)
}
return nil
}
var (
err error
blk BulkIndexerResponse
)
defer func() {
w.items = w.items[:0]
w.buf.Reset()
}()
if w.bi.config.DebugLogger != nil {
w.bi.config.DebugLogger.Printf("[worker-%03d] Flush: %s\n", w.id, w.buf.String())
}
atomic.AddUint64(&w.bi.stats.numRequests, 1)
req := esapi.BulkRequest{
Index: w.bi.config.Index,
Body: w.buf,
Pipeline: w.bi.config.Pipeline,
Refresh: w.bi.config.Refresh,
Routing: w.bi.config.Routing,
Source: w.bi.config.Source,
SourceExcludes: w.bi.config.SourceExcludes,
SourceIncludes: w.bi.config.SourceIncludes,
Timeout: w.bi.config.Timeout,
WaitForActiveShards: w.bi.config.WaitForActiveShards,
Pretty: w.bi.config.Pretty,
Human: w.bi.config.Human,
ErrorTrace: w.bi.config.ErrorTrace,
FilterPath: w.bi.config.FilterPath,
Header: w.bi.config.Header,
}
res, err := req.Do(ctx, w.bi.config.Client)
if err != nil {
atomic.AddUint64(&w.bi.stats.numFailed, uint64(len(w.items)))
if w.bi.config.OnError != nil {
w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err))
}
return fmt.Errorf("flush: %s", err)
}
if res.Body != nil {
defer res.Body.Close()
}
if res.IsError() {
atomic.AddUint64(&w.bi.stats.numFailed, uint64(len(w.items)))
// TODO(karmi): Wrap error (include response struct)
if w.bi.config.OnError != nil {
w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err))
}
return fmt.Errorf("flush: %s", res.String())
}
if err := w.bi.config.Decoder.UnmarshalFromReader(res.Body, &blk); err != nil {
// TODO(karmi): Wrap error (include response struct)
if w.bi.config.OnError != nil {
w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err))
}
return fmt.Errorf("flush: error parsing response body: %s", err)
}
for i, blkItem := range blk.Items {
var (
item BulkIndexerItem
info BulkIndexerResponseItem
op string
)
item = w.items[i]
// The Elasticsearch bulk response contains an array of maps like this:
// [ { "index": { ... } }, { "create": { ... } }, ... ]
// We range over the map, to set the first key and value as "op" and "info".
for k, v := range blkItem {
op = k
info = v
}
if info.Error.Type != "" || info.Status > 201 {
atomic.AddUint64(&w.bi.stats.numFailed, 1)
if item.OnFailure != nil {
item.OnFailure(ctx, item, info, nil)
}
} else {
atomic.AddUint64(&w.bi.stats.numFlushed, 1)
switch op {
case "index":
atomic.AddUint64(&w.bi.stats.numIndexed, 1)
case "create":
atomic.AddUint64(&w.bi.stats.numCreated, 1)
case "delete":
atomic.AddUint64(&w.bi.stats.numDeleted, 1)
case "update":
atomic.AddUint64(&w.bi.stats.numUpdated, 1)
}
if item.OnSuccess != nil {
item.OnSuccess(ctx, item, info)
}
}
}
return err
}
type defaultJSONDecoder struct{}
func (d defaultJSONDecoder) UnmarshalFromReader(r io.Reader, blk *BulkIndexerResponse) error {
return json.NewDecoder(r).Decode(blk)
}