-
Notifications
You must be signed in to change notification settings - Fork 625
/
Copy pathbulk_indexer_integration_test.go
159 lines (133 loc) · 4.47 KB
/
bulk_indexer_integration_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
// 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.
// +build integration
package esutil_test
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/estransport"
"github.com/elastic/go-elasticsearch/v7/esutil"
)
func TestBulkIndexerIntegration(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("Default", func(t *testing.T) {
var countSuccessful uint64
indexName := "test-bulk-integration"
es, _ := elasticsearch.NewClient(elasticsearch.Config{
Logger: &estransport.ColorLogger{Output: os.Stdout},
})
es.Indices.Delete([]string{indexName}, es.Indices.Delete.WithIgnoreUnavailable(true))
es.Indices.Create(
indexName,
es.Indices.Create.WithBody(strings.NewReader(`{"settings": {"number_of_shards": 1, "number_of_replicas": 0, "refresh_interval":"5s"}}`)),
es.Indices.Create.WithWaitForActiveShards("1"))
bi, _ := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
Index: indexName,
Client: es,
// FlushBytes: 3e+6,
})
numItems := 100000
start := time.Now().UTC()
for i := 1; i <= numItems; i++ {
err := bi.Add(context.Background(), esutil.BulkIndexerItem{
Action: "index",
DocumentID: strconv.Itoa(i),
Body: strings.NewReader(body),
OnSuccess: func(ctx context.Context, item esutil.BulkIndexerItem, res esutil.BulkIndexerResponseItem) {
atomic.AddUint64(&countSuccessful, 1)
},
})
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(numItems) {
t.Errorf("Unexpected NumAdded: want=%d, got=%d", numItems, stats.NumAdded)
}
if stats.NumIndexed != uint64(numItems) {
t.Errorf("Unexpected NumIndexed: want=%d, got=%d", numItems, stats.NumIndexed)
}
if stats.NumFailed != 0 {
t.Errorf("Unexpected NumFailed: want=0, got=%d", stats.NumFailed)
}
if countSuccessful != uint64(numItems) {
t.Errorf("Unexpected countSuccessful: want=%d, got=%d", numItems, countSuccessful)
}
fmt.Printf(" Added %d documents to indexer. Succeeded: %d. Failed: %d. Requests: %d. Duration: %s (%.0f docs/sec)\n",
stats.NumAdded,
stats.NumFlushed,
stats.NumFailed,
stats.NumRequests,
time.Since(start).Truncate(time.Millisecond),
1000.0/float64(time.Since(start)/time.Millisecond)*float64(stats.NumFlushed))
})
t.Run("Multiple indices", func(t *testing.T) {
es, _ := elasticsearch.NewClient(elasticsearch.Config{
Logger: &estransport.ColorLogger{Output: os.Stdout},
})
bi, _ := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
Index: "test-index-a",
Client: es,
})
// Default index
for i := 1; i <= 10; i++ {
err := bi.Add(context.Background(), esutil.BulkIndexerItem{
Action: "index",
DocumentID: strconv.Itoa(i),
Body: strings.NewReader(body),
})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
// Index 1
for i := 1; i <= 10; i++ {
err := bi.Add(context.Background(), esutil.BulkIndexerItem{
Action: "index",
Index: "test-index-b",
Body: strings.NewReader(body),
})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
// Index 2
for i := 1; i <= 10; i++ {
err := bi.Add(context.Background(), esutil.BulkIndexerItem{
Action: "index",
Index: "test-index-c",
Body: strings.NewReader(body),
})
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()
expectedIndexed := 10 + 10 + 10
if stats.NumIndexed != uint64(expectedIndexed) {
t.Errorf("Unexpected NumIndexed: want=%d, got=%d", expectedIndexed, stats.NumIndexed)
}
res, err := es.Indices.Exists([]string{"test-index-a", "test-index-b", "test-index-c"})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if res.StatusCode != 200 {
t.Errorf("Expected indices to exist, but got a [%s] response", res.Status())
}
})
}