Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions frac/processor/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"math"
"time"

"go.uber.org/zap"

"github.com/ozontech/seq-db/consts"
"github.com/ozontech/seq-db/frac/sealed/lids"
"github.com/ozontech/seq-db/logger"
"github.com/ozontech/seq-db/metric/stopwatch"
"github.com/ozontech/seq-db/node"
"github.com/ozontech/seq-db/parser"
Expand Down Expand Up @@ -119,28 +122,48 @@ func IndexSearch(
IDs: ids,
Aggs: aggsResult,
Total: uint64(total),
Histogram: histogram,
Histogram: convertHistToMap(params, histogram),
}

stats.UpdateMetrics()

return qpr, nil
}

func convertHistToMap(params SearchParams, hist []uint64) map[seq.MID]uint64 {
if len(hist) == 0 {
return nil
}
res := make(map[seq.MID]uint64, len(hist))
bucket := params.From - params.From%seq.MID(params.HistInterval)
for _, cnt := range hist {
if cnt > 0 {
res[bucket] = cnt
}
bucket += seq.MID(params.HistInterval)
}
return res
}

func iterateEvalTree(
ctx context.Context,
params SearchParams,
idsIndex idsIndex,
evalTree node.Node,
aggs []Aggregator,
sw *stopwatch.Stopwatch,
) (int, seq.IDSources, map[seq.MID]uint64, error) {
) (int, seq.IDSources, []uint64, error) {
hasHist := params.HasHist()
needScanAllRange := params.IsScanAllRequest()

var histogram map[seq.MID]uint64
var (
histBase uint64
histogram []uint64
)
if hasHist {
histogram = make(map[seq.MID]uint64)
histBase = uint64(params.From) / params.HistInterval
histSize := uint64(params.To)/params.HistInterval - histBase + 1
histogram = make([]uint64, histSize)
}

total := 0
Expand Down Expand Up @@ -172,9 +195,15 @@ func iterateEvalTree(
m.Stop()

if hasHist {
bucket := mid
bucket -= bucket % seq.MID(params.HistInterval)
histogram[bucket]++
if mid < params.From || mid > params.To {
logger.Error("MID value outside the query range",
zap.Time("from", params.From.Time()),
zap.Time("to", params.To.Time()),
zap.Time("mid", mid.Time()))
continue
}
bucketIndex := uint64(mid)/params.HistInterval - histBase
histogram[bucketIndex]++
}

if needMore {
Expand Down
4 changes: 4 additions & 0 deletions frac/sealed_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (dp *sealedDataProvider) Fetch(ids []seq.ID) ([][]byte, error) {
func (dp *sealedDataProvider) Search(params processor.SearchParams) (*seq.QPR, error) {
aggLimits := processor.AggLimits(dp.config.Search.AggLimits)

// Limit the parameter range to data boundaries to prevent histogram overflow
params.From = max(params.From, dp.info.From)
params.To = min(params.To, dp.info.To)

sw := stopwatch.New()

defer sw.Export(
Expand Down
Loading