-
Notifications
You must be signed in to change notification settings - Fork 840
Add bucket index support to querier #3614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pracucci
merged 18 commits into
cortexproject:master
from
pracucci:add-bucket-index-support-to-querier
Dec 21, 2020
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c922c4a
Introduced BucketIndexBlocksFinder
pracucci a8bf92d
Improved and tested bucket index Loader
pracucci e1e9039
Integrated bucket index in querier and added caching support
pracucci e93aa5a
Fixed unit tests
pracucci 7aedad1
Fixed typo in comment
pracucci 5139f2d
Fail queries if bucket index is too old
pracucci 601f05c
Fixed linter
pracucci 1367aec
Addressed review comments
pracucci 1f8c3f6
Addressed more comments
pracucci b3a4080
Updated doc
pracucci c466b56
Added CHANGELOG entry
pracucci 31b83bc
Added integration test
pracucci 9f6748d
Fixed integration test
pracucci 65d9bff
Replaced 1st with first
pracucci 716710c
Loader refactoring
pracucci 9ed6fd7
Clarified max size cached item
pracucci 7e9b43b
Updated bucket index doc
pracucci a89c483
Updated doc
pracucci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package querier | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/kit/log" | ||
| "github.com/oklog/ulid" | ||
| "github.com/pkg/errors" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/thanos-io/thanos/pkg/objstore" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/storage/tsdb/bucketindex" | ||
| "github.com/cortexproject/cortex/pkg/util/services" | ||
| ) | ||
|
|
||
| var ( | ||
| errBucketIndexBlocksFinderNotRunning = errors.New("bucket index blocks finder is not running") | ||
| errBucketIndexTooOld = errors.New("bucket index is too old and the last time it was updated exceeds the allowed max staleness") | ||
| ) | ||
|
|
||
| type BucketIndexBlocksFinderConfig struct { | ||
| IndexLoader bucketindex.LoaderConfig | ||
| MaxStalePeriod time.Duration | ||
| IgnoreDeletionMarksDelay time.Duration | ||
| } | ||
|
|
||
| // BucketIndexBlocksFinder implements BlocksFinder interface and find blocks in the bucket | ||
| // looking up the bucket index. | ||
| type BucketIndexBlocksFinder struct { | ||
| services.Service | ||
|
|
||
| cfg BucketIndexBlocksFinderConfig | ||
| loader *bucketindex.Loader | ||
|
|
||
| // Subservices manager. | ||
| subservices *services.Manager | ||
| subservicesWatcher *services.FailureWatcher | ||
| } | ||
|
|
||
| func NewBucketIndexBlocksFinder(cfg BucketIndexBlocksFinderConfig, bkt objstore.Bucket, logger log.Logger, reg prometheus.Registerer) (*BucketIndexBlocksFinder, error) { | ||
| f := &BucketIndexBlocksFinder{ | ||
| cfg: cfg, | ||
| loader: bucketindex.NewLoader(cfg.IndexLoader, bkt, logger, reg), | ||
| } | ||
|
|
||
| var err error | ||
| f.subservices, err = services.NewManager(f.loader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| f.Service = services.NewBasicService(f.starting, f.running, f.stopping) | ||
pracucci marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return f, nil | ||
| } | ||
|
|
||
| func (f *BucketIndexBlocksFinder) starting(ctx context.Context) error { | ||
| f.subservicesWatcher.WatchManager(f.subservices) | ||
|
|
||
| if err := services.StartManagerAndAwaitHealthy(ctx, f.subservices); err != nil { | ||
| return errors.Wrap(err, "unable to start blocks index querier subservices") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (f *BucketIndexBlocksFinder) running(ctx context.Context) error { | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| case err := <-f.subservicesWatcher.Chan(): | ||
| return errors.Wrap(err, "blocks undex querier set subservice failed") | ||
pracucci marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| func (f *BucketIndexBlocksFinder) stopping(_ error) error { | ||
| return services.StopManagerAndAwaitStopped(context.Background(), f.subservices) | ||
| } | ||
|
|
||
| // GetBlocks implements BlocksFinder. | ||
| func (f *BucketIndexBlocksFinder) GetBlocks(ctx context.Context, userID string, minT, maxT int64) (bucketindex.Blocks, map[ulid.ULID]*bucketindex.BlockDeletionMark, error) { | ||
| if f.State() != services.Running { | ||
| return nil, nil, errBucketIndexBlocksFinderNotRunning | ||
| } | ||
| if maxT < minT { | ||
| return nil, nil, errInvalidBlocksRange | ||
| } | ||
|
|
||
| // Get the bucket index for this user. | ||
| idx, err := f.loader.GetIndex(ctx, userID) | ||
| if errors.Is(err, bucketindex.ErrIndexNotFound) { | ||
| // This is a legit edge case, happening when a new tenant has not shipped blocks to the storage yet | ||
| // so the bucket index hasn't been created yet. | ||
| return nil, nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| // Ensure the bucket index is not too old. | ||
| if time.Since(idx.GetUpdatedAt()) > f.cfg.MaxStalePeriod { | ||
| return nil, nil, errBucketIndexTooOld | ||
| } | ||
|
|
||
| var ( | ||
| matchingBlocks = map[ulid.ULID]*bucketindex.Block{} | ||
| matchingDeletionMarks = map[ulid.ULID]*bucketindex.BlockDeletionMark{} | ||
| ) | ||
|
|
||
| // Filter blocks containing samples within the range. | ||
| for _, block := range idx.Blocks { | ||
| if !block.Within(minT, maxT) { | ||
| continue | ||
| } | ||
|
|
||
| matchingBlocks[block.ID] = block | ||
| } | ||
|
|
||
| for _, mark := range idx.BlockDeletionMarks { | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Filter deletion marks by matching blocks only. | ||
| if _, ok := matchingBlocks[mark.ID]; !ok { | ||
| continue | ||
| } | ||
|
|
||
| // Exclude blocks marked for deletion. This is the same logic as Thanos IgnoreDeletionMarkFilter. | ||
| if time.Since(time.Unix(mark.DeletionTime, 0)).Seconds() > f.cfg.IgnoreDeletionMarksDelay.Seconds() { | ||
| delete(matchingBlocks, mark.ID) | ||
| continue | ||
| } | ||
|
|
||
| matchingDeletionMarks[mark.ID] = mark | ||
| } | ||
|
|
||
| // Convert matching blocks into a list. | ||
| blocks := make(bucketindex.Blocks, 0, len(matchingBlocks)) | ||
| for _, b := range matchingBlocks { | ||
| blocks = append(blocks, b) | ||
| } | ||
|
|
||
| return blocks, matchingDeletionMarks, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.