-
Notifications
You must be signed in to change notification settings - Fork 12
/
bulk_indexer.go
253 lines (229 loc) · 7.27 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
// 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.
package docappender
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"strings"
"unsafe"
"go.elastic.co/fastjson"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
jsoniter "github.com/json-iterator/go"
)
// At the time of writing, the go-elasticsearch BulkIndexer implementation
// sends all items to a channel, and multiple persistent worker goroutines will
// receive those items and independently fill up their own buffers. Each one
// will independently flush when their buffer is filled up, or when the flush
// interval elapses. If there are many workers, then this may lead to sparse
// bulk requests.
//
// We take a different approach, where we fill up one bulk request at a time.
// When the buffer is filled up, or the flush interval elapses, we start a new
// goroutine to send the request in the background, with a limit on the number
// of concurrent bulk requests. This way we can ensure bulk requests have the
// maximum possible size, based on configuration and throughput.
type bulkIndexer struct {
client *elasticsearch.Client
itemsAdded int
bytesFlushed int
jsonw fastjson.Writer
gzipw *gzip.Writer
copybuf [32 * 1024]byte
writer io.Writer
buf bytes.Buffer
}
type BulkIndexerResponseStat struct {
Indexed int64
FailedDocs []BulkIndexerResponseItem
}
// BulkIndexerResponseItem represents the Elasticsearch response item.
type BulkIndexerResponseItem struct {
Index string `json:"_index"`
Status int `json:"status"`
Error struct {
Type string `json:"type"`
Reason string `json:"reason"`
} `json:"error,omitempty"`
}
func init() {
jsoniter.RegisterTypeDecoderFunc("docappender.BulkIndexerResponseStat", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
iter.ReadObjectCB(func(i *jsoniter.Iterator, s string) bool {
switch s {
case "items":
iter.ReadArrayCB(func(i *jsoniter.Iterator) bool {
return i.ReadMapCB(func(i *jsoniter.Iterator, s string) bool {
var item BulkIndexerResponseItem
i.ReadObjectCB(func(i *jsoniter.Iterator, s string) bool {
switch s {
case "_index":
item.Index = i.ReadString()
case "status":
item.Status = i.ReadInt()
case "error":
i.ReadObjectCB(func(i *jsoniter.Iterator, s string) bool {
switch s {
case "type":
item.Error.Type = i.ReadString()
case "reason":
// Match Elasticsearch field mapper field value:
// failed to parse field [%s] of type [%s] in %s. Preview of field's value: '%s'
// https://github.com/elastic/elasticsearch/blob/588eabe185ad319c0268a13480465966cef058cd/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java#L234
item.Error.Reason, _, _ = strings.Cut(
i.ReadString(), ". Preview",
)
default:
i.Skip()
}
return true
})
default:
i.Skip()
}
return true
})
if item.Error.Type != "" || item.Status > 201 {
(*((*BulkIndexerResponseStat)(ptr))).FailedDocs = append((*((*BulkIndexerResponseStat)(ptr))).FailedDocs, item)
} else {
(*((*BulkIndexerResponseStat)(ptr))).Indexed = (*((*BulkIndexerResponseStat)(ptr))).Indexed + 1
}
return true
})
})
// no need to proceed further, return early
return false
default:
i.Skip()
return true
}
})
})
}
func newBulkIndexer(client *elasticsearch.Client, compressionLevel int) *bulkIndexer {
b := &bulkIndexer{client: client}
if compressionLevel != gzip.NoCompression {
b.gzipw, _ = gzip.NewWriterLevel(&b.buf, compressionLevel)
b.writer = b.gzipw
} else {
b.writer = &b.buf
}
b.Reset()
return b
}
// BulkIndexer resets b, ready for a new request.
func (b *bulkIndexer) Reset() {
b.itemsAdded, b.bytesFlushed = 0, 0
b.buf.Reset()
if b.gzipw != nil {
b.gzipw.Reset(&b.buf)
}
}
// Added returns the number of buffered items.
func (b *bulkIndexer) Items() int {
return b.itemsAdded
}
// Len returns the number of buffered bytes.
func (b *bulkIndexer) Len() int {
return b.buf.Len()
}
// BytesFlushed returns the number of bytes flushed by the bulk indexer.
func (b *bulkIndexer) BytesFlushed() int {
return b.bytesFlushed
}
type bulkIndexerItem struct {
Index string
DocumentID string
Body io.Reader
}
// add encodes an item in the buffer.
func (b *bulkIndexer) add(item bulkIndexerItem) error {
b.writeMeta(item.Index, item.DocumentID)
if _, err := io.CopyBuffer(b.writer, item.Body, b.copybuf[:]); err != nil {
return err
}
if _, err := b.writer.Write([]byte("\n")); err != nil {
return err
}
b.itemsAdded++
return nil
}
func (b *bulkIndexer) writeMeta(index, documentID string) {
b.jsonw.RawString(`{"create":{`)
if documentID != "" {
b.jsonw.RawString(`"_id":`)
b.jsonw.String(documentID)
}
if index != "" {
if documentID != "" {
b.jsonw.RawByte(',')
}
b.jsonw.RawString(`"_index":`)
b.jsonw.String(index)
}
b.jsonw.RawString("}}\n")
b.writer.Write(b.jsonw.Bytes())
b.jsonw.Reset()
}
// Flush executes a bulk request if there are any items buffered, and clears out the buffer.
func (b *bulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error) {
if b.itemsAdded == 0 {
return BulkIndexerResponseStat{}, nil
}
if b.gzipw != nil {
if err := b.gzipw.Close(); err != nil {
return BulkIndexerResponseStat{}, fmt.Errorf("failed closing the gzip writer: %w", err)
}
}
req := esapi.BulkRequest{
Body: &b.buf,
Header: make(http.Header),
FilterPath: []string{"items.*._index", "items.*.status", "items.*.error.type", "items.*.error.reason"},
}
if b.gzipw != nil {
req.Header.Set("Content-Encoding", "gzip")
}
bytesFlushed := b.buf.Len()
res, err := req.Do(ctx, b.client)
if err != nil {
return BulkIndexerResponseStat{}, err
}
defer res.Body.Close()
// Record the number of flushed bytes only when err == nil. The body may
// not have been sent otherwise.
b.bytesFlushed = bytesFlushed
var resp BulkIndexerResponseStat
if res.IsError() {
if res.StatusCode == http.StatusTooManyRequests {
return resp, errorTooManyRequests{res: res}
}
return resp, fmt.Errorf("flush failed: %s", res.String())
}
if err := jsoniter.NewDecoder(res.Body).Decode(&resp); err != nil {
return resp, fmt.Errorf("error decoding bulk response: %w", err)
}
return resp, nil
}
type errorTooManyRequests struct {
res *esapi.Response
}
func (e errorTooManyRequests) Error() string {
return fmt.Sprintf("flush failed: %s", e.res.String())
}