-
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 bugs in logs results caching and its tests #7925
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,8 +180,7 @@ func (l *logResultCache) handleHit(ctx context.Context, cacheKey string, cachedR | |
result := emptyResponse(cachedRequest) | ||
// if the request is the same and cover the whole time range, | ||
// we can just return the cached result. | ||
if !lokiReq.GetStartTs().After(cachedRequest.GetStartTs()) && lokiReq.GetStartTs().Equal(cachedRequest.GetStartTs()) && | ||
!lokiReq.GetEndTs().Before(cachedRequest.GetEndTs()) && lokiReq.GetEndTs().Equal(cachedRequest.GetEndTs()) { | ||
if cachedRequest.StartTs.UnixNano() <= lokiReq.StartTs.UnixNano() && cachedRequest.EndTs.UnixNano() >= lokiReq.EndTs.UnixNano() { | ||
return result, nil | ||
} | ||
// we could be missing data at the start and the end. | ||
|
@@ -240,7 +239,7 @@ func (l *logResultCache) handleHit(ctx context.Context, cacheKey string, cachedR | |
if startResp.Status != loghttp.QueryStatusSuccess { | ||
return startResp, nil | ||
} | ||
result = mergeLokiResponse(startResp, result) | ||
result = mergeLokiResponse(extractLokiResponse(lokiReq.GetStartTs(), lokiReq.GetEndTs(), startResp), result) | ||
} | ||
} | ||
|
||
|
@@ -254,7 +253,7 @@ func (l *logResultCache) handleHit(ctx context.Context, cacheKey string, cachedR | |
if endResp.Status != loghttp.QueryStatusSuccess { | ||
return endResp, nil | ||
} | ||
result = mergeLokiResponse(endResp, result) | ||
result = mergeLokiResponse(extractLokiResponse(lokiReq.GetStartTs(), lokiReq.GetEndTs(), endResp), result) | ||
} | ||
} | ||
|
||
|
@@ -274,6 +273,45 @@ func (l *logResultCache) handleHit(ctx context.Context, cacheKey string, cachedR | |
return result, nil | ||
} | ||
|
||
// extractLokiResponse extracts response with interval [start, end) | ||
func extractLokiResponse(start, end time.Time, r *LokiResponse) *LokiResponse { | ||
extractedResp := LokiResponse{ | ||
Status: r.Status, | ||
Direction: r.Direction, | ||
Limit: r.Limit, | ||
Version: r.Version, | ||
ErrorType: r.ErrorType, | ||
Error: r.Error, | ||
Statistics: r.Statistics, | ||
Data: LokiData{ | ||
ResultType: r.Data.ResultType, | ||
Result: []logproto.Stream{}, | ||
}, | ||
} | ||
for _, stream := range r.Data.Result { | ||
if stream.Entries[0].Timestamp.After(end) || stream.Entries[len(stream.Entries)-1].Timestamp.Before(start) { | ||
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 condition one above uses 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. I went with different ways to compare the time for convenience and readability. |
||
continue | ||
} | ||
|
||
extractedStream := logproto.Stream{ | ||
Labels: stream.Labels, | ||
Entries: []logproto.Entry{}, | ||
Hash: stream.Hash, | ||
} | ||
for _, entry := range stream.Entries { | ||
if entry.Timestamp.Before(start) || entry.Timestamp.After(end) || entry.Timestamp.Equal(end) { | ||
continue | ||
} | ||
|
||
extractedStream.Entries = append(extractedStream.Entries, entry) | ||
} | ||
|
||
extractedResp.Data.Result = append(extractedResp.Data.Result, extractedStream) | ||
} | ||
|
||
return &extractedResp | ||
} | ||
|
||
func isEmpty(lokiRes *LokiResponse) bool { | ||
return lokiRes.Status == loghttp.QueryStatusSuccess && len(lokiRes.Data.Result) == 0 | ||
} | ||
|
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 check looked wrong to me, so I fixed it as well.