-
Notifications
You must be signed in to change notification settings - Fork 692
chore: copy localblocks into livestore module #6808
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
javiermolinar
merged 8 commits into
grafana:main
from
javiermolinar:chore/livestore-own-localblock
Mar 27, 2026
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ffd09f
chore(livestore): own local block and partition ring helpers
javiermolinar 75b42d4
docs(changelog): add entry for #6808
javiermolinar 2001862
tighten docs
javiermolinar de148c8
Merge branch 'main' into chore/livestore-own-localblock
javiermolinar 02b4e6f
use helper
javiermolinar ec4d6a4
improve test
javiermolinar ca52380
build
javiermolinar 4b66b23
remove parquet reference
javiermolinar 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
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,153 @@ | ||
| package livestore | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/grafana/tempo/pkg/tempopb" | ||
| "github.com/grafana/tempo/pkg/traceql" | ||
| "github.com/grafana/tempo/tempodb/backend" | ||
| "github.com/grafana/tempo/tempodb/backend/local" | ||
| "github.com/grafana/tempo/tempodb/encoding" | ||
| "github.com/grafana/tempo/tempodb/encoding/common" | ||
| "go.uber.org/atomic" | ||
| ) | ||
|
|
||
| const nameFlushed = "flushed" | ||
|
|
||
| // LocalBlock is a block stored in local storage. It can be searched and flushed to a remote backend, and | ||
| // permanently tracks the flushed time with a special file in the block. | ||
| type LocalBlock struct { | ||
| common.BackendBlock | ||
| reader backend.Reader | ||
| writer backend.Writer | ||
|
|
||
| flushedTime atomic.Int64 // protecting flushedTime because it's accessed concurrently | ||
| } | ||
|
|
||
| var ( | ||
| _ common.Finder = (*LocalBlock)(nil) | ||
| _ common.Searcher = (*LocalBlock)(nil) | ||
| ) | ||
|
|
||
| // NewLocalBlock creates a local block wrapper around an existing backend block. | ||
| func NewLocalBlock(ctx context.Context, existingBlock common.BackendBlock, l *local.Backend) *LocalBlock { | ||
| c := &LocalBlock{ | ||
| BackendBlock: existingBlock, | ||
| reader: backend.NewReader(l), | ||
| writer: backend.NewWriter(l), | ||
| } | ||
|
|
||
| flushedBytes, err := c.reader.Read(ctx, nameFlushed, (uuid.UUID)(c.BlockMeta().BlockID), c.BlockMeta().TenantID, nil) | ||
| if err == nil { | ||
| flushedTime := time.Time{} | ||
| if err := flushedTime.UnmarshalText(flushedBytes); err == nil { | ||
| c.flushedTime.Store(flushedTime.Unix()) | ||
| } | ||
| } | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| func (c *LocalBlock) FindTraceByID(ctx context.Context, id common.ID, opts common.SearchOptions) (*tempopb.TraceByIDResponse, error) { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.FindTraceByID") | ||
| defer span.End() | ||
| return c.BackendBlock.FindTraceByID(ctx, id, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) Search(ctx context.Context, req *tempopb.SearchRequest, opts common.SearchOptions) (*tempopb.SearchResponse, error) { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.Search") | ||
| defer span.End() | ||
| return c.BackendBlock.Search(ctx, req, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) SearchTags(ctx context.Context, scope traceql.AttributeScope, cb common.TagsCallback, mcb common.MetricsCallback, opts common.SearchOptions) error { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.SearchTags") | ||
| defer span.End() | ||
| return c.BackendBlock.SearchTags(ctx, scope, cb, mcb, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) SearchTagValues(ctx context.Context, tag string, cb common.TagValuesCallback, mcb common.MetricsCallback, opts common.SearchOptions) error { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.SearchTagValues") | ||
| defer span.End() | ||
| return c.BackendBlock.SearchTagValues(ctx, tag, cb, mcb, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) SearchTagValuesV2(ctx context.Context, tag traceql.Attribute, cb common.TagValuesCallbackV2, mcb common.MetricsCallback, opts common.SearchOptions) error { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.SearchTagValuesV2") | ||
| defer span.End() | ||
| return c.BackendBlock.SearchTagValuesV2(ctx, tag, cb, mcb, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) Fetch(ctx context.Context, req traceql.FetchSpansRequest, opts common.SearchOptions) (traceql.FetchSpansResponse, error) { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.Fetch") | ||
| defer span.End() | ||
| return c.BackendBlock.Fetch(ctx, req, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) FetchSpans(ctx context.Context, req traceql.FetchSpansRequest, opts common.SearchOptions) (traceql.FetchSpansOnlyResponse, error) { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.FetchSpans") | ||
| defer span.End() | ||
| return c.BackendBlock.FetchSpans(ctx, req, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) FetchTagValues(ctx context.Context, req traceql.FetchTagValuesRequest, cb traceql.FetchTagValuesCallback, mcb common.MetricsCallback, opts common.SearchOptions) error { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.FetchTagValues") | ||
| defer span.End() | ||
| return c.BackendBlock.FetchTagValues(ctx, req, cb, mcb, opts) | ||
| } | ||
|
|
||
| func (c *LocalBlock) FetchTagNames(ctx context.Context, req traceql.FetchTagsRequest, cb traceql.FetchTagsCallback, mcb common.MetricsCallback, opts common.SearchOptions) error { | ||
| ctx, span := tracer.Start(ctx, "LocalBlock.FetchTagNames") | ||
| defer span.End() | ||
| return c.BackendBlock.FetchTagNames(ctx, req, cb, mcb, opts) | ||
| } | ||
|
|
||
| // FlushedTime returns the time the block was flushed, or the zero time if it has never been flushed. | ||
| func (c *LocalBlock) FlushedTime() time.Time { | ||
| unixTime := c.flushedTime.Load() | ||
| if unixTime == 0 { | ||
| return time.Time{} | ||
| } | ||
| return time.Unix(unixTime, 0) | ||
| } | ||
|
|
||
| func (c *LocalBlock) SetFlushed(ctx context.Context) error { | ||
| flushedTime := time.Now() | ||
| flushedBytes, err := flushedTime.MarshalText() | ||
| if err != nil { | ||
| return fmt.Errorf("error marshalling flush time to text: %w", err) | ||
| } | ||
|
|
||
| err = c.writer.Write(ctx, nameFlushed, (uuid.UUID)(c.BlockMeta().BlockID), c.BlockMeta().TenantID, flushedBytes, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("error writing local block flushed file: %w", err) | ||
| } | ||
|
|
||
| c.flushedTime.Store(flushedTime.Unix()) | ||
| return nil | ||
| } | ||
|
|
||
| func (c *LocalBlock) Write(ctx context.Context, w backend.Writer) error { | ||
| if err := encoding.CopyBlock(ctx, c.BlockMeta(), c.reader, w); err != nil { | ||
| return fmt.Errorf("error copying block from local to remote backend: %w", err) | ||
| } | ||
|
|
||
| return c.SetFlushed(ctx) | ||
| } | ||
|
|
||
| func (c *LocalBlock) SetDiskCache(ctx context.Context, cacheKey string, data []byte) error { | ||
| return c.writer.Write(ctx, cacheKey, (uuid.UUID)(c.BlockMeta().BlockID), c.BlockMeta().TenantID, data, nil) | ||
| } | ||
|
|
||
| func (c *LocalBlock) GetDiskCache(ctx context.Context, cacheKey string) ([]byte, error) { | ||
| data, err := c.reader.Read(ctx, cacheKey, (uuid.UUID)(c.BlockMeta().BlockID), c.BlockMeta().TenantID, nil) | ||
| if errors.Is(err, backend.ErrDoesNotExist) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return data, err | ||
| } |
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.