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

GHA cache: support and use cache key prefixes #783

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions api/cirrus_ci_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ message HeartbeatResponse {
message CacheInfoRequest {
TaskIdentification task_identification = 1;
string cache_key = 2;
repeated string cache_key_prefixes = 3;
}

message CacheInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"io"
"net"
"net/http"
"testing"
"time"
)
Expand Down Expand Up @@ -138,8 +139,8 @@ func (mock *cirrusCIMock) CacheInfo(ctx context.Context, request *api.CacheInfoR
Key: aws.String(request.CacheKey),
})
if err != nil {
var aerr awserr.Error
if errors.As(err, &aerr) && aerr.Code() == s3.ErrCodeNoSuchKey {
var requestFailure awserr.RequestFailure
if errors.As(err, &requestFailure) && requestFailure.StatusCode() == http.StatusNotFound {
return nil, status.Errorf(codes.NotFound, "cache entry for key %s is not found",
request.CacheKey)
}
Expand Down
52 changes: 32 additions & 20 deletions internal/agent/http_cache/ghacache/ghacache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/cirruslabs/cirrus-cli/pkg/api"
"github.com/go-chi/render"
"github.com/puzpuzpuz/xsync/v3"
"github.com/samber/lo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"math"
"math/rand"
Expand Down Expand Up @@ -55,33 +58,42 @@ func (cache *GHACache) get(writer http.ResponseWriter, request *http.Request) {
keys := strings.Split(request.URL.Query().Get("keys"), ",")
version := request.URL.Query().Get("version")

for _, key := range keys {
httpCacheURL := cache.httpCacheURL(key, version)
keysWithVersions := lo.Map(keys, func(key string, _ int) string {
return httpCacheKey(key, version)
})

grpcRequest := &api.CacheInfoRequest{
TaskIdentification: client.CirrusTaskIdentification,
CacheKey: keysWithVersions[0],
}

resp, err := http.Head(httpCacheURL)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"retrieve %q: %v", httpCacheURL, err)
if len(keysWithVersions) > 1 {
grpcRequest.CacheKeyPrefixes = keysWithVersions[1:]
}

grpcResponse, err := client.CirrusClient.CacheInfo(request.Context(), grpcRequest)
if err != nil {
if status, ok := status.FromError(err); ok && status.Code() == codes.NotFound {
writer.WriteHeader(http.StatusNoContent)

return
}

if resp.StatusCode == http.StatusOK {
jsonResp := struct {
Key string `json:"cacheKey"`
URL string `json:"archiveLocation"`
}{
Key: key,
URL: httpCacheURL,
}
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"retrieve information about cache key %q: %v", keys[0], err)

render.JSON(writer, request, &jsonResp)
return
}

return
}
jsonResp := struct {
Key string `json:"cacheKey"`
URL string `json:"archiveLocation"`
}{
Key: strings.TrimSuffix(grpcResponse.Info.Key, "-"+version),
URL: cache.httpCacheURL(grpcResponse.Info.Key),
}

writer.WriteHeader(http.StatusNoContent)
render.JSON(writer, request, &jsonResp)
}

func (cache *GHACache) reserveUploadable(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -279,8 +291,8 @@ func httpCacheKey(key string, version string) string {
return fmt.Sprintf("%s-%s", url.PathEscape(key), url.PathEscape(version))
}

func (cache *GHACache) httpCacheURL(key string, version string) string {
return fmt.Sprintf("http://%s/%s", cache.cacheHost, httpCacheKey(key, version))
func (cache *GHACache) httpCacheURL(keyWithVersion string) string {
return fmt.Sprintf("http://%s/%s", cache.cacheHost, keyWithVersion)
}

func getID(request *http.Request) (int64, bool) {
Expand Down
Loading