Skip to content

Commit

Permalink
Remove pointsQueryArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Nov 22, 2024
1 parent f7963c2 commit 4c96771
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion internal/api/pcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newPointsCache(approxMaxSize int, utcOffset int64, loader pointsLoadFunc, n
}

func (c *pointsCache) get(ctx context.Context, h *requestHandler, pq *pointsQuery, lod data_model.LOD, avoidCache bool) ([]pSelectRow, error) {
key := pq.key
key := pq.cacheKey()
if !avoidCache {
rows, ok := c.loadCached(key, lod.FromSec, lod.ToSec)
if ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/promql.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (h *requestHandler) QuerySeries(ctx context.Context, qry *promql.SeriesQuer
for kind, what := range h.getHandlerWhat(qry, step) {
var tx int // time index
for _, lod := range lods {
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
version: lod.Version,
user: h.accessInfo.user,
metricID: qry.Metric.MetricID,
Expand All @@ -468,7 +468,7 @@ func (h *requestHandler) QuerySeries(ctx context.Context, qry *promql.SeriesQuer
by: qry.GroupBy,
filterIn: qry.FilterIn,
filterNotIn: qry.FilterNotIn,
})
}
switch qry.Options.Mode {
case data_model.PointQuery:
data, err := h.pointsCache.get(ctx, h, &pq, lod, qry.Options.AvoidCache)
Expand Down
46 changes: 20 additions & 26 deletions internal/api/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (q *preparedTagValuesQuery) stringTag() bool {
return q.tagID == format.StringTopTagID
}

type pointsQueryArgs struct {
type pointsQuery struct {
version string
user string
metricID int32
Expand All @@ -48,22 +48,18 @@ type pointsQueryArgs struct {
by []string
filterIn data_model.TagFilters
filterNotIn data_model.TagFilters

// for table view requests
orderBy bool
desc bool
sort pointsQuerySort // for table view requests
}

type pointsQuery struct {
key string
pointsQueryArgs
}
type pointsQuerySort int

func newPointsQuery(args pointsQueryArgs) pointsQuery {
return pointsQuery{key: args.cacheKey(), pointsQueryArgs: args}
}
const (
sortNone pointsQuerySort = iota
sortAscending
sortDescending
)

func (pq *pointsQueryArgs) cacheKey() string {
func (pq *pointsQuery) cacheKey() string {
var sb strings.Builder
sb.WriteString(";v=")
sb.WriteString(fmt.Sprint(pq.version))
Expand All @@ -74,7 +70,7 @@ func (pq *pointsQueryArgs) cacheKey() string {
sb.WriteString(";st=")
sb.WriteString(fmt.Sprint(pq.isStringTop))
sb.WriteString(";kind=")
sb.WriteString(fmt.Sprint(pq.kind))
sb.WriteString(fmt.Sprint(int(pq.kind)))
sb.WriteString(";by=")
if len(pq.by) != 0 {
sort.Strings(pq.by)
Expand All @@ -89,10 +85,8 @@ func (pq *pointsQueryArgs) cacheKey() string {
s = writeTagFiltersCacheKey(&sb, pq.filterIn, s)
sb.WriteString(";exl=")
writeTagFiltersCacheKey(&sb, pq.filterNotIn, s)
sb.WriteString(";order=")
sb.WriteString(fmt.Sprint(pq.orderBy))
sb.WriteString(";desc=")
sb.WriteString(fmt.Sprint(pq.desc))
sb.WriteString(";sort=")
sb.WriteString(fmt.Sprint(int(pq.sort)))
return sb.String()
}

Expand Down Expand Up @@ -340,7 +334,7 @@ type pointsQueryMeta struct {
version string
}

func loadPointsSelectWhat(sb *strings.Builder, pq *pointsQueryArgs, version string) (int, error) {
func loadPointsSelectWhat(sb *strings.Builder, pq *pointsQuery, version string) (int, error) {
var (
isStringTop = pq.isStringTop
kind = pq.kind
Expand Down Expand Up @@ -422,7 +416,7 @@ func (pq *pointsQuery) loadPointsQueryV2(lod data_model.LOD, utcOffset int64) (s
for _, b := range pq.by {
sb.WriteString(fmt.Sprintf(",%s AS key%s", mappedColumnName(lod.HasPreKey, b, pq.preKeyTagID), b))
}
cnt, err := loadPointsSelectWhat(&sb, &pq.pointsQueryArgs, lod.Version)
cnt, err := loadPointsSelectWhat(&sb, pq, lod.Version)
if err != nil {
return "", pointsQueryMeta{}, err
}
Expand Down Expand Up @@ -473,7 +467,7 @@ func (pq *pointsQuery) loadPointsQueryV2(lod data_model.LOD, utcOffset int64) (s
having = true
}
limit := maxSeriesRows
if pq.orderBy {
if pq.sort != sortNone {
limit = maxTableRows
if having {
sb.WriteString(" AND _count>0")
Expand All @@ -484,7 +478,7 @@ func (pq *pointsQuery) loadPointsQueryV2(lod data_model.LOD, utcOffset int64) (s
for _, b := range pq.by {
sb.WriteString(fmt.Sprintf(",%s AS key%s", mappedColumnName(lod.HasPreKey, b, pq.preKeyTagID), b))
}
if pq.desc {
if pq.sort == sortDescending {
sb.WriteString(" DESC")
}
}
Expand Down Expand Up @@ -518,7 +512,7 @@ func (pq *pointsQuery) loadPointsQueryV3(lod data_model.LOD, utcOffset int64) (s
}
sb.WriteString(fmt.Sprintf(",%s AS stag%s", unmappedColumnNameV3(b), b))
}
cnt, err := loadPointsSelectWhat(&sb, &pq.pointsQueryArgs, lod.Version)
cnt, err := loadPointsSelectWhat(&sb, pq, lod.Version)
if err != nil {
return "", pointsQueryMeta{}, err
}
Expand All @@ -542,7 +536,7 @@ func (pq *pointsQuery) loadPointsQueryV3(lod data_model.LOD, utcOffset int64) (s
sb.WriteString(" AS stag")
sb.WriteString(b)
}
if pq.orderBy {
if pq.sort != sortNone {
sb.WriteString(" HAVING _count>0")
switch pq.kind {
case data_model.DigestKindPercentiles:
Expand All @@ -557,7 +551,7 @@ func (pq *pointsQuery) loadPointsQueryV3(lod data_model.LOD, utcOffset int64) (s
}
sb.WriteString(fmt.Sprintf(",%s AS stag%s", unmappedColumnNameV3(b), b))
}
if pq.desc {
if pq.sort == sortDescending {
sb.WriteString(" DESC")
}
sb.WriteString(" LIMIT ")
Expand Down Expand Up @@ -677,7 +671,7 @@ func (s *stringFixed) String() string {
}
}

func (pq *pointsQueryArgs) preKeyTableName(lod data_model.LOD) string {
func (pq *pointsQuery) preKeyTableName(lod data_model.LOD) string {
var usePreKey bool
if lod.HasPreKey {
usePreKey = lod.PreKeyOnly ||
Expand Down
16 changes: 8 additions & 8 deletions internal/api/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func TestTagValuesQueryV3(t *testing.T) {

func TestLoadPointsQueryV2(t *testing.T) {
// prepare
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
metricID: metricID,
user: "test-user",
isStringTop: false,
kind: data_model.DigestCountSec.Kind(false),
})
}
pq.filterIn.AppendMapped(1, 1, 2)
pq.filterNotIn.AppendMapped(0, 3)
lod := getLod(t, Version2)
Expand All @@ -125,12 +125,12 @@ func TestLoadPointsQueryV2(t *testing.T) {

func TestLoadPointsQueryV2_maxHost(t *testing.T) {
// prepare
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
metricID: metricID,
user: "test-user",
isStringTop: false,
kind: data_model.DigestCountSec.Kind(true),
})
}
pq.filterIn.AppendMapped(1, 1, 2)
pq.filterNotIn.AppendMapped(0, 3)
lod := getLod(t, Version2)
Expand All @@ -149,12 +149,12 @@ func TestLoadPointsQueryV2_maxHost(t *testing.T) {

func TestLoadPointsQueryV3(t *testing.T) {
// prepare
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
metricID: metricID,
user: "test-user",
isStringTop: false,
kind: data_model.DigestCountSec.Kind(false),
})
}
pq.filterIn.Append(1, data_model.NewTagValue("one", 1), data_model.NewTagValue("two", 2))
pq.filterNotIn.AppendValue(0, "staging")
lod := getLod(t, Version3)
Expand All @@ -173,12 +173,12 @@ func TestLoadPointsQueryV3(t *testing.T) {

func TestLoadPointsQueryV3_maxHost(t *testing.T) {
// prepare
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
metricID: metricID,
user: "test-user",
isStringTop: false,
kind: data_model.DigestCountSec.Kind(true),
})
}
pq.filterIn.Append(1, data_model.NewTagValue("one", 1), data_model.NewTagValue("two", 2))
pq.filterNotIn.AppendValue(0, "staging")
lod := getLod(t, Version3)
Expand Down
15 changes: 11 additions & 4 deletions internal/api/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (h *requestHandler) getTableFromLODs(ctx context.Context, lods []data_model
if toTime < lod.FromSec || lod.ToSec < fromTime {
continue
}
pq := newPointsQuery(pointsQueryArgs{
pq := pointsQuery{
version: lod.Version,
user: tableReqParams.user,
metricID: metricMeta.MetricID,
Expand All @@ -70,9 +70,8 @@ func (h *requestHandler) getTableFromLODs(ctx context.Context, lods []data_model
by: req.by,
filterIn: tableReqParams.mappedFilterIn,
filterNotIn: tableReqParams.mappedFilterNotIn,
orderBy: true,
desc: req.fromEnd,
})
sort: req.tableSort(),
}
m, err := loadPoints(ctx, h, &pq, data_model.LOD{
FromSec: shiftTimestamp(lod.FromSec, lod.StepSec, 0, lod.Location),
ToSec: shiftTimestamp(lod.ToSec, lod.StepSec, 0, lod.Location),
Expand Down Expand Up @@ -194,3 +193,11 @@ func inRange(row tsSelectRow, from, to RowMarker, fromEnd bool) bool {
}
return true
}

func (r *seriesRequest) tableSort() pointsQuerySort {
if r.fromEnd {
return sortDescending
} else {
return sortAscending
}
}
2 changes: 1 addition & 1 deletion internal/api/tscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (c *tsCache) get(ctx context.Context, h *requestHandler, pq *pointsQuery, l
cachedRows := 0
realLoadFrom := lod.FromSec
realLoadTo := lod.ToSec
key := pq.key
key := pq.cacheKey()
if !avoidCache {
realLoadFrom, realLoadTo = c.loadCached(h, key, lod.FromSec, lod.ToSec, ret, 0, lod.Location, &cachedRows)
if realLoadFrom == 0 && realLoadTo == 0 {
Expand Down

0 comments on commit 4c96771

Please sign in to comment.