-
Notifications
You must be signed in to change notification settings - Fork 2.2k
thanos/receive: add feature flag to put ext labels into TSDB #8546
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
Open
GiedriusS
wants to merge
1
commit into
main
Choose a base branch
from
ext_as_normal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ type MultiTSDB struct { | |
| exemplarClients map[string]*exemplars.TSDB | ||
|
|
||
| metricNameFilterEnabled bool | ||
| extLabelsInTSDB bool | ||
|
|
||
| headExpandedPostingsCacheSize uint64 | ||
| blockExpandedPostingsCacheSize uint64 | ||
|
|
@@ -80,6 +81,14 @@ type MultiTSDB struct { | |
| // MultiTSDBOption is a functional option for MultiTSDB. | ||
| type MultiTSDBOption func(mt *MultiTSDB) | ||
|
|
||
| // WithExternalLabelsInTSDB enables putting external labels in the TSDB. | ||
| // This permits streaming from the TSDB to the querier. | ||
|
Comment on lines
+84
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| func WithExternalLabelsInTSDB() MultiTSDBOption { | ||
| return func(s *MultiTSDB) { | ||
| s.extLabelsInTSDB = true | ||
| } | ||
| } | ||
|
|
||
| // WithMetricNameFilterEnabled enables metric name filtering on TSDB clients. | ||
| func WithMetricNameFilterEnabled() MultiTSDBOption { | ||
| return func(s *MultiTSDB) { | ||
|
|
@@ -302,10 +311,13 @@ func (t *tenant) blocksToDelete(blocks []*tsdb.Block) map[ulid.ULID]struct{} { | |
| return deletable | ||
| } | ||
|
|
||
| func newTenant() *tenant { | ||
| func newTenant(extLabels labels.Labels, addExtLabels bool) *tenant { | ||
| return &tenant{ | ||
| readyS: &ReadyStorage{}, | ||
| mtx: &sync.RWMutex{}, | ||
| readyS: &ReadyStorage{ | ||
| extLabels: extLabels, | ||
| addExtLabels: addExtLabels, | ||
| }, | ||
| mtx: &sync.RWMutex{}, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -705,6 +717,71 @@ func (t *MultiTSDB) TenantStats(limit int, statsByLabelName string, tenantIDs .. | |
| return result | ||
| } | ||
|
|
||
| func (t *MultiTSDB) getLastBlockPath(dataDir string, s *tsdb.DB) string { | ||
| bls := s.Blocks() | ||
| if len(bls) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| sort.Slice(bls, func(i, j int) bool { | ||
| return bls[i].MinTime() > bls[j].MinTime() | ||
| }) | ||
|
|
||
| lastBlock := bls[0] | ||
|
|
||
| return path.Join(dataDir, lastBlock.Meta().ULID.String()) | ||
|
|
||
| } | ||
|
|
||
| func (t *MultiTSDB) maybePruneHead(dataDir, tenantID, lastMetaPath string, curLset labels.Labels, pruneHead func() error) error { | ||
| if !t.extLabelsInTSDB { | ||
| return nil | ||
| } | ||
|
|
||
| if lastMetaPath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| m, err := metadata.ReadFromDir(lastMetaPath) | ||
| if err != nil { | ||
| return fmt.Errorf("reading meta %s: %w", lastMetaPath, err) | ||
| } | ||
|
|
||
| oldLset := labels.FromMap(m.Thanos.Labels) | ||
| if labels.Equal(oldLset, curLset) { | ||
| return nil | ||
| } | ||
|
|
||
| level.Info(t.logger).Log("msg", "changed external labelset detected, dumping the head block", "newLset", curLset.String(), "oldLset", oldLset.String()) | ||
|
Comment on lines
+750
to
+755
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider "flushing ... to disk" instead of "dumping"; the latter makes it sound like the data is being discarded. |
||
|
|
||
| if err := pruneHead(); err != nil { | ||
| return fmt.Errorf("flushing head: %w", err) | ||
| } | ||
|
|
||
| if t.bucket != nil { | ||
| logger := log.With(t.logger, "tenant", tenantID, "oldLset", oldLset.String()) | ||
| reg := NewUnRegisterer(prometheus.WrapRegistererWith(prometheus.Labels{"tenant": tenantID}, t.reg)) | ||
|
|
||
| ship := shipper.New( | ||
| t.bucket, | ||
| dataDir, | ||
| shipper.WithLogger(logger), | ||
| shipper.WithRegisterer(reg), | ||
| shipper.WithSource(metadata.ReceiveSource), | ||
| shipper.WithHashFunc(t.hashFunc), | ||
| shipper.WithMetaFileName(shipper.DefaultMetaFilename), | ||
| shipper.WithLabels(func() labels.Labels { return oldLset }), | ||
| shipper.WithAllowOutOfOrderUploads(t.allowOutOfOrderUpload), | ||
| shipper.WithSkipCorruptedBlocks(t.skipCorruptedBlocks), | ||
| ) | ||
| if _, err := ship.Sync(context.Background()); err != nil { | ||
| return fmt.Errorf("syncing head for old label set: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (t *MultiTSDB) startTSDB(logger log.Logger, tenantID string, tenant *tenant) error { | ||
| reg := prometheus.WrapRegistererWith(prometheus.Labels{"tenant": tenantID}, t.reg) | ||
| reg = NewUnRegisterer(reg) | ||
|
|
@@ -754,19 +831,35 @@ func (t *MultiTSDB) startTSDB(logger log.Logger, tenantID string, tenant *tenant | |
| t.removeTenantLocked(tenantID) | ||
| return err | ||
| } | ||
|
|
||
| if err := t.maybePruneHead(dataDir, tenantID, t.getLastBlockPath(dataDir, s), lset, func() error { return t.flushHead(s) }); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var ship *shipper.Shipper | ||
| if t.bucket != nil { | ||
| ship = shipper.New( | ||
| t.bucket, | ||
| dataDir, | ||
| shipper.WithLogger(logger), | ||
| shipperOpts := []shipper.Option{} | ||
|
|
||
| shipperOpts = append(shipperOpts, shipper.WithLogger(logger), | ||
| shipper.WithRegisterer(reg), | ||
| shipper.WithSource(metadata.ReceiveSource), | ||
| shipper.WithHashFunc(t.hashFunc), | ||
| shipper.WithMetaFileName(shipper.DefaultMetaFilename), | ||
| shipper.WithLabels(func() labels.Labels { return lset }), | ||
| shipper.WithAllowOutOfOrderUploads(t.allowOutOfOrderUpload), | ||
| shipper.WithSkipCorruptedBlocks(t.skipCorruptedBlocks), | ||
| shipper.WithSkipCorruptedBlocks(t.skipCorruptedBlocks)) | ||
|
|
||
| if t.extLabelsInTSDB { | ||
| shipperOpts = append(shipperOpts, shipper.WithExtensions( | ||
| map[string]any{ | ||
| metadata.ExtLabelsInTSDBKey: "", | ||
| }, | ||
| )) | ||
| } | ||
| ship = shipper.New( | ||
| t.bucket, | ||
| dataDir, | ||
| shipperOpts..., | ||
| ) | ||
| } | ||
| var options []store.TSDBStoreOption | ||
|
|
@@ -776,7 +869,10 @@ func (t *MultiTSDB) startTSDB(logger log.Logger, tenantID string, tenant *tenant | |
| if t.matcherCache != nil { | ||
| options = append(options, store.WithMatcherCacheInstance(t.matcherCache)) | ||
| } | ||
| options = append(options, store.WithExtLabelsInTSDB(t.extLabelsInTSDB)) | ||
|
|
||
| tenant.set(store.NewTSDBStore(logger, s, component.Receive, lset, options...), s, ship, exemplars.NewTSDB(s, lset), reg.(*UnRegisterer)) | ||
|
|
||
| t.addTenantLocked(tenantID, tenant) // need to update the client list once store is ready & client != nil | ||
| level.Info(logger).Log("msg", "TSDB is now ready") | ||
| return nil | ||
|
|
@@ -805,7 +901,7 @@ func (t *MultiTSDB) getOrLoadTenant(tenantID string, blockingStart bool) (*tenan | |
| return tenant, nil | ||
| } | ||
|
|
||
| tenant = newTenant() | ||
| tenant = newTenant(t.labels, t.extLabelsInTSDB) | ||
| t.addTenantUnlocked(tenantID, tenant) | ||
| t.mtx.Unlock() | ||
|
|
||
|
|
@@ -866,10 +962,12 @@ var ErrNotReady = errors.New("TSDB not ready") | |
|
|
||
| // ReadyStorage implements the Storage interface while allowing to set the actual | ||
| // storage at a later point in time. | ||
| // TODO: Replace this with upstream Prometheus implementation when it is exposed. | ||
| type ReadyStorage struct { | ||
| mtx sync.RWMutex | ||
| a *adapter | ||
|
|
||
| extLabels labels.Labels | ||
| addExtLabels bool | ||
| } | ||
|
|
||
| // Set the storage. | ||
|
|
@@ -920,9 +1018,39 @@ func (s *ReadyStorage) ExemplarQuerier(ctx context.Context) (storage.ExemplarQue | |
| return nil, ErrNotReady | ||
| } | ||
|
|
||
| type wrappingAppender struct { | ||
| addLabels labels.Labels | ||
| storage.Appender | ||
| gr storage.GetRef | ||
| } | ||
|
|
||
| var _ storage.Appender = (*wrappingAppender)(nil) | ||
| var _ storage.GetRef = (*wrappingAppender)(nil) | ||
|
|
||
| func (w *wrappingAppender) GetRef(lset labels.Labels, hash uint64) (storage.SeriesRef, labels.Labels) { | ||
| return w.gr.GetRef(labelpb.ExtendSortedLabels(lset, w.addLabels), hash) | ||
| } | ||
|
|
||
| func (w *wrappingAppender) Append(ref storage.SeriesRef, l labels.Labels, t int64, v float64) (storage.SeriesRef, error) { | ||
| l = labelpb.ExtendSortedLabels(l, w.addLabels) | ||
| return w.Appender.Append(ref, l, t, v) | ||
| } | ||
|
|
||
| // Appender implements the Storage interface. | ||
| func (s *ReadyStorage) Appender(ctx context.Context) (storage.Appender, error) { | ||
| if x := s.get(); x != nil { | ||
| if s.addExtLabels { | ||
| app, err := x.Appender(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &wrappingAppender{ | ||
| Appender: app, | ||
| gr: app.(storage.GetRef), | ||
| addLabels: s.extLabels, | ||
| }, nil | ||
| } | ||
| return x.Appender(ctx) | ||
| } | ||
| return nil, ErrNotReady | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check if this proposed addition to the readme for feature flags you've added looks sensible?:
...
If all blocks matched by a request had this feature flag enabled at the time they were written, Receive can stream responses to Series gRPC requests from Query instead of buffering them in memory. This lowers Receive's peak memory consumption for high-cardinality or high-sample-count requests.
However, storing external labels in the TSDB will increase TSDB index sizes because each external label must appear in the inverted index for every series. TSDB blocks will be somewhat larger and need more disk I/O to read, and HEAD chunks will require somewhat more memory.
If the configured external labels are changed, Receive will flush the current HEAD block to disk and start a new HEAD block. No data is discarded.