Skip to content

Commit baa63a0

Browse files
authored
feat(tagged): refactor tag count querying, use it in autocomplete (#318)
1 parent 3b4cdf8 commit baa63a0

File tree

7 files changed

+1036
-198
lines changed

7 files changed

+1036
-198
lines changed

autocomplete/autocomplete.go

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,29 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7171
}
7272
}
7373

74-
func (h *Handler) requestExpr(r *http.Request) (*where.Where, *where.Where, map[string]bool, error) {
75-
f := r.Form["expr"]
76-
expr := make([]string, 0, len(f))
74+
func getTagCountQuerier(config *config.Config, opts clickhouse.Options) *finder.TagCountQuerier {
75+
var tcq *finder.TagCountQuerier = nil
76+
if config.ClickHouse.TagsCountTable != "" {
77+
tcq = finder.NewTagCountQuerier(
78+
config.ClickHouse.URL,
79+
config.ClickHouse.TagsCountTable,
80+
opts,
81+
config.FeatureFlags.UseCarbonBehavior,
82+
config.FeatureFlags.DontMatchMissingTags,
83+
config.ClickHouse.TaggedUseDaily,
84+
)
85+
}
86+
87+
return tcq
88+
}
89+
90+
func (h *Handler) requestExpr(r *http.Request, tcq *finder.TagCountQuerier, from, until int64) (*where.Where, *where.Where, map[string]bool, error) {
91+
formExpr := r.Form["expr"]
92+
expr := make([]string, 0, len(formExpr))
7793

78-
for i := 0; i < len(f); i++ {
79-
if f[i] != "" {
80-
expr = append(expr, f[i])
94+
for i := 0; i < len(formExpr); i++ {
95+
if formExpr[i] != "" {
96+
expr = append(expr, formExpr[i])
8197
}
8298
}
8399

@@ -95,6 +111,21 @@ func (h *Handler) requestExpr(r *http.Request) (*where.Where, *where.Where, map[
95111
return wr, pw, usedTags, err
96112
}
97113

114+
if tcq != nil {
115+
tagValuesCosts, err := tcq.GetCostsFromCountTable(r.Context(), terms, from, until)
116+
if err != nil {
117+
return wr, pw, usedTags, err
118+
}
119+
120+
if tagValuesCosts != nil {
121+
finder.SetCosts(terms, tagValuesCosts)
122+
} else if len(h.config.ClickHouse.TaggedCosts) != 0 {
123+
finder.SetCosts(terms, h.config.ClickHouse.TaggedCosts)
124+
}
125+
}
126+
127+
finder.SortTaggedTermsByCost(terms)
128+
98129
wr, pw, err = finder.TaggedWhere(terms, h.config.FeatureFlags.UseCarbonBehavior, h.config.FeatureFlags.DontMatchMissingTags)
99130
if err != nil {
100131
return wr, pw, usedTags, err
@@ -214,6 +245,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
214245
queueFail bool
215246
queueDuration time.Duration
216247
findCache bool
248+
opts clickhouse.Options
217249
)
218250

219251
username := r.Header.Get("X-Forwarded-User")
@@ -239,7 +271,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
239271
limiter.SendDuration(queueDuration.Milliseconds())
240272
metrics.SendFindMetrics(metrics.TagsRequestMetric, status, dMS, 0, h.config.Metrics.ExtendedStat, metricsCount)
241273

242-
if !findCache && chReadRows != 0 && chReadBytes != 0 {
274+
if !findCache && chReadRows > 0 && chReadBytes > 0 {
243275
errored := status != http.StatusOK && status != http.StatusNotFound
244276
metrics.SendQueryRead(metrics.AutocompleteQMetric, 0, 0, dMS, metricsCount, readBytes, chReadRows, chReadBytes, errored)
245277
}
@@ -290,7 +322,21 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
290322
}
291323
}
292324

293-
wr, pw, usedTags, err := h.requestExpr(r)
325+
opts = clickhouse.Options{
326+
TLSConfig: h.config.ClickHouse.TLSConfig,
327+
Timeout: h.config.ClickHouse.IndexTimeout,
328+
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
329+
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
330+
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
331+
}
332+
333+
wr, pw, usedTags, err := h.requestExpr(
334+
r,
335+
getTagCountQuerier(h.config, opts),
336+
start.AddDate(0, 0, -h.config.ClickHouse.TaggedAutocompleDays).Unix(),
337+
start.Unix(),
338+
)
339+
294340
if err != nil {
295341
status = http.StatusBadRequest
296342
http.Error(w, err.Error(), status)
@@ -366,13 +412,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
366412
scope.WithTable(r.Context(), h.config.ClickHouse.TaggedTable),
367413
h.config.ClickHouse.URL,
368414
sql,
369-
clickhouse.Options{
370-
TLSConfig: h.config.ClickHouse.TLSConfig,
371-
Timeout: h.config.ClickHouse.IndexTimeout,
372-
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
373-
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
374-
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
375-
},
415+
opts,
376416
nil,
377417
)
378418

@@ -490,6 +530,7 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
490530
queueFail bool
491531
queueDuration time.Duration
492532
findCache bool
533+
opts clickhouse.Options
493534
)
494535

495536
username := r.Header.Get("X-Forwarded-User")
@@ -567,8 +608,22 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
567608
}
568609
}
569610

611+
opts = clickhouse.Options{
612+
TLSConfig: h.config.ClickHouse.TLSConfig,
613+
Timeout: h.config.ClickHouse.IndexTimeout,
614+
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
615+
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
616+
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
617+
}
618+
570619
if !findCache {
571-
wr, pw, usedTags, err := h.requestExpr(r)
620+
wr, pw, usedTags, err := h.requestExpr(
621+
r,
622+
getTagCountQuerier(h.config, opts),
623+
start.AddDate(0, 0, -h.config.ClickHouse.TaggedAutocompleDays).Unix(),
624+
start.Unix(),
625+
)
626+
572627
if err == finder.ErrCostlySeriesByTag {
573628
status = http.StatusForbidden
574629
http.Error(w, err.Error(), status)
@@ -640,13 +695,7 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
640695
scope.WithTable(r.Context(), h.config.ClickHouse.TaggedTable),
641696
h.config.ClickHouse.URL,
642697
sql,
643-
clickhouse.Options{
644-
TLSConfig: h.config.ClickHouse.TLSConfig,
645-
Timeout: h.config.ClickHouse.IndexTimeout,
646-
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
647-
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
648-
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
649-
},
698+
opts,
650699
nil,
651700
)
652701

0 commit comments

Comments
 (0)