forked from buildbarn/bb-storage
/
action_cache_blob_access.go
73 lines (62 loc) · 2.28 KB
/
action_cache_blob_access.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package blobstore
import (
"bytes"
"context"
"io"
"io/ioutil"
remoteexecution "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
"github.com/buildbarn/bb-storage/pkg/util"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type actionCacheBlobAccess struct {
actionCacheClient remoteexecution.ActionCacheClient
}
// NewActionCacheBlobAccess creates a BlobAccess handle that relays any
// requests to a GRPC service that implements the
// remoteexecution.ActionCache service. That is the service that Bazel
// uses to access action results stored in the Action Cache.
func NewActionCacheBlobAccess(client *grpc.ClientConn) BlobAccess {
return &actionCacheBlobAccess{
actionCacheClient: remoteexecution.NewActionCacheClient(client),
}
}
func (ba *actionCacheBlobAccess) Get(ctx context.Context, digest *util.Digest) (int64, io.ReadCloser, error) {
actionResult, err := ba.actionCacheClient.GetActionResult(ctx, &remoteexecution.GetActionResultRequest{
InstanceName: digest.GetInstance(),
ActionDigest: digest.GetPartialDigest(),
})
if err != nil {
return 0, nil, err
}
data, err := proto.Marshal(actionResult)
if err != nil {
return 0, nil, err
}
return int64(len(data)), ioutil.NopCloser(bytes.NewBuffer(data)), nil
}
func (ba *actionCacheBlobAccess) Put(ctx context.Context, digest *util.Digest, sizeBytes int64, r io.ReadCloser) error {
data, err := ioutil.ReadAll(r)
r.Close()
if err != nil {
return err
}
var actionResult remoteexecution.ActionResult
if err := proto.Unmarshal(data, &actionResult); err != nil {
return err
}
_, err = ba.actionCacheClient.UpdateActionResult(ctx, &remoteexecution.UpdateActionResultRequest{
InstanceName: digest.GetInstance(),
ActionDigest: digest.GetPartialDigest(),
ActionResult: &actionResult,
})
return err
}
func (ba *actionCacheBlobAccess) Delete(ctx context.Context, digest *util.Digest) error {
return status.Error(codes.Unimplemented, "Bazel remote execution protocol does not support object deletion")
}
func (ba *actionCacheBlobAccess) FindMissing(ctx context.Context, digests []*util.Digest) ([]*util.Digest, error) {
return nil, status.Error(codes.Unimplemented, "Bazel action cache does not support bulk existence checking")
}