From 5afa54e8459d00888232ac7e736d04a58df44023 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 5 Jul 2022 23:47:46 +0300 Subject: [PATCH] lib/vmselectapi: use string type for tagKey and tagValuePrefix args at TagValueSuffixes() This improves the API consistency --- app/vmstorage/servers/vmselect.go | 2 +- lib/storage/index_db.go | 16 ++++++++-------- lib/storage/storage.go | 7 ++++--- lib/vmselectapi/api.go | 2 +- lib/vmselectapi/server.go | 4 ++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/vmstorage/servers/vmselect.go b/app/vmstorage/servers/vmselect.go index 28601d2ec1..d300880932 100644 --- a/app/vmstorage/servers/vmselect.go +++ b/app/vmstorage/servers/vmselect.go @@ -67,7 +67,7 @@ func (api *vmstorageAPI) LabelValues(qt *querytracer.Tracer, accountID, projectI return api.s.SearchLabelValuesWithFiltersOnTimeRange(qt, accountID, projectID, labelName, tfss, tr, maxLabelValues, maxMetrics, deadline) } -func (api *vmstorageAPI) TagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr storage.TimeRange, tagKey, tagValuePrefix []byte, delimiter byte, +func (api *vmstorageAPI) TagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr storage.TimeRange, tagKey, tagValuePrefix string, delimiter byte, maxSuffixes int, deadline uint64) ([]string, error) { return api.s.SearchTagValueSuffixes(qt, accountID, projectID, tr, tagKey, tagValuePrefix, delimiter, maxSuffixes, deadline) } diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 32fd80880f..6311e8ea03 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -1061,7 +1061,7 @@ func (is *indexSearch) searchLabelValuesWithFiltersOnDate(qt *querytracer.Tracer // This allows implementing https://graphite-api.readthedocs.io/en/latest/api.html#metrics-find or similar APIs. // // If it returns maxTagValueSuffixes suffixes, then it is likely more than maxTagValueSuffixes suffixes is found. -func (db *indexDB) SearchTagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr TimeRange, tagKey, tagValuePrefix []byte, +func (db *indexDB) SearchTagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr TimeRange, tagKey, tagValuePrefix string, delimiter byte, maxTagValueSuffixes int, deadline uint64) ([]string, error) { qt = qt.NewChild("search tag value suffixes for accountID=%d, projectID=%d, timeRange=%s, tagKey=%q, tagValuePrefix=%q, delimiter=%c, maxTagValueSuffixes=%d", accountID, projectID, &tr, tagKey, tagValuePrefix, delimiter, maxTagValueSuffixes) @@ -1102,7 +1102,7 @@ func (db *indexDB) SearchTagValueSuffixes(qt *querytracer.Tracer, accountID, pro return suffixes, nil } -func (is *indexSearch) searchTagValueSuffixesForTimeRange(tvss map[string]struct{}, tr TimeRange, tagKey, tagValuePrefix []byte, delimiter byte, maxTagValueSuffixes int) error { +func (is *indexSearch) searchTagValueSuffixesForTimeRange(tvss map[string]struct{}, tr TimeRange, tagKey, tagValuePrefix string, delimiter byte, maxTagValueSuffixes int) error { minDate := uint64(tr.MinTimestamp) / msecPerDay maxDate := uint64(tr.MaxTimestamp-1) / msecPerDay if minDate > maxDate || maxDate-minDate > maxDaysForPerDaySearch { @@ -1143,24 +1143,24 @@ func (is *indexSearch) searchTagValueSuffixesForTimeRange(tvss map[string]struct return errGlobal } -func (is *indexSearch) searchTagValueSuffixesAll(tvss map[string]struct{}, tagKey, tagValuePrefix []byte, delimiter byte, maxTagValueSuffixes int) error { +func (is *indexSearch) searchTagValueSuffixesAll(tvss map[string]struct{}, tagKey, tagValuePrefix string, delimiter byte, maxTagValueSuffixes int) error { kb := &is.kb nsPrefix := byte(nsPrefixTagToMetricIDs) kb.B = is.marshalCommonPrefix(kb.B[:0], nsPrefix) - kb.B = marshalTagValue(kb.B, tagKey) - kb.B = marshalTagValue(kb.B, tagValuePrefix) + kb.B = marshalTagValue(kb.B, bytesutil.ToUnsafeBytes(tagKey)) + kb.B = marshalTagValue(kb.B, bytesutil.ToUnsafeBytes(tagValuePrefix)) kb.B = kb.B[:len(kb.B)-1] // remove tagSeparatorChar from the end of kb.B prefix := append([]byte(nil), kb.B...) return is.searchTagValueSuffixesForPrefix(tvss, nsPrefix, prefix, len(tagValuePrefix), delimiter, maxTagValueSuffixes) } -func (is *indexSearch) searchTagValueSuffixesForDate(tvss map[string]struct{}, date uint64, tagKey, tagValuePrefix []byte, delimiter byte, maxTagValueSuffixes int) error { +func (is *indexSearch) searchTagValueSuffixesForDate(tvss map[string]struct{}, date uint64, tagKey, tagValuePrefix string, delimiter byte, maxTagValueSuffixes int) error { nsPrefix := byte(nsPrefixDateTagToMetricIDs) kb := &is.kb kb.B = is.marshalCommonPrefix(kb.B[:0], nsPrefix) kb.B = encoding.MarshalUint64(kb.B, date) - kb.B = marshalTagValue(kb.B, tagKey) - kb.B = marshalTagValue(kb.B, tagValuePrefix) + kb.B = marshalTagValue(kb.B, bytesutil.ToUnsafeBytes(tagKey)) + kb.B = marshalTagValue(kb.B, bytesutil.ToUnsafeBytes(tagValuePrefix)) kb.B = kb.B[:len(kb.B)-1] // remove tagSeparatorChar from the end of kb.B prefix := append([]byte(nil), kb.B...) return is.searchTagValueSuffixesForPrefix(tvss, nsPrefix, prefix, len(tagValuePrefix), delimiter, maxTagValueSuffixes) diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 787d3158d6..8a1b492389 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -18,6 +18,7 @@ import ( "unsafe" "github.com/VictoriaMetrics/VictoriaMetrics/lib/bloomfilter" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup" "github.com/VictoriaMetrics/VictoriaMetrics/lib/decimal" "github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" @@ -1379,7 +1380,7 @@ func (s *Storage) SearchLabelValuesWithFiltersOnTimeRange(qt *querytracer.Tracer // This allows implementing https://graphite-api.readthedocs.io/en/latest/api.html#metrics-find or similar APIs. // // If more than maxTagValueSuffixes suffixes is found, then only the first maxTagValueSuffixes suffixes is returned. -func (s *Storage) SearchTagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr TimeRange, tagKey, tagValuePrefix []byte, +func (s *Storage) SearchTagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr TimeRange, tagKey, tagValuePrefix string, delimiter byte, maxTagValueSuffixes int, deadline uint64) ([]string, error) { return s.idb().SearchTagValueSuffixes(qt, accountID, projectID, tr, tagKey, tagValuePrefix, delimiter, maxTagValueSuffixes, deadline) } @@ -1437,7 +1438,7 @@ func (s *Storage) searchGraphitePaths(qt *querytracer.Tracer, accountID, project if n < 0 { // Verify that qHead matches a metric name. qHead = append(qHead, qTail...) - suffixes, err := s.SearchTagValueSuffixes(qt, accountID, projectID, tr, nil, qHead, '.', 1, deadline) + suffixes, err := s.SearchTagValueSuffixes(qt, accountID, projectID, tr, "", bytesutil.ToUnsafeString(qHead), '.', 1, deadline) if err != nil { return nil, err } @@ -1452,7 +1453,7 @@ func (s *Storage) searchGraphitePaths(qt *querytracer.Tracer, accountID, project return []string{string(qHead)}, nil } qHead = append(qHead, qTail[:n]...) - suffixes, err := s.SearchTagValueSuffixes(qt, accountID, projectID, tr, nil, qHead, '.', maxPaths, deadline) + suffixes, err := s.SearchTagValueSuffixes(qt, accountID, projectID, tr, "", bytesutil.ToUnsafeString(qHead), '.', maxPaths, deadline) if err != nil { return nil, err } diff --git a/lib/vmselectapi/api.go b/lib/vmselectapi/api.go index b4f2f3ca03..1d2b8d0818 100644 --- a/lib/vmselectapi/api.go +++ b/lib/vmselectapi/api.go @@ -19,7 +19,7 @@ type API interface { LabelValues(qt *querytracer.Tracer, accountID, projectID uint32, tfss []*storage.TagFilters, tr storage.TimeRange, labelName string, maxLabelValues, maxMetrics int, deadline uint64) ([]string, error) // TagValueSuffixes returns tag value suffixes for the given args. - TagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr storage.TimeRange, tagKey, tagValuePrefix []byte, delimiter byte, maxSuffixes int, deadline uint64) ([]string, error) + TagValueSuffixes(qt *querytracer.Tracer, accountID, projectID uint32, tr storage.TimeRange, tagKey, tagValuePrefix string, delimiter byte, maxSuffixes int, deadline uint64) ([]string, error) // LabelNames returns lable names for series matching the given tfss. LabelNames(qt *querytracer.Tracer, accountID, projectID uint32, tfss []*storage.TagFilters, tr storage.TimeRange, maxLableNames, maxMetrics int, deadline uint64) ([]string, error) diff --git a/lib/vmselectapi/server.go b/lib/vmselectapi/server.go index 97b362b0c6..58aa58878d 100644 --- a/lib/vmselectapi/server.go +++ b/lib/vmselectapi/server.go @@ -692,11 +692,11 @@ func (s *Server) processTagValueSuffixes(ctx *vmselectRequestCtx) error { if err := ctx.readDataBufBytes(maxLabelValueSize); err != nil { return fmt.Errorf("cannot read tagKey: %w", err) } - tagKey := append([]byte{}, ctx.dataBuf...) + tagKey := string(ctx.dataBuf) if err := ctx.readDataBufBytes(maxLabelValueSize); err != nil { return fmt.Errorf("cannot read tagValuePrefix: %w", err) } - tagValuePrefix := append([]byte{}, ctx.dataBuf...) + tagValuePrefix := string(ctx.dataBuf) delimiter, err := ctx.readByte() if err != nil { return fmt.Errorf("cannot read delimiter: %w", err)