-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscroll.go
More file actions
257 lines (223 loc) · 5.79 KB
/
scroll.go
File metadata and controls
257 lines (223 loc) · 5.79 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/charmbracelet/log"
json "github.com/json-iterator/go"
"golang.org/x/sync/errgroup"
)
func scroll(ctx context.Context, scrollers []func(context.Context) error) error {
grp, ctx := errgroup.WithContext(ctx)
for _, scroller := range scrollers {
scroller := scroller
grp.Go(func() error {
return scroller(ctx)
})
}
return grp.Wait()
}
func (d *dumper) scrollSlice(ctx context.Context, index string, sliceIdx, sliceTotal int) error {
q := d.scrollQuery(sliceIdx, sliceTotal)
reqStart := time.Now()
scrollID, totalHits, more, err := d.scrollRequest(ctx, index+"/_search?scroll="+d.scrollTimeoutES, q)
d.totalHitsCtr.Report(totalHits)
defer func() {
d.clearScrollContext(scrollID)
}()
if err != nil || !more {
return err
}
for {
cancelableSleep(ctx, d.throttlingDuration(time.Since(reqStart)))
scrollReq := map[string]string{
"scroll": d.scrollTimeoutES,
"scroll_id": scrollID,
}
qBytes, err := json.Marshal(scrollReq)
if err != nil {
return fmt.Errorf("marshaling scroll request: %w", err)
}
q = string(qBytes)
reqStart = time.Now()
// do not immediately overwrite the scrollID, in case of error
// we want to clear the previous one
newScrollID, _, more, err := d.scrollRequest(ctx, "_search/scroll", q)
if err != nil {
return err
}
scrollID = newScrollID
if !more {
return nil
}
}
}
// sendHits sends hits to the output and returns whether the count limit has been reached.
func (d *dumper) sendHits(hits []json.RawMessage) bool {
scrolled := atomic.LoadUint64(&d.scrolled)
if d.count > 0 && scrolled >= d.count {
return true
}
for _, hit := range hits {
d.scrolledCh <- hit
}
scrolled = atomic.AddUint64(&d.scrolled, uint64(len(hits)))
return d.count > 0 && scrolled >= d.count
}
func (d *dumper) clearScrollContext(scrollID string) {
if scrollID == "" {
return
}
// we want to clear the scroll context even after the Go ctx is canceled, so
// we use our own ctx.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
status, raw, err := d.cl.Delete(ctx, "_search/scroll/"+scrollID, "", nil)
if err != nil {
log.Error("clearing scroll context", "err", err)
}
if status != http.StatusOK {
log.Error("clearing scroll context", "code", status, "response", string(raw))
}
}
func cancelableSleep(ctx context.Context, d time.Duration) {
if d <= 0 {
return
}
select {
case <-ctx.Done():
case <-time.After(d):
}
}
func (d *dumper) throttlingDuration(reqDuration time.Duration) time.Duration {
if d.throttle <= 0 {
return 0
}
delay := time.Duration(d.throttle * float32(reqDuration))
// make sure we don't sleep for more than the scroll context timeout, or
// even get too close to it, as we must avoid it expiring
maxDelay := 3 * d.scrollTimeout / 4
if delay > maxDelay {
delay = maxDelay
}
return delay
}
func (d *dumper) scrollQuery(sliceIdx, sliceTotal int) string {
q := d.query
if sliceTotal > 1 {
qCopy := make(obj)
for k := range d.query {
qCopy[k] = d.query[k]
}
qCopy["slice"] = obj{
"id": sliceIdx,
"max": sliceTotal,
}
q = qCopy
}
b, err := json.Marshal(q)
if err != nil {
log.Fatal("marshaling query", "err", err)
}
return string(b)
}
type scrollResp interface {
GetHits() []json.RawMessage
GetScrollID() string
GetTotal() uint64
}
type scrollRespMetadata struct {
Hits struct {
Total struct {
Value uint64 `json:"value"`
} `json:"total"`
Hits []json.RawMessage `json:"hits"`
} `json:"hits"`
ScrollID string `json:"_scroll_id"`
}
func (r scrollRespMetadata) GetHits() []json.RawMessage {
return r.Hits.Hits
}
func (r scrollRespMetadata) GetScrollID() string {
return r.ScrollID
}
func (r scrollRespMetadata) GetTotal() uint64 {
return r.Hits.Total.Value
}
type scrollRespSourceOnly struct {
Hits struct {
Total struct {
Value uint64 `json:"value"`
} `json:"total"`
Hits []struct {
Source json.RawMessage `json:"_source"`
} `json:"hits"`
} `json:"hits"`
ScrollID string `json:"_scroll_id"`
}
func (r scrollRespSourceOnly) GetHits() []json.RawMessage {
hits := make([]json.RawMessage, len(r.Hits.Hits))
for i, hit := range r.Hits.Hits {
hits[i] = hit.Source
}
return hits
}
func (r scrollRespSourceOnly) GetScrollID() string {
return r.ScrollID
}
func (r scrollRespSourceOnly) GetTotal() uint64 {
return r.Hits.Total.Value
}
func (d *dumper) scrollRequest(ctx context.Context, path, query string) (string, uint64, bool, error) {
var resp scrollResp
if d.metadata || d.metadataOnly {
resp = &scrollRespMetadata{}
} else {
resp = &scrollRespSourceOnly{}
}
status, raw, err := d.cl.Get(ctx, path, query, resp)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Error("sending scroll request", "err", err)
}
return "", 0, false, err
}
if status != http.StatusOK {
log.Error("got unexpected status code", "code", status, "response", string(raw))
return "", 0, false, errors.New("unexpected status code")
}
hits := resp.GetHits()
limitReached := d.sendHits(hits)
return resp.GetScrollID(), resp.GetTotal(), len(hits) == d.size && !limitReached, err
}
// GroupCounter is a counter that aggregates counts from a given number of
// goroutines. Each goroutine must call Report() exactly once.
type GroupCounter struct {
cnt uint64
pending int
mu sync.RWMutex
}
func NewGroupCounter(n int) *GroupCounter {
return &GroupCounter{
pending: n,
}
}
func (gc *GroupCounter) Report(cnt uint64) {
gc.mu.Lock()
defer gc.mu.Unlock()
gc.pending--
if gc.pending < 0 {
panic("GroupCounter pending < 0")
}
gc.cnt += cnt
}
// Get returns the current count and whether all goroutines have reported.
func (gc *GroupCounter) Get() (uint64, bool) {
gc.mu.RLock()
defer gc.mu.RUnlock()
return gc.cnt, gc.pending == 0
}