-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GHA cache v2 Twirp protocol support (#829)
* GHA cache v2 Twirp protocol support * CI: install protoc-gen-twirp Had to use golang:latest because bufbuild/buf container image lacks Golang. * buf.gen.yaml: explicitly use local generation
- Loading branch information
Showing
13 changed files
with
15,046 additions
and
6 deletions.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
syntax = "proto3"; | ||
|
||
package github.actions.results.api.v1; | ||
|
||
option go_package = "github.com/cirruslabs/cirrus-cli/pkg/api/gharesults"; | ||
|
||
service CacheService { | ||
rpc CreateCacheEntry (CreateCacheEntryRequest) returns (CreateCacheEntryResponse); | ||
rpc FinalizeCacheEntryUpload (FinalizeCacheEntryUploadRequest) returns (FinalizeCacheEntryUploadResponse); | ||
rpc GetCacheEntryDownloadURL (GetCacheEntryDownloadURLRequest) returns (GetCacheEntryDownloadURLResponse); | ||
} | ||
|
||
message CreateCacheEntryRequest { | ||
CacheMetadata metadata = 1; | ||
string key = 2; | ||
string version = 3; | ||
} | ||
|
||
message CacheMetadata { | ||
string repository_id = 1; | ||
repeated CacheScope scope = 2; | ||
} | ||
|
||
message CacheScope { | ||
string scope = 1; | ||
string permission = 2; | ||
} | ||
|
||
message CreateCacheEntryResponse { | ||
bool ok = 1; | ||
string signed_upload_url = 2; | ||
} | ||
|
||
message FinalizeCacheEntryUploadRequest { | ||
CacheMetadata metadata = 1; | ||
string key = 2; | ||
int64 size_bytes = 3; | ||
string version = 4; | ||
} | ||
|
||
message FinalizeCacheEntryUploadResponse { | ||
bool ok = 1; | ||
int64 entry_id = 2; | ||
} | ||
|
||
message GetCacheEntryDownloadURLRequest { | ||
CacheMetadata metadata = 1; | ||
string key = 2; | ||
repeated string restore_keys = 3; | ||
string version = 4; | ||
} | ||
|
||
message GetCacheEntryDownloadURLResponse { | ||
bool ok = 1; | ||
string signed_download_url = 2; | ||
string matched_key = 3; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
version: v1 | ||
version: v2 | ||
plugins: | ||
- plugin: buf.build/protocolbuffers/go | ||
- remote: buf.build/protocolbuffers/go | ||
out: pkg/ | ||
opt: paths=source_relative | ||
- plugin: buf.build/grpc/go | ||
- remote: buf.build/grpc/go | ||
out: pkg/ | ||
opt: paths=source_relative | ||
# Use local generation, as recommended in https://github.com/bufbuild/plugins/issues/58: | ||
# | ||
# >We're not actively working on supporting the twitchtv/twirp plugin on the BSR at the moment. | ||
# >For now we recommend that users use local generation with buf.gen.yaml. | ||
- local: protoc-gen-twirp | ||
out: pkg/ | ||
opt: paths=source_relative |
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
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ghacachev2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/cirruslabs/cirrus-cli/internal/agent/client" | ||
"github.com/cirruslabs/cirrus-cli/pkg/api" | ||
"github.com/cirruslabs/cirrus-cli/pkg/api/gharesults" | ||
"github.com/samber/lo" | ||
"github.com/twitchtv/twirp" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"hash/fnv" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// Interface guard | ||
// | ||
// Ensures that Cache struct implements gharesults.CacheService interface. | ||
var _ gharesults.CacheService = (*Cache)(nil) | ||
|
||
const APIMountPoint = "/twirp" | ||
|
||
type Cache struct { | ||
cacheHost string | ||
twirpServer gharesults.TwirpServer | ||
} | ||
|
||
func New(cacheHost string) *Cache { | ||
cache := &Cache{ | ||
cacheHost: cacheHost, | ||
} | ||
|
||
cache.twirpServer = gharesults.NewCacheServiceServer(cache) | ||
|
||
return cache | ||
} | ||
|
||
func (cache *Cache) PathPrefix() string { | ||
return cache.twirpServer.PathPrefix() | ||
} | ||
|
||
func (cache *Cache) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | ||
cache.twirpServer.ServeHTTP(writer, request) | ||
} | ||
|
||
func (cache *Cache) GetCacheEntryDownloadURL(ctx context.Context, request *gharesults.GetCacheEntryDownloadURLRequest) (*gharesults.GetCacheEntryDownloadURLResponse, error) { | ||
grpcRequest := &api.CacheInfoRequest{ | ||
TaskIdentification: client.CirrusTaskIdentification, | ||
CacheKey: httpCacheKey(request.Key, request.Version), | ||
CacheKeyPrefixes: lo.Map(request.RestoreKeys, func(restoreKey string, _ int) string { | ||
return httpCacheKey(restoreKey, request.Version) | ||
}), | ||
} | ||
|
||
grpcResponse, err := client.CirrusClient.CacheInfo(ctx, grpcRequest) | ||
if err != nil { | ||
if status, ok := status.FromError(err); ok && status.Code() == codes.NotFound { | ||
return nil, twirp.NewErrorf(twirp.NotFound, "cache entry not found") | ||
} | ||
|
||
return nil, twirp.NewErrorf(twirp.Internal, "GHA cache v2 failed to retrieve information "+ | ||
"about cache entry with key %q and version %q: %v", request.Key, request.Version, err) | ||
} | ||
|
||
return &gharesults.GetCacheEntryDownloadURLResponse{ | ||
Ok: true, | ||
SignedDownloadUrl: cache.httpCacheURL(grpcResponse.Info.Key), | ||
MatchedKey: strings.TrimPrefix(grpcResponse.Info.Key, httpCacheKey("", request.Version)), | ||
}, nil | ||
} | ||
|
||
func (cache *Cache) CreateCacheEntry(ctx context.Context, request *gharesults.CreateCacheEntryRequest) (*gharesults.CreateCacheEntryResponse, error) { | ||
grpcResponse, err := client.CirrusClient.GenerateCacheUploadURL(ctx, &api.CacheKey{ | ||
TaskIdentification: client.CirrusTaskIdentification, | ||
CacheKey: httpCacheKey(request.Key, request.Version), | ||
}) | ||
if err != nil { | ||
return nil, twirp.NewErrorf(twirp.Internal, "GHA cache v2 failed to create cache entry "+ | ||
"with key %q and version %q: %v", request.Key, request.Version, err) | ||
} | ||
|
||
return &gharesults.CreateCacheEntryResponse{ | ||
Ok: true, | ||
SignedUploadUrl: grpcResponse.Url, | ||
}, nil | ||
} | ||
|
||
func (cache *Cache) FinalizeCacheEntryUpload(ctx context.Context, request *gharesults.FinalizeCacheEntryUploadRequest) (*gharesults.FinalizeCacheEntryUploadResponse, error) { | ||
hash := fnv.New64a() | ||
|
||
_, _ = hash.Write([]byte(request.Key)) | ||
_, _ = hash.Write([]byte(fmt.Sprintf("%d", request.SizeBytes))) | ||
_, _ = hash.Write([]byte(request.Version)) | ||
|
||
return &gharesults.FinalizeCacheEntryUploadResponse{ | ||
Ok: true, | ||
EntryId: int64(hash.Sum64()), | ||
}, nil | ||
} | ||
|
||
func httpCacheKey(key string, version string) string { | ||
return fmt.Sprintf("%s-%s", url.PathEscape(version), url.PathEscape(key)) | ||
} | ||
|
||
func (cache *Cache) httpCacheURL(keyWithVersion string) string { | ||
return fmt.Sprintf("http://%s/%s", cache.cacheHost, url.PathEscape(keyWithVersion)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ghacachev2_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/cirruslabs/cirrus-cli/internal/agent/client" | ||
"github.com/cirruslabs/cirrus-cli/internal/agent/http_cache" | ||
"github.com/cirruslabs/cirrus-cli/internal/agent/http_cache/ghacache/cirruscimock" | ||
"github.com/cirruslabs/cirrus-cli/internal/testutil" | ||
"github.com/cirruslabs/cirrus-cli/pkg/api/gharesults" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/twitchtv/twirp" | ||
"io" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestGHACacheV2(t *testing.T) { | ||
testutil.NeedsContainerization(t) | ||
|
||
ctx := context.Background() | ||
|
||
client.InitClient(cirruscimock.ClientConn(t), "test", "test") | ||
|
||
httpCacheURL := "http://" + http_cache.Start() | ||
|
||
client := gharesults.NewCacheServiceJSONClient(httpCacheURL, &http.Client{}) | ||
|
||
cacheKey := uuid.NewString() | ||
cacheValue := []byte("Hello, World!\n") | ||
|
||
// Ensure that an entry for our cache key is not present | ||
_, err := client.GetCacheEntryDownloadURL(ctx, &gharesults.GetCacheEntryDownloadURLRequest{ | ||
Key: cacheKey, | ||
}) | ||
var twirpError twirp.Error | ||
require.ErrorAs(t, err, &twirpError) | ||
require.Equal(t, twirp.NotFound, twirpError.Code()) | ||
|
||
// Upload an entry for our cache key | ||
createCacheEntryRes, err := client.CreateCacheEntry(ctx, &gharesults.CreateCacheEntryRequest{ | ||
Key: cacheKey, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, createCacheEntryRes.Ok) | ||
|
||
uploadReq, err := http.NewRequest(http.MethodPut, createCacheEntryRes.SignedUploadUrl, bytes.NewReader(cacheValue)) | ||
require.NoError(t, err) | ||
|
||
uploadResp, err := http.DefaultClient.Do(uploadReq) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, uploadResp.StatusCode) | ||
|
||
// Ensure that an entry for our cache key is present | ||
// and matches to what we've previously put in the cache | ||
getCacheEntryDownloadURLResp, err := client.GetCacheEntryDownloadURL(ctx, &gharesults.GetCacheEntryDownloadURLRequest{ | ||
Key: cacheKey, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, getCacheEntryDownloadURLResp.Ok) | ||
require.Equal(t, cacheKey, getCacheEntryDownloadURLResp.MatchedKey) | ||
|
||
downloadResp, err := http.Get(getCacheEntryDownloadURLResp.SignedDownloadUrl) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, downloadResp.StatusCode) | ||
|
||
downloadRespBodyBytes, err := io.ReadAll(downloadResp.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, cacheValue, downloadRespBodyBytes) | ||
} |
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
Oops, something went wrong.