| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"golang.org/x/sync/errgroup" |
| 6 |
"os" |
| 7 |
) |
| 8 |
|
| 9 |
// SeedSequencer is used to find sequences of chunks from seed files when assembling |
| 10 |
// a file from an index. Using seeds reduces the need to download and decompress chunks |
| 11 |
// from chunk stores. It also enables the use of reflinking/cloning of sections of |
| 12 |
// files from a seed file where supported to reduce disk usage. |
| 13 |
type SeedSequencer struct { |
| 14 |
seeds []Seed |
| 15 |
index Index |
| 16 |
current int |
| 17 |
} |
| 18 |
|
| 19 |
// SeedSegmentCandidate represent a single segment that we expect to use |
| 20 |
// in a Plan |
| 21 |
type SeedSegmentCandidate struct { |
| 22 |
seed Seed |
| 23 |
source SeedSegment |
| 24 |
indexSegment IndexSegment |
| 25 |
} |
| 26 |
|
| 27 |
type Plan []SeedSegmentCandidate |
| 28 |
|
| 29 |
var MockValidate = false |
| 30 |
|
| 31 |
// NewSeedSequencer initializes a new sequencer from a number of seeds. |
| 32 |
func NewSeedSequencer(idx Index, src ...Seed) *SeedSequencer { |
| 33 |
return &SeedSequencer{ |
| 34 |
seeds: src, |
| 35 |
index: idx, |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Plan returns a new possible plan, representing an ordered list of |
| 40 |
// segments that can be used to re-assemble the requested file |
| 41 |
func (r *SeedSequencer) Plan() (plan Plan) { |
| 42 |
for { |
| 43 |
seed, segment, source, done := r.Next() |
| 44 |
plan = append(plan, SeedSegmentCandidate{seed, source, segment}) |
| 45 |
if done { |
| 46 |
break |
| 47 |
} |
| 48 |
} |
| 49 |
return plan |
| 50 |
} |
| 51 |
|
| 52 |
// Next returns a sequence of index chunks (from the target index) and the |
| 53 |
// longest matching segment from one of the seeds. If source is nil, no |
| 54 |
// match was found in the seeds and the chunk needs to be retrieved from a |
| 55 |
// store. If done is true, the sequencer is complete. |
| 56 |
func (r *SeedSequencer) Next() (seed Seed, segment IndexSegment, source SeedSegment, done bool) { |
| 57 |
var ( |
| 58 |
max uint64 |
| 59 |
advance = 1 |
| 60 |
) |
| 61 |
for _, s := range r.seeds { |
| 62 |
n, m := s.LongestMatchWith(r.index.Chunks[r.current:]) |
| 63 |
if n > 0 && m.Size() > max { |
| 64 |
seed = s |
| 65 |
source = m |
| 66 |
advance = n |
| 67 |
max = m.Size() |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
segment = IndexSegment{index: r.index, first: r.current, last: r.current + advance - 1} |
| 72 |
r.current += advance |
| 73 |
return seed, segment, source, r.current >= len(r.index.Chunks) |
| 74 |
} |
| 75 |
|
| 76 |
// Rewind resets the current target index to the beginning. |
| 77 |
func (r *SeedSequencer) Rewind() { |
| 78 |
r.current = 0 |
| 79 |
} |
| 80 |
|
| 81 |
// isFileSeed returns true if this segment is pointing to a fileSeed |
| 82 |
func (s SeedSegmentCandidate) isFileSeed() bool { |
| 83 |
// We expect an empty filename when using nullSeeds |
| 84 |
return s.source != nil && s.source.FileName() != "" |
| 85 |
} |
| 86 |
|
| 87 |
// RegenerateInvalidSeeds regenerates the index to match the unexpected seed content |
| 88 |
func (r *SeedSequencer) RegenerateInvalidSeeds(ctx context.Context, n int, attempt int) error { |
| 89 |
seedNumber := 1 |
| 90 |
for _, s := range r.seeds { |
| 91 |
if s.IsInvalid() { |
| 92 |
if err := s.RegenerateIndex(ctx, n, attempt, seedNumber); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
seedNumber += 1 |
| 96 |
} |
| 97 |
} |
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
// Validate validates a proposed plan by checking if all the chosen chunks |
| 102 |
// are correctly provided from the seeds. In case a seed has invalid chunks, the |
| 103 |
// entire seed is marked as invalid and an error is returned. |
| 104 |
func (p Plan) Validate(ctx context.Context, n int, pb ProgressBar) (err error) { |
| 105 |
type Job struct { |
| 106 |
candidate SeedSegmentCandidate |
| 107 |
file *os.File |
| 108 |
} |
| 109 |
var ( |
| 110 |
in = make(chan Job) |
| 111 |
fileMap = make(map[string]*os.File) |
| 112 |
) |
| 113 |
if MockValidate { |
| 114 |
// This is used in the automated tests to mock a plan that is valid |
| 115 |
return nil |
| 116 |
} |
| 117 |
length := 0 |
| 118 |
for _, s := range p { |
| 119 |
if !s.isFileSeed() { |
| 120 |
continue |
| 121 |
} |
| 122 |
length += s.indexSegment.lengthChunks() |
| 123 |
} |
| 124 |
pb.SetTotal(length) |
| 125 |
pb.Start() |
| 126 |
defer pb.Finish() |
| 127 |
// Share a single file descriptor per seed for all the goroutines |
| 128 |
for _, s := range p { |
| 129 |
if !s.isFileSeed() { |
| 130 |
continue |
| 131 |
} |
| 132 |
name := s.source.FileName() |
| 133 |
if _, present := fileMap[name]; present { |
| 134 |
continue |
| 135 |
} else { |
| 136 |
file, err := os.Open(name) |
| 137 |
if err != nil { |
| 138 |
// We were not able to open the seed. Mark it as invalid and return |
| 139 |
s.seed.SetInvalid(true) |
| 140 |
return err |
| 141 |
} |
| 142 |
fileMap[name] = file |
| 143 |
defer file.Close() |
| 144 |
} |
| 145 |
} |
| 146 |
g, ctx := errgroup.WithContext(ctx) |
| 147 |
// Concurrently validate all the chunks in this plan |
| 148 |
for range n { |
| 149 |
g.Go(func() error { |
| 150 |
for job := range in { |
| 151 |
if err := job.candidate.source.Validate(job.file); err != nil { |
| 152 |
job.candidate.seed.SetInvalid(true) |
| 153 |
return err |
| 154 |
} |
| 155 |
pb.Add(job.candidate.indexSegment.lengthChunks()) |
| 156 |
} |
| 157 |
return nil |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
loop: |
| 162 |
for _, s := range p { |
| 163 |
if !s.isFileSeed() { |
| 164 |
// This is not a fileSeed, we have nothing to validate |
| 165 |
continue |
| 166 |
} |
| 167 |
select { |
| 168 |
case <-ctx.Done(): |
| 169 |
break loop |
| 170 |
case in <- Job{s, fileMap[s.source.FileName()]}: |
| 171 |
} |
| 172 |
} |
| 173 |
close(in) |
| 174 |
|
| 175 |
return g.Wait() |
| 176 |
} |