-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
246 lines (190 loc) · 6.07 KB
/
main.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
// Copyright (C) 2015 Foursquare Labs Inc.
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"runtime"
"strconv"
"time"
_ "expvar"
_ "net/http/pprof"
"github.com/apache/thrift/lib/go/thrift"
"github.com/foursquare/fsgo/adminz"
"github.com/foursquare/fsgo/report"
pb "github.com/foursquare/quiver/gen_proto"
"github.com/foursquare/quiver/hfile"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var version string = "HEAD?"
var buildTime string = "unknown?"
type SettingDefs struct {
port int
rpcPort int
grpcPort int
downloadOnly bool
debug bool
bloom int
mlock bool
onDisk bool
configJsonUrl string
cachePath string
zk string
discoveryPath string
packageVersion string
}
var Settings SettingDefs
func readSettings() []string {
s := SettingDefs{}
flag.IntVar(&s.port, "port", 9999, "listen port")
flag.IntVar(&s.rpcPort, "rpc-port", 0, "listen port for raw thrift rpc (framed tbinary)")
flag.IntVar(&s.grpcPort, "grpc-port", 0, "listen port for gRPC")
flag.BoolVar(&s.debug, "debug", false, "print more output")
flag.IntVar(&s.bloom, "bloom", 0, "bloom filter wrong-positive % (or 0 to disable): lower numbers use more RAM but filter more queries.")
flag.BoolVar(&s.downloadOnly, "download-only", false, "exit after downloading remote files to local cache.")
flag.BoolVar(&s.onDisk, "mnolock", false, "mmap files in memory rather than copy to heap, but don't mlock.")
flag.BoolVar(&s.mlock, "mlock", false, "mlock mapped files in memory rather than copy to heap.")
flag.StringVar(&s.configJsonUrl, "config-json", "", "URL of collection configuration json")
flag.StringVar(&s.cachePath, "cache", os.TempDir(), "local path to write files fetched (*not* cleaned up automatically)")
flag.StringVar(&s.zk, "zookeeper", "", "zookeeper")
flag.StringVar(&s.discoveryPath, "discovery", "", "service discovery base path")
flag.StringVar(&s.packageVersion, "package-version", "", "version of the deployed package")
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
`
Usage: %s [options] col1=path1 col2=path2 ...
`, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
Settings = s
if (len(flag.Args()) > 0) == (Settings.configJsonUrl != "") {
log.Println("Collections must be specified OR URL to configuration json.")
flag.Usage()
os.Exit(-1)
}
return flag.Args()
}
func main() {
log.Printf("Quiver version %s (built %s, %s).\n\n", version, buildTime, runtime.Version())
t := time.Now()
graphite := report.Flag()
args := readSettings()
stats := report.NewRecorder().
EnableGCInfoCollection().
MaybeReportTo(graphite).
RegisterHttp().
SetAsDefault()
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
registrations := new(Registrations)
if Settings.discoveryPath != "" && !Settings.downloadOnly {
registrations.Connect()
defer registrations.Close()
}
configs := getCollectionConfig(args)
log.Println("Loading collections...")
cs, err := hfile.LoadCollections(configs, Settings.cachePath, Settings.downloadOnly, stats)
if err != nil {
log.Fatal(err)
}
if Settings.downloadOnly {
stats.FlushNow()
return
}
if Settings.bloom > 0 {
beforeBloom := time.Now()
for _, c := range cs.Collections {
log.Println("Calculating bloom filter for", c.Name)
c.CalculateBloom(float64(Settings.bloom) / 100)
}
stats.TimeSince("startup.bloom", beforeBloom)
}
log.Printf("Serving on http://%s:%d/ \n", hostname, Settings.port)
http.Handle("/rpc/HFileService", WrapHttpRpcHandler(cs, stats))
admin := adminz.New()
admin.KillfilePaths(adminz.Killfiles(Settings.port))
admin.Servicez(func() interface{} {
return struct {
Collections map[string]*hfile.Reader `json:"collections"`
Impl string `json:"implementation"`
QuiverVersion string `json:"quiver_version"`
PackageVersion string `json:"package_version"`
}{
cs.Collections,
"quiver",
version,
Settings.packageVersion,
}
})
admin.OnPause(registrations.Leave)
admin.OnResume(func() {
if Settings.discoveryPath != "" {
registrations.Join(hostname, Settings.discoveryPath, configs, 0)
}
})
http.HandleFunc("/hfilez", admin.ServicezHandler)
http.HandleFunc("/", admin.ServicezHandler)
http.HandleFunc("/debug/bloom/enable", func(w http.ResponseWriter, r *http.Request) {
for _, c := range cs.Collections {
c.EnableBloom()
}
})
http.HandleFunc("/debug/bloom/disable", func(w http.ResponseWriter, r *http.Request) {
for _, c := range cs.Collections {
c.DisableBloom()
}
})
http.HandleFunc("/debug/bloom/calc", func(w http.ResponseWriter, r *http.Request) {
if falsePos, err := strconv.Atoi(r.URL.Query().Get("err")); err != nil {
http.Error(w, err.Error(), 400)
} else if falsePos > 99 || falsePos < 1 {
http.Error(w, "`err` param must be a false pos rate between 0 and 100", 400)
} else {
admin.Pause()
defer admin.Resume()
for _, c := range cs.Collections {
fmt.Fprintln(w, "Recalculating bloom for", c.Name)
c.CalculateBloom(float64(falsePos) / 100)
}
}
})
runtime.GC()
stats.FlushNow()
admin.Start()
stats.TimeSince("startup.total", t)
if Settings.rpcPort > 0 {
s, err := NewTRpcServer(fmt.Sprintf(":%d", Settings.rpcPort), WrapProcessor(cs, stats), thrift.NewTBinaryProtocolFactory(true, true))
if err != nil {
log.Fatalln("Could not open RPC port", Settings.rpcPort, err)
} else {
if err := s.Listen(); err != nil {
log.Fatalln("Failed to listen on RPC port", err)
}
go func() {
log.Fatalln(s.Serve())
}()
log.Println("Listening for raw RPC on", Settings.rpcPort)
}
}
if Settings.grpcPort > 0 {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", Settings.grpcPort))
if err != nil {
log.Fatalf("failed to listen on gRPC port %d: %v", Settings.grpcPort, err)
}
s := grpc.NewServer()
pb.RegisterQuiverServiceServer(s, &GrpcImpl{&RpcShared{cs}})
reflection.Register(s)
go func() {
log.Fatalln(s.Serve(lis))
}()
log.Println("Listening for gRPC on", Settings.grpcPort)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", Settings.port), nil))
}