From 0896bb8a95b2b63c48760132717e8c63937ee0c9 Mon Sep 17 00:00:00 2001 From: Mikhail Alpinskiy Date: Wed, 27 Nov 2024 12:12:46 +0300 Subject: [PATCH] Initialize request version --- internal/api/handler.go | 48 +++++++++------------------------ internal/api/http_router.go | 14 +++++----- internal/api/promql.go | 10 +++---- internal/api/request_parsers.go | 2 +- internal/api/rpc_handler.go | 5 +++- internal/api/table.go | 2 +- internal/api/tscache.go | 1 + 7 files changed, 31 insertions(+), 51 deletions(-) diff --git a/internal/api/handler.go b/internal/api/handler.go index e87067e31..473e578a9 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -853,25 +853,6 @@ func (h *requestHandler) doSelect(ctx context.Context, meta util.QueryMetaInto, return err } -func (r *requestHandler) effectiveVersion(version string) string { - switch version { - case "", Version2: - return r.version() - default: - return version - } -} - -func (r *requestHandler) version() string { - if r.forceVersion != "" { - return r.forceVersion - } - if r.versionDice != nil { - return r.versionDice() - } - return Version3 -} - func (h *Handler) getMetricNameWithNamespace(metricID int32) (string, error) { if metricID == format.TagValueIDUnspecified { return format.CodeTagValue(format.TagValueIDUnspecified), nil @@ -1799,7 +1780,7 @@ func (h *requestHandler) handleGetMetricTagValues(ctx context.Context, req getMe return nil, false, err } - version := h.version() + version := h.version err = validateQuery(metricMeta, version) if err != nil { return nil, false, err @@ -2114,9 +2095,8 @@ func (h *requestHandler) queryBadges(ctx context.Context, req seriesRequest, met user: h.endpointStat.user, priority: h.endpointStat.priority, }, - debug: h.debug, - forceVersion: h.forceVersion, - versionDice: h.versionDice, + debug: h.debug, + version: h.version, query: promql.Query{ Start: req.from.Unix(), End: req.to.Unix(), @@ -3384,18 +3364,16 @@ func pprofAccessAllowed(h *httpRequestHandler) bool { func (h *requestHandler) init(accessToken, version string) (err error) { switch version { - case Version1, Version3: - h.requestVersion = version - h.forceVersion = version - case Version2, "": - h.requestVersion = Version2 - h.versionDice = sync.OnceValue(func() string { - if rand.Float64() < h.Handler.Version3Prob.Load() { - return Version3 - } else { - return Version2 - } - }) + case Version1: + h.version = Version1 + case Version2: + if rand.Float64() < h.Handler.Version3Prob.Load() { + h.version = Version3 + } else { + h.version = Version2 + } + case Version3, "": + h.version = Version3 default: return fmt.Errorf("invalid version: %q", version) } diff --git a/internal/api/http_router.go b/internal/api/http_router.go index e25471977..753ca4d4d 100644 --- a/internal/api/http_router.go +++ b/internal/api/http_router.go @@ -27,14 +27,12 @@ type httpRoute struct { type requestHandler struct { *Handler - accessInfo accessInfo - endpointStat endpointStat - trace []string - debug bool - requestVersion string - forceVersion string - versionDice func() string - query promql.Query + accessInfo accessInfo + endpointStat endpointStat + trace []string + debug bool + version string + query promql.Query } type httpRequestHandler struct { diff --git a/internal/api/promql.go b/internal/api/promql.go index c6bb71c92..04164c53f 100644 --- a/internal/api/promql.go +++ b/internal/api/promql.go @@ -37,7 +37,7 @@ func HandleInstantQuery(r *httpRequestHandler) { q := promql.Query{ Expr: r.FormValue("query"), Options: promql.Options{ - Version: r.version(), + Version: r.version, Mode: data_model.InstantQuery, Compat: true, TimeNow: time.Now().Unix(), @@ -73,7 +73,7 @@ func HandleRangeQuery(r *httpRequestHandler) { q := promql.Query{ Expr: r.FormValue("query"), Options: promql.Options{ - Version: r.version(), + Version: r.version, Compat: true, Namespace: r.Header.Get("X-StatsHouse-Namespace"), }, @@ -140,7 +140,7 @@ func HandlePromSeriesQuery(r *httpRequestHandler) { End: end, Expr: expr, Options: promql.Options{ - Version: r.version(), + Version: r.version, Version3Start: r.Version3Start.Load(), Limit: 1000, Mode: data_model.TagsQuery, @@ -215,7 +215,7 @@ func HandlePromLabelValuesQuery(r *httpRequestHandler) { End: end, Expr: expr, Options: promql.Options{ - Version: r.version(), + Version: r.version, Version3Start: r.Version3Start.Load(), Limit: 1000, Mode: data_model.TagsQuery, @@ -459,7 +459,7 @@ func (h *requestHandler) QuerySeries(ctx context.Context, qry *promql.SeriesQuer var tx int // time index for _, lod := range lods { pq := pointsQuery{ - version: h.requestVersion, + version: h.version, user: h.accessInfo.user, metricID: qry.Metric.MetricID, preKeyTagX: format.TagIndex(qry.Metric.PreKeyTagID), diff --git a/internal/api/request_parsers.go b/internal/api/request_parsers.go index a6fa60848..edb8bb5ef 100644 --- a/internal/api/request_parsers.go +++ b/internal/api/request_parsers.go @@ -486,7 +486,7 @@ func (r *httpRequestHandler) parseSeriesRequestS(maxTabs int) (res []seriesReque // parse dependent paramemeters var ( finalize = func(t *seriesRequestEx) error { - t.version = r.effectiveVersion(t.version) + t.version = r.version numResultsMax := maxSeries if len(t.shifts) != 0 { numResultsMax /= len(t.shifts) diff --git a/internal/api/rpc_handler.go b/internal/api/rpc_handler.go index 9fba4f6dd..82d535104 100644 --- a/internal/api/rpc_handler.go +++ b/internal/api/rpc_handler.go @@ -123,6 +123,9 @@ func (h *rpcRequestHandler) rawGetQueryPoint(ctx context.Context, hctx *rpc.Hand err = fmt.Errorf("failed to deserialize statshouseApi.GetQueryPoint request: %w", err) return err } + if err = h.init(args.AccessToken, args.Query.Version); err != nil { + return err + } qry := seriesRequestRPC{ filter: args.Query.Filter, function: args.Query.Function, @@ -349,7 +352,7 @@ func (h *rpcRequestHandler) rawReleaseChunks(ctx context.Context, hctx *rpc.Hand func (qry *seriesRequestRPC) toSeriesRequest(h *rpcRequestHandler) (seriesRequest, error) { req := seriesRequest{ - version: h.version(), + version: h.version, numResults: int(qry.topN), metricName: qry.metricName, from: time.Unix(qry.timeFrom, 0), diff --git a/internal/api/table.go b/internal/api/table.go index 032a046d9..39f069b15 100644 --- a/internal/api/table.go +++ b/internal/api/table.go @@ -60,7 +60,7 @@ func (h *requestHandler) getTableFromLODs(ctx context.Context, lods []data_model continue } pq := pointsQuery{ - version: h.requestVersion, + version: h.version, user: tableReqParams.user, metricID: metricMeta.MetricID, preKeyTagX: format.TagIndex(metricMeta.PreKeyTagID), diff --git a/internal/api/tscache.go b/internal/api/tscache.go index 1c43391b3..07ea488f9 100644 --- a/internal/api/tscache.go +++ b/internal/api/tscache.go @@ -164,6 +164,7 @@ func (g *tsCacheGroup) changeMaxSize(newSize int) { func (g *tsCacheGroup) Invalidate(lodLevel int64, times []int64) { g.pointCaches[Version2][lodLevel].invalidate(times) + g.pointCaches[Version3][lodLevel].invalidate(times) } func (g *tsCacheGroup) Get(ctx context.Context, h *requestHandler, pq *pointsQuery, lod data_model.LOD, avoidCache bool) ([][]tsSelectRow, error) {