-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_sharder.go
230 lines (200 loc) · 6.35 KB
/
file_sharder.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
// Package rsutils provides a hgih level API for https://github.com/klauspost/reedsolomon
package rsutils
import (
"crypto/sha256"
"fmt"
"hash"
"io"
"os"
"time"
"github.com/klauspost/reedsolomon"
)
// Encode reads an *os.File f, divides it into dataShards shards, and outputs parity shard data to parityWriters.
// It returns a Metadata object that contains information useful in reading or reconstructing the data again.
func Encode(f *os.File, dataShards int, parityWriters []io.Writer) (*Metadata, error) {
parityShards := len(parityWriters)
fstat, err := f.Stat()
if err != nil {
return nil, err
}
fsize := fstat.Size()
paddedChunks := SplitIntoPaddedChunks(f, fsize, dataShards)
hashers := make([]hash.Hash, dataShards+parityShards)
for i := range hashers {
hashers[i] = sha256.New()
}
hashingReaders := make([]io.Reader, dataShards)
for i := range paddedChunks {
hashingReaders[i] = io.TeeReader(paddedChunks[i], hashers[i])
}
hashingWriters := make([]io.Writer, parityShards)
for i := range hashingWriters {
hashingWriters[i] = io.MultiWriter(parityWriters[i], hashers[dataShards+i])
}
encoder, err := reedsolomon.NewStream(dataShards, parityShards)
if err != nil {
return nil, err
}
err = encoder.Encode(hashingReaders, hashingWriters)
if err != nil {
return nil, err
}
hashes := make([]string, dataShards+parityShards)
for i := range hashers {
hashes[i] = fmt.Sprintf("%x", hashers[i].Sum(nil))
}
return &Metadata{
Size: fsize,
Hashes: hashes,
DataShards: dataShards,
ParityShards: parityShards,
}, nil
}
type FileDecoder struct {
data *os.File
parityFiles []*os.File
md *Metadata
dataMTime time.Time
parityMTimes []time.Time
}
// Open accepts a data file, some parityFiles, and a Metadata object. It returns a
// FileDecoder object which can be used to Read the data back.
func Open(data *os.File, parityFiles []*os.File, md *Metadata) (*FileDecoder, error) {
if len(parityFiles) != md.ParityShards {
return nil, fmt.Errorf("Cannot open encoded files: need %d parity shards, got %d", md.ParityShards, len(parityFiles))
}
return &FileDecoder{
data: data,
parityFiles: parityFiles,
md: md,
dataMTime: time.Time{},
parityMTimes: make([]time.Time, md.ParityShards),
}, nil
}
type CorruptShard struct {
index int
hash string
}
func (f *FileDecoder) checkDataShardHealth() ([]*CorruptShard, error) {
var _stat os.FileInfo
corruptShards := make([]*CorruptShard, 0)
_stat, err := f.data.Stat()
if err != nil {
return nil, err
}
dataMTime := _stat.ModTime()
var checkDataShards bool = dataMTime != f.dataMTime
f.dataMTime = dataMTime
if checkDataShards {
for i, chunk := range SplitIntoPaddedChunks(f.data, f.md.Size, f.md.DataShards) {
hasher := sha256.New()
_, err := io.Copy(hasher, chunk)
if err != nil {
return nil, err
}
dShardHash := fmt.Sprintf("%x", hasher.Sum(nil))
desiredDShardHash := f.md.Hashes[i]
if dShardHash != desiredDShardHash {
corruptShards = append(corruptShards, &CorruptShard{index: i, hash: dShardHash})
}
}
}
return corruptShards, nil
}
func (f *FileDecoder) checkParityShardsHealth() ([]*CorruptShard, error) {
corruptShards := make([]*CorruptShard, 0)
modifiedParityShards := make(map[int]time.Time, 0)
for i := range f.parityFiles {
_stat, err := f.parityFiles[i].Stat()
if err != nil {
return nil, err
}
if parityMTime := _stat.ModTime(); parityMTime != f.parityMTimes[i] {
modifiedParityShards[i] = parityMTime
f.parityMTimes[i] = parityMTime
}
}
if len(modifiedParityShards) != 0 {
for i := range f.parityFiles {
hasher := sha256.New()
_, err := io.Copy(hasher, f.parityFiles[i])
defer f.parityFiles[i].Seek(0, os.SEEK_SET)
if err != nil {
return nil, err
}
parityShardIdx := f.md.DataShards + i
pShardHash := fmt.Sprintf("%x", hasher.Sum(nil))
desiredPShardHash := f.md.Hashes[parityShardIdx]
if pShardHash != desiredPShardHash {
corruptShards = append(corruptShards, &CorruptShard{index: parityShardIdx, hash: pShardHash})
}
}
}
return corruptShards, nil
}
func (f *FileDecoder) checkShardHealth() ([]*CorruptShard, error) {
corruptDataShards, err := f.checkDataShardHealth()
if err != nil {
return nil, err
}
corruptParityShards, err := f.checkParityShardsHealth()
if err != nil {
return nil, err
}
return append(corruptDataShards, corruptParityShards...), nil
}
func (f *FileDecoder) attemptRepair(corruptShards []*CorruptShard) error {
if len(corruptShards) > len(f.parityFiles) {
return fmt.Errorf("Cannot repair data: %d shards corrupt, only have %d parity shards", len(corruptShards), len(f.parityFiles))
}
paddedChunks := SplitIntoPaddedChunks(f.data, f.md.Size, f.md.DataShards)
shardCount := len(paddedChunks) + len(f.parityFiles)
shardReaders := make([]io.Reader, shardCount)
shardWriters := make([]io.Writer, shardCount)
for i := range paddedChunks {
shardReaders[i] = paddedChunks[i]
}
for i := range f.parityFiles {
shardReaders[f.md.DataShards+i] = f.parityFiles[i]
}
var corruptIdx int
for _, corruptShard := range corruptShards {
corruptIdx = corruptShard.index
shardReaders[corruptIdx] = nil
if corruptIdx < f.md.DataShards {
shardWriters[corruptIdx] = paddedChunks[corruptIdx]
} else {
shardWriters[corruptIdx] = f.parityFiles[corruptIdx-f.md.DataShards]
}
}
encoder, err := reedsolomon.NewStream(f.md.DataShards, f.md.ParityShards)
if err != nil {
return err
}
err = encoder.Reconstruct(shardReaders, shardWriters)
if err != nil {
return err
}
return nil
}
// Read attempts to read the Reed-Solomon-encoded data into []byte p.
// It will check the integrity of the data first and use file modified time to
// keep track whether it needs to check the integrity again in the case that the file
// changed between calling Open and FileEncoder.Read.
// If data or parity shards are corrupted, calling Read will trigger an attempt to
// repair the data. This will make the Read call take longer than when the data is
// not corrupted. It may fail if the corruption is too extensive.
// It returns the number of bytes read or an error.
func (f *FileDecoder) Read(p []byte) (int, error) {
corruptShards, err := f.checkShardHealth()
if err != nil {
return 0, err
}
if len(corruptShards) != 0 {
err := f.attemptRepair(corruptShards)
if err != nil {
return 0, err
}
}
return f.data.Read(p)
}