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

Download specs gzipped for 'compute vocabulary' and visitor list operations. #1122

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion cmd/registry/cmd/compute/vocabulary/vocabulary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"

"github.com/apigee/registry/cmd/registry/compress"
"github.com/apigee/registry/cmd/registry/tasks"
"github.com/apigee/registry/pkg/connection"
"github.com/apigee/registry/pkg/log"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/apigee/registry/rpc"
"github.com/google/gnostic/metrics/vocabulary"
"github.com/spf13/cobra"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -117,12 +119,19 @@ func (task *computeVocabularyTask) String() string {
}

func (task *computeVocabularyTask) Run(ctx context.Context) error {
ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip")
contents, err := task.client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{
Name: task.specName,
})
if err != nil {
return err
}
if mime.IsGZipCompressed(contents.ContentType) {
contents.Data, err = compress.GUnzippedBytes(contents.Data)
if err != nil {
return err
}
}

log.Debugf(ctx, "Computing %s/artifacts/vocabulary", task.specName)
var vocab *metrics.Vocabulary
Expand Down Expand Up @@ -155,7 +164,7 @@ func (task *computeVocabularyTask) Run(ctx context.Context) error {
return nil
}
} else {
return fmt.Errorf("we don't know how to summarize %s", task.specName)
return fmt.Errorf("we don't know how to compute the vocabulary of %s", task.specName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was just a kick of a tire to get the CLA check to run again... but it was also needed

}

if task.dryRun {
Expand Down
16 changes: 11 additions & 5 deletions pkg/visitor/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package visitor

import (
"bytes"
"context"
"testing"

"github.com/apigee/registry/cmd/registry/compress"
"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/pkg/names"
"github.com/apigee/registry/rpc"
Expand All @@ -29,6 +31,10 @@ func TestFetch(t *testing.T) {
parent := project.String() + "/locations/global"

specContents := "hello"
gzippedSpecContents, err := compress.GZippedBytes([]byte(specContents))
if err != nil {
t.Fatalf("Setup: Failed to compress test data: %s", err)
}
artifactContents := "hello"

ctx := context.Background()
Expand Down Expand Up @@ -62,8 +68,8 @@ func TestFetch(t *testing.T) {
ApiSpecId: "s",
Parent: versionName.String(),
ApiSpec: &rpc.ApiSpec{
Contents: []byte(specContents),
MimeType: "text/plain",
Contents: gzippedSpecContents,
MimeType: "text/plain+gzip",
},
})
if err != nil {
Expand Down Expand Up @@ -91,7 +97,7 @@ func TestFetch(t *testing.T) {
if err != nil {
t.Fatalf("Failed to fetch spec contents: %s", err)
}
if string(spec.Contents) != specContents {
if !bytes.Equal(spec.Contents, gzippedSpecContents) {
Copy link
Contributor Author

@timburks timburks Mar 23, 2023

Choose a reason for hiding this comment

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

This and the following two changes show an inconsistency -- ListSpecs is returning specs uncompressed, even when they are downloaded compressed (as of this PR). GetSpecs was already downloading specs compressed but returning them without uncompressing. I think it's probably better for GetSpecs to also uncompress specs before returning them, but think it might overcomplicate this PR by making that change here.

t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents)
}
})
Expand All @@ -111,7 +117,7 @@ func TestFetch(t *testing.T) {
t.Fatalf("Failed to parse spec name: %s", err)
}
err = GetSpec(ctx, registryClient, specName, true, func(ctx context.Context, spec *rpc.ApiSpec) error {
if string(spec.Contents) != specContents {
if !bytes.Equal(spec.Contents, gzippedSpecContents) {
t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents)
}
return nil
Expand Down Expand Up @@ -146,7 +152,7 @@ func TestFetch(t *testing.T) {
t.Fatalf("Failed to parse spec revision name: %s", err)
}
err = GetSpecRevision(ctx, registryClient, specRevisionName, true, func(ctx context.Context, spec *rpc.ApiSpec) error {
if string(spec.Contents) != specContents {
if !bytes.Equal(spec.Contents, gzippedSpecContents) {
t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents)
}
return nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/visitor/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import (
"context"
"fmt"

"github.com/apigee/registry/cmd/registry/compress"
"github.com/apigee/registry/gapic"
"github.com/apigee/registry/pkg/mime"
"github.com/apigee/registry/pkg/names"
"github.com/apigee/registry/rpc"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

Expand Down Expand Up @@ -186,13 +189,20 @@ func ListSpecs(ctx context.Context,
}

if getContents {
ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip")
resp, err := client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{
Name: r.GetName(),
})
if err != nil {
return err
}
r.Contents = resp.GetData()
if mime.IsGZipCompressed(resp.ContentType) {
r.Contents, err = compress.GUnzippedBytes(r.Contents)
if err != nil {
return err
}
}
}

if err := handler(ctx, r); err != nil {
Expand All @@ -217,13 +227,20 @@ func ListSpecRevisions(ctx context.Context,
}

if getContents {
ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip")
resp, err := client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{
Name: r.GetName(),
})
if err != nil {
return err
}
r.Contents = resp.GetData()
if mime.IsGZipCompressed(resp.ContentType) {
r.Contents, err = compress.GUnzippedBytes(r.Contents)
if err != nil {
return err
}
}
}

if err := handler(ctx, r); err != nil {
Expand Down