-
Notifications
You must be signed in to change notification settings - Fork 79
Load packages concurrently in filesystem indexers #1489
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6ed5f33
Load in parallel packages from filesystem
mrodm c9eff98
Add benchmark
mrodm 01407b7
Refactor workers logic into functions - moved to an internal package
mrodm 024d5d3
Add changelog entries
mrodm 9e0a0d0
Rephrase changelog entry
mrodm 9241261
Ensure packages kept in the final list are the ones loaded first
mrodm 4e78562
Re-phrase comment
mrodm 6612363
Merge upstream/main into concurrent-load-packages-fsys
mrodm 21ec258
Fix parameter for filesystem constructors
mrodm 44f3419
Update changelog entry descriptions
mrodm 6dcc859
Assign directly GOMAXPROCS as parameter
mrodm 5aea1c6
Add changelog entry
mrodm a62499d
Remove unnecessary function
mrodm e9ca9a6
Rephrase comment about duplicates
mrodm fcb290e
Add flag to override number of workers assigned to read packages conc…
mrodm 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
Some comments aren't visible on the classic Files Changed page.
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,55 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
| // you may not use this file except in compliance with the Elastic License 2.0. | ||
|
|
||
| package workers | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sync" | ||
| ) | ||
|
|
||
| type taskPool struct { | ||
| wg sync.WaitGroup | ||
| pool chan struct{} | ||
| errC chan error | ||
| errors []error | ||
| } | ||
|
|
||
| func NewTaskPool(size int) *taskPool { | ||
| p := &taskPool{ | ||
| pool: make(chan struct{}, size), | ||
| errC: make(chan error), | ||
| } | ||
| go p.errorLoop() | ||
| return p | ||
| } | ||
|
|
||
| func (p *taskPool) errorLoop() { | ||
| go func() { | ||
| for err := range p.errC { | ||
| if err != nil { | ||
| p.errors = append(p.errors, err) | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| // Do runs the task in a goroutine, ensuring no more tasks are running than the size of the pool. | ||
| func (p *taskPool) Do(task func() error) { | ||
| p.pool <- struct{}{} | ||
| p.wg.Add(1) | ||
| go func() { | ||
| defer func() { <-p.pool }() | ||
| defer p.wg.Done() | ||
| p.errC <- task() | ||
| }() | ||
| } | ||
|
|
||
| // Wait waits for all the tasks to finish, and joins the errors found. The pool cannot be used after calling Wait. | ||
| func (p *taskPool) Wait() error { | ||
| close(p.pool) | ||
| p.wg.Wait() | ||
| close(p.errC) | ||
| return errors.Join(p.errors...) | ||
| } |
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import ( | |
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
|
|
@@ -85,6 +86,7 @@ var ( | |
| serviceName = getServiceName() | ||
|
|
||
| packagePathsEnableWatcher = false | ||
| packagePathsWorkers = 1 | ||
|
|
||
| defaultConfig = Config{ | ||
| CacheTimeIndex: 10 * time.Second, | ||
|
|
@@ -132,6 +134,7 @@ func init() { | |
| flag.StringVar(&proxyTo, "proxy-to", "https://epr.elastic.co/", "Proxy-to endpoint") | ||
|
|
||
| flag.BoolVar(&packagePathsEnableWatcher, "package-paths-enable-watcher", false, "Enable file system watcher for package paths to automatically detect new packages.") | ||
| flag.IntVar(&packagePathsWorkers, "package-paths-workers", runtime.GOMAXPROCS(0), "Number of workers to use for reading packages concurrently from the configured paths. Default is the number of CPU cores returned by GOMAXPROCS.") | ||
|
Contributor
Author
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. Tested using flag and environment variable |
||
| } | ||
|
|
||
| type Config struct { | ||
|
|
@@ -370,7 +373,6 @@ func initMetricsServer(logger *zap.Logger) { | |
| } | ||
| }() | ||
| } | ||
|
|
||
| func initIndexer(ctx context.Context, logger *zap.Logger, options serverOptions) Indexer { | ||
| tx := options.apmTracer.StartTransaction("initIndexer", "backend.init") | ||
| defer tx.End() | ||
|
|
@@ -400,7 +402,10 @@ func initIndexer(ctx context.Context, logger *zap.Logger, options serverOptions) | |
| Logger: logger, | ||
| EnablePathsWatcher: packagePathsEnableWatcher, | ||
| APMTracer: options.apmTracer, | ||
| PathsWorkers: packagePathsWorkers, | ||
| } | ||
| logger.Debug("Using workers to read packages from package paths", zap.Int("workers", fsOptions.PathsWorkers)) | ||
| logger.Debug("Watching package paths for changes", zap.Bool("enabled", fsOptions.EnablePathsWatcher)) | ||
|
Comment on lines
+407
to
+408
Contributor
Author
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. I thought to show in debug level the values used by the service. WDYT ? Examples of logs shown: {
"log.level": "debug",
"@timestamp": "2025-11-27T20:17:56.815+0100",
"log.origin": {
"function": "main.initIndexer",
"file.name": "package-registry-load-packages-fsys/main.go",
"file.line": 407
},
"message": "Using workers to read packages from package paths",
"service.name": "package-registry",
"service.version": "1.33.1",
"workers": 16,
"ecs.version": "1.6.0"
}
{
"log.level": "debug",
"@timestamp": "2025-11-27T20:17:56.815+0100",
"log.origin": {
"function": "main.initIndexer",
"file.name": "package-registry-load-packages-fsys/main.go",
"file.line": 408
},
"message": "Wathching package paths for changes",
"service.name": "package-registry",
"service.version": "1.33.1",
"enabled": false,
"ecs.version": "1.6.0"
} |
||
|
|
||
| combined = append(combined, | ||
| packages.NewZipFileSystemIndexer(fsOptions, packagesBasePaths...), | ||
|
|
@@ -753,5 +758,9 @@ func validateFlags() error { | |
| return fmt.Errorf("categories cache is just supported with SQL Storage indexer: feature-enable-categories-cache is enabled, but feature-sql-storage-indexer is not enabled") | ||
| } | ||
|
|
||
| if packagePathsWorkers <= 0 { | ||
| return fmt.Errorf("package-paths-workers must be greater than 0") | ||
| } | ||
|
|
||
| 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
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
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.