-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathelasticClientCommon.go
More file actions
270 lines (225 loc) · 6.64 KB
/
elasticClientCommon.go
File metadata and controls
270 lines (225 loc) · 6.64 KB
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
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
)
func exists(res *esapi.Response, err error) bool {
defer func() {
if res != nil && res.Body != nil {
err = res.Body.Close()
if err != nil {
log.Warn("elasticClient.exists", "could not close body: ", err.Error())
}
}
}()
if err != nil {
log.Warn("elasticClient.IndexExists", "could not check index on the elastic nodes:", err.Error())
return false
}
switch res.StatusCode {
case http.StatusOK:
return true
case http.StatusNotFound:
return false
default:
log.Warn("elasticClient.exists", "invalid status code returned by the elastic nodes:", res.StatusCode)
return false
}
}
func loadResponseBody(body io.ReadCloser, dest interface{}) error {
if body == nil {
return nil
}
if dest == nil {
_, err := io.Copy(io.Discard, body)
return err
}
err := json.NewDecoder(body).Decode(dest)
return err
}
func elasticDefaultErrorResponseHandler(res *esapi.Response) error {
responseBody := map[string]interface{}{}
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("%w cannot read elastic response body bytes", err)
}
err = json.Unmarshal(bodyBytes, &responseBody)
if err != nil {
errToReturn := err
isBackOffError := strings.Contains(string(bodyBytes), fmt.Sprintf("%d", http.StatusForbidden)) ||
strings.Contains(string(bodyBytes), fmt.Sprintf("%d", http.StatusTooManyRequests))
if isBackOffError {
errToReturn = dataindexer.ErrBackOff
}
return fmt.Errorf("%w, cannot unmarshal elastic response body to map[string]interface{}, "+
"decode error: %s, body response: %s", errToReturn, err.Error(), string(bodyBytes))
}
if res.IsError() {
if errIsAlreadyExists(responseBody) {
return nil
}
if isErrAliasAlreadyExists(responseBody) {
log.Debug("alias already exists", "response", responseBody)
return nil
}
}
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
return nil
}
return fmt.Errorf("error while parsing the response: code returned: %v, body: %v, bodyBytes: %v",
res.StatusCode, responseBody, string(bodyBytes))
}
func elasticBulkRequestResponseHandler(res *esapi.Response) error {
if res.IsError() {
return fmt.Errorf("%s", res.String())
}
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("%w cannot read elastic response body bytes", err)
}
return extractErrorFromBulkBodyResponseBytes(bodyBytes)
}
func extractErrorFromBulkBodyResponseBytes(bodyBytes []byte) error {
response := BulkRequestResponse{}
err := json.Unmarshal(bodyBytes, &response)
if err != nil {
return err
}
count := 0
errorsString := ""
for _, item := range response.Items {
var selectedItem Item
switch {
case item.ItemIndex != nil:
selectedItem = *item.ItemIndex
case item.ItemUpdate != nil:
selectedItem = *item.ItemUpdate
}
log.Trace("worked on", "index", selectedItem.Index,
"_id", selectedItem.ID,
"result", selectedItem.Result,
"status", selectedItem.Status,
)
if selectedItem.Status < http.StatusBadRequest {
continue
}
count++
errorsString += fmt.Sprintf(`{ "index": "%s", "id": "%s", "statusCode": %d, "errorType": "%s", "reason": "%s", "causedBy": { "type": "%s", "reason": "%s", "script_stack":"%s", "script":"%s" }}\n`,
selectedItem.Index, selectedItem.ID, selectedItem.Status, selectedItem.Error.Type, selectedItem.Error.Reason, selectedItem.Error.Cause.Type, selectedItem.Error.Cause.Reason, selectedItem.Error.Cause.ScriptStack, selectedItem.Error.Cause.Script)
if count == numOfErrorsToExtractBulkResponse {
break
}
}
if errorsString == "" {
return nil
}
return fmt.Errorf("%s", errorsString)
}
func errIsAlreadyExists(response map[string]interface{}) bool {
alreadyExistsMessage := "resource_already_exists_exception"
errKey := "error"
typeKey := "type"
errMapI, ok := response[errKey]
if !ok {
return false
}
errMap, ok := errMapI.(map[string]interface{})
if !ok {
return false
}
existsString, ok := errMap[typeKey].(string)
if !ok {
return false
}
return existsString == alreadyExistsMessage
}
func isErrAliasAlreadyExists(response map[string]interface{}) bool {
aliasExistsMessage := "invalid_alias_name_exception"
errKey := "error"
typeKey := "type"
errMapI, ok := response[errKey]
if !ok {
return false
}
errMap, ok := errMapI.(map[string]interface{})
if !ok {
return false
}
existsString, ok := errMap[typeKey].(string)
if !ok {
return false
}
return existsString == aliasExistsMessage
}
func kibanaResponseErrorHandler(res *esapi.Response) error {
errorRes := &data.Response{}
decodeErr := loadResponseBody(res.Body, errorRes)
if decodeErr != nil {
return decodeErr
}
errStr := fmt.Sprintf("%v", errorRes.Error)
if errorRes.Status == http.StatusConflict && strings.Contains(errStr, errPolicyAlreadyExists) {
return nil
}
if errorRes.Error == nil && errorRes.Status < http.StatusBadRequest {
return nil
}
log.Warn("elasticClient.parseResponse",
"error returned by elastic API", errorRes.Error,
"code", res.StatusCode)
return dataindexer.ErrBackOff
}
func newRequest(method, path string, body *bytes.Buffer) *http.Request {
r := http.Request{
Method: method,
URL: &url.URL{Path: path},
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
}
if body != nil {
r.Body = ioutil.NopCloser(body)
r.ContentLength = int64(body.Len())
}
return &r
}
/**
* parseResponse will check and load the elastic/kibana api response into the destination objectsMap. Custom errorHandler
* can be passed for special requests that want to handle StatusCode != 200. Every responseErrorHandler
* implementation should call loadResponseBody or consume the response body in order to be able to
* reuse persistent TCP connections: https://github.com/elastic/go-elasticsearch#usage
*/
func parseResponse(res *esapi.Response, dest interface{}, errorHandler responseErrorHandler) error {
defer func() {
if res != nil && res.Body != nil {
err := res.Body.Close()
if err != nil {
log.Warn("elasticClient.parseResponse",
"could not close body", err.Error())
}
}
}()
if errorHandler == nil {
errorHandler = elasticDefaultErrorResponseHandler
}
if res.StatusCode != http.StatusOK {
return errorHandler(res)
}
err := loadResponseBody(res.Body, dest)
if err != nil {
log.Warn("elasticClient.parseResponse",
"could not load response body:", err.Error())
return dataindexer.ErrBackOff
}
return nil
}