-
Notifications
You must be signed in to change notification settings - Fork 41
/
run.go
337 lines (323 loc) · 8.33 KB
/
run.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2021 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <[email protected]>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
package esbulk
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"os"
"runtime/pprof"
"strings"
"sync"
"time"
gzip "github.com/klauspost/pgzip"
"github.com/segmentio/encoding/json"
"github.com/sethgrid/pester"
)
var (
// Version of application.
Version = "0.7.20"
ErrIndexNameRequired = errors.New("index name required")
ErrNoWorkers = errors.New("no workers configured")
ErrInvalidBatchSize = errors.New("cannot use zero batch size")
)
// Runner bundles various options. Factored out of a former main func and
// should be further split up (TODO).
type Runner struct {
BatchSize int
Config string
CpuProfile string
OpType string
DocType string
File *os.File
FileGzipped bool
IdentifierField string
IndexName string
Mapping string
MemProfile string
NumWorkers int
Password string
Pipeline string
Purge bool
PurgePause time.Duration
RefreshInterval string
Scheme string
Servers []string
Settings string
ShowVersion bool
SkipBroken bool
Username string
Verbose bool
InsecureSkipVerify bool
ZeroReplica bool
}
// Run starts indexing documents from file into a given index.
func (r *Runner) Run() (err error) {
if r.ShowVersion {
fmt.Println(Version)
return nil
}
if r.NumWorkers == 0 {
return ErrNoWorkers
}
if r.BatchSize == 0 {
return ErrInvalidBatchSize
}
if r.CpuProfile != "" {
f, err := os.Create(r.CpuProfile)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if r.IndexName == "" {
return ErrIndexNameRequired
}
if r.OpType == "" {
r.OpType = "index"
}
if len(r.Servers) == 0 {
r.Servers = append(r.Servers, "http://localhost:9200")
}
r.Servers = mapString(prependSchema, r.Servers)
if r.Verbose {
log.Printf("using %d server(s)", len(r.Servers))
}
options := Options{
Servers: r.Servers,
Index: r.IndexName,
OpType: r.OpType,
DocType: r.DocType,
BatchSize: r.BatchSize,
Verbose: r.Verbose,
Scheme: "http", // deprecated
IDField: r.IdentifierField,
Username: r.Username,
Password: r.Password,
Pipeline: r.Pipeline,
}
if r.Verbose {
log.Println(options)
}
if r.Purge {
if err := DeleteIndex(options); err != nil {
return err
}
time.Sleep(r.PurgePause)
}
var createIndexBody io.Reader
if r.Config != "" {
if _, err := os.Stat(r.Config); os.IsNotExist(err) {
createIndexBody = strings.NewReader(r.Config)
} else {
file, err := os.Open(r.Config)
if err != nil {
return err
}
defer file.Close()
createIndexBody = bufio.NewReader(file)
}
}
if err := CreateIndex(options, createIndexBody); err != nil {
return err
}
if r.Mapping != "" {
var reader io.Reader
if _, err := os.Stat(r.Mapping); os.IsNotExist(err) {
reader = strings.NewReader(r.Mapping)
} else {
file, err := os.Open(r.Mapping)
if err != nil {
return err
}
defer file.Close()
reader = bufio.NewReader(file)
}
err := PutMapping(options, reader)
if err != nil {
return err
}
}
var (
queue = make(chan string)
wg sync.WaitGroup
)
wg.Add(r.NumWorkers)
for i := 0; i < r.NumWorkers; i++ {
name := fmt.Sprintf("worker-%d", i)
go Worker(name, options, queue, &wg)
}
if r.Verbose {
log.Printf("started %d workers", r.NumWorkers)
}
for i, _ := range options.Servers {
// Store number_of_replicas settings for restoration later.
doc, err := GetSettings(i, options)
if err != nil {
return err
}
// TODO(miku): Rework this.
numberOfReplicas := doc[options.Index].(map[string]interface{})["settings"].(map[string]interface{})["index"].(map[string]interface{})["number_of_replicas"]
if r.Verbose {
log.Printf("on shutdown, number_of_replicas will be set back to %s", numberOfReplicas)
}
if r.Verbose {
log.Printf("on shutdown, refresh_interval will be set back to %s", r.RefreshInterval)
}
// Shutdown procedure. TODO(miku): Handle signals, too.
defer func() {
// Realtime search.
if _, err = indexSettingsRequest(fmt.Sprintf(`{"index": {"refresh_interval": "%s"}}`, r.RefreshInterval), options); err != nil {
return
}
// Reset number of replicas.
if _, err = indexSettingsRequest(fmt.Sprintf(`{"index": {"number_of_replicas": %q}}`, numberOfReplicas), options); err != nil {
return
}
// Persist documents.
err = FlushIndex(i, options)
}()
// Realtime search.
resp, err := indexSettingsRequest(`{"index": {"refresh_interval": "-1"}}`, options)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
b, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
return fmt.Errorf("got %v: %v", resp.StatusCode, string(b))
}
if r.ZeroReplica {
// Reset number of replicas.
if _, err := indexSettingsRequest(`{"index": {"number_of_replicas": 0}}`, options); err != nil {
return err
}
}
}
var (
reader = bufio.NewReader(r.File)
counter = 0
start = time.Now()
)
if r.FileGzipped {
zreader, err := gzip.NewReader(r.File)
if err != nil {
log.Fatal(err)
}
reader = bufio.NewReader(zreader)
}
if r.Verbose && r.File != nil {
log.Printf("start reading from %v", r.File.Name())
}
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return err
}
if line = strings.TrimSpace(line); len(line) == 0 {
continue
}
if r.SkipBroken {
if !(isJSON(line)) {
if r.Verbose {
fmt.Printf("skipped line [%s]\n", line)
}
continue
}
}
queue <- line
counter++
}
close(queue)
wg.Wait()
elapsed := time.Since(start)
if r.MemProfile != "" {
f, err := os.Create(r.MemProfile)
if err != nil {
return err
}
pprof.WriteHeapProfile(f)
f.Close()
}
if r.Verbose {
elapsed := elapsed.Seconds()
if elapsed < 0.1 {
elapsed = 0.1
}
rate := float64(counter) / elapsed
log.Printf("%d docs in %0.2fs at %0.3f docs/s with %d workers\n", counter, elapsed, rate, r.NumWorkers)
}
return nil
}
// indexSettingsRequest runs updates an index setting, given a body and
// options. Body consist of the JSON document, e.g. `{"index":
// {"refresh_interval": "1s"}}`.
func indexSettingsRequest(body string, options Options) (*http.Response, error) {
r := strings.NewReader(body)
rand.Seed(time.Now().Unix())
server := options.Servers[rand.Intn(len(options.Servers))]
link := fmt.Sprintf("%s/%s/_settings", server, options.Index)
req, err := http.NewRequest("PUT", link, r)
if err != nil {
return nil, err
}
// Auth handling.
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return nil, err
}
if options.Verbose {
log.Printf("applied setting: %s with status %s\n", body, resp.Status)
}
return resp, nil
}
// isJSON checks if a string is valid json.
func isJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
func prependSchema(s string) string {
if !strings.HasPrefix(s, "http") {
return fmt.Sprintf("http://%s", s)
}
return s
}
func mapString(f func(string) string, vs []string) (result []string) {
for _, v := range vs {
result = append(result, f(v))
}
return
}