This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
/
async_splitter.go
841 lines (773 loc) · 24 KB
/
async_splitter.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
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
/*
* Copyright (C) 2017-2018 GIG Technology NV and Contributors
*
* 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 pipeline
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"github.com/threefoldtech/0-stor/client/datastor/pipeline/storage"
"github.com/threefoldtech/0-stor/client/metastor/metatypes"
"golang.org/x/sync/errgroup"
)
// NewAsyncSplitterPipeline creates a parallel pipeline,
// which chunks all the content as it reads, and, processes
// and stores them as multiple objects. It is guaranteed that content
// that is written using this pipeline, can be read back
// in the same order as it is written.
//
// NewAsyncSplitterPipeline requires a non-nil ChunkStorage and will panic if it is missing.
// It also requires chunkSize to be positive, if not, it will panic as well.
//
// If no ProcessorConstructor is given, a default constructor will be created for you,
// which will construct a processing.NopProcessor, effectively keeping the data unprocessed at all times.
// While the ProcessorConstructor is optional, it is recommended to define a valid constructor,
// as the storage of unprocessed data is both insecure and inefficient.
//
// If no HasherConstructor is given, a default constructor will be created for you,
// which will construct a 256-bit crypto hasher for you, producing checksums as keys.
// While the HasherConstructor is optional and the default one performs well,
// it is still recommended to define a valid constructor, as it will allow you
// to give a HasherConstructor which creates a crypto Hasher that
// produces signatures as keys, rather than checksums.
//
// If no jobCount is given, meaning it is 0 or less, DefaultJobCount will be used.
func NewAsyncSplitterPipeline(cs storage.ChunkStorage, chunkSize int, pc ProcessorConstructor, hc HasherConstructor, jobCount int) *AsyncSplitterPipeline {
if cs == nil {
panic("no ChunkStorage given")
}
if chunkSize <= 0 {
panic("chunk size has to be positive")
}
if pc == nil {
pc = DefaultProcessorConstructor
}
if hc == nil {
hc = DefaultHasherConstructor
}
if jobCount <= 0 {
jobCount = DefaultJobCount
}
storageJobCount := jobCount * 2
return &AsyncSplitterPipeline{
hasher: hc,
processor: pc,
storage: cs,
storageJobCount: storageJobCount,
processorJobCount: jobCount,
chunkSize: chunkSize,
}
}
// AsyncSplitterPipeline defines a parallel pipeline,
// which chunks all the content as it reads, and, processes
// and stores the read data as multiple objects.
// It is guaranteed that content that is written using this pipeline,
// can be read back in the same order as it is written.
type AsyncSplitterPipeline struct {
hasher HasherConstructor
processor ProcessorConstructor
storage storage.ChunkStorage
storageJobCount, processorJobCount int
chunkSize int
}
// Write implements Pipeline.Write
//
// The following graph visualizes the logic of this pipeline's Write method:
//
// +-------------------------------------------------------------------+
// | +----------+ |
// | | Splitter +---> Processor.Write + +-----------+ |
// | | + | ... +---> buf-chan +-------+ |
// | | Hasher +---> Processor.Write + +-----------+ | |
// | +----------+ | |
// | +-----------------+----------------------+ |
// | v v v |
// | Storage.Write Storage.Write ... Storage.Write |
// | + + + |
// | | | | |
// | | (ChunkMeta) | (ChunkMeta) | |
// | | | | |
// | +-------v-----------------v----------------------v-----+ |
// | | ordered [] metatypes.Chunk | |
// | +------------------------------------------------------+ |
// +-------------------------------------------------------------------+
//
// All channels are buffered, as to keep the pipeline as smooth as possible.
//
// The chunks are stored and returned in an ordered slice.
// The order respected and defined by the order in which
// the data that created those chunks was read from the input io.Reader.
//
// As soon as an error happens within any stage, at any point,
// the entire pipeline will be cancelled and that error is returned to the callee of this method.
func (asp *AsyncSplitterPipeline) Write(r io.Reader) ([]metatypes.Chunk, error) {
if r == nil {
return nil, errors.New("no reader given to read from")
}
group, ctx := errgroup.WithContext(context.Background())
// start the data splitter
inputCh, splitter := newAsyncDataSplitter(
ctx, r, asp.chunkSize, asp.processorJobCount)
group.Go(splitter)
// start all the processors,
// which will also create key, using the hasher
type indexedData struct {
Index int
Hash []byte
Data []byte
}
dataCh := make(chan indexedData)
processorGroup, _ := errgroup.WithContext(ctx)
for i := 0; i < asp.processorJobCount; i++ {
hasher, err := asp.hasher()
if err != nil {
return nil, err
}
processor, err := asp.processor()
if err != nil {
return nil, err
}
processorGroup.Go(func() error {
for input := range inputCh {
// generate the data's hash
hash := hasher.HashBytes(input.Data)
// process the data
data, err := processor.WriteProcess(input.Data)
if err != nil {
return err
}
// ensure to copy the data,
// in case the used processor is sharing
// the buffer between sequential write processes
if processor.SharedWriteBuffer() {
b := make([]byte, len(data))
copy(b, data)
data = b
}
select {
case dataCh <- indexedData{input.Index, hash, data}:
case <-ctx.Done():
return nil
}
}
return nil
})
}
group.Go(func() error {
err := processorGroup.Wait()
close(dataCh)
return err
})
// start all storage goroutines,
// which will generate and send all the metadata as part of their job
type indexedChunk struct {
Index int
Chunk metatypes.Chunk
}
chunkCh := make(chan indexedChunk)
storageGroup, _ := errgroup.WithContext(ctx)
for i := 0; i < asp.storageJobCount; i++ {
storageGroup.Go(func() error {
for data := range dataCh {
cfg, err := asp.storage.WriteChunk(data.Data)
if err != nil {
return err
}
chunk := metatypes.Chunk{
Size: cfg.Size,
Objects: cfg.Objects,
Hash: data.Hash,
}
select {
case chunkCh <- indexedChunk{data.Index, chunk}:
case <-ctx.Done():
return nil
}
}
return nil
})
}
group.Go(func() error {
err := storageGroup.Wait()
close(chunkCh)
return err
})
// collect all chunks, in the correct order
var (
chunks []metatypes.Chunk
chunkSize int
)
group.Go(func() error {
var (
receivedChunkCount int
bufferSize = asp.storageJobCount
)
chunks = make([]metatypes.Chunk, asp.storageJobCount)
// receive all chunks that are send by our storage goroutines
for chunk := range chunkCh {
// grow the buffer if needed
if chunk.Index >= bufferSize {
bufferSize = chunk.Index + (bufferSize * 2)
buf := make([]metatypes.Chunk, bufferSize)
copy(buf, chunks)
chunks = buf
}
// store our chunk and increase our received count
chunks[chunk.Index] = chunk.Chunk
receivedChunkCount++
// update our chunkSize if needed,
// this to know the final chunk length in the end
if chunk.Index >= chunkSize {
chunkSize = chunk.Index + 1
}
}
// ensure we have no gaps in our buffered chunk slice
if receivedChunkCount != chunkSize {
return errors.New("not all chunks were received")
}
return nil
})
// wait until all data has been
// read, chunked, processed and stored
err := group.Wait()
if err != nil {
return nil, err
}
// return all received chunks, and nothing more
return chunks[:chunkSize], nil
}
// Read implements Pipeline.Read
//
// The following graph visualizes the logic of this pipeline's Read method:
//
// +--------------------------------------------------------------------------------+
// | +--------------+ |
// | |[]*ChunkMeta +----> Storage.Read +-+ |
// | | to | | |
// | |chan ChunkMeta+----> Storage.Read +-+ +----------+ |
// | +----------+---+ +-----> channels +--------------+ |
// | | ... | +----+-----+ | |
// | | | | ... | |
// | +--------> Storage.Read +-+ | | |
// | +-------v--------+ +--------v-------+ |
// | | Processor.Read | | Processor.Read | |
// | | + | | + | |
// | | Hash/Data | | Hash/Data | |
// | | Validation | | Validation | |
// | +-------+--------+ +--------+-------+ |
// | | | |
// | | | |
// | +---v--------------------v---+ |
// | | | |
// | | Data Composer | |
// | io.Writer <-------+ (with internal buffer) | |
// | (input param) | | |
// | +----------------------------+ |
// +--------------------------------------------------------------------------------+
//
// The data composer (and its internal buffer) is used,
// to ensure we write the raw chunks in the correct order to the io.Writer.
//
// As soon as an error happens within any stage, at any point,
// the entire pipeline will be cancelled and that error is returned to the callee of this method.
//
// If however only one chunk is given, a temporary created SingleObjectPipeline will be used,
// to read the data using the Read method of that pipeline,
// as to now spawn an entire async pipeline, when only one chunk is to be read.
// See (*SingleObjectPipeline).Read for more information about the logic for this scenario.
func (asp *AsyncSplitterPipeline) Read(chunks []metatypes.Chunk, w io.Writer) error {
chunkLength := len(chunks)
if chunkLength == 0 {
return errors.New("no chunks given to read")
}
if chunkLength == 1 {
// if only one chunk has to be read,
// we can fall back on the simpler single-object-pipeline,
// which executes everything on a single goroutine in a blocking fashion.
sop := SingleObjectPipeline{
hasher: asp.hasher,
processor: asp.processor,
storage: asp.storage,
}
return sop.Read(chunks, w)
}
if w == nil {
return errors.New("no writer given to write to")
}
// limit our job count in case the chunk size is exceptionally low,
// as to not spawn goroutines that will never be used
storageJobCount, processorJobCount := asp.storageJobCount, asp.processorJobCount
if storageJobCount > chunkLength {
storageJobCount, processorJobCount = chunkLength, chunkLength
} else if processorJobCount > chunkLength {
processorJobCount = chunkLength
}
// the master group, which will spawn all non-grouped goroutines,
// and the close-goroutines for the (processor and storage) sub groups.
group, ctx := errgroup.WithContext(context.Background())
// send all chunks one by one,
// until all chunks have been send, or until the context is cancelled
type indexedChunk struct {
Index int
Chunk metatypes.Chunk
}
chunkCh := make(chan indexedChunk)
go func() {
defer close(chunkCh)
for index, chunk := range chunks {
select {
case chunkCh <- indexedChunk{index, chunk}:
case <-ctx.Done():
return
}
}
}()
// start all storage readers
// which will read all data until all data has been read,
// or until the context is cancelled
type indexedInput struct {
Index int
Data []byte
Hash []byte
}
storageGroup, _ := errgroup.WithContext(ctx)
inputCh := make(chan indexedInput)
for i := 0; i < storageJobCount; i++ {
storageGroup.Go(func() error {
for ic := range chunkCh {
data, err := asp.storage.ReadChunk(storage.ChunkConfig{
Size: ic.Chunk.Size,
Objects: ic.Chunk.Objects,
})
if err != nil {
return err
}
// send the object data and key, for further processing and validation
select {
case inputCh <- indexedInput{ic.Index, data, ic.Chunk.Hash}:
case <-ctx.Done():
return nil
}
}
return nil
})
}
group.Go(func() error {
err := storageGroup.Wait()
close(inputCh)
return err
})
// start all processors
// which will process all incoming objects,
// until:
// - all objects have been processed;
// - or an object has an invalid hash;
// - the context has been cancelled due to an error somewhere else;
type indexedData struct {
Index int
DataChunk []byte
}
processorGroup, _ := errgroup.WithContext(ctx)
outputCh := make(chan indexedData, processorJobCount)
for i := 0; i < processorJobCount; i++ {
hasher, err := asp.hasher()
if err != nil {
return err
}
processor, err := asp.processor()
if err != nil {
return err
}
processorGroup.Go(func() error {
for input := range inputCh {
data, err := processor.ReadProcess(input.Data)
if err != nil {
return fmt.Errorf("read pipeline failure: %v", err)
}
if bytes.Compare(input.Hash, hasher.HashBytes(data)) != 0 {
return fmt.Errorf("object chunk #%d's data and hash do not match", input.Index)
}
// ensure to copy the data,
// in case the used processor is sharing
// the buffer between sequential read processes
if processor.SharedReadBuffer() {
b := make([]byte, len(data))
copy(b, data)
data = b
}
result := indexedData{
Index: input.Index,
DataChunk: data,
}
select {
case outputCh <- result:
case <-ctx.Done():
return nil
}
}
return nil
})
}
group.Go(func() error {
err := processorGroup.Wait()
close(outputCh)
return err
})
// start the output goroutine,
// this one will write all output data,
// to the given writer, respecting the original order
// as defined by the input chunks.
group.Go(func() error {
var (
err error
ok bool
data []byte
expectedIndex int
buffer = make(map[int][]byte, processorJobCount)
)
for output := range outputCh {
if output.Index != expectedIndex {
// if this data is not the one we're expecting,
// buffer it for now
buffer[output.Index] = output.DataChunk
continue
}
// write the current data, as we expect it
// also write all buffered data that can be written,
// should it exist
data, ok = output.DataChunk, true
for ok {
_, err = w.Write(data)
if err != nil {
return err
}
expectedIndex++
data, ok = buffer[expectedIndex]
if ok {
delete(buffer, expectedIndex)
}
}
}
// we're done, success!
return nil
})
// wait for all goroutines to be finished
return group.Wait()
}
// Check implements Pipeline.Check
func (asp *AsyncSplitterPipeline) Check(chunks []metatypes.Chunk, fast bool) (storage.CheckStatus, error) {
chunkLength := len(chunks)
if chunkLength == 0 {
return storage.CheckStatus(0), errors.New("no chunks given to check")
}
if chunkLength == 1 {
// if only one chunk has to be checked,
// we can fall back on the simpler single-object-pipeline,
// which will simply check the single chunk we have
sop := SingleObjectPipeline{
hasher: asp.hasher,
processor: asp.processor,
storage: asp.storage,
}
return sop.Check(chunks, fast)
}
// limit our job count,
// in case we don't have that many chunks to check
jobCount := asp.storageJobCount
if jobCount > chunkLength {
jobCount = chunkLength
}
// create an errgroup for all our check jobs,
// and one for the master jobs
storageGroup, ctx := errgroup.WithContext(context.Background())
group, ctx := errgroup.WithContext(ctx)
// spawn our chunk fetcher
indexCh := make(chan int, jobCount)
go func() {
defer close(indexCh)
for i := range chunks {
select {
case indexCh <- i:
case <-ctx.Done():
return
}
}
}()
// returns whether the state is optimal or not,
// if not it means valid, but not optimal,
// invalid gets returned as an error
resultCh := make(chan bool, jobCount)
// spawn all our repair jobs
for i := 0; i < jobCount; i++ {
storageGroup.Go(func() error {
var (
err error
chunk *metatypes.Chunk
status storage.CheckStatus
)
for index := range indexCh {
chunk = &chunks[index]
status, err = asp.storage.CheckChunk(storage.ChunkConfig{
Size: chunk.Size,
Objects: chunk.Objects,
}, fast)
if err != nil {
return err
}
if status == storage.CheckStatusInvalid {
return errInvalidCheckStatus
}
select {
case resultCh <- (status == storage.CheckStatusOptimal):
case <-ctx.Done():
return nil
}
}
return nil
})
}
// spawn our result closer
group.Go(func() error {
err := storageGroup.Wait()
close(resultCh)
return err
})
// spawn our result fetcher
dataIsOptimal := true
group.Go(func() error {
for chunkIsOptimal := range resultCh {
dataIsOptimal = dataIsOptimal && chunkIsOptimal
}
return nil
})
// simply wait for all jobs to finish,
// afterward returns either the storage-originated error,
// or compute the data's current status based on the received information
err := group.Wait()
switch err {
case nil:
if dataIsOptimal {
return storage.CheckStatusOptimal, nil
}
return storage.CheckStatusValid, nil
case errInvalidCheckStatus:
return storage.CheckStatusInvalid, nil
default:
return storage.CheckStatus(0), err
}
}
var (
errInvalidCheckStatus = errors.New("invalid check status")
)
// Repair implements Pipeline.Repair
func (asp *AsyncSplitterPipeline) Repair(chunks []metatypes.Chunk) ([]metatypes.Chunk, error) {
chunkLength := len(chunks)
if chunkLength == 0 {
return nil, errors.New("no chunks given to repair")
}
if chunkLength == 1 {
// if only one chunk has to be repaired,
// we can fall back on the simpler single-object-pipeline,
// which will simply repair the single chunk we have
sop := SingleObjectPipeline{
hasher: asp.hasher,
processor: asp.processor,
storage: asp.storage,
}
return sop.Repair(chunks)
}
// limit our job count,
// in case we don't have that many chunks to repair
jobCount := asp.storageJobCount
if jobCount > chunkLength {
jobCount = chunkLength
}
// create an errgroup for all our repair jobs,
// and one for the master jobs
storageGroup, ctx := errgroup.WithContext(context.Background())
group, ctx := errgroup.WithContext(ctx)
// spawn our chunk fetcher
indexCh := make(chan int, jobCount)
go func() {
defer close(indexCh)
for i := range chunks {
select {
case indexCh <- i:
case <-ctx.Done():
return
}
}
}()
type repairResult struct {
Index int
Config storage.ChunkConfig
}
resultCh := make(chan repairResult, jobCount)
// spawn all our repair jobs
for i := 0; i < jobCount; i++ {
storageGroup.Go(func() error {
var (
err error
chunk *metatypes.Chunk
cfg *storage.ChunkConfig
)
for index := range indexCh {
chunk = &chunks[index]
cfg, err = asp.storage.RepairChunk(storage.ChunkConfig{
Size: chunk.Size,
Objects: chunk.Objects,
})
if err != nil {
return err
}
select {
case resultCh <- repairResult{index, *cfg}:
case <-ctx.Done():
return nil
}
}
return nil
})
}
// spawn our result closer
group.Go(func() error {
err := storageGroup.Wait()
close(resultCh)
return err
})
// spawn our result fetcher
outputChunks := make([]metatypes.Chunk, len(chunks))
group.Go(func() error {
for result := range resultCh {
outputChunks[result.Index] = metatypes.Chunk{
Size: result.Config.Size,
Objects: result.Config.Objects,
Hash: chunks[result.Index].Hash,
}
}
return nil
})
// simply wait for all jobs to finish,
// and return its (nil) error + the output chunks
err := group.Wait()
return outputChunks, err
}
// Delete implements Pipeline.Delete
func (asp *AsyncSplitterPipeline) Delete(chunks []metatypes.Chunk) error {
chunkLength := len(chunks)
if chunkLength == 0 {
return errors.New("no chunks given to delete")
}
if chunkLength == 1 {
// if only one chunk has to be deleted,
// we can fall back on the simpler single-object-pipeline,
// which will simply delete the single chunk we have
sop := SingleObjectPipeline{
hasher: asp.hasher,
processor: asp.processor,
storage: asp.storage,
}
return sop.Delete(chunks)
}
// limit our job count,
// in case we don't have that many chunks to delete
jobCount := asp.storageJobCount
if jobCount > chunkLength {
jobCount = chunkLength
}
// create an errgroup for all our delete jobs
group, ctx := errgroup.WithContext(context.Background())
// spawn our chunk fetcher
indexCh := make(chan int, jobCount)
go func() {
defer close(indexCh)
for i := range chunks {
select {
case indexCh <- i:
case <-ctx.Done():
return
}
}
}()
// spawn all our delete jobs
for i := 0; i < jobCount; i++ {
group.Go(func() error {
var (
err error
chunk *metatypes.Chunk
)
for index := range indexCh {
chunk = &chunks[index]
err = asp.storage.DeleteChunk(storage.ChunkConfig{
Size: chunk.Size,
Objects: chunk.Objects,
})
if err != nil {
return err
}
}
return nil
})
}
// simply wait for all jobs to finish,
// and return its (nil) error
return group.Wait()
}
// ChunkSize implements Pipeline.ChunkSize
func (asp *AsyncSplitterPipeline) ChunkSize() int {
return asp.chunkSize
}
// Close implements Pipeline.Close
func (asp *AsyncSplitterPipeline) Close() error {
return asp.storage.Close()
}
type indexedDataChunk struct {
Index int
Data []byte
}
// newAsyncDataSplitter creates a functional data splitter,
// which can be used to split streaming input data into fixed-sized chunks,
// in an asynchronous fashion.
func newAsyncDataSplitter(ctx context.Context, r io.Reader, chunkSize, _bufferSize int) (<-chan indexedDataChunk, func() error) {
inputCh := make(chan indexedDataChunk)
return inputCh, func() error {
defer close(inputCh)
var index int
buf := make([]byte, chunkSize)
for {
n, err := io.ReadFull(r, buf)
if n > 0 {
data := make([]byte, n)
copy(data, buf)
select {
case inputCh <- indexedDataChunk{index, data}:
index++
case <-ctx.Done():
return nil
}
}
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
// we'll consider an EOF
// as a signal to let us know the reader is exhausted
return nil
}
return err
}
}
}
}