Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make log query results cacheable #4095

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion pkg/querier/queryrange/split_by_interval.go
Expand Up @@ -145,7 +145,6 @@ func (h *splitByInterval) loop(ctx context.Context, ch <-chan *lokiResult, next

sp, ctx := opentracing.StartSpanFromContext(ctx, "interval")
data.req.LogToSpan(sp)

resp, err := next.Do(ctx, data.req)

select {
Expand Down Expand Up @@ -218,6 +217,18 @@ func splitByTime(req queryrange.Request, interval time.Duration) []queryrange.Re

switch r := req.(type) {
case *LokiRequest:
// Align the queries time range to multiples of the interval to allow for better caching.
ins := interval.Nanoseconds()
ogStart := r.StartTs
ogEnd := r.EndTs
start := (ogStart.UnixNano() - (ogStart.UnixNano() % ins)) / int64(time.Millisecond)
end := ogEnd.UnixNano()
if ogEnd.UnixNano()%ins != 0 {
end = ogEnd.UnixNano() + (ins - (ogEnd.UnixNano() % ins))

}
end = end / int64(time.Millisecond)
r = r.WithStartEnd(start, end).(*LokiRequest)
forInterval(interval, r.StartTs, r.EndTs, func(start, end time.Time) {
reqs = append(reqs, &LokiRequest{
Query: r.Query,
Expand All @@ -229,6 +240,26 @@ func splitByTime(req queryrange.Request, interval time.Duration) []queryrange.Re
EndTs: end,
})
})

for i, interval := range reqs {
// Reset the start and end for the first/last interval to the original
// times so that we don't receive data we didn't ask for.
if interval.GetStart() == start {
r, ok := reqs[i].(*LokiRequest)
if ok {
r.StartTs = ogStart
}
reqs[i] = r

}
if interval.GetEnd() == end {
r, ok := reqs[i].(*LokiRequest)
if ok {
r.EndTs = ogEnd
}
reqs[i] = r
}
}
case *LokiSeriesRequest:
forInterval(interval, r.StartTs, r.EndTs, func(start, end time.Time) {
reqs = append(reqs, &LokiSeriesRequest{
Expand Down