-
Notifications
You must be signed in to change notification settings - Fork 57
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
🐛 incremental improvement in catalogmetadata cache/client performance #965
Merged
joelanford
merged 1 commit into
operator-framework:main
from
joelanford:improve-catalog-reconcile-time
Jun 24, 2024
Merged
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 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 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 |
---|---|---|
|
@@ -3,13 +3,14 @@ package cache | |
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" | ||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
|
||
"github.com/operator-framework/operator-controller/internal/catalogmetadata/client" | ||
) | ||
|
@@ -66,7 +67,7 @@ type filesystemCache struct { | |
// resources that have been successfully reconciled, unpacked, and are being served. | ||
// These requirements help ensure that we can rely on status conditions to determine | ||
// when to issue a request to update the cached Catalog contents. | ||
func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *catalogd.ClusterCatalog) (io.ReadCloser, error) { | ||
func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *catalogd.ClusterCatalog) (fs.FS, error) { | ||
if catalog == nil { | ||
return nil, fmt.Errorf("error: provided catalog must be non-nil") | ||
} | ||
|
@@ -80,25 +81,23 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c | |
} | ||
|
||
cacheDir := filepath.Join(fsc.cachePath, catalog.Name) | ||
cacheFilePath := filepath.Join(cacheDir, "data.json") | ||
|
||
fsc.mutex.RLock() | ||
if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok { | ||
if catalog.Status.ResolvedSource.Image.ResolvedRef == data.ResolvedRef { | ||
fsc.mutex.RUnlock() | ||
return os.Open(cacheFilePath) | ||
return os.DirFS(cacheDir), nil | ||
} | ||
} | ||
fsc.mutex.RUnlock() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, catalog.Status.ContentURL, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("error forming request: %s", err) | ||
return nil, fmt.Errorf("error forming request: %v", err) | ||
} | ||
|
||
resp, err := fsc.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error performing request: %s", err) | ||
return nil, fmt.Errorf("error performing request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
|
@@ -117,34 +116,49 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c | |
// the cached contents | ||
if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok { | ||
if data.ResolvedRef == catalog.Status.ResolvedSource.Image.Ref { | ||
return os.Open(cacheFilePath) | ||
return os.DirFS(cacheDir), nil | ||
} | ||
} | ||
|
||
if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil { | ||
return nil, fmt.Errorf("error creating cache directory for Catalog %q: %s", catalog.Name, err) | ||
} | ||
|
||
file, err := os.Create(cacheFilePath) | ||
tmpDir, err := os.MkdirTemp(fsc.cachePath, fmt.Sprintf(".%s-", catalog.Name)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating cache file for Catalog %q: %s", catalog.Name, err) | ||
return nil, fmt.Errorf("error creating temporary directory to unpack catalog metadata: %v", err) | ||
} | ||
|
||
if _, err := io.Copy(file, resp.Body); err != nil { | ||
return nil, fmt.Errorf("error writing contents to cache file for Catalog %q: %s", catalog.Name, err) | ||
if err := declcfg.WalkMetasReader(resp.Body, func(meta *declcfg.Meta, err error) error { | ||
if err != nil { | ||
return fmt.Errorf("error parsing catalog contents: %v", err) | ||
} | ||
pkgName := meta.Package | ||
if meta.Schema == declcfg.SchemaPackage { | ||
pkgName = meta.Name | ||
} | ||
metaName := meta.Name | ||
if meta.Name == "" { | ||
metaName = meta.Schema | ||
} | ||
metaPath := filepath.Join(tmpDir, pkgName, meta.Schema, metaName+".json") | ||
if err := os.MkdirAll(filepath.Dir(metaPath), os.ModePerm); err != nil { | ||
return fmt.Errorf("error creating directory for catalog metadata: %v", err) | ||
} | ||
if err := os.WriteFile(metaPath, meta.Blob, os.ModePerm); err != nil { | ||
return fmt.Errorf("error writing catalog metadata to file: %v", err) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = file.Sync(); err != nil { | ||
return nil, fmt.Errorf("error syncing contents to cache file for Catalog %q: %s", catalog.Name, err) | ||
if err := os.RemoveAll(cacheDir); err != nil { | ||
return nil, fmt.Errorf("error removing old cache directory: %v", err) | ||
} | ||
|
||
if _, err = file.Seek(0, 0); err != nil { | ||
return nil, fmt.Errorf("error resetting offset for cache file reader for Catalog %q: %s", catalog.Name, err) | ||
if err := os.Rename(tmpDir, cacheDir); err != nil { | ||
return nil, fmt.Errorf("error moving temporary directory to cache directory: %v", err) | ||
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. curious to know what happens if fail here? Just run of the mill expbackoff? what happens to resolution during that period? Same thing? 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. Yes to both. |
||
} | ||
|
||
fsc.cacheDataByCatalogName[catalog.Name] = cacheData{ | ||
ResolvedRef: catalog.Status.ResolvedSource.Image.ResolvedRef, | ||
} | ||
|
||
return file, nil | ||
return os.DirFS(cacheDir), nil | ||
} |
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.
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.
CGO is required for the race detector. TIL
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.
yeah - got caught on that one rejiggering the v0 Makefile