-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathbulk_indexer_internal_test.go
967 lines (847 loc) · 25.5 KB
/
bulk_indexer_internal_test.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build !integration
// +build !integration
package esutil
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/elastic/elastic-transport-go/v8/elastictransport"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
var defaultRoundTripFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{}`))}, nil
}
type mockTransport struct {
RoundTripFunc func(*http.Request) (*http.Response, error)
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.RoundTripFunc == nil {
return defaultRoundTripFunc(req)
}
return t.RoundTripFunc(req)
}
func TestBulkIndexer(t *testing.T) {
t.Run("Basic", func(t *testing.T) {
var (
wg sync.WaitGroup
countReqs int
testfile string
numItems = 6
)
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
countReqs++
switch countReqs {
case 1:
testfile = "testdata/bulk_response_1a.json"
case 2:
testfile = "testdata/bulk_response_1b.json"
case 3:
testfile = "testdata/bulk_response_1c.json"
}
bodyContent, _ := ioutil.ReadFile(testfile)
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
}})
cfg := BulkIndexerConfig{
NumWorkers: 1,
FlushBytes: 39 * 2, // 38 bytes header + body, times 2 to match 2 responses per file in testdata
FlushInterval: time.Hour, // Disable auto-flushing, because response doesn't match number of items
Client: es}
if os.Getenv("DEBUG") != "" {
cfg.DebugLogger = log.New(os.Stdout, "", 0)
}
bi, _ := NewBulkIndexer(cfg)
for i := 1; i <= numItems; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
err := bi.Add(context.Background(), BulkIndexerItem{
Action: "foo",
DocumentID: strconv.Itoa(i),
Body: strings.NewReader(fmt.Sprintf(`{"title":"foo-%d"}`, i)),
})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
}(i)
}
wg.Wait()
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
stats := bi.Stats()
// added = numitems
if stats.NumAdded != uint64(numItems) {
t.Errorf("Unexpected NumAdded: want=%d, got=%d", numItems, stats.NumAdded)
}
// flushed = numitems - 1x conflict + 1x not_found
if stats.NumFlushed != uint64(numItems-2) {
t.Errorf("Unexpected NumFlushed: want=%d, got=%d", numItems-2, stats.NumFlushed)
}
// failed = 1x conflict + 1x not_found
if stats.NumFailed != 2 {
t.Errorf("Unexpected NumFailed: want=%d, got=%d", 2, stats.NumFailed)
}
// indexed = 1x
if stats.NumIndexed != 1 {
t.Errorf("Unexpected NumIndexed: want=%d, got=%d", 1, stats.NumIndexed)
}
// created = 1x
if stats.NumCreated != 1 {
t.Errorf("Unexpected NumCreated: want=%d, got=%d", 1, stats.NumCreated)
}
// deleted = 1x
if stats.NumDeleted != 1 {
t.Errorf("Unexpected NumDeleted: want=%d, got=%d", 1, stats.NumDeleted)
}
if stats.NumUpdated != 1 {
t.Errorf("Unexpected NumUpdated: want=%d, got=%d", 1, stats.NumUpdated)
}
// 3 items * 40 bytes, 2 workers, 1 request per worker
if stats.NumRequests != 3 {
t.Errorf("Unexpected NumRequests: want=%d, got=%d", 3, stats.NumRequests)
}
})
t.Run("Add() Timeout", func(t *testing.T) {
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{}})
bi, _ := NewBulkIndexer(BulkIndexerConfig{NumWorkers: 1, Client: es})
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
time.Sleep(100 * time.Millisecond)
var errs []error
for i := 0; i < 10; i++ {
errs = append(errs, bi.Add(ctx, BulkIndexerItem{Action: "delete", DocumentID: "timeout"}))
}
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
var gotError bool
for _, err := range errs {
if err != nil && err.Error() == "context deadline exceeded" {
gotError = true
}
}
if !gotError {
t.Errorf("Expected timeout error, but none in: %q", errs)
}
})
t.Run("Close() Cancel", func(t *testing.T) {
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{}})
bi, _ := NewBulkIndexer(BulkIndexerConfig{
NumWorkers: 1,
FlushBytes: 1,
Client: es,
})
for i := 0; i < 10; i++ {
bi.Add(context.Background(), BulkIndexerItem{Action: "foo"})
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := bi.Close(ctx); err == nil {
t.Errorf("Expected context cancelled error, but got: %v", err)
}
})
t.Run("Indexer Callback", func(t *testing.T) {
esCfg := elasticsearch.Config{
Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
return nil, fmt.Errorf("Mock transport error")
},
},
}
if os.Getenv("DEBUG") != "" {
esCfg.Logger = &elastictransport.ColorLogger{
Output: os.Stdout,
EnableRequestBody: true,
EnableResponseBody: true,
}
}
es, _ := elasticsearch.NewClient(esCfg)
var indexerError error
biCfg := BulkIndexerConfig{
NumWorkers: 1,
Client: es,
OnError: func(ctx context.Context, err error) { indexerError = err },
}
if os.Getenv("DEBUG") != "" {
biCfg.DebugLogger = log.New(os.Stdout, "", 0)
}
bi, _ := NewBulkIndexer(biCfg)
if err := bi.Add(context.Background(), BulkIndexerItem{
Action: "foo",
}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
bi.Close(context.Background())
if indexerError == nil {
t.Errorf("Expected indexerError to not be nil")
}
})
t.Run("Item Callbacks", func(t *testing.T) {
var (
countSuccessful uint64
countFailed uint64
failedIDs []string
successfulItemBodies []string
failedItemBodies []string
numItems = 4
numFailed = 2
bodyContent, _ = ioutil.ReadFile("testdata/bulk_response_2.json")
)
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
}})
cfg := BulkIndexerConfig{NumWorkers: 1, Client: es}
if os.Getenv("DEBUG") != "" {
cfg.DebugLogger = log.New(os.Stdout, "", 0)
}
bi, _ := NewBulkIndexer(cfg)
successFunc := func(ctx context.Context, item BulkIndexerItem, res BulkIndexerResponseItem) {
atomic.AddUint64(&countSuccessful, 1)
buf, err := ioutil.ReadAll(item.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
successfulItemBodies = append(successfulItemBodies, string(buf))
}
failureFunc := func(ctx context.Context, item BulkIndexerItem, res BulkIndexerResponseItem, err error) {
atomic.AddUint64(&countFailed, 1)
failedIDs = append(failedIDs, item.DocumentID)
buf, err := ioutil.ReadAll(item.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
failedItemBodies = append(failedItemBodies, string(buf))
}
if err := bi.Add(context.Background(), BulkIndexerItem{
Action: "index",
DocumentID: "1",
Body: strings.NewReader(`{"title":"foo"}`),
OnSuccess: successFunc,
OnFailure: failureFunc,
}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Add(context.Background(), BulkIndexerItem{
Action: "create",
DocumentID: "1",
Body: strings.NewReader(`{"title":"bar"}`),
OnSuccess: successFunc,
OnFailure: failureFunc,
}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Add(context.Background(), BulkIndexerItem{
Action: "delete",
DocumentID: "2",
Body: strings.NewReader(`{"title":"baz"}`),
OnSuccess: successFunc,
OnFailure: failureFunc,
}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Add(context.Background(), BulkIndexerItem{
Action: "update",
DocumentID: "3",
Body: strings.NewReader(`{"doc":{"title":"qux"}}`),
OnSuccess: successFunc,
OnFailure: failureFunc,
}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
stats := bi.Stats()
if stats.NumAdded != uint64(numItems) {
t.Errorf("Unexpected NumAdded: %d", stats.NumAdded)
}
// Two failures are expected:
//
// * Operation #2: document can't be created, because a document with the same ID already exists.
// * Operation #3: document can't be deleted, because it doesn't exist.
if stats.NumFailed != uint64(numFailed) {
t.Errorf("Unexpected NumFailed: %d", stats.NumFailed)
}
if stats.NumFlushed != 2 {
t.Errorf("Unexpected NumFailed: %d", stats.NumFailed)
}
if stats.NumIndexed != 1 {
t.Errorf("Unexpected NumIndexed: %d", stats.NumIndexed)
}
if stats.NumUpdated != 1 {
t.Errorf("Unexpected NumUpdated: %d", stats.NumUpdated)
}
if countSuccessful != uint64(numItems-numFailed) {
t.Errorf("Unexpected countSuccessful: %d", countSuccessful)
}
if countFailed != uint64(numFailed) {
t.Errorf("Unexpected countFailed: %d", countFailed)
}
if !reflect.DeepEqual(failedIDs, []string{"1", "2"}) {
t.Errorf("Unexpected failedIDs: %#v", failedIDs)
}
if !reflect.DeepEqual(successfulItemBodies, []string{`{"title":"foo"}`, `{"doc":{"title":"qux"}}`}) {
t.Errorf("Unexpected successfulItemBodies: %#v", successfulItemBodies)
}
if !reflect.DeepEqual(failedItemBodies, []string{`{"title":"bar"}`, `{"title":"baz"}`}) {
t.Errorf("Unexpected failedItemBodies: %#v", failedItemBodies)
}
})
t.Run("OnFlush callbacks", func(t *testing.T) {
type contextKey string
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{}})
bi, _ := NewBulkIndexer(BulkIndexerConfig{
Client: es,
Index: "foo",
OnFlushStart: func(ctx context.Context) context.Context {
fmt.Println(">>> Flush started")
return context.WithValue(ctx, contextKey("start"), time.Now().UTC())
},
OnFlushEnd: func(ctx context.Context) {
var duration time.Duration
if v := ctx.Value("start"); v != nil {
duration = time.Since(v.(time.Time))
}
fmt.Printf(">>> Flush finished (duration: %s)\n", duration)
},
})
err := bi.Add(context.Background(), BulkIndexerItem{
Action: "index",
Body: strings.NewReader(`{"title":"foo"}`),
})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
stats := bi.Stats()
if stats.NumAdded != uint64(1) {
t.Errorf("Unexpected NumAdded: %d", stats.NumAdded)
}
})
t.Run("Automatic flush", func(t *testing.T) {
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(strings.NewReader(`{"items":[{"index": {}}]}`)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
}})
cfg := BulkIndexerConfig{
NumWorkers: 1,
Client: es,
FlushInterval: 50 * time.Millisecond, // Decrease the flush timeout
}
if os.Getenv("DEBUG") != "" {
cfg.DebugLogger = log.New(os.Stdout, "", 0)
}
bi, _ := NewBulkIndexer(cfg)
bi.Add(context.Background(),
BulkIndexerItem{Action: "index", Body: strings.NewReader(`{"title":"foo"}`)})
// Allow some time for auto-flush to kick in
time.Sleep(250 * time.Millisecond)
stats := bi.Stats()
expected := uint64(1)
if stats.NumAdded != expected {
t.Errorf("Unexpected NumAdded: want=%d, got=%d", expected, stats.NumAdded)
}
if stats.NumFailed != 0 {
t.Errorf("Unexpected NumFailed: want=%d, got=%d", 0, stats.NumFlushed)
}
if stats.NumFlushed != expected {
t.Errorf("Unexpected NumFlushed: want=%d, got=%d", expected, stats.NumFlushed)
}
if stats.NumIndexed != expected {
t.Errorf("Unexpected NumIndexed: want=%d, got=%d", expected, stats.NumIndexed)
}
// Wait some time before closing the indexer to clear the timer
time.Sleep(200 * time.Millisecond)
bi.Close(context.Background())
})
t.Run("TooManyRequests", func(t *testing.T) {
var (
wg sync.WaitGroup
countReqs int
numItems = 2
)
esCfg := elasticsearch.Config{
Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
countReqs++
if countReqs <= 4 {
return &http.Response{
StatusCode: http.StatusTooManyRequests,
Status: "429 TooManyRequests",
Body: ioutil.NopCloser(strings.NewReader(`{"took":1}`)),
}, nil
}
bodyContent, _ := ioutil.ReadFile("testdata/bulk_response_1c.json")
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
},
MaxRetries: 5,
RetryOnStatus: []int{502, 503, 504, 429},
RetryBackoff: func(i int) time.Duration {
if os.Getenv("DEBUG") != "" {
fmt.Printf("*** Retry #%d\n", i)
}
return time.Duration(i) * 100 * time.Millisecond
},
}
if os.Getenv("DEBUG") != "" {
esCfg.Logger = &elastictransport.ColorLogger{Output: os.Stdout}
}
es, _ := elasticsearch.NewClient(esCfg)
biCfg := BulkIndexerConfig{NumWorkers: 1, FlushBytes: 28 * 2, Client: es}
if os.Getenv("DEBUG") != "" {
biCfg.DebugLogger = log.New(os.Stdout, "", 0)
}
bi, _ := NewBulkIndexer(biCfg)
for i := 1; i <= numItems; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
err := bi.Add(context.Background(), BulkIndexerItem{
Action: "foo",
Body: strings.NewReader(`{"title":"foo"}`),
})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
}(i)
}
wg.Wait()
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
stats := bi.Stats()
if stats.NumAdded != uint64(numItems) {
t.Errorf("Unexpected NumAdded: want=%d, got=%d", numItems, stats.NumAdded)
}
if stats.NumFlushed != uint64(numItems) {
t.Errorf("Unexpected NumFlushed: want=%d, got=%d", numItems, stats.NumFlushed)
}
if stats.NumFailed != 0 {
t.Errorf("Unexpected NumFailed: want=%d, got=%d", 0, stats.NumFailed)
}
// Stats don't include the retries in client
if stats.NumRequests != 1 {
t.Errorf("Unexpected NumRequests: want=%d, got=%d", 1, stats.NumRequests)
}
})
t.Run("Custom JSON Decoder", func(t *testing.T) {
es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{}})
bi, _ := NewBulkIndexer(BulkIndexerConfig{Client: es, Decoder: customJSONDecoder{}})
err := bi.Add(context.Background(), BulkIndexerItem{
Action: "index",
DocumentID: "1",
Body: strings.NewReader(`{"title":"foo"}`),
})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bi.Close(context.Background()); err != nil {
t.Errorf("Unexpected error: %s", err)
}
stats := bi.Stats()
if stats.NumAdded != uint64(1) {
t.Errorf("Unexpected NumAdded: %d", stats.NumAdded)
}
})
t.Run("Worker.writeMeta()", func(t *testing.T) {
v := int64(23)
ifSeqNo := int64(45)
ifPrimaryTerm := int64(67)
type args struct {
item BulkIndexerItem
}
tests := []struct {
name string
args args
want string
}{
{
"without _index and _id",
args{BulkIndexerItem{Action: "index"}},
`{"index":{}}` + "\n",
},
{
"with _id",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
}},
`{"index":{"_id":"42"}}` + "\n",
},
{
"with _index",
args{BulkIndexerItem{
Action: "index",
Index: "test",
}},
`{"index":{"_index":"test"}}` + "\n",
},
{
"with _index and _id",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
Index: "test",
}},
`{"index":{"_id":"42","_index":"test"}}` + "\n",
},
{
"with version and no document",
args{BulkIndexerItem{
Action: "index",
Index: "test",
Version: &v,
}},
`{"index":{"_index":"test"}}` + "\n",
},
{
"with version",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
Index: "test",
Version: &v,
}},
`{"index":{"_id":"42","version":23,"_index":"test"}}` + "\n",
},
{
"with version and version_type",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
Index: "test",
Version: &v,
VersionType: "external",
}},
`{"index":{"_id":"42","version":23,"version_type":"external","_index":"test"}}` + "\n",
},
{
"with require_alias",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
Index: "test",
RequireAlias: true,
}},
`{"index":{"_id":"42","_index":"test","require_alias":true}}` + "\n",
},
{
"with version, version_type and require_alias",
args{BulkIndexerItem{
Action: "index",
DocumentID: "42",
Index: "test",
Version: &v,
VersionType: "external",
RequireAlias: true,
}},
`{"index":{"_id":"42","version":23,"version_type":"external","_index":"test","require_alias":true}}` + "\n",
},
{
"with retry_on_conflict and bad action",
args{BulkIndexerItem{
Action: "index",
DocumentID: "1",
RetryOnConflict: esapi.IntPtr(3),
}},
`{"index":{"_id":"1"}}` + "\n",
},
{
"with retry_on_conflict",
args{BulkIndexerItem{
Action: "update",
DocumentID: "1",
RetryOnConflict: esapi.IntPtr(3),
}},
`{"update":{"_id":"1","retry_on_conflict":3}}` + "\n",
},
{
"with if_seq_no and if_primary_term",
args{BulkIndexerItem{
Action: "index",
DocumentID: "1",
IfSeqNo: &ifSeqNo,
IfPrimaryTerm: &ifPrimaryTerm,
}},
`{"index":{"_id":"1","if_seq_no":45,"if_primary_term":67}}` + "\n",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
w := &worker{
buf: bytes.NewBuffer(make([]byte, 0, 5e+6)),
}
tt.args.item.marshallMeta()
if err := w.writeMeta(&tt.args.item); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if w.buf.String() != tt.want {
t.Errorf("worker.writeMeta() %s = got [%s], want [%s]", tt.name, w.buf.String(), tt.want)
}
})
}
})
t.Run("MetaHeader presence in Request header", func(t *testing.T) {
type args struct {
disableMetaHeader bool
header http.Header
}
tests := []struct {
name string
args args
want string
}{
{
name: "Meta header is present in header",
args: args{
disableMetaHeader: false,
},
want: `^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*,h=bp$`,
},
{
name: "Header should be empty of meta header",
args: args{
disableMetaHeader: true,
},
want: ``,
},
{
name: "User has set header",
args: args{
disableMetaHeader: false,
header: http.Header{
"X-Test-User": []string{"UserValue"},
},
},
},
{
name: "User should not temper with Meta Header",
args: args{
disableMetaHeader: false,
header: http.Header{
"X-Test-User": []string{"UserValue"},
elasticsearch.HeaderClientMeta: []string{"h=shouldntbechanged"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reValidation := regexp.MustCompile(tt.want)
esConfig := elasticsearch.Config{
DisableMetaHeader: tt.args.disableMetaHeader,
Header: tt.args.header,
Transport: &mockTransport{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
headerMeta := request.Header.Get(elasticsearch.HeaderClientMeta)
if !reValidation.MatchString(headerMeta) {
t.Errorf("Meta Header presence is invalid, got : %s, want : %s", headerMeta, tt.want)
}
if tt.args.disableMetaHeader && headerMeta != "" {
t.Errorf("Meta Header is disabled, should be empty, got : %s", headerMeta)
}
if tt.args.header != nil {
if userHeader := request.Header.Get("X-Test-User"); userHeader != "UserValue" {
t.Errorf("User header should be preserved, got : %s", userHeader)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: io.NopCloser(bytes.NewBuffer(nil)),
}, nil
},
},
}
client, err := elasticsearch.NewClient(esConfig)
if err != nil {
log.Fatal(err)
}
cfg := BulkIndexerConfig{
Client: client,
Header: tt.args.header,
}
bi, err := NewBulkIndexer(cfg)
if err != nil {
log.Fatal(err)
}
err = bi.Add(context.Background(), BulkIndexerItem{
Action: "foo",
DocumentID: strconv.Itoa(1),
Body: strings.NewReader(fmt.Sprintf(`{"title":"foo-%d"}`, 1)),
})
if err != nil {
log.Fatal(err)
}
bi.Close(context.Background())
})
}
})
t.Run("Concurrent Flushing", func(t *testing.T) {
esConfig := elasticsearch.Config{
Transport: &mockTransport{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: io.NopCloser(bytes.NewBuffer(nil)),
}, nil
},
},
}
client, err := elasticsearch.NewClient(esConfig)
if err != nil {
log.Fatal(err)
}
cfg := BulkIndexerConfig{
NumWorkers: 10,
Client: client,
Header: http.Header{"X-Test": []string{"TestValue"}},
FlushBytes: 1,
}
bi, err := NewBulkIndexer(cfg)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 100; i++ {
err = bi.Add(context.Background(), BulkIndexerItem{
Action: "foo",
DocumentID: strconv.Itoa(1),
Body: strings.NewReader(`{"title":"foo"}`),
})
}
if err != nil {
log.Fatal(err)
}
bi.Close(context.Background())
})
t.Run("Oversized Payload", func(t *testing.T) {
esConfig := elasticsearch.Config{
Transport: &mockTransport{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: io.NopCloser(bytes.NewBuffer(nil)),
}, nil
},
},
}
client, err := elasticsearch.NewClient(esConfig)
if err != nil {
log.Fatal(err)
}
logbuf := bytes.Buffer{}
cfg := BulkIndexerConfig{
NumWorkers: 1,
Client: client,
Header: http.Header{"X-Test": []string{"TestValue"}},
FlushBytes: 1,
DebugLogger: log.New(&logbuf, "", 0),
}
bi, err := NewBulkIndexer(cfg)
if err != nil {
log.Fatal(err)
}
bi.Add(context.Background(), BulkIndexerItem{
Action: "index",
DocumentID: strconv.Itoa(1),
Body: strings.NewReader(`{"title":"foo"}`),
})
bi.Close(context.Background())
if !bytes.Contains(logbuf.Bytes(), []byte("[worker-001] Oversize Payload in item [index:1]")) {
t.Fatalf("Expected detection of oversize payload, got: \n%s", logbuf.String())
}
})
}
func TestBulkIndexerItem(t *testing.T) {
body := `{"body":"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."}`
t.Run("correct computeLength size", func(t *testing.T) {
expectedLength := 266
bi := BulkIndexerItem{
Action: "index",
DocumentID: strconv.Itoa(1),
Body: strings.NewReader(body),
}
bi.marshallMeta()
bi.computeLength()
if bi.payloadLength != expectedLength {
t.Fatalf("invalid length, expected %d, got %d", expectedLength, bi.payloadLength)
}
})
t.Run("empty reader length should be meta length plus newlines", func(t *testing.T) {
expectedLength := 23
bi := BulkIndexerItem{
Action: "index",
DocumentID: strconv.Itoa(1),
Body: strings.NewReader(""),
}
bi.marshallMeta()
bi.computeLength()
if bi.payloadLength != expectedLength {
t.Fatalf("invalid length, expected %d, got %d", expectedLength, bi.payloadLength)
}
})
}
type customJSONDecoder struct{}
func (d customJSONDecoder) UnmarshalFromReader(r io.Reader, blk *BulkIndexerResponse) error {
return json.NewDecoder(r).Decode(blk)
}