-
Notifications
You must be signed in to change notification settings - Fork 3.9k
chore: Split indexing into distinct steps #19987
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
+138
−51
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e3c148
chore: Split indexing into distinct steps
benclive 8dccafc
Move mutex outside of steps
benclive ee88410
Comments
benclive c7c9773
linting
benclive 9cc78bd
linting again
benclive d20941a
Lint III: The reckoning
benclive 13e8215
driveby: Fix flaky test
benclive 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package index | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/bits-and-blooms/bloom/v3" | ||
| "github.com/grafana/loki/v3/pkg/dataobj" | ||
| "github.com/grafana/loki/v3/pkg/dataobj/sections/logs" | ||
| "github.com/prometheus/prometheus/model/labels" | ||
| ) | ||
|
|
||
| type columnValuesCalculation struct { | ||
| columnBloomBuilders map[string]*bloom.BloomFilter | ||
| columnIndexes map[string]int64 | ||
| } | ||
|
|
||
| func (c *columnValuesCalculation) Prepare(ctx context.Context, _ *dataobj.Section, stats logs.Stats) error { | ||
| c.columnBloomBuilders = make(map[string]*bloom.BloomFilter) | ||
| c.columnIndexes = make(map[string]int64) | ||
|
|
||
| for _, column := range stats.Columns { | ||
| logsType, _ := logs.ParseColumnType(column.Type) | ||
| if logsType != logs.ColumnTypeMetadata { | ||
| continue | ||
| } | ||
| c.columnBloomBuilders[column.Name] = bloom.NewWithEstimates(uint(column.Cardinality), 1.0/128.0) | ||
| c.columnIndexes[column.Name] = column.ColumnIndex | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *columnValuesCalculation) ProcessBatch(ctx context.Context, _ *logsCalculationContext, batch []logs.Record) error { | ||
| for _, log := range batch { | ||
| log.Metadata.Range(func(md labels.Label) { | ||
| c.columnBloomBuilders[md.Name].Add([]byte(md.Value)) | ||
| }) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *columnValuesCalculation) Flush(ctx context.Context, context *logsCalculationContext) error { | ||
| context.builderMtx.Lock() | ||
| defer context.builderMtx.Unlock() | ||
| for columnName, bloom := range c.columnBloomBuilders { | ||
| bloomBytes, err := bloom.MarshalBinary() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal bloom filter: %w", err) | ||
| } | ||
| err = context.builder.AppendColumnIndex(context.tenantID, context.objectPath, context.sectionIdx, columnName, c.columnIndexes[columnName], bloomBytes) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to append column index: %w", err) | ||
| } | ||
| } | ||
| return nil | ||
| } |
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,31 @@ | ||
| package index | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/grafana/loki/v3/pkg/dataobj" | ||
| "github.com/grafana/loki/v3/pkg/dataobj/sections/logs" | ||
| ) | ||
|
|
||
| type streamStatisticsCalculation struct{} | ||
|
|
||
| func (c *streamStatisticsCalculation) Prepare(ctx context.Context, section *dataobj.Section, stats logs.Stats) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (c *streamStatisticsCalculation) ProcessBatch(ctx context.Context, context *logsCalculationContext, batch []logs.Record) error { | ||
| context.builderMtx.Lock() | ||
| defer context.builderMtx.Unlock() | ||
| for _, log := range batch { | ||
| err := context.builder.ObserveLogLine(context.tenantID, context.objectPath, context.sectionIdx, log.StreamID, context.streamIDLookup[log.StreamID], log.Timestamp, int64(len(log.Line))) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to observe log line: %w", err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *streamStatisticsCalculation) Flush(ctx context.Context, context *logsCalculationContext) error { | ||
| return 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.
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.
nit: sharing mutex pointers can be a source of bugs because the spread of where logic is defined gets wider and it can be harder to keep track of making sure the mutex is being used properly in all places.
I'd recommend considering changing the semantics such that:
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.
Good idea, I've made the changes you suggested and locked the mutex before calling the step implementations.