diff --git a/app/vlselect/main.go b/app/vlselect/main.go index dbb35ae53..0a4d842bc 100644 --- a/app/vlselect/main.go +++ b/app/vlselect/main.go @@ -252,5 +252,5 @@ var ( logsqlStreamIDsRequests = metrics.NewCounter(`vl_http_requests_total{path="/select/logsql/stream_ids"}`) logsqlStreamsRequests = metrics.NewCounter(`vl_http_requests_total{path="/select/logsql/streams"}`) logsqlTailRequests = metrics.NewCounter(`vl_http_requests_total{path="/select/logsql/tail"}`) - logsqlAdminTenantsRequests = metrics.NewCounter(`vl_http_requests_total{path="/admin/tenants"}`) + logsqlAdminTenantsRequests = metrics.NewCounter(`vl_http_requests_total{path="/select/admin/tenants"}`) ) diff --git a/app/vlstorage/main.go b/app/vlstorage/main.go index 6850eaab8..663f479f8 100644 --- a/app/vlstorage/main.go +++ b/app/vlstorage/main.go @@ -176,7 +176,7 @@ func GetStreamIDs(ctx context.Context, tenantIDs []logstorage.TenantID, q *logst return strg.GetStreamIDs(ctx, tenantIDs, q, limit) } -// GetTenantIDs returns tenantIDs in the storage. +// GetTenantIDs returns tenantIDs from the storage by the given start and end. func GetTenantIDs(ctx context.Context, start, end int64) ([]string, error) { return strg.GetTenantIDs(ctx, start, end) } diff --git a/lib/logstorage/indexdb_test.go b/lib/logstorage/indexdb_test.go index 90b072a8c..dad58e8d3 100644 --- a/lib/logstorage/indexdb_test.go +++ b/lib/logstorage/indexdb_test.go @@ -3,6 +3,7 @@ package logstorage import ( "fmt" "reflect" + "slices" "testing" "github.com/VictoriaMetrics/VictoriaMetrics/lib/fs" @@ -254,5 +255,76 @@ func TestStorageSearchStreamIDs(t *testing.T) { } func TestGetTenantsIds(t *testing.T) { + t.Parallel() + path := t.Name() + const partitionName = "foobar" + s := newTestStorage() + mustCreateIndexdb(path) + idb := mustOpenIndexdb(path, partitionName, s) + + tenantIDs := []TenantID{ + {AccountID: 0, ProjectID: 0}, + {AccountID: 0, ProjectID: 1}, + {AccountID: 1, ProjectID: 0}, + {AccountID: 1, ProjectID: 1}, + {AccountID: 123, ProjectID: 567}, + } + getStreamIDForTags := func(tags map[string]string) ([]streamID, []byte) { + st := GetStreamTags() + for k, v := range tags { + st.Add(k, v) + } + streamTagsCanonical := st.MarshalCanonical(nil) + PutStreamTags(st) + id := hash128(streamTagsCanonical) + sids := make([]streamID, 0, len(tenantIDs)) + for _, tenantID := range tenantIDs { + sid := streamID{ + tenantID: tenantID, + id: id, + } + + sids = append(sids, sid) + } + + return sids, streamTagsCanonical + } + + // Create indexdb entries + const jobsCount = 7 + const instancesCount = 5 + for i := 0; i < jobsCount; i++ { + for j := 0; j < instancesCount; j++ { + sids, streamTagsCanonical := getStreamIDForTags(map[string]string{ + "job": fmt.Sprintf("job-%d", i), + "instance": fmt.Sprintf("instance-%d", j), + }) + for _, sid := range sids { + idb.mustRegisterStream(&sid, streamTagsCanonical) + } + + } + } + idb.debugFlush() + + f := func(expectedTenantIDs []string) { + t.Helper() + tenantIDs := idb.searchTenants() + slices.Sort(tenantIDs) + slices.Sort(expectedTenantIDs) + if !reflect.DeepEqual(tenantIDs, expectedTenantIDs) { + fs.MustRemoveAll(path) + t.Fatalf("unexpected tensntIds; got %v; want %v", tenantIDs, expectedTenantIDs) + } + } + + expectedTenantIDs := []string{"1:1", "123:567", "0:0", "0:1", "1:0"} + + f(expectedTenantIDs) + + mustCloseIndexdb(idb) + fs.MustRemoveAll(path) + + closeTestStorage(s) } diff --git a/lib/logstorage/storage_search.go b/lib/logstorage/storage_search.go index 1e91b0692..c5e6e57ea 100644 --- a/lib/logstorage/storage_search.go +++ b/lib/logstorage/storage_search.go @@ -394,7 +394,7 @@ func (s *Storage) GetStreamIDs(ctx context.Context, tenantIDs []TenantID, q *Que return s.GetFieldValues(ctx, tenantIDs, q, "_stream_id", limit) } -// GetTenantIDs returns tenantIDs for the given q. +// GetTenantIDs returns tenantIDs for the given start and end. func (s *Storage) GetTenantIDs(ctx context.Context, start, end int64) ([]string, error) { return s.getTenantIDs(ctx, start, end) } @@ -420,7 +420,7 @@ func (s *Storage) getTenantIDs(ctx context.Context, start, end int64) ([]string, for tid := range m { tenants = append(tenants, tid) } - sort.Strings(tenants) + slices.Sort(tenants) return tenants, nil }