-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_backend.go
310 lines (276 loc) · 6.49 KB
/
disk_backend.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
package main
import (
"crypto/sha1"
"errors"
"fmt"
"io"
"log"
"math/rand"
"os"
"path/filepath"
"syscall"
"time"
"github.com/thraxil/randwalk"
)
type diskBackend struct {
Root string
}
func newDiskBackend(root string) *diskBackend {
return &diskBackend{Root: root}
}
func (d diskBackend) String() string {
return "Disk"
}
func (d *diskBackend) Write(key key, r io.ReadCloser) error {
path := d.Root + key.Algorithm + "/" + key.AsPath()
log.Println(fmt.Sprintf("writing to %s\n", path))
err := os.MkdirAll(path, 0755)
if err != nil {
log.Println("couldn't make directory path")
log.Println(err)
return err
}
fullpath := path + "/data"
f, err := os.OpenFile(fullpath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Println("couldn't write file")
log.Println(err)
return err
}
defer f.Close()
_, err = io.Copy(f, r)
if err != nil {
log.Println("error copying data into file")
log.Println(err)
return err
}
return nil
}
func (d diskBackend) Read(key key) ([]byte, error) {
path := d.Root + key.Algorithm + "/" + key.AsPath() + "/data"
return os.ReadFile(path)
}
func (d diskBackend) Exists(key key) bool {
path := d.Root + key.Algorithm + "/" + key.AsPath() + "/data"
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func (d diskBackend) Delete(key key) error {
path := d.Root + key.Algorithm + "/" + key.AsPath()
return os.RemoveAll(path)
}
// the only File methods that we care about
// makes it easier to mock
type fileish interface {
IsDir() bool
Name() string
}
// part of the path that's not a directory or extension
func basename(path string) string {
filename := filepath.Base(path)
ext := filepath.Ext(filename)
return filename[:len(filename)-len(ext)]
}
func visitPreChecks(path string, f fileish, err error, c *cluster) (bool, error) {
if err != nil {
log.Printf("visit was handed an error: %s", err.Error())
return true, err
}
if c == nil {
log.Println("verifier.visit was given a nil cluster")
return true, errors.New("nil cluster")
}
if f.IsDir() {
return true, nil
}
if basename(path) != "data" {
return true, nil
}
return false, nil
}
type diskVerifier struct {
b diskBackend
c *cluster
chF chan func()
}
func (d diskBackend) NewVerifier(c *cluster) verifier {
v := &diskVerifier{
b: d,
c: c,
chF: make(chan func()),
}
go v.run()
return v
}
func (v *diskVerifier) run() {
for f := range v.chF {
f()
}
}
func (v *diskVerifier) Verify(path string, key key, h string) error {
r := make(chan error)
go func() {
v.chF <- func() {
r <- v.doVerify(path, key, h)
}
}()
return <-r
}
// does the same thing as Verify(), but given only the key
// so it is expected to get the path, compute the hash of the
// file and then do the verify.
func (v *diskVerifier) VerifyKey(key key) error {
r := make(chan error)
go func() {
v.chF <- func() {
path := v.b.Root + key.Algorithm + "/" + key.AsPath() + "/data"
h := sha1.New()
file, err := os.Open(path)
defer file.Close()
if err != nil {
log.Printf("error opening %s\n", path)
return
}
_, err = io.Copy(h, file)
if err != nil {
log.Printf("error copying %s\n", path)
return
}
hash := fmt.Sprintf("%x", h.Sum(nil))
r <- v.doVerify(path, key, hash)
}
}()
return <-r
}
func (v *diskVerifier) doVerify(path string, key key, h string) error {
if key.String() == "sha1:"+h {
return nil
}
log.Printf("corrupted file %s\n", path)
repaired, err := v.repairFile(path, key)
if err != nil {
log.Printf("error trying to repair file")
return err
}
if repaired {
log.Printf("successfully repaired file")
return nil
}
return errors.New("unrepairable file")
}
func (v *diskVerifier) repairFile(path string, key key) (bool, error) {
nodesToCheck := v.c.ReadOrder(key.String())
for _, n := range nodesToCheck {
if n.UUID == v.c.Myself.UUID {
continue
}
found, f, err := n.CheckFile(key, v.c.secret)
if found && err == nil {
err := v.replaceFile(path, f)
if err != nil {
log.Println("error replacing the file")
continue
}
return true, nil
}
}
return false, errors.New("no good copies found")
}
func (v *diskVerifier) replaceFile(path string, file []byte) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Println("couldn't open for writing")
f.Close()
return err
}
_, err = f.Write(file)
f.Close()
if err != nil {
log.Println("couldn't write file")
return err
}
return nil
}
func visit(path string, f os.FileInfo, err error, c *cluster, s site) error {
if aaeSkip < aaeOffset {
aaeSkip++
return nil
}
defer func() {
if r := recover(); r != nil {
log.Printf("Error in active anti-entropy.visit() [%s] %s\n", c.Myself.UUID, path)
log.Println(r)
}
}()
done, err := visitPreChecks(path, f, err, c)
if done {
return err
}
log.Printf("AAE visiting %s\n", path)
key, err := keyFromPath(path)
if err != nil {
log.Println("couldn't get key from path")
return nil
}
h := sha1.New()
file, err := os.Open(path)
defer file.Close()
if err != nil {
log.Printf("error opening %s\n", path)
return err
}
_, err = io.Copy(h, file)
if err != nil {
log.Printf("error copying %s\n", path)
return err
}
hash := fmt.Sprintf("%x", h.Sum(nil))
err = s.Verify(path, *key, hash)
if err != nil {
return err
}
err = s.Rebalance(*key)
if err != nil {
return err
}
// slow things down a little to keep server load down
var baseTime = 10
jitter := rand.Intn(5)
time.Sleep(time.Duration(baseTime+jitter) * time.Second)
return nil
}
func makeVisitor(fn func(string, os.FileInfo, error, *cluster, site) error,
c *cluster, s site) func(path string, f os.FileInfo, err error) error {
return func(path string, f os.FileInfo, err error) error {
return fn(path, f, err, c, s)
}
}
var aaeOffset = 0
var aaeSkip = 0
func (d diskBackend) ActiveAntiEntropy(cluster *cluster, site site, interval int) {
aaeOffset = rand.Intn(10000)
var jitter = 1
for {
_, err := os.ReadDir(d.Root)
if err != nil {
fmt.Printf("Can't get a directory listing for %s. Let's fail fast.\n", d.Root)
os.Exit(1)
}
if aaeSkip >= aaeOffset {
jitter = rand.Intn(5)
time.Sleep(time.Duration(interval+jitter) * time.Second)
log.Println("AAE starting at the top")
}
err = randwalk.Walk(d.Root, makeVisitor(visit, cluster, site))
if err != nil {
log.Printf("randwalk.Walk() returned %v\n", err)
}
}
}
func (d diskBackend) FreeSpace() uint64 {
var stat syscall.Statfs_t
syscall.Statfs(d.Root, &stat)
return stat.Bavail * uint64(stat.Bsize)
}