Skip to content

Commit

Permalink
add most_recent query hint
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Elliott <[email protected]>
  • Loading branch information
joe-elliott committed Oct 25, 2024
1 parent 3d9fc4d commit f3bc8f9
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 332 deletions.
56 changes: 23 additions & 33 deletions modules/frontend/combiner/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package combiner

import (
"net/http"
"sort"
"time"

"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/search"
Expand Down Expand Up @@ -40,8 +38,8 @@ func (s *SearchJobResponse) IsMetadata() bool {
var _ GRPCCombiner[*tempopb.SearchResponse] = (*genericCombiner[*tempopb.SearchResponse])(nil)

// NewSearch returns a search combiner
func NewSearch(keepMostRecent int) Combiner {
metadataCombiner := traceql.NewMetadataCombiner(keepMostRecent)
func NewSearch(limit int, keepMostRecent bool) Combiner {
metadataCombiner := traceql.NewMetadataCombiner(limit, keepMostRecent)
diffTraces := map[string]struct{}{}
completedThroughTracker := &ShardCompletionTracker{}

Expand Down Expand Up @@ -95,18 +93,25 @@ func NewSearch(keepMostRecent int) Combiner {
Metrics: current.Metrics,
}

completedThroughSeconds := completedThroughTracker.completedThroughSeconds
// if all jobs are completed then let's just return everything the combiner has
if current.Metrics.CompletedJobs == current.Metrics.TotalJobs && current.Metrics.TotalJobs > 0 {
completedThroughSeconds = 1
}

// if we've not completed any shards, then return nothing
if completedThroughSeconds == 0 {
return diff, nil
metadataFn := metadataCombiner.Metadata
if keepMostRecent {
metadataFn = func() []*tempopb.TraceSearchMetadata {
completedThroughSeconds := completedThroughTracker.completedThroughSeconds
// if all jobs are completed then let's just return everything the combiner has
if current.Metrics.CompletedJobs == current.Metrics.TotalJobs && current.Metrics.TotalJobs > 0 {
completedThroughSeconds = 1
}

// if we've not completed any shards, then return nothing
if completedThroughSeconds == 0 {
return nil
}

return metadataCombiner.MetadataAfter(completedThroughSeconds)
}
}

for _, tr := range metadataCombiner.MetadataAfter(completedThroughSeconds) {
for _, tr := range metadataFn() {
// if not in the map, skip. we haven't seen an update
if _, ok := diffTraces[tr.TraceID]; !ok {
continue
Expand All @@ -116,35 +121,20 @@ func NewSearch(keepMostRecent int) Combiner {
diff.Traces = append(diff.Traces, tr)
}

sort.Slice(diff.Traces, func(i, j int) bool {
return diff.Traces[i].StartTimeUnixNano > diff.Traces[j].StartTimeUnixNano
})

addRootSpanNotReceivedText(diff.Traces)

return diff, nil
},
// search combiner doesn't use current in the way i would have expected. it only tracks metrics through current and uses the results map for the actual traces.
// should we change this?
quit: func(_ *tempopb.SearchResponse) bool {
// are we tracking a limit at all?
if keepMostRecent <= 0 {
return false
}

completedThroughSeconds := completedThroughTracker.completedThroughSeconds
// have we completed any shards?
if completedThroughSeconds == 0 {
return false
}

// do we have enought?
if metadataCombiner.Count() < keepMostRecent {
return false
completedThroughSeconds = traceql.TimestampNever
}

// is our oldest trace newer than the completedThrough?
return metadataCombiner.OldestTimestampNanos() > uint64(completedThroughSeconds)*uint64(time.Second)
return metadataCombiner.IsCompleteFor(completedThroughSeconds)
},
}
initHTTPCombiner(c, api.HeaderAcceptJSON)
Expand All @@ -159,8 +149,8 @@ func addRootSpanNotReceivedText(results []*tempopb.TraceSearchMetadata) {
}
}

func NewTypedSearch(limit int) GRPCCombiner[*tempopb.SearchResponse] {
return NewSearch(limit).(GRPCCombiner[*tempopb.SearchResponse])
func NewTypedSearch(limit int, keepMostRecent bool) GRPCCombiner[*tempopb.SearchResponse] {
return NewSearch(limit, keepMostRecent).(GRPCCombiner[*tempopb.SearchResponse])
}

// ShardCompletionTracker
Expand Down
Loading

0 comments on commit f3bc8f9

Please sign in to comment.