7286: Add comments clarifying arvados_node_missing() and broken(). Also bump
[arvados.git] / services / keepstore / volume_unix.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "log"
9         "os"
10         "path/filepath"
11         "regexp"
12         "strconv"
13         "strings"
14         "sync"
15         "syscall"
16         "time"
17 )
18
19 // A UnixVolume stores and retrieves blocks in a local directory.
20 type UnixVolume struct {
21         // path to the volume's root directory
22         root string
23         // something to lock during IO, typically a sync.Mutex (or nil
24         // to skip locking)
25         locker   sync.Locker
26         readonly bool
27 }
28
29 // Touch sets the timestamp for the given locator to the current time
30 func (v *UnixVolume) Touch(loc string) error {
31         if v.readonly {
32                 return MethodDisabledError
33         }
34         p := v.blockPath(loc)
35         f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
36         if err != nil {
37                 return err
38         }
39         defer f.Close()
40         if v.locker != nil {
41                 v.locker.Lock()
42                 defer v.locker.Unlock()
43         }
44         if e := lockfile(f); e != nil {
45                 return e
46         }
47         defer unlockfile(f)
48         now := time.Now().Unix()
49         utime := syscall.Utimbuf{now, now}
50         return syscall.Utime(p, &utime)
51 }
52
53 // Mtime returns the stored timestamp for the given locator.
54 func (v *UnixVolume) Mtime(loc string) (time.Time, error) {
55         p := v.blockPath(loc)
56         fi, err := os.Stat(p)
57         if err != nil {
58                 return time.Time{}, err
59         }
60         return fi.ModTime(), nil
61 }
62
63 // Lock the locker (if one is in use), open the file for reading, and
64 // call the given function if and when the file is ready to read.
65 func (v *UnixVolume) getFunc(path string, fn func(io.Reader) error) error {
66         if v.locker != nil {
67                 v.locker.Lock()
68                 defer v.locker.Unlock()
69         }
70         f, err := os.Open(path)
71         if err != nil {
72                 return err
73         }
74         defer f.Close()
75         return fn(f)
76 }
77
78 // stat is os.Stat() with some extra sanity checks.
79 func (v *UnixVolume) stat(path string) (os.FileInfo, error) {
80         stat, err := os.Stat(path)
81         if err == nil {
82                 if stat.Size() < 0 {
83                         err = os.ErrInvalid
84                 } else if stat.Size() > BlockSize {
85                         err = TooLongError
86                 }
87         }
88         return stat, err
89 }
90
91 // Get retrieves a block identified by the locator string "loc", and
92 // returns its contents as a byte slice.
93 //
94 // Get returns a nil buffer IFF it returns a non-nil error.
95 func (v *UnixVolume) Get(loc string) ([]byte, error) {
96         path := v.blockPath(loc)
97         stat, err := v.stat(path)
98         if err != nil {
99                 return nil, err
100         }
101         buf := bufs.Get(int(stat.Size()))
102         err = v.getFunc(path, func(rdr io.Reader) error {
103                 _, err = io.ReadFull(rdr, buf)
104                 return err
105         })
106         if err != nil {
107                 bufs.Put(buf)
108                 return nil, err
109         }
110         return buf, nil
111 }
112
113 // Compare returns nil if Get(loc) would return the same content as
114 // expect. It is functionally equivalent to Get() followed by
115 // bytes.Compare(), but uses less memory.
116 func (v *UnixVolume) Compare(loc string, expect []byte) error {
117         path := v.blockPath(loc)
118         stat, err := v.stat(path)
119         if err != nil {
120                 return err
121         }
122         bufLen := 1 << 20
123         if int64(bufLen) > stat.Size() {
124                 bufLen = int(stat.Size())
125                 if bufLen < 1 {
126                         // len(buf)==0 would prevent us from handling
127                         // empty files the same way as non-empty
128                         // files, because reading 0 bytes at a time
129                         // never reaches EOF.
130                         bufLen = 1
131                 }
132         }
133         cmp := expect
134         buf := make([]byte, bufLen)
135         return v.getFunc(path, func(rdr io.Reader) error {
136                 // Loop invariants: all data read so far matched what
137                 // we expected, and the first N bytes of cmp are
138                 // expected to equal the next N bytes read from
139                 // reader.
140                 for {
141                         n, err := rdr.Read(buf)
142                         if n > len(cmp) || bytes.Compare(cmp[:n], buf[:n]) != 0 {
143                                 return collisionOrCorrupt(loc[:32], expect[:len(expect)-len(cmp)], buf[:n], rdr)
144                         }
145                         cmp = cmp[n:]
146                         if err == io.EOF {
147                                 if len(cmp) != 0 {
148                                         return collisionOrCorrupt(loc[:32], expect[:len(expect)-len(cmp)], nil, nil)
149                                 }
150                                 return nil
151                         } else if err != nil {
152                                 return err
153                         }
154                 }
155         })
156 }
157
158 // Put stores a block of data identified by the locator string
159 // "loc".  It returns nil on success.  If the volume is full, it
160 // returns a FullError.  If the write fails due to some other error,
161 // that error is returned.
162 func (v *UnixVolume) Put(loc string, block []byte) error {
163         if v.readonly {
164                 return MethodDisabledError
165         }
166         if v.IsFull() {
167                 return FullError
168         }
169         bdir := v.blockDir(loc)
170         if err := os.MkdirAll(bdir, 0755); err != nil {
171                 log.Printf("%s: could not create directory %s: %s",
172                         loc, bdir, err)
173                 return err
174         }
175
176         tmpfile, tmperr := ioutil.TempFile(bdir, "tmp"+loc)
177         if tmperr != nil {
178                 log.Printf("ioutil.TempFile(%s, tmp%s): %s", bdir, loc, tmperr)
179                 return tmperr
180         }
181         bpath := v.blockPath(loc)
182
183         if v.locker != nil {
184                 v.locker.Lock()
185                 defer v.locker.Unlock()
186         }
187         if _, err := tmpfile.Write(block); err != nil {
188                 log.Printf("%s: writing to %s: %s\n", v, bpath, err)
189                 tmpfile.Close()
190                 os.Remove(tmpfile.Name())
191                 return err
192         }
193         if err := tmpfile.Close(); err != nil {
194                 log.Printf("closing %s: %s\n", tmpfile.Name(), err)
195                 os.Remove(tmpfile.Name())
196                 return err
197         }
198         if err := os.Rename(tmpfile.Name(), bpath); err != nil {
199                 log.Printf("rename %s %s: %s\n", tmpfile.Name(), bpath, err)
200                 os.Remove(tmpfile.Name())
201                 return err
202         }
203         return nil
204 }
205
206 // Status returns a VolumeStatus struct describing the volume's
207 // current state, or nil if an error occurs.
208 //
209 func (v *UnixVolume) Status() *VolumeStatus {
210         var fs syscall.Statfs_t
211         var devnum uint64
212
213         if fi, err := os.Stat(v.root); err == nil {
214                 devnum = fi.Sys().(*syscall.Stat_t).Dev
215         } else {
216                 log.Printf("%s: os.Stat: %s\n", v, err)
217                 return nil
218         }
219
220         err := syscall.Statfs(v.root, &fs)
221         if err != nil {
222                 log.Printf("%s: statfs: %s\n", v, err)
223                 return nil
224         }
225         // These calculations match the way df calculates disk usage:
226         // "free" space is measured by fs.Bavail, but "used" space
227         // uses fs.Blocks - fs.Bfree.
228         free := fs.Bavail * uint64(fs.Bsize)
229         used := (fs.Blocks - fs.Bfree) * uint64(fs.Bsize)
230         return &VolumeStatus{v.root, devnum, free, used}
231 }
232
233 var blockDirRe = regexp.MustCompile(`^[0-9a-f]+$`)
234
235 // IndexTo writes (to the given Writer) a list of blocks found on this
236 // volume which begin with the specified prefix. If the prefix is an
237 // empty string, IndexTo writes a complete list of blocks.
238 //
239 // Each block is given in the format
240 //
241 //     locator+size modification-time {newline}
242 //
243 // e.g.:
244 //
245 //     e4df392f86be161ca6ed3773a962b8f3+67108864 1388894303
246 //     e4d41e6fd68460e0e3fc18cc746959d2+67108864 1377796043
247 //     e4de7a2810f5554cd39b36d8ddb132ff+67108864 1388701136
248 //
249 func (v *UnixVolume) IndexTo(prefix string, w io.Writer) error {
250         var lastErr error = nil
251         rootdir, err := os.Open(v.root)
252         if err != nil {
253                 return err
254         }
255         defer rootdir.Close()
256         for {
257                 names, err := rootdir.Readdirnames(1)
258                 if err == io.EOF {
259                         return lastErr
260                 } else if err != nil {
261                         return err
262                 }
263                 if !strings.HasPrefix(names[0], prefix) && !strings.HasPrefix(prefix, names[0]) {
264                         // prefix excludes all blocks stored in this dir
265                         continue
266                 }
267                 if !blockDirRe.MatchString(names[0]) {
268                         continue
269                 }
270                 blockdirpath := filepath.Join(v.root, names[0])
271                 blockdir, err := os.Open(blockdirpath)
272                 if err != nil {
273                         log.Print("Error reading ", blockdirpath, ": ", err)
274                         lastErr = err
275                         continue
276                 }
277                 for {
278                         fileInfo, err := blockdir.Readdir(1)
279                         if err == io.EOF {
280                                 break
281                         } else if err != nil {
282                                 log.Print("Error reading ", blockdirpath, ": ", err)
283                                 lastErr = err
284                                 break
285                         }
286                         name := fileInfo[0].Name()
287                         if !strings.HasPrefix(name, prefix) {
288                                 continue
289                         }
290                         _, err = fmt.Fprint(w,
291                                 name,
292                                 "+", fileInfo[0].Size(),
293                                 " ", fileInfo[0].ModTime().Unix(),
294                                 "\n")
295                 }
296                 blockdir.Close()
297         }
298 }
299
300 // Delete deletes the block data from the unix storage
301 func (v *UnixVolume) Delete(loc string) error {
302         // Touch() must be called before calling Write() on a block.  Touch()
303         // also uses lockfile().  This avoids a race condition between Write()
304         // and Delete() because either (a) the file will be deleted and Touch()
305         // will signal to the caller that the file is not present (and needs to
306         // be re-written), or (b) Touch() will update the file's timestamp and
307         // Delete() will read the correct up-to-date timestamp and choose not to
308         // delete the file.
309
310         if v.readonly {
311                 return MethodDisabledError
312         }
313         if v.locker != nil {
314                 v.locker.Lock()
315                 defer v.locker.Unlock()
316         }
317         p := v.blockPath(loc)
318         f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
319         if err != nil {
320                 return err
321         }
322         defer f.Close()
323         if e := lockfile(f); e != nil {
324                 return e
325         }
326         defer unlockfile(f)
327
328         // If the block has been PUT in the last blobSignatureTTL
329         // seconds, return success without removing the block. This
330         // protects data from garbage collection until it is no longer
331         // possible for clients to retrieve the unreferenced blocks
332         // anyway (because the permission signatures have expired).
333         if fi, err := os.Stat(p); err != nil {
334                 return err
335         } else {
336                 if time.Since(fi.ModTime()) < blobSignatureTTL {
337                         return nil
338                 }
339         }
340         return os.Remove(p)
341 }
342
343 // blockDir returns the fully qualified directory name for the directory
344 // where loc is (or would be) stored on this volume.
345 func (v *UnixVolume) blockDir(loc string) string {
346         return filepath.Join(v.root, loc[0:3])
347 }
348
349 // blockPath returns the fully qualified pathname for the path to loc
350 // on this volume.
351 func (v *UnixVolume) blockPath(loc string) string {
352         return filepath.Join(v.blockDir(loc), loc)
353 }
354
355 // IsFull returns true if the free space on the volume is less than
356 // MinFreeKilobytes.
357 //
358 func (v *UnixVolume) IsFull() (isFull bool) {
359         fullSymlink := v.root + "/full"
360
361         // Check if the volume has been marked as full in the last hour.
362         if link, err := os.Readlink(fullSymlink); err == nil {
363                 if ts, err := strconv.Atoi(link); err == nil {
364                         fulltime := time.Unix(int64(ts), 0)
365                         if time.Since(fulltime).Hours() < 1.0 {
366                                 return true
367                         }
368                 }
369         }
370
371         if avail, err := v.FreeDiskSpace(); err == nil {
372                 isFull = avail < MinFreeKilobytes
373         } else {
374                 log.Printf("%s: FreeDiskSpace: %s\n", v, err)
375                 isFull = false
376         }
377
378         // If the volume is full, timestamp it.
379         if isFull {
380                 now := fmt.Sprintf("%d", time.Now().Unix())
381                 os.Symlink(now, fullSymlink)
382         }
383         return
384 }
385
386 // FreeDiskSpace returns the number of unused 1k blocks available on
387 // the volume.
388 //
389 func (v *UnixVolume) FreeDiskSpace() (free uint64, err error) {
390         var fs syscall.Statfs_t
391         err = syscall.Statfs(v.root, &fs)
392         if err == nil {
393                 // Statfs output is not guaranteed to measure free
394                 // space in terms of 1K blocks.
395                 free = fs.Bavail * uint64(fs.Bsize) / 1024
396         }
397         return
398 }
399
400 func (v *UnixVolume) String() string {
401         return fmt.Sprintf("[UnixVolume %s]", v.root)
402 }
403
404 // Writable returns false if all future Put, Mtime, and Delete calls are expected to fail.
405 func (v *UnixVolume) Writable() bool {
406         return !v.readonly
407 }
408
409 // lockfile and unlockfile use flock(2) to manage kernel file locks.
410 func lockfile(f *os.File) error {
411         return syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
412 }
413
414 func unlockfile(f *os.File) error {
415         return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
416 }