Skip to content

Commit 683321f

Browse files
authored
fix: GetTenantStats reports wrong stats (#4394)
1 parent 9b7d9db commit 683321f

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

pkg/metastore/index/index.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,16 @@ func (i *Index) GetTenantStats(tx *bbolt.Tx, tenant string) *metastorev1.TenantS
204204
// Partition not found.
205205
continue
206206
}
207-
if q.Shards(tenant) == nil {
208-
// No shards for this tenant in this partition.
209-
continue
210-
}
211-
oldest := p.StartTime().UnixMilli()
212-
newest := p.EndTime().UnixMilli()
213-
stats.DataIngested = true
214-
if oldest < stats.OldestProfileTime {
215-
stats.OldestProfileTime = oldest
216-
}
217-
if newest > stats.NewestProfileTime {
218-
stats.NewestProfileTime = newest
207+
for shard := range q.Shards(tenant) {
208+
stats.DataIngested = true
209+
oldest := shard.ShardIndex.MinTime
210+
newest := shard.ShardIndex.MaxTime
211+
if oldest < stats.OldestProfileTime {
212+
stats.OldestProfileTime = oldest
213+
}
214+
if newest > stats.NewestProfileTime {
215+
stats.NewestProfileTime = newest
216+
}
219217
}
220218
}
221219
if !stats.DataIngested {

pkg/metastore/index/index_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,49 @@ func TestIndex_DeleteShard(t *testing.T) {
365365
assertShard(t, db, p, tenant2, 1, true)
366366
})
367367
}
368+
369+
func TestIndex_GetTenantStats(t *testing.T) {
370+
const (
371+
existingTenant = "tenant"
372+
)
373+
var (
374+
minTime = test.UnixMilli("2024-09-11T07:00:00.000Z")
375+
maxTime = test.UnixMilli("2024-09-11T09:00:00.000Z")
376+
)
377+
378+
db := test.BoltDB(t)
379+
idx := NewIndex(util.Logger, NewStore(), DefaultConfig)
380+
require.NoError(t, db.Update(idx.Init))
381+
382+
shardID := uint32(42)
383+
blockMeta := &metastorev1.BlockMeta{
384+
Id: test.ULID("2024-09-11T07:00:00.001Z"),
385+
Tenant: 1,
386+
Shard: shardID,
387+
MinTime: minTime,
388+
MaxTime: maxTime,
389+
CreatedBy: 1,
390+
StringTable: []string{"", existingTenant, "ingester"},
391+
}
392+
393+
require.NoError(t, db.Update(func(tx *bbolt.Tx) error {
394+
return idx.InsertBlock(tx, blockMeta.CloneVT())
395+
}))
396+
397+
require.NoError(t, db.View(func(tx *bbolt.Tx) error {
398+
stats := idx.GetTenantStats(tx, existingTenant)
399+
assert.Equal(t, true, stats.GetDataIngested())
400+
assert.Equal(t, minTime, stats.GetOldestProfileTime())
401+
assert.Equal(t, maxTime, stats.GetNewestProfileTime())
402+
return nil
403+
}))
404+
405+
require.NoError(t, db.View(func(tx *bbolt.Tx) error {
406+
stats := idx.GetTenantStats(tx, "tenant-never-sent")
407+
assert.Equal(t, false, stats.GetDataIngested())
408+
assert.Equal(t, int64(0), stats.GetOldestProfileTime())
409+
assert.Equal(t, int64(0), stats.GetNewestProfileTime())
410+
return nil
411+
}))
412+
413+
}

0 commit comments

Comments
 (0)