Skip to content

Commit 866e6a3

Browse files
committed
Introduce SearchBackend
A search backend (server) transforms `virt.v1.a-domain:specific.query-lang:uage` queries into metric names, much like a brace or glob expansion. This provides a means to transparently support additional [tag] hierarchies. For an example of a SearchBackend see: https://github.com/kanatohodets/carbonsearch - :8070 port was chosen as it comes before our typical StorageBackend port :8080
1 parent 18e7db2 commit 866e6a3

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

example.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,11 @@
5151
"http://192.168.0.100:8080",
5252
"http://192.168.0.200:8080",
5353
"http://192.168.1.212:8080"
54-
]
54+
],
55+
56+
// Instance of carbonsearch backend
57+
"SearchBackend": "http://127.0.0.1:8070",
58+
59+
// carbonsearch prefix to reserve/register
60+
"SearchPrefix": "virt.v1."
5561
}

main.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var Config = struct {
4141
TimeoutMs int
4242
TimeoutMsAfterAllStarted int
4343

44+
SearchBackend string
45+
SearchPrefix string
46+
4447
GraphiteHost string
4548

4649
pathCache pathCache
@@ -67,6 +70,8 @@ var Metrics = struct {
6770
FindRequests *expvar.Int
6871
FindErrors *expvar.Int
6972

73+
SearchRequests *expvar.Int
74+
7075
RenderRequests *expvar.Int
7176
RenderErrors *expvar.Int
7277

@@ -81,6 +86,8 @@ var Metrics = struct {
8186
FindRequests: expvar.NewInt("find_requests"),
8287
FindErrors: expvar.NewInt("find_errors"),
8388

89+
SearchRequests: expvar.NewInt("search_requests"),
90+
8491
RenderRequests: expvar.NewInt("render_requests"),
8592
RenderErrors: expvar.NewInt("render_errors"),
8693

@@ -294,38 +301,62 @@ func findHandler(w http.ResponseWriter, req *http.Request) {
294301

295302
Metrics.FindRequests.Add(1)
296303

304+
queries := []string{req.FormValue("query")}
305+
297306
rewrite, _ := url.ParseRequestURI(req.URL.RequestURI())
298307
v := rewrite.Query()
299308
format := req.FormValue("format")
300309
v.Set("format", "protobuf")
301310
rewrite.RawQuery = v.Encode()
302311

303-
query := req.FormValue("query")
304-
var tld string
305-
if i := strings.IndexByte(query, '.'); i > 0 {
306-
tld = query[:i]
307-
}
308-
// lookup tld in our map of where they live to reduce the set of
309-
// servers we bug with our find
310-
var backends []string
311-
var ok bool
312-
if backends, ok = Config.pathCache.get(tld); !ok || backends == nil || len(backends) == 0 {
313-
backends = Config.Backends
312+
if strings.HasPrefix(queries[0], Config.SearchPrefix) {
313+
Metrics.SearchRequests.Add(1)
314+
// Send query to SearchBackend. The result is []queries for StorageBackends
315+
searchResponse := multiGet([]string{Config.SearchBackend}, rewrite.RequestURI())
316+
m, _ := findUnpackPB(req, searchResponse)
317+
queries = make([]string, 0, len(m))
318+
for _, v := range m {
319+
queries = append(queries, *v.Path)
320+
}
314321
}
315322

316-
responses := multiGet(backends, rewrite.RequestURI())
323+
var metrics []*pb.GlobMatch
324+
// TODO(nnuss): Rewrite the result queries to a series of brace expansions based on TLD?
325+
// [a.b, a.c, a.dee.eee.eff, x.y] => [ "a.{b,c,dee.eee.eff}", "x.y" ]
326+
// Be mindful that carbonserver's default MaxGlobs is 10
327+
for _, query := range queries {
317328

318-
if len(responses) == 0 {
319-
logger.Logln("find: error querying backends for: ", rewrite.RequestURI())
320-
http.Error(w, "find: error querying backends", http.StatusInternalServerError)
321-
return
322-
}
329+
v.Set("query", query)
330+
rewrite.RawQuery = v.Encode()
323331

324-
metrics, paths := findUnpackPB(req, responses)
332+
var tld string
333+
if i := strings.IndexByte(query, '.'); i > 0 {
334+
tld = query[:i]
335+
}
325336

326-
// update our cache of which servers have which metrics
327-
for k, v := range paths {
328-
Config.pathCache.set(k, v)
337+
// lookup tld in our map of where they live to reduce the set of
338+
// servers we bug with our find
339+
var backends []string
340+
var ok bool
341+
if backends, ok = Config.pathCache.get(tld); !ok || backends == nil || len(backends) == 0 {
342+
backends = Config.Backends
343+
}
344+
345+
responses := multiGet(backends, rewrite.RequestURI())
346+
347+
if len(responses) == 0 {
348+
logger.Logln("find: error querying backends for: ", rewrite.RequestURI())
349+
http.Error(w, "find: error querying backends", http.StatusInternalServerError)
350+
return
351+
}
352+
353+
m, paths := findUnpackPB(req, responses)
354+
metrics = append(metrics, m...)
355+
356+
// update our cache of which servers have which metrics
357+
for k, v := range paths {
358+
Config.pathCache.set(k, v)
359+
}
329360
}
330361

331362
switch format {

0 commit comments

Comments
 (0)