/
cas.go
762 lines (692 loc) · 23.7 KB
/
cas.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package fakes
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/chunker"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/client"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/digest"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/uploadinfo"
"github.com/google/uuid"
"github.com/klauspost/compress/zstd"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
// Redundant imports are required for the google3 mirror. Aliases should not be changed.
regrpc "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
repb "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
bsgrpc "google.golang.org/genproto/googleapis/bytestream"
bspb "google.golang.org/genproto/googleapis/bytestream"
)
var (
zstdEncoder, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true))
zstdDecoder, _ = zstd.NewReader(nil)
)
// Reader implements ByteStream's Read interface, returning one blob.
type Reader struct {
// Blob is the blob being read.
Blob []byte
// Chunks is a list of chunk sizes, in the order they are produced. The sum must be equal to the
// length of blob.
Chunks []int
// ExpectCompressed signals whether this writer should error on non-compressed blob calls.
ExpectCompressed bool
}
// Validate ensures that a Reader has the chunk sizes set correctly.
func (f *Reader) Validate(t *testing.T) {
t.Helper()
sum := 0
for _, c := range f.Chunks {
if c < 0 {
t.Errorf("Invalid chunk specification: chunk with negative size %d", c)
}
sum += c
}
if sum != len(f.Blob) {
t.Errorf("Invalid chunk specification: chunk sizes sum to %d but blob is length %d", sum, len(f.Blob))
}
}
// Read implements the corresponding RE API function.
func (f *Reader) Read(req *bspb.ReadRequest, stream bsgrpc.ByteStream_ReadServer) error {
path := strings.Split(req.ResourceName, "/")
if (len(path) != 4 && len(path) != 5) || path[0] != "instance" || (path[1] != "blobs" && path[1] != "compressed-blobs") {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
// indexOffset for all 2+ paths - `compressed-blobs` has one more URI element.
indexOffset := 0
if path[1] == "compressed-blobs" {
indexOffset = 1
}
dg := digest.NewFromBlob(f.Blob)
if path[2+indexOffset] != dg.Hash || path[3+indexOffset] != strconv.FormatInt(dg.Size, 10) {
return status.Errorf(codes.NotFound, "test fake only has blob with digest %s, but %s/%s was requested", dg, path[2+indexOffset], path[3+indexOffset])
}
offset := req.ReadOffset
limit := req.ReadLimit
blob := f.Blob
chunks := f.Chunks
if path[1] == "compressed-blobs" {
if !f.ExpectCompressed {
return status.Errorf(codes.FailedPrecondition, "fake expected a call with uncompressed bytes")
}
if path[2] != "zstd" {
return status.Error(codes.InvalidArgument, "test fake expected valid compressor, eg zstd")
}
blob = zstdEncoder.EncodeAll(blob[offset:], nil)
offset = 0
// For simplicity in coordinating test server & client, compressed blobs are returned as
// one chunk.
chunks = []int{len(blob)}
} else if f.ExpectCompressed {
return status.Errorf(codes.FailedPrecondition, "fake expected a call with compressed bytes")
}
for len(chunks) > 0 {
buf := blob[:chunks[0]]
if offset >= int64(len(buf)) {
offset -= int64(len(buf))
} else {
if offset > 0 {
buf = buf[offset:]
offset = 0
}
if limit > 0 {
if limit < int64(len(buf)) {
buf = buf[:limit]
}
limit -= int64(len(buf))
}
if err := stream.Send(&bspb.ReadResponse{Data: buf}); err != nil {
return err
}
if limit == 0 && req.ReadLimit != 0 {
break
}
}
blob = blob[chunks[0]:]
chunks = chunks[1:]
}
return nil
}
// Write implements the corresponding RE API function.
func (f *Reader) Write(bsgrpc.ByteStream_WriteServer) error {
return status.Error(codes.Unimplemented, "test fake does not implement method")
}
// QueryWriteStatus implements the corresponding RE API function.
func (f *Reader) QueryWriteStatus(context.Context, *bspb.QueryWriteStatusRequest) (*bspb.QueryWriteStatusResponse, error) {
return nil, status.Error(codes.Unimplemented, "test fake does not implement method")
}
// Writer expects to receive Write calls and fills the buffer.
type Writer struct {
// Buf is a buffer that is set to the contents of a Write call after one is received.
Buf []byte
// Err is a copy of the error returned by Write.
Err error
// ExpectCompressed signals whether this writer should error on non-compressed blob calls.
ExpectCompressed bool
}
// Write implements the corresponding RE API function.
func (f *Writer) Write(stream bsgrpc.ByteStream_WriteServer) (err error) {
// Store the error so we can verify that the client didn't drop the stream early, meaning the
// request won't error.
defer func() { f.Err = err }()
off := int64(0)
buf := new(bytes.Buffer)
req, err := stream.Recv()
if err == io.EOF {
return status.Error(codes.InvalidArgument, "no write request received")
}
if err != nil {
return err
}
path := strings.Split(req.ResourceName, "/")
if (len(path) != 6 && len(path) != 7) || path[0] != "instance" || path[1] != "uploads" || (path[3] != "blobs" && path[3] != "compressed-blobs") {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
// indexOffset for all 4+ paths - `compressed-blobs` paths have one more element.
indexOffset := 0
if path[3] == "compressed-blobs" {
indexOffset = 1
// TODO(rubensf): Change this to all the possible compressors in https://github.com/bazelbuild/remote-apis/pull/168.
if path[4] != "zstd" {
return status.Error(codes.InvalidArgument, "test fake expected valid compressor, eg zstd")
}
}
size, err := strconv.ParseInt(path[5+indexOffset], 10, 64)
if err != nil {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
dg, e := digest.New(path[4+indexOffset], size)
if e != nil {
return status.Error(codes.InvalidArgument, "test fake expected valid digest as part of resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
if _, err := uuid.Parse(path[2]); err != nil {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
res := req.ResourceName
done := false
for {
if req.ResourceName != res && req.ResourceName != "" {
return status.Errorf(codes.InvalidArgument, "follow-up request had resource name %q different from original %q", req.ResourceName, res)
}
if req.WriteOffset != off {
return status.Errorf(codes.InvalidArgument, "request had incorrect offset %d, expected %d", req.WriteOffset, off)
}
if done {
return status.Errorf(codes.InvalidArgument, "received write request after the client finished writing")
}
// 2 MB is the protocol max.
if len(req.Data) > 2*1024*1024 {
return status.Errorf(codes.InvalidArgument, "data chunk greater than 2MB")
}
// bytes.Buffer.Write can't error
_, _ = buf.Write(req.Data)
off += int64(len(req.Data))
if req.FinishWrite {
done = true
}
req, err = stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
}
if !done {
return status.Errorf(codes.InvalidArgument, "reached end of stream before the client finished writing")
}
if path[3] == "compressed-blobs" {
if !f.ExpectCompressed {
return status.Errorf(codes.FailedPrecondition, "fake expected a call with uncompressed bytes")
}
if path[4] != "zstd" {
return status.Errorf(codes.InvalidArgument, "%s compressor isn't supported", path[4])
}
f.Buf, err = zstdDecoder.DecodeAll(buf.Bytes(), nil)
if err != nil {
return status.Errorf(codes.InvalidArgument, "served bytes can't be decompressed: %v", err)
}
} else {
if f.ExpectCompressed {
return status.Errorf(codes.FailedPrecondition, "fake expected a call with compressed bytes")
}
f.Buf = buf.Bytes()
}
cDg := digest.NewFromBlob(f.Buf)
if dg != cDg {
return status.Errorf(codes.InvalidArgument, "mismatched digest: received %s, computed %s", dg, cDg)
}
return stream.SendAndClose(&bspb.WriteResponse{CommittedSize: dg.Size})
}
// Read implements the corresponding RE API function.
func (f *Writer) Read(*bspb.ReadRequest, bsgrpc.ByteStream_ReadServer) error {
return status.Error(codes.Unimplemented, "test fake does not implement method")
}
// QueryWriteStatus implements the corresponding RE API function.
func (f *Writer) QueryWriteStatus(context.Context, *bspb.QueryWriteStatusRequest) (*bspb.QueryWriteStatusResponse, error) {
return nil, status.Error(codes.Unimplemented, "test fake does not implement method")
}
// CAS is a fake CAS that implements FindMissingBlobs, Read and Write, storing stored blobs
// in a map. It also counts the number of requests to store received, for validating batching logic.
type CAS struct {
// Maximum batch byte size to verify requests against.
BatchSize int
ReqSleepDuration time.Duration
ReqSleepRandomize bool
PerDigestBlockFn map[digest.Digest]func()
blobs map[digest.Digest][]byte
reads map[digest.Digest]int
writes map[digest.Digest]int
missingReqs map[digest.Digest]int
mu sync.RWMutex
batchReqs int
writeReqs int
concReqs int
maxConcReqs int
}
// NewCAS returns a new empty fake CAS.
func NewCAS() *CAS {
c := &CAS{
BatchSize: client.DefaultMaxBatchSize,
PerDigestBlockFn: make(map[digest.Digest]func()),
}
c.Clear()
return c
}
// Clear removes all results from the cache.
func (f *CAS) Clear() {
f.mu.Lock()
defer f.mu.Unlock()
f.blobs = map[digest.Digest][]byte{
// For https://github.com/bazelbuild/remote-apis/blob/6345202a036a297b22b0a0e7531ef702d05f2130/build/bazel/remote/execution/v2/remote_execution.proto#L249
digest.Empty: {},
}
f.reads = make(map[digest.Digest]int)
f.writes = make(map[digest.Digest]int)
f.missingReqs = make(map[digest.Digest]int)
f.batchReqs = 0
f.writeReqs = 0
f.concReqs = 0
f.maxConcReqs = 0
}
// Put adds a given blob to the cache and returns its digest.
func (f *CAS) Put(blob []byte) digest.Digest {
f.mu.Lock()
defer f.mu.Unlock()
d := digest.NewFromBlob(blob)
f.blobs[d] = blob
return d
}
// Get returns the bytes corresponding to the given digest, and whether it was found.
func (f *CAS) Get(d digest.Digest) ([]byte, bool) {
f.mu.RLock()
defer f.mu.RUnlock()
res, ok := f.blobs[d]
return res, ok
}
// BlobReads returns the total number of read requests for a particular digest.
func (f *CAS) BlobReads(d digest.Digest) int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.reads[d]
}
// BlobWrites returns the total number of update requests for a particular digest.
func (f *CAS) BlobWrites(d digest.Digest) int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.writes[d]
}
// BlobMissingReqs returns the total number of GetMissingBlobs requests for a particular digest.
func (f *CAS) BlobMissingReqs(d digest.Digest) int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.missingReqs[d]
}
// BatchReqs returns the total number of BatchUpdateBlobs requests to this fake.
func (f *CAS) BatchReqs() int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.batchReqs
}
// WriteReqs returns the total number of Write requests to this fake.
func (f *CAS) WriteReqs() int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.writeReqs
}
// MaxConcurrency returns the maximum number of concurrent Write/Batch requests to this fake.
func (f *CAS) MaxConcurrency() int {
f.mu.RLock()
defer f.mu.RUnlock()
return f.maxConcReqs
}
// FindMissingBlobs implements the corresponding RE API function.
func (f *CAS) FindMissingBlobs(ctx context.Context, req *repb.FindMissingBlobsRequest) (*repb.FindMissingBlobsResponse, error) {
f.maybeSleep()
f.mu.Lock()
defer f.mu.Unlock()
if req.InstanceName != "instance" {
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
}
resp := new(repb.FindMissingBlobsResponse)
for _, dg := range req.BlobDigests {
d := digest.NewFromProtoUnvalidated(dg)
f.missingReqs[d]++
if _, ok := f.blobs[d]; !ok {
resp.MissingBlobDigests = append(resp.MissingBlobDigests, dg)
}
}
return resp, nil
}
func (f *CAS) maybeBlock(dg digest.Digest) {
if fn, ok := f.PerDigestBlockFn[dg]; ok {
fn()
}
}
func (f *CAS) maybeSleep() {
if f.ReqSleepDuration != 0 {
d := f.ReqSleepDuration
if f.ReqSleepRandomize {
d = time.Duration(rand.Float32()*float32(d.Microseconds())) * time.Microsecond
}
time.Sleep(d)
}
}
// BatchUpdateBlobs implements the corresponding RE API function.
func (f *CAS) BatchUpdateBlobs(ctx context.Context, req *repb.BatchUpdateBlobsRequest) (*repb.BatchUpdateBlobsResponse, error) {
f.maybeSleep()
f.mu.Lock()
f.batchReqs++
f.concReqs++
defer func() {
f.mu.Lock()
f.concReqs--
f.mu.Unlock()
}()
if f.concReqs > f.maxConcReqs {
f.maxConcReqs = f.concReqs
}
f.mu.Unlock()
if req.InstanceName != "instance" {
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
}
reqBlob, _ := proto.Marshal(req)
size := len(reqBlob)
if size > f.BatchSize {
return nil, status.Errorf(codes.InvalidArgument, "test fake received batch update for more than the maximum of %d bytes: %d bytes", f.BatchSize, size)
}
var resps []*repb.BatchUpdateBlobsResponse_Response
for _, r := range req.Requests {
if r.Compressor == repb.Compressor_ZSTD {
d, err := zstdDecoder.DecodeAll(r.Data, nil)
if err != nil {
resps = append(resps, &repb.BatchUpdateBlobsResponse_Response{
Digest: r.Digest,
Status: status.Newf(codes.InvalidArgument, "invalid blob: could not decompress: %s", err).Proto(),
})
continue
}
r.Data = d
}
dg := digest.NewFromBlob(r.Data)
rdg := digest.NewFromProtoUnvalidated(r.Digest)
if dg != rdg {
resps = append(resps, &repb.BatchUpdateBlobsResponse_Response{
Digest: r.Digest,
Status: status.Newf(codes.InvalidArgument, "Digest mismatch: digest of data was %s but digest of content was %s",
dg, rdg).Proto(),
})
continue
}
f.mu.Lock()
f.blobs[dg] = r.Data
f.writes[dg]++
f.mu.Unlock()
resps = append(resps, &repb.BatchUpdateBlobsResponse_Response{
Digest: r.Digest,
Status: status.New(codes.OK, "").Proto(),
})
}
return &repb.BatchUpdateBlobsResponse{Responses: resps}, nil
}
// BatchReadBlobs implements the corresponding RE API function.
func (f *CAS) BatchReadBlobs(ctx context.Context, req *repb.BatchReadBlobsRequest) (*repb.BatchReadBlobsResponse, error) {
f.maybeSleep()
f.mu.Lock()
f.batchReqs++
f.concReqs++
defer func() {
f.mu.Lock()
f.concReqs--
f.mu.Unlock()
}()
if f.concReqs > f.maxConcReqs {
f.maxConcReqs = f.concReqs
}
f.mu.Unlock()
if req.InstanceName != "instance" {
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
}
reqBlob, _ := proto.Marshal(req)
size := len(reqBlob)
if size > f.BatchSize {
return nil, status.Errorf(codes.InvalidArgument, "test fake received batch read for more than the maximum of %d bytes: %d bytes", f.BatchSize, size)
}
var resps []*repb.BatchReadBlobsResponse_Response
for _, dgPb := range req.Digests {
dg := digest.NewFromProtoUnvalidated(dgPb)
f.mu.Lock()
data, ok := f.blobs[dg]
f.mu.Unlock()
if !ok {
resps = append(resps, &repb.BatchReadBlobsResponse_Response{
Digest: dgPb,
Status: status.Newf(codes.NotFound, "digest %s was not found in the fake CAS", dg).Proto(),
})
continue
}
f.mu.Lock()
f.reads[dg]++
f.mu.Unlock()
useZSTDCompression := false
compressor := repb.Compressor_IDENTITY
for _, c := range req.AcceptableCompressors {
if c == repb.Compressor_ZSTD {
compressor = repb.Compressor_ZSTD
useZSTDCompression = true
break
}
}
if useZSTDCompression {
data = zstdEncoder.EncodeAll(data, nil)
}
resps = append(resps, &repb.BatchReadBlobsResponse_Response{
Digest: dgPb,
Status: status.New(codes.OK, "").Proto(),
Data: data,
Compressor: compressor,
})
}
return &repb.BatchReadBlobsResponse{Responses: resps}, nil
}
// GetTree implements the corresponding RE API function.
func (f *CAS) GetTree(req *repb.GetTreeRequest, stream regrpc.ContentAddressableStorage_GetTreeServer) error {
f.maybeSleep()
rootDigest, err := digest.NewFromProto(req.RootDigest)
if err != nil {
return fmt.Errorf("unable to parsse root digest %v", req.RootDigest)
}
blob, ok := f.Get(rootDigest)
if !ok {
return fmt.Errorf("root digest %v not found", rootDigest)
}
rootDir := &repb.Directory{}
proto.Unmarshal(blob, rootDir)
res := []*repb.Directory{}
queue := []*repb.Directory{rootDir}
for len(queue) > 0 {
ele := queue[0]
res = append(res, ele)
queue = queue[1:]
for _, dir := range ele.GetDirectories() {
fd, err := digest.NewFromProto(dir.GetDigest())
if err != nil {
return fmt.Errorf("unable to parse directory digest %v", dir.GetDigest())
}
blob, ok := f.Get(fd)
if !ok {
return fmt.Errorf("directory digest %v not found", fd)
}
directory := &repb.Directory{}
proto.Unmarshal(blob, directory)
queue = append(queue, directory)
}
}
resp := &repb.GetTreeResponse{
Directories: res,
}
return stream.Send(resp)
}
// Write implements the corresponding RE API function.
func (f *CAS) Write(stream bsgrpc.ByteStream_WriteServer) (err error) {
off := int64(0)
buf := new(bytes.Buffer)
req, err := stream.Recv()
if err == io.EOF {
return status.Error(codes.InvalidArgument, "no write request received")
}
if err != nil {
return err
}
path := strings.Split(req.ResourceName, "/")
if (len(path) != 6 && len(path) != 7) || path[0] != "instance" || path[1] != "uploads" || (path[3] != "blobs" && path[3] != "compressed-blobs") {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
// indexOffset for all 4+ paths - `compressed-blobs` paths have one more element.
indexOffset := 0
if path[3] == "compressed-blobs" {
indexOffset = 1
// TODO(rubensf): Change this to all the possible compressors in https://github.com/bazelbuild/remote-apis/pull/168.
if path[4] != "zstd" {
return status.Error(codes.InvalidArgument, "test fake expected valid compressor, eg zstd")
}
}
size, err := strconv.ParseInt(path[5+indexOffset], 10, 64)
if err != nil {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
dg, err := digest.New(path[4+indexOffset], size)
if err != nil {
return status.Error(codes.InvalidArgument, "test fake expected a valid digest as part of the resource name: \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
if _, err := uuid.Parse(path[2]); err != nil {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
f.maybeSleep()
f.maybeBlock(dg)
f.mu.Lock()
f.writeReqs++
f.concReqs++
defer func() {
f.mu.Lock()
f.concReqs--
f.mu.Unlock()
}()
if f.concReqs > f.maxConcReqs {
f.maxConcReqs = f.concReqs
}
f.mu.Unlock()
res := req.ResourceName
done := false
for {
if req.ResourceName != res && req.ResourceName != "" {
return status.Errorf(codes.InvalidArgument, "follow-up request had resource name %q different from original %q", req.ResourceName, res)
}
if req.WriteOffset != off {
return status.Errorf(codes.InvalidArgument, "request had incorrect offset %d, expected %d", req.WriteOffset, off)
}
if done {
return status.Errorf(codes.InvalidArgument, "received write request after the client finished writing")
}
// 2 MB is the protocol max.
if len(req.Data) > 2*1024*1024 {
return status.Errorf(codes.InvalidArgument, "data chunk greater than 2MB")
}
// bytes.Buffer.Write can't error
_, _ = buf.Write(req.Data)
off += int64(len(req.Data))
if req.FinishWrite {
done = true
}
req, err = stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
}
if !done {
return status.Errorf(codes.InvalidArgument, "reached end of stream before the client finished writing")
}
uncompressedBuf := buf.Bytes()
if path[3] == "compressed-blobs" {
if path[4] != "zstd" {
return status.Errorf(codes.InvalidArgument, "%s compressor isn't supported", path[4])
}
var err error
uncompressedBuf, err = zstdDecoder.DecodeAll(buf.Bytes(), nil)
if err != nil {
return status.Errorf(codes.InvalidArgument, "served bytes can't be decompressed: %v", err)
}
}
f.mu.Lock()
f.blobs[dg] = uncompressedBuf
f.writes[dg]++
f.mu.Unlock()
cDg := digest.NewFromBlob(uncompressedBuf)
if dg != cDg {
return status.Errorf(codes.InvalidArgument, "mismatched digest: received %s, computed %s", dg, cDg)
}
return stream.SendAndClose(&bspb.WriteResponse{CommittedSize: dg.Size})
}
// Read implements the corresponding RE API function.
func (f *CAS) Read(req *bspb.ReadRequest, stream bsgrpc.ByteStream_ReadServer) error {
if req.ReadOffset < 0 {
return status.Error(codes.InvalidArgument, "test fake expected a positive value for offset")
}
if req.ReadLimit != 0 {
return status.Error(codes.Unimplemented, "test fake does not implement limit")
}
path := strings.Split(req.ResourceName, "/")
if (len(path) != 4 && len(path) != 5) || path[0] != "instance" || (path[1] != "blobs" && path[1] != "compressed-blobs") {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
// indexOffset for all 2+ paths - `compressed-blobs` has one more URI element.
indexOffset := 0
if path[1] == "compressed-blobs" {
indexOffset = 1
}
size, err := strconv.Atoi(path[3+indexOffset])
if err != nil {
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
}
dg := digest.TestNew(path[2+indexOffset], int64(size))
f.maybeSleep()
f.maybeBlock(dg)
f.mu.Lock()
blob, ok := f.blobs[dg]
f.reads[dg]++
f.mu.Unlock()
if !ok {
return status.Errorf(codes.NotFound, "test fake missing blob with digest %s was requested", dg)
}
if path[1] == "compressed-blobs" {
if path[2] != "zstd" {
return status.Error(codes.InvalidArgument, "test fake expected valid compressor, eg zstd")
}
blob = zstdEncoder.EncodeAll(blob, nil)
}
ue := uploadinfo.EntryFromBlob(blob)
ch, err := chunker.New(ue, false, 2*1024*1024)
if err != nil {
return status.Errorf(codes.Internal, "test fake failed to create chunker: %v", err)
}
resp := &bspb.ReadResponse{}
var offset int64
for ch.HasNext() {
chunk, err := ch.Next()
if err != nil {
return err
}
// Seek to req.ReadOffset.
offset += int64(len(chunk.Data))
if offset < req.ReadOffset {
continue
}
// Scale the offset to the chunk.
offset = offset - req.ReadOffset // The chunk tail that we want.
offset = int64(len(chunk.Data)) - offset // The chunk head that we don't want.
if offset < 0 {
// The chunk is past the offset.
offset = 0
}
resp.Data = chunk.Data[int(offset):]
err = stream.Send(resp)
if err != nil {
return err
}
}
return nil
}
// QueryWriteStatus implements the corresponding RE API function.
func (f *CAS) QueryWriteStatus(context.Context, *bspb.QueryWriteStatusRequest) (*bspb.QueryWriteStatusResponse, error) {
return nil, status.Error(codes.Unimplemented, "test fake does not implement method")
}