Skip to content

Commit

Permalink
add test for shuffled user order
Browse files Browse the repository at this point in the history
  • Loading branch information
dmwilson-grafana committed Jan 24, 2025
1 parent c511896 commit b39115f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 0 additions & 5 deletions pkg/compactor/blocks_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package compactor
import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -194,10 +193,6 @@ func (c *BlocksCleaner) runCleanup(ctx context.Context, async bool) {
return
}

rand.Shuffle(len(allUsers), func(i, j int) {
allUsers[i], allUsers[j] = allUsers[j], allUsers[i]
})

doCleanup := func() {
err := c.cleanUsers(ctx, allUsers, isDeleted, logger)
c.instrumentFinishedCleanupRun(err, logger)
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/tsdb/users_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tsdb

import (
"context"
"math/rand"
"strings"

"github.com/go-kit/log"
Expand Down Expand Up @@ -46,6 +47,10 @@ func (s *UsersScanner) ScanUsers(ctx context.Context) (users, markedForDeletion
return nil, nil, err
}

rand.Shuffle(len(users), func(i, j int) {
users[i], users[j] = users[j], users[i]
})

// Check users for being owned by instance, and split users into non-deleted and deleted.
// We do these checks after listing all users, to improve cacheability of Iter (result is only cached at the end of Iter call).
for ix := 0; ix < len(users); {
Expand Down
39 changes: 35 additions & 4 deletions pkg/storage/tsdb/users_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tsdb
import (
"context"
"errors"
"fmt"
"path"
"testing"

Expand Down Expand Up @@ -36,10 +37,10 @@ func TestUsersScanner_ScanUsers_ShouldReturnedOwnedUsersOnly(t *testing.T) {
}

func TestUsersScanner_ScanUsers_ShouldReturnUsersForWhichOwnerCheckOrTenantDeletionCheckFailed(t *testing.T) {
expected := []string{"user-1", "user-2"}
users := []string{"user-1", "user-2"}

bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", expected, nil)
bucketClient.MockIter("", users, nil)
bucketClient.MockExists(path.Join("user-1", TenantDeletionMarkPath), false, nil)
bucketClient.MockExists(path.Join("user-2", TenantDeletionMarkPath), false, errors.New("fail"))

Expand All @@ -50,11 +51,14 @@ func TestUsersScanner_ScanUsers_ShouldReturnUsersForWhichOwnerCheckOrTenantDelet
s := NewUsersScanner(bucketClient, isOwned, log.NewNopLogger())
actual, deleted, err := s.ScanUsers(context.Background())
require.NoError(t, err)
assert.Equal(t, expected, actual)
for _, userID := range users {
assert.Contains(t, actual, userID)
}
assert.Empty(t, deleted)
}

func TestUsersScanner_ScanUsers_ShouldNotReturnPrefixedUsedByMimirInternals(t *testing.T) {
users := []string{"user-1", "user-2"}
bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", []string{"user-1", "user-2", bucket.MimirInternalsPrefix}, nil)
bucketClient.MockExists(path.Join("user-1", TenantDeletionMarkPath), false, nil)
Expand All @@ -63,5 +67,32 @@ func TestUsersScanner_ScanUsers_ShouldNotReturnPrefixedUsedByMimirInternals(t *t
s := NewUsersScanner(bucketClient, AllUsers, log.NewNopLogger())
actual, _, err := s.ScanUsers(context.Background())
require.NoError(t, err)
assert.Equal(t, []string{"user-1", "user-2"}, actual)
for _, userID := range users {
assert.Contains(t, actual, userID)
}
}

func TestUsersScanner_ScanUsers_ShouldReturnRandomizedOrder(t *testing.T) {
var users = make([]string, 20)
for i := 0; i < len(users); i++ {
users[i] = fmt.Sprintf("user-%d", i)
}
bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", users, nil)
for i := 0; i < len(users); i++ {
bucketClient.MockExists(path.Join(users[i], TenantDeletionMarkPath), false, nil)
}

isOwned := func(_ string) (bool, error) {
return true, nil
}

s := NewUsersScanner(bucketClient, isOwned, log.NewNopLogger())
actual, deleted, err := s.ScanUsers(context.Background())
require.NoError(t, err)
for _, userID := range users {
assert.Contains(t, actual, userID)
}
assert.Empty(t, deleted)
assert.NotEqual(t, actual, users)
}

0 comments on commit b39115f

Please sign in to comment.