-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
fix store query bug when running loki in single binary mode with boltdb-shipper #2655
Merged
sandeepsukhani
merged 2 commits into
grafana:master
from
sandeepsukhani:fix-store-query-bug
Sep 25, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,10 @@ const ( | |
tailerWaitEntryThrottle = time.Second / 2 | ||
) | ||
|
||
type interval struct { | ||
start, end time.Time | ||
} | ||
|
||
// Config for a querier. | ||
type Config struct { | ||
QueryTimeout time.Duration `yaml:"query_timeout"` | ||
|
@@ -80,51 +84,40 @@ func (q *Querier) SelectLogs(ctx context.Context, params logql.SelectLogParams) | |
return nil, err | ||
} | ||
|
||
var chunkStoreIter iter.EntryIterator | ||
ingesterQueryInterval, storeQueryInterval := q.buildQueryIntervals(params.Start, params.End) | ||
|
||
if q.cfg.IngesterQueryStoreMaxLookback == 0 { | ||
// IngesterQueryStoreMaxLookback is zero, the default state, query the store normally | ||
chunkStoreIter, err = q.store.SelectLogs(ctx, params) | ||
iters := []iter.EntryIterator{} | ||
if ingesterQueryInterval != nil { | ||
// Make a copy of the request before modifying | ||
// because the initial request is used below to query stores | ||
queryRequestCopy := *params.QueryRequest | ||
newParams := logql.SelectLogParams{ | ||
QueryRequest: &queryRequestCopy, | ||
} | ||
newParams.Start = ingesterQueryInterval.start | ||
newParams.End = ingesterQueryInterval.end | ||
|
||
ingesterIters, err := q.ingesterQuerier.SelectLogs(ctx, newParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else if q.cfg.IngesterQueryStoreMaxLookback > 0 { | ||
// IngesterQueryStoreMaxLookback is greater than zero | ||
// Adjust the store query range to only query for data ingesters are not already querying for | ||
adjustedEnd := params.End.Add(-q.cfg.IngesterQueryStoreMaxLookback) | ||
if params.Start.After(adjustedEnd) { | ||
chunkStoreIter = iter.NoopIterator | ||
} else { | ||
// Make a copy of the request before modifying | ||
// because the initial request is used below to query ingesters | ||
queryRequestCopy := *params.QueryRequest | ||
newParams := logql.SelectLogParams{ | ||
QueryRequest: &queryRequestCopy, | ||
} | ||
newParams.End = adjustedEnd | ||
chunkStoreIter, err = q.store.SelectLogs(ctx, newParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} else { | ||
// IngesterQueryStoreMaxLookback is less than zero | ||
// ingesters will be querying all the way back in time so there is no reason to query the store | ||
chunkStoreIter = iter.NoopIterator | ||
} | ||
|
||
// skip ingester queries only when QueryIngestersWithin is enabled (not the zero value) and | ||
// the end of the query is earlier than the lookback | ||
if !shouldQueryIngester(q.cfg, params) { | ||
return chunkStoreIter, nil | ||
iters = append(iters, ingesterIters...) | ||
} | ||
|
||
iters, err := q.ingesterQuerier.SelectLogs(ctx, params) | ||
if err != nil { | ||
return nil, err | ||
if storeQueryInterval != nil { | ||
params.Start = storeQueryInterval.start | ||
params.End = storeQueryInterval.end | ||
|
||
storeIter, err := q.store.SelectLogs(ctx, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
iters = append(iters, storeIter) | ||
} | ||
|
||
return iter.NewHeapIterator(ctx, append(iters, chunkStoreIter), params.Direction), nil | ||
return iter.NewHeapIterator(ctx, iters, params.Direction), nil | ||
} | ||
|
||
func (q *Querier) SelectSamples(ctx context.Context, params logql.SelectSampleParams) (iter.SampleIterator, error) { | ||
|
@@ -133,52 +126,106 @@ func (q *Querier) SelectSamples(ctx context.Context, params logql.SelectSamplePa | |
return nil, err | ||
} | ||
|
||
var chunkStoreIter iter.SampleIterator | ||
ingesterQueryInterval, storeQueryInterval := q.buildQueryIntervals(params.Start, params.End) | ||
|
||
switch { | ||
case q.cfg.IngesterQueryStoreMaxLookback == 0: | ||
// IngesterQueryStoreMaxLookback is zero, the default state, query the store normally | ||
chunkStoreIter, err = q.store.SelectSamples(ctx, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
case q.cfg.IngesterQueryStoreMaxLookback > 0: | ||
adjustedEnd := params.End.Add(-q.cfg.IngesterQueryStoreMaxLookback) | ||
if params.Start.After(adjustedEnd) { | ||
chunkStoreIter = iter.NoopIterator | ||
break | ||
} | ||
iters := []iter.SampleIterator{} | ||
if ingesterQueryInterval != nil { | ||
// Make a copy of the request before modifying | ||
// because the initial request is used below to query ingesters | ||
// because the initial request is used below to query stores | ||
queryRequestCopy := *params.SampleQueryRequest | ||
newParams := logql.SelectSampleParams{ | ||
SampleQueryRequest: &queryRequestCopy, | ||
} | ||
newParams.End = adjustedEnd | ||
chunkStoreIter, err = q.store.SelectSamples(ctx, newParams) | ||
newParams.Start = ingesterQueryInterval.start | ||
newParams.End = ingesterQueryInterval.end | ||
|
||
ingesterIters, err := q.ingesterQuerier.SelectSample(ctx, newParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
chunkStoreIter = iter.NoopIterator | ||
|
||
} | ||
// skip ingester queries only when QueryIngestersWithin is enabled (not the zero value) and | ||
// the end of the query is earlier than the lookback | ||
if !shouldQueryIngester(q.cfg, params) { | ||
return chunkStoreIter, nil | ||
iters = append(iters, ingesterIters...) | ||
} | ||
|
||
iters, err := q.ingesterQuerier.SelectSample(ctx, params) | ||
if err != nil { | ||
return nil, err | ||
if storeQueryInterval != nil { | ||
params.Start = storeQueryInterval.start | ||
params.End = storeQueryInterval.end | ||
|
||
storeIter, err := q.store.SelectSamples(ctx, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
iters = append(iters, storeIter) | ||
} | ||
return iter.NewHeapSampleIterator(ctx, append(iters, chunkStoreIter)), nil | ||
return iter.NewHeapSampleIterator(ctx, iters), nil | ||
} | ||
|
||
func shouldQueryIngester(cfg Config, params logql.QueryParams) bool { | ||
lookback := time.Now().Add(-cfg.QueryIngestersWithin) | ||
return !(cfg.QueryIngestersWithin != 0 && params.GetEnd().Before(lookback)) | ||
func (q *Querier) buildQueryIntervals(queryStart, queryEnd time.Time) (*interval, *interval) { | ||
// limitQueryInterval is a flag for whether store queries should be limited to start time of ingester queries. | ||
limitQueryInterval := false | ||
// ingesterMLB having -1 means query ingester for whole duration. | ||
ingesterMLB := time.Duration(-1) | ||
if q.cfg.IngesterQueryStoreMaxLookback != 0 { | ||
// IngesterQueryStoreMaxLookback takes the precedence over QueryIngestersWithin while also limiting the store query range. | ||
limitQueryInterval = true | ||
ingesterMLB = q.cfg.IngesterQueryStoreMaxLookback | ||
} else if q.cfg.QueryIngestersWithin != 0 { | ||
ingesterMLB = q.cfg.QueryIngestersWithin | ||
} | ||
|
||
// query ingester for whole duration. | ||
if ingesterMLB == -1 { | ||
i := &interval{ | ||
start: queryStart, | ||
end: queryEnd, | ||
} | ||
|
||
if limitQueryInterval { | ||
// query only ingesters. | ||
return i, nil | ||
} | ||
|
||
// query both stores and ingesters without limiting the query interval. | ||
return i, i | ||
} | ||
|
||
// see if there is an overlap between ingester query interval and actual query interval, if not just do the store query. | ||
ingesterOldestStartTime := time.Now().Add(-ingesterMLB) | ||
if queryEnd.Before(ingesterOldestStartTime) { | ||
return nil, &interval{ | ||
start: queryStart, | ||
end: queryEnd, | ||
} | ||
} | ||
|
||
// if there is an overlap and we are not limiting the query interval then do both store and ingester query for whole query interval. | ||
if !limitQueryInterval { | ||
i := &interval{ | ||
start: queryStart, | ||
end: queryEnd, | ||
} | ||
return i, i | ||
} | ||
Comment on lines
+203
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The microservices deployment mode with object store will run here no matter if there is an overlap.
This case will also query ingester for whole duration. |
||
|
||
// limit the start of ingester query interval to ingesterOldestStartTime. | ||
ingesterQueryInterval := &interval{ | ||
start: ingesterOldestStartTime, | ||
end: queryEnd, | ||
} | ||
|
||
// limit the end of ingester query interval to ingesterOldestStartTime. | ||
storeQueryInterval := &interval{ | ||
start: queryStart, | ||
end: ingesterOldestStartTime, | ||
} | ||
|
||
// query touches only ingester query interval so do not do store query. | ||
if storeQueryInterval.start.After(storeQueryInterval.end) { | ||
storeQueryInterval = nil | ||
} | ||
|
||
return ingesterQueryInterval, storeQueryInterval | ||
} | ||
|
||
// Label does the heavy lifting for a Label query. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why query ingester for whole duration ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is inconsistent with the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to support the rare usecase where you run Loki with multiple single binaries and each of them storing the data on local filesystem without any sharing.