Skip to content
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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 60 additions & 23 deletions pkg/cache/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
})
Comment on lines +71 to +79
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For e.g. #1069

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.
Copy link
Contributor

@grokspawn grokspawn Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
// key path is set.
// bundle path is set.

bundle.CsvJson = ""
bundle.Object = nil
}
if err := s.Send(&bundle); err != nil {
return err
}
index += 1
}
return nil
}
Expand Down
Loading