forked from Ingensi/dockbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkapi.go
183 lines (157 loc) · 4.1 KB
/
bulkapi.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
package elasticsearch
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/elastic/libbeat/logp"
)
// MetaBuilder creates meta data for bulk requests
type MetaBuilder interface {
Meta(interface{}) interface{}
}
// The request is retransmitted up to max_retries before returning an error.
func (es *Elasticsearch) sendBulkRequest(
method string,
path string,
params map[string]string,
buf *bytes.Buffer,
) ([]byte, error) {
var errors []error
for attempt := 0; attempt < es.MaxRetries; attempt++ {
conn := es.connectionPool.GetConnection()
logp.Debug("elasticsearch", "Use connection %s", conn.URL)
url := conn.URL + path
if len(params) > 0 {
url = url + "?" + urlEncode(params)
}
logp.Debug("elasticsearch", "Sending bulk request to %s", url)
req, err := http.NewRequest(method, url, buf)
if err != nil {
return nil, fmt.Errorf("NewRequest fails: %s", err)
}
resp, retry, err := es.performRequest(conn, req)
if retry == true {
// retry
if err != nil {
errors = append(errors, err)
}
continue
}
if err != nil {
return nil, fmt.Errorf("PerformRequest fails: %s", err)
}
return resp, nil
}
logp.Warn("Request fails to be send after %d retries", es.MaxRetries)
return nil, fmt.Errorf("Request fails after %d retries. Errors: %v", es.MaxRetries, errors)
}
type bulkRequest struct {
es *Elasticsearch
buf bytes.Buffer
enc *json.Encoder
path string
params map[string]string
}
func (es *Elasticsearch) startBulkRequest(
index string,
docType string,
params map[string]string,
) (*bulkRequest, error) {
path, err := makePath(index, docType, "_bulk")
if err != nil {
return nil, err
}
r := &bulkRequest{
es: es,
path: path,
params: params,
}
r.enc = json.NewEncoder(&r.buf)
return r, nil
}
func (r *bulkRequest) Send(meta, obj interface{}) error {
var err error
pos := r.buf.Len()
if err = r.enc.Encode(meta); err != nil {
return err
}
if err = r.enc.Encode(obj); err != nil {
r.buf.Truncate(pos) // remove meta object from buffer
}
return err
}
func (r *bulkRequest) Flush() (*QueryResult, error) {
if r.buf.Len() == 0 {
logp.Debug("elasticsearch", "Empty channel. Wait for more data.")
return nil, nil
}
resp, err := r.es.sendBulkRequest("POST", r.path, r.params, &r.buf)
if err != nil {
return nil, err
}
r.buf.Truncate(0)
return readQueryResult(resp)
}
// Bulk performs many index/delete operations in a single API call.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
func (es *Elasticsearch) Bulk(index string, docType string,
params map[string]string, body []interface{}) (*QueryResult, error) {
return es.BulkWith(index, docType, params, nil, body)
}
// BulkWith creates a HTTP request containing a bunch of operations and send
// them to Elasticsearch. The request is retransmitted up to max_retries before
// returning an error.
func (es *Elasticsearch) BulkWith(
index string,
docType string,
params map[string]string,
metaBuilder MetaBuilder,
body []interface{},
) (*QueryResult, error) {
if len(body) == 0 {
logp.Debug("elasticsearch", "Empty channel. Wait for more data.")
return nil, nil
}
path, err := makePath(index, docType, "_bulk")
if err != nil {
return nil, err
}
buf := bulkEncode(metaBuilder, body)
if buf.Len() == 0 {
logp.Debug("elasticsearch", "Empty channel. Wait for more data.")
return nil, nil
}
resp, err := es.sendBulkRequest("POST", path, params, &buf)
if err != nil {
return nil, err
}
return readQueryResult(resp)
}
func bulkEncode(metaBuilder MetaBuilder, body []interface{}) bytes.Buffer {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if metaBuilder == nil {
for _, obj := range body {
pos := buf.Len()
if err := enc.Encode(obj); err != nil {
debug("Failed to encode message: %s", err)
buf.Truncate(pos)
}
}
} else {
for _, obj := range body {
pos := buf.Len()
meta := metaBuilder.Meta(obj)
err := enc.Encode(meta)
if err == nil {
err = enc.Encode(obj)
}
if err != nil {
debug("Failed to encode message: %s", err)
buf.Truncate(pos)
}
}
}
return buf
}