-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathelastic.go
207 lines (177 loc) · 6.07 KB
/
elastic.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
package generator
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// Elastic contains all configurations needed to send documents to Elastic
type Elastic struct {
Auth string
URL string
IndexName string
Client *http.Client
GeneratorIdentifier string
}
func (e *Elastic) SendPatch(docs []interface{}) error {
numDocs := len(docs)
numEventIngested.Add(float64(numDocs))
var builder bytes.Buffer
for i := 0; i < len(docs); i++ {
mdoc, errb := docs[i].(map[string]interface{})
if !errb {
return fmt.Errorf("document is not a map of string to interface")
}
index := make(map[string]interface{})
index["_index"] = e.IndexName
index["_id"] = mdoc["_id"]
line, err := json.Marshal(mdoc["patch"])
if err != nil {
return fmt.Errorf("failed to marshal document: %w", err)
}
ret := make(map[string]interface{})
ret["update"] = index
metaLine, err := json.Marshal(ret)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
builder.Write(metaLine)
builder.WriteByte('\n')
builder.Write(line)
builder.WriteByte('\n')
}
body := builder.Bytes()
bulkURL := e.URL + "/_bulk"
elasticHTTPRequest, _ := http.NewRequest(http.MethodPost, bulkURL, bytes.NewBuffer(body))
elasticHTTPRequest.Header.Add("Authorization", e.Auth)
elasticHTTPRequest.Header.Add("Content-Type", "application/x-ndjson")
resp, err := e.Client.Do(elasticHTTPRequest)
if err != nil {
recordPatchesErrored(float64(numDocs))
return fmt.Errorf("failed to send request: %w", err)
}
defer deferredErrorCloser(resp.Body)
if resp.StatusCode != http.StatusOK {
recordPatchesErrored(float64(numDocs))
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
return fmt.Errorf("error code: %d, body: %s", resp.StatusCode, string(bodyBytes))
}
recordPatchesCompleted(float64(numDocs))
return nil
}
// SendDocument sends a batch of documents to Elastic
func (e *Elastic) SendDocument(docs []any) error {
numDocs := len(docs)
numEventIngested.Add(float64(numDocs))
var builder bytes.Buffer
for i := 0; i < len(docs); i++ {
mdoc, errb := docs[i].(map[string]interface{})
if !errb {
return fmt.Errorf("document is not a map of string to interface")
}
index := make(map[string]interface{})
index["_index"] = e.IndexName
index["_id"] = mdoc["_id"]
// "_id" is not allowed in the doc
delete(mdoc, "_id")
line, err := json.Marshal(mdoc)
if err != nil {
return fmt.Errorf("failed to marshal document: %w", err)
}
ret := make(map[string]interface{})
ret["index"] = index
metaLine, err := json.Marshal(ret)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
builder.Write(metaLine)
builder.WriteByte('\n')
builder.Write(line)
builder.WriteByte('\n')
}
body := builder.Bytes()
bulkURL := e.URL + "/_bulk"
elasticHTTPRequest, _ := http.NewRequest(http.MethodPost, bulkURL, bytes.NewBuffer(body))
elasticHTTPRequest.Header.Add("Authorization", e.Auth)
elasticHTTPRequest.Header.Add("Content-Type", "application/x-ndjson")
resp, err := e.Client.Do(elasticHTTPRequest)
if err != nil {
recordWritesErrored(float64(numDocs))
return fmt.Errorf("failed to send request: %w", err)
}
defer deferredErrorCloser(resp.Body)
if resp.StatusCode != http.StatusOK {
recordWritesErrored(float64(numDocs))
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
return fmt.Errorf("error code: %d, body: %s", resp.StatusCode, string(bodyBytes))
}
recordWritesCompleted(float64(numDocs))
return nil
}
// GetLatestTimestamp returns the latest _event_time in Rockset
func (e *Elastic) GetLatestTimestamp() (time.Time, error) {
searchURL := fmt.Sprintf("%s/%s/_search?size=0", e.URL, e.IndexName)
// The identifier needs to be lowercased because by default, Elastic will index text in lowercase and the term query is case-sensitive
// This can be avoided using the match query, but this is slightly slower than the term query
jsonBody := fmt.Sprintf("{\"query\":{\"term\":{\"generator_identifier\": \"%s\"}},\"aggs\":{\"max_event_time_for_identifier\":{\"max\":{\"field\":\"_event_time\"}}}}", strings.ToLower(e.GeneratorIdentifier))
req, err := http.NewRequest(http.MethodPost, searchURL, bytes.NewBufferString(jsonBody))
if err != nil {
return time.Time{}, fmt.Errorf("failed to create new request: %w", err)
}
req.Header.Add("Authorization", e.Auth)
req.Header.Add("Content-Type", "application/json")
resp, err := e.Client.Do(req)
if err != nil {
return time.Time{}, fmt.Errorf("failed to perform request: %w", err)
}
defer deferredErrorCloser(resp.Body)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return time.Time{}, fmt.Errorf("failed to read %s response body: %w", resp.Status, err)
}
return time.Time{}, fmt.Errorf("request failed: expected OK got %s: %s", resp.Status, string(bodyBytes))
}
// Received status 200. Result structure will look something like
// {
// ...
// "aggregations": {
// "max_event_time_for_identifier": {
// "doc_count": 201874000,
// "max_event_time": {
// "value": 1.677014840315018E15
// }
// }
// }
// }
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return time.Time{}, fmt.Errorf("failed to read response body: %w", err)
}
var result map[string]interface{}
if err := json.Unmarshal(bodyBytes, &result); err != nil {
return time.Time{}, fmt.Errorf("failed to unmarshal reponse: %w", err)
}
// TODO: check type assertions
result = result["aggregations"].(map[string]interface{})
result = result["max_event_time_for_identifier"].(map[string]interface{})
if result["value"] == nil {
return time.Time{}, errors.New("malformed result, value is nil")
}
timeMicro := int64(result["value"].(float64))
// Convert from microseconds to (secs, nanosecs)
return time.Unix(timeMicro/1_000_000, (timeMicro%1_000_000)*1_000), nil
}
func (e *Elastic) ConfigureDestination() error {
return nil
}