-
Notifications
You must be signed in to change notification settings - Fork 6
/
solrbulk.go
221 lines (194 loc) · 5.3 KB
/
solrbulk.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
// Copyright 2015 by Leipzig University Library, http://ub.uni-leipzig.de
// by The Finc Authors, http://finc.info
// by Martin Czygan, <martin.czygan@uni-leipzig.de>
//
// 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 main
import (
"bufio"
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"time"
"github.com/miku/solrbulk"
)
func main() {
version := flag.Bool("v", false, "prints current program version")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
memprofile := flag.String("memprofile", "", "write heap profile to file")
host := flag.String("host", "localhost", "SOLR host (deprecated, use -server)")
port := flag.Int("port", 8983, "SOLR port (deprecated, use -server)")
collection := flag.String("collection", "", "SOLR core / collection")
batchSize := flag.Int("size", 1000, "bulk batch size")
commitSize := flag.Int("commit", 1000000, "commit after this many docs")
numWorkers := flag.Int("w", runtime.NumCPU(), "number of workers to use")
verbose := flag.Bool("verbose", false, "output basic progress")
gzipped := flag.Bool("z", false, "unzip gz'd file on the fly")
reset := flag.Bool("reset", false, "remove all docs from index")
server := flag.String("server", "", "url to SOLR server, including host, port and path to collection")
optimize := flag.Bool("optimize", false, "optimize index")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *version {
fmt.Println(solrbulk.Version)
os.Exit(0)
}
options := solrbulk.Options{
Collection: *collection,
BatchSize: *batchSize,
CommitSize: *commitSize,
Verbose: *verbose,
}
// Assemble a new server option, that behaves like the old one, if -server
// is not specified.
var srv string
if *server != "" {
srv = *server
} else {
if *collection != "" {
srv = fmt.Sprintf("http://%s:%d/solr/%s", *host, *port, options.Collection)
} else {
srv = fmt.Sprintf("http://%s:%d/solr", *host, *port)
}
}
if !strings.HasPrefix(srv, "http") {
srv = fmt.Sprintf("http://%s", srv)
}
options.Server = srv
if *reset {
hostpath := fmt.Sprintf("%s/update", options.Server)
urls := []string{
fmt.Sprintf("%s?stream.body=<delete><query>*:*</query></delete>", hostpath),
fmt.Sprintf("%s?stream.body=<commit/>", hostpath),
}
for _, url := range urls {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
log.Printf("%s %s", resp.Status, url)
}
os.Exit(0)
}
var file io.Reader = os.Stdin
if flag.NArg() > 0 {
f, err := os.Open(flag.Args()[0])
if err != nil {
log.Fatalln(err)
}
defer f.Close()
file = f
}
runtime.GOMAXPROCS(*numWorkers)
queue := make(chan string)
var wg sync.WaitGroup
for i := 0; i < *numWorkers; i++ {
wg.Add(1)
go solrbulk.Worker(fmt.Sprintf("worker-%d", i), options, queue, &wg)
}
commitURL := fmt.Sprintf("%s/update?commit=true", options.Server)
// final commit
defer func() {
resp, err := http.Get(commitURL)
if err != nil {
log.Fatal(err)
}
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
log.Printf("final commit: %s\n", resp.Status)
}()
reader := bufio.NewReader(file)
if *gzipped {
zreader, err := gzip.NewReader(file)
if err != nil {
log.Fatal(err)
}
reader = bufio.NewReader(zreader)
}
i := 0
start := time.Now()
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
line = strings.TrimSpace(line)
queue <- line
i++
if i%options.CommitSize == 0 {
resp, err := http.Get(commitURL)
if err != nil {
log.Fatal(err)
}
if options.Verbose {
log.Printf("commit @%d %s\n", i, resp.Status)
}
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
}
}
close(queue)
wg.Wait()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
if *verbose {
elapsed := time.Since(start)
rate := float64(i) / elapsed.Seconds()
log.Printf("%d docs in %s at %0.3f docs/s with %d workers\n", i, elapsed, rate, *numWorkers)
}
if *optimize {
hostpath := fmt.Sprintf("%s/update", options.Server)
url := fmt.Sprintf("%s?stream.body=<optimize/>", hostpath)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
log.Printf("%s %s", resp.Status, url)
elapsed := time.Since(start)
if *verbose {
log.Printf("indexed and optimized in %s", elapsed)
}
}
}