-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathgrpc_reader_multi_range.go
More file actions
1485 lines (1350 loc) · 38 KB
/
Copy pathgrpc_reader_multi_range.go
File metadata and controls
1485 lines (1350 loc) · 38 KB
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package storage
import (
"container/list"
"context"
"errors"
"fmt"
"io"
"log"
"sync"
"cloud.google.com/go/storage/internal/apiv2/storagepb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/mem"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
gax "github.com/googleapis/gax-go/v2"
)
const (
mrdCommandChannelSize = 1
mrdResponseChannelSize = 100
mrdSendChannelSize = 100
mrdAddStreamsChannelSize = 100
// This should never be hit in practice, but is a safety valve to prevent
// unbounded memory usage if the user is adding ranges faster than they
// can be processed.
mrdAddInternalQueueMaxSize = 50000
defaultTargetPendingBytes = 1 << 30 // 1 GiB
defaultTargetPendingRanges = 500
)
// --- internalMultiRangeDownloader Interface ---
// This provides an internal wrapper for the gRPC methods to avoid polluting
// reader.go with gRPC implementation details. The only implementation
// currently is for the gRPC transport with bidi APIs enabled. Creating
// a MultiRangeDownloader with any other client type will fail.
type internalMultiRangeDownloader interface {
add(output io.Writer, offset, length int64, callback func(int64, int64, error))
close(err error) error
wait()
getHandle() []byte
getPermanentError() error
getSpanCtx() context.Context
}
// streamPickerStrategy is an interface which each stream picker must implement.
type streamPickerStrategy interface {
pick(streams map[int]*mrdStream) int
}
// weightedPicker picks the stream with the minimum combined score of
// pending ranges and pending bytes, normalized by their respective targets.
type weightedPicker struct {
targetPendingRanges int
targetPendingBytes int
}
func (p *weightedPicker) pick(streams map[int]*mrdStream) int {
minScore := -1.0
returnID := -1
for id, stream := range streams {
if stream.reconnecting || stream.session == nil {
continue
}
// If the stream's request channel is full, skip it to avoid blocking the event loop.
// This ensures we only attempt a send if it can likely proceed immediately.
if len(stream.session.reqC) >= cap(stream.session.reqC) {
continue
}
// Calculate normalized score.
// Score = (PendingRanges / TargetRanges) + (PendingBytes / TargetBytes)
// Lower score is better (least loaded).
score := float64(stream.totalRanges)/float64(p.targetPendingRanges) +
float64(stream.totalRangeBytes)/float64(p.targetPendingBytes)
if returnID == -1 || score < minScore {
minScore = score
returnID = id
}
}
return returnID
}
// mrdStream holds all the relevant information of a single
// bidi stream.
type mrdStream struct {
id int
pendingRanges map[int64]*rangeRequest
session *bidiReadStreamSession
reconnecting bool
atCapacity bool
totalRanges int
totalRangeBytes int64
// statsRanges and statsRangeBytes help understand the
// distribution of ranges on different streams.
statsRanges uint
statsRangeBytes int64
}
func (s *mrdStream) updateCapacity(m *multiRangeDownloaderManager, deltaRanges int, deltaBytes int64) {
s.totalRanges = s.totalRanges + deltaRanges
m.pendingRangesCount += deltaRanges
s.totalRangeBytes += deltaBytes
wasAtCapacity := s.atCapacity
s.atCapacity = s.totalRanges >= m.params.targetPendingRanges || s.totalRangeBytes >= int64(m.params.targetPendingBytes)
if wasAtCapacity && !s.atCapacity {
m.atCapacityCount--
} else if !wasAtCapacity && s.atCapacity {
m.atCapacityCount++
}
}
// --- grpcStorageClient method ---
// Top level entry point into the MultiRangeDownloader via the storageClient interface.
func (c *grpcStorageClient) NewMultiRangeDownloader(ctx context.Context, params *newMultiRangeDownloaderParams, opts ...storageOption) (*MultiRangeDownloader, error) {
if !c.config.grpcBidiReads {
return nil, errors.New("storage: MultiRangeDownloader requires the experimental.WithGRPCBidiReads option")
}
s := callSettings(c.settings, opts...)
// Force the use of the custom codec to enable zero-copy reads.
s.gax = append(s.gax, gax.WithGRPCOptions(
grpc.ForceCodecV2(bytesCodecV2{}),
))
if s.userProject != "" {
ctx = setUserProjectMetadata(ctx, s.userProject)
}
if s.retry == nil {
s.retry = defaultRetry
}
params.defaults()
readSpec := makeBidiReadObjectSpec(params)
mCtx, cancel := context.WithCancel(ctx)
// Create the manager
manager := &multiRangeDownloaderManager{
ctx: mCtx,
cancel: cancel,
client: c,
settings: s,
params: params,
cmds: make(chan mrdCommand, mrdCommandChannelSize),
sessionResps: make(chan mrdSessionResult, mrdResponseChannelSize),
readIDCounter: 1,
readSpec: readSpec,
attrsReady: make(chan struct{}),
spanCtx: ctx,
streams: make(map[int]*mrdStream),
streamPicker: &weightedPicker{targetPendingRanges: params.targetPendingRanges, targetPendingBytes: params.targetPendingBytes},
unsentRequests: newRequestQueue(),
addStreams: make(chan mrdCommand, mrdAddStreamsChannelSize),
}
mrd := &MultiRangeDownloader{
impl: manager,
}
// Blocking call to establish the first session and get attributes.
initialStreamID := manager.streamIDCounter
manager.streamIDCounter++
manager.streams[initialStreamID] = &mrdStream{
id: initialStreamID,
pendingRanges: make(map[int64]*rangeRequest),
}
session, finalSpec, err := manager.createNewSession(initialStreamID, readSpec, true)
if err != nil {
manager.setPermanentError(err)
return nil, err
}
// Update the manager's readSpec with any changes (like routing token) from the first session.
manager.readSpec = finalSpec
manager.streams[initialStreamID].session = session
manager.wg.Add(1)
go func() {
defer manager.wg.Done()
manager.eventLoop()
}()
// Wait for attributes to be ready
select {
case <-manager.attrsReady:
if pErr := manager.getPermanentError(); pErr != nil {
cancel()
manager.wg.Wait()
return nil, pErr
}
if manager.attrs != nil {
mrd.Attrs = *manager.attrs
}
return mrd, nil
case <-ctx.Done():
cancel()
manager.wg.Wait()
return nil, ctx.Err()
}
}
func makeBidiReadObjectSpec(params *newMultiRangeDownloaderParams) *storagepb.BidiReadObjectSpec {
b := bucketResourceName(globalProjectAlias, params.bucket)
readSpec := &storagepb.BidiReadObjectSpec{
Bucket: b,
Object: params.object,
CommonObjectRequestParams: toProtoCommonObjectRequestParams(params.encryptionKey),
}
if params.gen >= 0 {
readSpec.Generation = params.gen
}
if params.handle != nil && len(*params.handle) > 0 {
readSpec.ReadHandle = &storagepb.BidiReadHandle{
Handle: *params.handle,
}
}
return readSpec
}
func (m *newMultiRangeDownloaderParams) defaults() {
if m.minConnections <= 0 {
m.minConnections = 1
}
if m.maxConnections < m.minConnections {
m.maxConnections = m.minConnections
}
if m.targetPendingRanges <= 0 {
m.targetPendingRanges = defaultTargetPendingRanges
}
if m.targetPendingBytes <= 0 {
m.targetPendingBytes = defaultTargetPendingBytes
}
}
// --- mrdCommand Interface and Implementations ---
// Used to pass commands from the user-facing code to the MRD manager.
// mrdCommand handlers are applied sequentially in the event loop. Therefore, it's okay
// for them to read/modify the manager state without concern for thread safety.
type mrdCommand interface {
apply(ctx context.Context, m *multiRangeDownloaderManager)
}
type mrdAddCmd struct {
output io.Writer
offset int64
length int64
callback func(int64, int64, error)
}
func (c *mrdAddCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.handleAddCmd(ctx, c)
}
type mrdCloseCmd struct {
err error
}
func (c *mrdCloseCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.handleCloseCmd(ctx, c)
}
type mrdWaitCmd struct {
doneC chan struct{}
}
func (c *mrdWaitCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.handleWaitCmd(ctx, c)
}
type mrdGetHandleCmd struct {
respC chan []byte
}
func (c *mrdGetHandleCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
select {
case <-m.attrsReady:
select {
case c.respC <- m.lastReadHandle:
case <-m.ctx.Done():
close(c.respC)
}
case <-m.ctx.Done():
close(c.respC)
}
}
type addStreamCmd struct {
id int
spec *storagepb.BidiReadObjectSpec
stream *mrdStream
}
func (c *addStreamCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.handleAddStreamCmd(ctx, c)
}
type reconnectStreamCmd struct {
id int
session *bidiReadStreamSession
spec *storagepb.BidiReadObjectSpec
err error
}
func (c *reconnectStreamCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.handleReconnectStreamCmd(ctx, c)
}
type mrdAddStreamErrorCmd struct {
err error
}
func (c *mrdAddStreamErrorCmd) apply(ctx context.Context, m *multiRangeDownloaderManager) {
m.streamCreating = false
if len(m.streams) == 0 {
var err error
if c.err != nil {
err = fmt.Errorf("no streams available. Last observed error: %w", c.err)
} else {
err = errors.New("no streams available")
}
m.failManager(err)
}
}
// --- mrdSessionResult ---
// This is used to pass the zero-copy decoded response from the recv stream
// back up to the multiRangeDownloadManager for processing, or to pass
// an error if the session failed.
type mrdSessionResult struct {
id int
decoder *readResponseDecoder
err error
session *bidiReadStreamSession
redirect *storagepb.BidiReadObjectRedirectedError
}
var (
errClosed = errors.New("downloader closed")
errNoStreams = errors.New("no streams available")
)
// --- multiRangeDownloaderManager ---
// Manages main event loop for MRD commands and processing responses.
// Spawns bidiStreamSession to deal with actual stream management, retries, etc.
type multiRangeDownloaderManager struct {
ctx context.Context
cancel context.CancelFunc
client *grpcStorageClient
settings *settings
params *newMultiRangeDownloaderParams
wg sync.WaitGroup // syncs completion of event loop.
cmds chan mrdCommand
sessionResps chan mrdSessionResult
// State
mu sync.Mutex
readIDCounter int64
permanentErr error
waiters []chan struct{}
readSpec *storagepb.BidiReadObjectSpec
lastReadHandle []byte
pendingRangesCount int
attrs *ReaderObjectAttrs
attrsReady chan struct{}
attrsOnce sync.Once
spanCtx context.Context
callbackWg sync.WaitGroup
streamCreating bool
streamPicker streamPickerStrategy
streamIDCounter int
streams map[int]*mrdStream
unsentRequests *requestQueue
addStreams chan mrdCommand
atCapacityCount int
}
type rangeRequest struct {
output io.Writer
offset int64
length int64
callback func(int64, int64, error)
origOffset int64
origLength int64
readID int64
bytesWritten int64
completed bool
}
// Methods implementing internalMultiRangeDownloader
func (m *multiRangeDownloaderManager) add(output io.Writer, offset, length int64, callback func(int64, int64, error)) {
if err := m.ctx.Err(); err != nil {
if pErr := m.getPermanentError(); pErr != nil {
err = pErr
}
m.runCallback(offset, length, err, callback)
return
}
if length < 0 {
m.runCallback(offset, length, fmt.Errorf("storage: MultiRangeDownloader.Add limit cannot be negative"), callback)
return
}
cmd := &mrdAddCmd{output: output, offset: offset, length: length, callback: callback}
select {
case m.cmds <- cmd:
case <-m.ctx.Done():
err := m.ctx.Err()
if pErr := m.getPermanentError(); pErr != nil {
err = pErr
}
m.runCallback(offset, length, err, callback)
}
}
func (m *multiRangeDownloaderManager) close(err error) error {
if m.ctx.Err() != nil {
m.wg.Wait()
if pErr := m.getPermanentError(); pErr != nil {
return pErr
}
return m.ctx.Err()
}
cmd := &mrdCloseCmd{err: err}
select {
case m.cmds <- cmd:
<-m.ctx.Done()
m.wg.Wait()
if pErr := m.getPermanentError(); pErr != nil && !errors.Is(pErr, errClosed) {
return pErr
}
return nil
case <-m.ctx.Done():
m.wg.Wait()
if m.getPermanentError() != nil {
return m.getPermanentError()
}
return m.ctx.Err()
}
}
func (m *multiRangeDownloaderManager) wait() {
if err := m.ctx.Err(); err != nil {
m.callbackWg.Wait()
return
}
doneC := make(chan struct{})
cmd := &mrdWaitCmd{doneC: doneC}
select {
case m.cmds <- cmd:
select {
case <-doneC:
m.callbackWg.Wait()
return
case <-m.ctx.Done():
m.callbackWg.Wait()
return
}
case <-m.ctx.Done():
m.callbackWg.Wait()
return
}
}
func (m *multiRangeDownloaderManager) getHandle() []byte {
select {
case <-m.attrsReady:
case <-m.ctx.Done():
return nil
}
if err := m.ctx.Err(); err != nil {
return nil
}
respC := make(chan []byte, 1)
cmd := &mrdGetHandleCmd{respC: respC}
select {
case m.cmds <- cmd:
select {
case h, ok := <-respC:
if !ok {
return nil
}
return h
case <-m.ctx.Done():
return nil
}
case <-m.ctx.Done():
return nil
}
}
func (m *multiRangeDownloaderManager) getPermanentError() error {
m.mu.Lock()
defer m.mu.Unlock()
return m.permanentErr
}
func (m *multiRangeDownloaderManager) getSpanCtx() context.Context {
return m.spanCtx
}
func (m *multiRangeDownloaderManager) runCallback(origOffset, numBytes int64, err error, cb func(int64, int64, error)) {
m.callbackWg.Add(1)
go func() {
defer m.callbackWg.Done()
cb(origOffset, numBytes, err)
}()
}
func (m *multiRangeDownloaderManager) getReqAndTargetStream(req *rangeRequest) (*storagepb.BidiReadObjectRequest, *mrdStream) {
streamID := m.streamPicker.pick(m.streams)
if streamID == -1 {
return nil, nil
}
stream := m.streams[streamID]
if stream == nil {
return nil, nil
}
protoReq := &storagepb.BidiReadObjectRequest{
ReadRanges: []*storagepb.ReadRange{{
ReadOffset: req.offset,
ReadLength: req.length,
ReadId: req.readID,
}},
}
return protoReq, stream
}
func (m *multiRangeDownloaderManager) eventLoop() {
defer m.cleanup()
for {
if m.ctx.Err() != nil {
return
}
var nextReq *storagepb.BidiReadObjectRequest
var nextRangeReq *rangeRequest
var targetStream *mrdStream
var targetChan chan *storagepb.BidiReadObjectRequest
// Only try to send if we have queued requests
if m.unsentRequests.Len() > 0 {
nextRangeReq = m.unsentRequests.Front()
if nextRangeReq != nil {
nextReq, targetStream = m.getReqAndTargetStream(nextRangeReq)
}
}
if targetStream != nil && targetStream.session != nil {
targetChan = targetStream.session.reqC
}
// Only read from cmds if we have space in the unsentRequests queue.
var cmdsChan chan mrdCommand
if m.unsentRequests.Len() < mrdAddInternalQueueMaxSize {
cmdsChan = m.cmds
}
select {
case <-m.ctx.Done():
return
// This path only triggers if space is available in the channel.
// It never blocks the eventLoop.
case targetChan <- nextReq:
targetStream.pendingRanges[nextRangeReq.readID] = nextRangeReq
targetStream.updateCapacity(m, 1, nextRangeReq.length)
targetStream.statsRanges++
targetStream.statsRangeBytes += nextRangeReq.length
m.unsentRequests.RemoveFront()
case cmd := <-m.addStreams:
cmd.apply(m.ctx, m)
case cmd := <-cmdsChan:
cmd.apply(m.ctx, m)
if _, ok := cmd.(*mrdCloseCmd); ok {
return
}
case result := <-m.sessionResps:
m.processSessionResult(result)
}
// Check if new stream has to be added.
if m.shouldAddStream() {
m.addNewStream()
}
// Notify waiters if all ranges are done.
if m.pendingRangesCount == 0 && m.unsentRequests.Len() == 0 {
for _, waiter := range m.waiters {
close(waiter)
}
m.waiters = nil
}
}
}
func (m *multiRangeDownloaderManager) cleanup() {
for id, stream := range m.streams {
if stream.session != nil {
stream.session.Shutdown()
}
delete(m.streams, id)
}
// Drain and free any remaining responses to prevent buffer leaks.
sessionDrainLoop:
for {
select {
case result, ok := <-m.sessionResps:
if !ok {
break sessionDrainLoop
}
if result.decoder != nil {
result.decoder.databufs.Free()
}
default:
break sessionDrainLoop
}
}
finalErr := m.getPermanentError()
if finalErr == nil {
if ctxErr := m.ctx.Err(); ctxErr != nil {
finalErr = ctxErr
}
}
if finalErr == nil {
finalErr = errClosed
}
m.failAllPending(finalErr)
for _, waiter := range m.waiters {
close(waiter)
}
m.attrsOnce.Do(func() { close(m.attrsReady) })
// Complete any commands leftover in cmds channel.
cmdDrainLoop:
for {
select {
case cmd, ok := <-m.cmds:
if !ok {
break cmdDrainLoop
}
// Parse type of command.
switch cmd := cmd.(type) {
case *mrdCloseCmd:
case *mrdWaitCmd:
close(cmd.doneC)
case *mrdAddCmd:
m.runCallback(cmd.offset, cmd.length, finalErr, cmd.callback)
case *mrdGetHandleCmd:
// Non-blocking send of handle if attributes are ready, otherwise close.
select {
case <-m.attrsReady:
select {
case cmd.respC <- m.lastReadHandle:
default:
close(cmd.respC)
}
default:
close(cmd.respC)
}
}
default:
break cmdDrainLoop
}
}
// Wait for all callbacks (including any initiated by the drained command) to finish.
m.callbackWg.Wait()
}
func (m *multiRangeDownloaderManager) addNewStream() {
m.streamCreating = true
id := int(m.streamIDCounter)
m.streamIDCounter++
// Clone the spec within the event loop.
clonedSpec := proto.Clone(m.readSpec).(*storagepb.BidiReadObjectSpec)
if m.ctx.Err() != nil {
m.streamCreating = false
return
}
go func(id int, readSpec *storagepb.BidiReadObjectSpec) {
newSession, newSpec, err := m.createNewSession(id, readSpec, false)
if err != nil || newSession == nil {
// If we can't create a stream, the handler checks the health of
// manager and then decides to kill the manager.
select {
case m.addStreams <- &mrdAddStreamErrorCmd{err: err}:
case <-m.ctx.Done():
if newSession != nil {
newSession.Shutdown()
}
return
}
return
}
select {
case m.addStreams <- &addStreamCmd{
id: id,
spec: newSpec,
stream: &mrdStream{
id: id,
session: newSession,
pendingRanges: make(map[int64]*rangeRequest),
},
}:
case <-m.ctx.Done():
newSession.Shutdown()
return
}
}(id, clonedSpec)
}
func (m *multiRangeDownloaderManager) createNewSession(id int, readSpec *storagepb.BidiReadObjectSpec, waitForResult bool) (*bidiReadStreamSession, *storagepb.BidiReadObjectSpec, error) {
retry := m.settings.retry
var firstResult mrdSessionResult
var newSession *bidiReadStreamSession
err := run(m.ctx, func(ctx context.Context) error {
if newSession != nil {
newSession.Shutdown()
newSession = nil
}
session, result := m.openAndInitializeSession(ctx, id, readSpec, waitForResult)
if result.err != nil {
if session != nil {
session.Shutdown()
}
if result.redirect != nil {
readSpec.RoutingToken = result.redirect.RoutingToken
readSpec.ReadHandle = result.redirect.ReadHandle
// We might get a redirect error here for an out-of-region request.
// Add the routing token and read handle to the request and do one
// retry.
session, result = m.openAndInitializeSession(ctx, id, readSpec, waitForResult)
if result.err != nil {
if session != nil {
session.Shutdown()
}
return result.err
}
} else {
// Not a redirect error, return to run()
return result.err
}
}
// Success
newSession = session
firstResult = result
return nil
}, retry, true, withOperation("ReadObject"), withBucket(m.params.bucket), withObject(m.params.object))
if err != nil {
return nil, nil, err
}
if waitForResult {
// Process the successful first result
m.processSessionResult(firstResult)
if pErr := m.getPermanentError(); pErr != nil {
return nil, nil, pErr
}
}
return newSession, readSpec, nil
}
func (m *multiRangeDownloaderManager) openAndInitializeSession(ctx context.Context, id int, spec *storagepb.BidiReadObjectSpec, waitForResult bool) (*bidiReadStreamSession, mrdSessionResult) {
session, err := newBidiReadStreamSession(m.ctx, id, m.sessionResps, m.client, m.settings, m.params, spec)
if err != nil {
return nil, mrdSessionResult{err: err}
}
if !waitForResult {
return session, mrdSessionResult{}
}
for {
select {
case result := <-m.sessionResps:
if result.session != session {
// Stale session result, free it if it has data.
if result.decoder != nil {
result.decoder.databufs.Free()
}
continue
}
return session, result
case <-ctx.Done():
session.Shutdown()
return nil, mrdSessionResult{err: ctx.Err()}
}
}
}
func (m *multiRangeDownloaderManager) handleAddCmd(ctx context.Context, cmd *mrdAddCmd) {
if pErr := m.getPermanentError(); pErr != nil {
m.runCallback(cmd.offset, cmd.length, pErr, cmd.callback)
return
}
req := &rangeRequest{
output: cmd.output,
offset: cmd.offset,
length: cmd.length,
origOffset: cmd.offset,
origLength: cmd.length,
callback: cmd.callback,
readID: m.readIDCounter,
}
m.readIDCounter++
// Convert to positive offset only if attributes are available.
if m.attrs != nil && req.offset < 0 {
err := m.convertToPositiveOffset(nil, req)
if err != nil {
return
}
}
if m.attrs != nil && req.length == 0 {
req.length = m.attrs.Size - req.offset
}
m.unsentRequests.PushBack(req)
}
func (m *multiRangeDownloaderManager) shouldAddStream() bool {
if m.ctx.Err() != nil ||
m.streamCreating ||
len(m.streams) >= m.params.maxConnections {
return false
}
if len(m.streams) < m.params.minConnections {
return true
}
// Beyond minConnections, we only add if all existing active streams are at capacity.
return m.atCapacityCount >= len(m.streams)
}
func (m *multiRangeDownloaderManager) convertToPositiveOffset(mrdStream *mrdStream, req *rangeRequest) error {
if req.offset >= 0 {
return nil
}
var objSize int64
if m.attrs != nil {
objSize = m.attrs.Size
}
if objSize <= 0 {
err := errors.New("storage: cannot resolve negative offset with object size as 0")
m.failRange(mrdStream, req, err)
return err
}
start := max(objSize+req.offset, 0)
req.offset = start
if req.length == 0 {
diff := (objSize - start)
req.length = objSize - start
if mrdStream != nil {
mrdStream.updateCapacity(m, 0, diff)
}
}
return nil
}
func (m *multiRangeDownloaderManager) handleCloseCmd(ctx context.Context, cmd *mrdCloseCmd) {
var err error
if cmd.err != nil {
err = cmd.err
} else {
err = errClosed
}
m.setPermanentError(err)
m.cancel()
}
func (m *multiRangeDownloaderManager) handleWaitCmd(ctx context.Context, cmd *mrdWaitCmd) {
// unsentRequests could be non-empty when eventLoop is busy
// in select statements other than Add commands and cleared up
// existing pending ranges.
if m.pendingRangesCount == 0 && m.unsentRequests.Len() == 0 {
close(cmd.doneC)
} else {
m.waiters = append(m.waiters, cmd.doneC)
}
}
func (m *multiRangeDownloaderManager) handleAddStreamCmd(ctx context.Context, cmd *addStreamCmd) {
// Check for any error in stream before adding this stream.
var streamErr error
if cmd.stream != nil && cmd.stream.session != nil {
streamErr = cmd.stream.session.getError()
}
if cmd.stream == nil ||
cmd.stream.session == nil ||
streamErr != nil {
m.streamCreating = false
if len(m.streams) == 0 {
err := streamErr
if err == nil {
err = errors.New("no streams available: stream creation failed or has error")
}
m.failManager(err)
}
return
}
m.streams[cmd.id] = cmd.stream
if cmd.spec != nil {
m.readSpec = cmd.spec
}
m.streamCreating = false
}
func (m *multiRangeDownloaderManager) handleReconnectStreamCmd(ctx context.Context, cmd *reconnectStreamCmd) {
stream, ok := m.streams[cmd.id]
if !ok || stream == nil {
// Stream might have been removed during shutdown.
if cmd.session != nil {
cmd.session.Shutdown()
}
return
}
stream.reconnecting = false
var streamErr error
if cmd.session != nil {
streamErr = cmd.session.getError()
}
if cmd.err != nil ||
cmd.session == nil ||
streamErr != nil {
finalErr := cmd.err
if finalErr == nil && cmd.session == nil {
finalErr = errors.New("session nil for reconnected stream")
} else if finalErr == nil {
finalErr = streamErr
}
m.failStream(stream, finalErr)
if len(m.streams) == 0 && !m.streamCreating {
err := fmt.Errorf("no streams available. Last observed error: %w", finalErr)
m.failManager(err)
}
return
}
stream.session = cmd.session
if cmd.spec != nil {
m.readSpec = cmd.spec
}
var rangesToResend []*storagepb.ReadRange
for _, req := range stream.pendingRanges {
if !req.completed {
readLength := req.length
if req.length > 0 {
readLength -= req.bytesWritten
}
if readLength < 0 {
readLength = 0
}
if req.length == 0 || readLength > 0 {
rangesToResend = append(rangesToResend, &storagepb.ReadRange{
ReadOffset: req.offset + req.bytesWritten,
ReadLength: readLength,
ReadId: req.readID,
})
}
}
}
if len(rangesToResend) > 0 {
retryReq := &storagepb.BidiReadObjectRequest{ReadRanges: rangesToResend}
stream.session.SendRequest(retryReq)
}
}
func (m *multiRangeDownloaderManager) processSessionResult(result mrdSessionResult) {
if result.decoder != nil {
defer result.decoder.databufs.Free()
}