-
Notifications
You must be signed in to change notification settings - Fork 248
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
OCPBUGS-17157: cache/json: use shared buffers for JSON decoding #1140
Merged
openshift-merge-robot
merged 1 commit into
operator-framework:master
from
stevekuznetsov:skuznets/encoding-decoding-shared-buffers
Sep 7, 2023
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,15 +6,17 @@ import ( | |||||
"errors" | ||||||
"fmt" | ||||||
"hash/fnv" | ||||||
"io" | ||||||
"io/fs" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"sort" | ||||||
"strings" | ||||||
|
||||||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||||||
"github.com/operator-framework/operator-registry/pkg/api" | ||||||
"github.com/operator-framework/operator-registry/pkg/registry" | ||||||
"k8s.io/apimachinery/pkg/util/sets" | ||||||
"github.com/sirupsen/logrus" | ||||||
) | ||||||
|
||||||
var _ Cache = &JSON{} | ||||||
|
@@ -58,31 +60,66 @@ func (q *JSON) ListBundles(ctx context.Context) ([]*api.Bundle, error) { | |||||
} | ||||||
|
||||||
func (q *JSON) SendBundles(_ context.Context, s registry.BundleSender) error { | ||||||
var keys []apiBundleKey | ||||||
for _, pkg := range q.packageIndex { | ||||||
channels := sets.KeySet(pkg.Channels) | ||||||
for _, chName := range sets.List(channels) { | ||||||
ch := pkg.Channels[chName] | ||||||
|
||||||
bundles := sets.KeySet(ch.Bundles) | ||||||
for _, bName := range sets.List(bundles) { | ||||||
b := ch.Bundles[bName] | ||||||
apiBundle, err := q.loadAPIBundle(apiBundleKey{pkg.Name, ch.Name, b.Name}) | ||||||
if err != nil { | ||||||
return fmt.Errorf("convert bundle %q: %v", b.Name, err) | ||||||
} | ||||||
if apiBundle.BundlePath != "" { | ||||||
// The SQLite-based server | ||||||
// configures its querier to | ||||||
// omit these fields when | ||||||
// bundle path is set. | ||||||
apiBundle.CsvJson = "" | ||||||
apiBundle.Object = nil | ||||||
} | ||||||
if err := s.Send(apiBundle); err != nil { | ||||||
return err | ||||||
} | ||||||
for _, ch := range pkg.Channels { | ||||||
for _, b := range ch.Bundles { | ||||||
keys = append(keys, apiBundleKey{pkg.Name, ch.Name, b.Name}) | ||||||
} | ||||||
} | ||||||
} | ||||||
sort.Slice(keys, func(i, j int) bool { | ||||||
if keys[i].chName != keys[j].chName { | ||||||
return keys[i].chName < keys[j].chName | ||||||
} | ||||||
if keys[i].pkgName != keys[j].pkgName { | ||||||
return keys[i].pkgName < keys[j].pkgName | ||||||
} | ||||||
return keys[i].name < keys[j].name | ||||||
}) | ||||||
var files []*os.File | ||||||
var readers []io.Reader | ||||||
for _, key := range keys { | ||||||
filename, ok := q.apiBundles[key] | ||||||
if !ok { | ||||||
return fmt.Errorf("package %q, channel %q, key %q not found", key.pkgName, key.chName, key.name) | ||||||
} | ||||||
file, err := os.Open(filename) | ||||||
joelanford marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if err != nil { | ||||||
return fmt.Errorf("failed to open file for package %q, channel %q, key %q: %w", key.pkgName, key.chName, key.name, err) | ||||||
} | ||||||
files = append(files, file) | ||||||
readers = append(readers, file) | ||||||
} | ||||||
defer func() { | ||||||
for _, file := range files { | ||||||
if err := file.Close(); err != nil { | ||||||
logrus.WithError(err).WithField("file", file.Name()).Warn("could not close file") | ||||||
} | ||||||
} | ||||||
}() | ||||||
multiReader := io.MultiReader(readers...) | ||||||
decoder := json.NewDecoder(multiReader) | ||||||
index := 0 | ||||||
for { | ||||||
var bundle api.Bundle | ||||||
stevekuznetsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if err := decoder.Decode(&bundle); err == io.EOF { | ||||||
break | ||||||
} else if err != nil { | ||||||
return fmt.Errorf("failed to decode file for package %q, channel %q, key %q: %w", keys[index].pkgName, keys[index].chName, keys[index].name, err) | ||||||
} | ||||||
if bundle.BundlePath != "" { | ||||||
// The SQLite-based server | ||||||
// configures its querier to | ||||||
// omit these fields when | ||||||
// key path is set. | ||||||
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. nit:
Suggested change
|
||||||
bundle.CsvJson = "" | ||||||
bundle.Object = nil | ||||||
} | ||||||
if err := s.Send(&bundle); err != nil { | ||||||
return err | ||||||
} | ||||||
index += 1 | ||||||
} | ||||||
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.
Why are we ordering the list here?
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.
The ordering existed before, I just made it explicit. I imagine consumers (or tests) of this output benefit from a stable list.
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.
For e.g. #1069