Skip to content

Commit

Permalink
move shuffle to refreshOwnedUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmwilson-grafana committed Jan 24, 2025
1 parent 3de014c commit f9cab59
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* `cortex_ingester_tsdb_block_postings_for_matchers_cache_misses_total`
* `cortex_ingester_tsdb_block_postings_for_matchers_cache_requests_total`
* `cortex_ingester_tsdb_block_postings_for_matchers_cache_skips_total`
* [ENHANCEMENT] Compactor: Shuffle order of users in `UsersScanner` before submitting to `BlocksCleaner`. #10513
* [ENHANCEMENT] Compactor: Shuffle users' order in `BlocksCleaner`. Prevents bucket indexes from going an extended period without cleanup during compactor restarts. #10513
* [BUGFIX] Distributor: Use a boolean to track changes while merging the ReplicaDesc components, rather than comparing the objects directly. #10185
* [BUGFIX] Querier: fix timeout responding to query-frontend when response size is very close to `-querier.frontend-client.grpc-max-send-msg-size`. #10154
* [BUGFIX] Query-frontend and querier: show warning/info annotations in some cases where they were missing (if a lazy querier was used). #10277
Expand Down
5 changes: 5 additions & 0 deletions pkg/compactor/blocks_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package compactor
import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -232,6 +233,10 @@ func (c *BlocksCleaner) refreshOwnedUsers(ctx context.Context) ([]string, map[st
return nil, nil, errors.Wrap(err, "failed to discover users from bucket")
}

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

isActive := util.StringsMap(users)
isDeleted := util.StringsMap(deleted)
allUsers := append(users, deleted...)
Expand Down
5 changes: 0 additions & 5 deletions pkg/storage/tsdb/users_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package tsdb

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

"github.com/go-kit/log"
Expand Down Expand Up @@ -47,10 +46,6 @@ 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
33 changes: 4 additions & 29 deletions pkg/storage/tsdb/users_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package tsdb
import (
"context"
"errors"
"fmt"
"path"
"testing"

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

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

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

Expand All @@ -51,12 +50,11 @@ func TestUsersScanner_ScanUsers_ShouldReturnUsersForWhichOwnerCheckOrTenantDelet
s := NewUsersScanner(bucketClient, isOwned, log.NewNopLogger())
actual, deleted, err := s.ScanUsers(context.Background())
require.NoError(t, err)
assert.ElementsMatch(t, actual, users)
assert.Equal(t, expected, actual)
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 @@ -65,28 +63,5 @@ func TestUsersScanner_ScanUsers_ShouldNotReturnPrefixedUsedByMimirInternals(t *t
s := NewUsersScanner(bucketClient, AllUsers, log.NewNopLogger())
actual, _, err := s.ScanUsers(context.Background())
require.NoError(t, err)
assert.ElementsMatch(t, actual, users)
}

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)
assert.ElementsMatch(t, actual, users)
assert.Empty(t, deleted)
assert.NotEqual(t, actual, users)
assert.Equal(t, []string{"user-1", "user-2"}, actual)
}

0 comments on commit f9cab59

Please sign in to comment.