Skip to content

Commit 5ec6f50

Browse files
authored
Add Retries to Sync Blocks (#3975)
* Added retries to sync blocks Signed-off-by: Levi Harrison <[email protected]> * Add MaxBackoff and MinBackoff Signed-off-by: Levi Harrison <[email protected]> * Changed structure of functions and error behavior Signed-off-by: Levi Harrison <[email protected]> * Changed how errors are collected and returned Signed-off-by: Levi Harrison <[email protected]> * Change error returning Signed-off-by: Levi Harrison <[email protected]>
1 parent 43a9f83 commit 5ec6f50

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

pkg/storegateway/bucket_stores.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/cortexproject/cortex/pkg/storage/bucket"
3434
"github.com/cortexproject/cortex/pkg/storage/tsdb"
35+
"github.com/cortexproject/cortex/pkg/util"
3536
util_log "github.com/cortexproject/cortex/pkg/util/log"
3637
"github.com/cortexproject/cortex/pkg/util/spanlogger"
3738
"github.com/cortexproject/cortex/pkg/util/validation"
@@ -151,11 +152,35 @@ func (u *BucketStores) InitialSync(ctx context.Context) error {
151152

152153
// SyncBlocks synchronizes the stores state with the Bucket store for every user.
153154
func (u *BucketStores) SyncBlocks(ctx context.Context) error {
154-
return u.syncUsersBlocks(ctx, func(ctx context.Context, s *store.BucketStore) error {
155+
return u.syncUsersBlocksWithRetries(ctx, func(ctx context.Context, s *store.BucketStore) error {
155156
return s.SyncBlocks(ctx)
156157
})
157158
}
158159

160+
func (u *BucketStores) syncUsersBlocksWithRetries(ctx context.Context, f func(context.Context, *store.BucketStore) error) error {
161+
retries := util.NewBackoff(ctx, util.BackoffConfig{
162+
MinBackoff: 100 * time.Millisecond,
163+
MaxBackoff: 10 * time.Second,
164+
MaxRetries: 3,
165+
})
166+
167+
var lastErr error
168+
for retries.Ongoing() {
169+
lastErr = u.syncUsersBlocks(ctx, f)
170+
if lastErr == nil {
171+
return nil
172+
}
173+
174+
retries.Wait()
175+
}
176+
177+
if lastErr == nil {
178+
return retries.Err()
179+
}
180+
181+
return lastErr
182+
}
183+
159184
func (u *BucketStores) syncUsersBlocks(ctx context.Context, f func(context.Context, *store.BucketStore) error) (returnErr error) {
160185
defer func(start time.Time) {
161186
u.syncTimes.Observe(time.Since(start).Seconds())

0 commit comments

Comments
 (0)