-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnap.go
361 lines (330 loc) · 9.08 KB
/
snap.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
/* See the file "LICENSE.txt" for the full license governing this code. */
// Snapshot in memory and on-disk format
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
type snapshotState uint
const none snapshotState = 0
const (
stateIncomplete snapshotState = 1 << iota
stateComplete
stateObsolete
statePurging
any = (1 << iota) - 1
)
func (st snapshotState) String() string {
switch st {
case stateIncomplete:
return "Incomplete"
case stateComplete:
return "Complete"
case stateObsolete:
return "Obsolete"
case statePurging:
return "Purging"
}
return "Unknown"
}
type snapshot struct {
startTime time.Time
endTime time.Time
state snapshotState
}
func newSnapshot(startTime, endTime time.Time, state snapshotState) *snapshot {
return &snapshot{startTime, endTime, state}
}
func newIncompleteSnapshot(cl clock) *snapshot {
return &snapshot{cl.Now(), time.Time{}, stateIncomplete}
}
func (s *snapshot) String() string {
stime := s.startTime.Unix()
etime := s.endTime.Unix()
return fmt.Sprintf("%d-%d %s", stime, etime, s.state.String())
}
// Name returns the relative pathname for the receiver snapshot.
func (s *snapshot) Name() string {
stime := s.startTime.Unix()
etime := s.endTime.Unix()
switch s.state {
case stateIncomplete:
return fmt.Sprintf("%d-0-incomplete", stime)
case stateComplete:
return fmt.Sprintf("%d-%d-complete", stime, etime)
case stateObsolete:
return fmt.Sprintf("%d-%d-obsolete", stime, etime)
case statePurging:
return fmt.Sprintf("%d-%d-purging", stime, etime)
}
return fmt.Sprintf("%d-%d-unknown", stime, etime)
}
// FullName returns the full pathname for the receiver snapshot.
func (s *snapshot) FullName() string {
return filepath.Join(config.repository, dataSubdir, s.Name())
}
// transComplete transitions the receiver to complete state.
func (s *snapshot) transComplete(cl clock) error {
oldName := s.FullName()
etime := cl.Now()
if etime.Before(s.startTime) {
return errors.New("endTime before startTime!")
}
// make all snapshots at least 1 second long
if etime.Sub(s.startTime).Seconds() < 1 {
etime = etime.Add(time.Second)
}
s.endTime = etime
s.state = stateComplete
newName := s.FullName()
debugf("renaming complete snapshot %s -> %s", oldName, newName)
if oldName != newName {
err := os.Rename(oldName, newName)
if err != nil {
return err
}
}
updateSymlinks()
overwriteSymlink(filepath.Join(dataSubdir, s.Name()), filepath.Join(config.repository, "latest"))
return nil
}
// transObsolete transitions the receiver to obsolete state.
func (s *snapshot) transObsolete() error {
oldName := s.FullName()
s.state = stateObsolete
newName := s.FullName()
if oldName != newName {
err := os.Rename(oldName, newName)
if err != nil {
return err
}
}
updateSymlinks()
return nil
}
// transPurging transitions the receiver to purging state.
func (s *snapshot) transPurging() error {
oldName := s.FullName()
s.state = statePurging
newName := s.FullName()
if oldName != newName {
err := os.Rename(oldName, newName)
if err != nil {
return err
}
}
return nil
}
// transIncomplete generates a new incomplete snapshot based on a previous one.
// Can be used to try to use previous incomplete snapshots, or even to reuse
// obsolete ones.
func (s *snapshot) transIncomplete(cl clock) error {
oldName := s.FullName()
s.startTime = cl.Now()
s.endTime = time.Time{}
s.state = stateIncomplete
newName := s.FullName()
debugf("renaming incomplete snapshot %s -> %s", oldName, newName)
if oldName != newName {
err := os.Rename(oldName, newName)
if err != nil {
return err
}
}
return nil
}
// purge deletes the receiver snapshot from disk.
func (s *snapshot) purge() {
err := s.transPurging()
if err != nil {
log.Printf("error peparing %s for purging: %s", s.Name(), err)
}
path := s.FullName()
log.Println("purging", s.Name())
err = os.RemoveAll(path)
if err != nil {
log.Printf("error when purging \"%s\" (ignored): %s", s.Name(), err)
}
log.Println("finished purging", s.Name())
}
func (s *snapshot) matchFilter(f snapshotState) bool {
//log.Println("filter:", strconv.FormatInt(int64(s.state), 2), strconv.FormatInt(int64(f), 2), strconv.FormatBool(s.state & f == s.state))
//log.Println(strconv.FormatInt(int64(any), 2))
return (s.state & f) == s.state
}
type snapshotList []*snapshot
// Find the last snapshot to use as a basis for the next one.
func (sl snapshotList) lastGood() *snapshot {
var t time.Time
var ix = -1
for i, sn := range sl {
if (sn.startTime.After(t)) && (sn.state == stateComplete) {
t = sn.startTime
ix = i
}
}
if ix == -1 {
return nil
}
return sl[ix]
}
// Find the last snapshot in a given list.
func (sl snapshotList) last() *snapshot {
var t time.Time
var ix = -1
for i, sn := range sl {
if sn.startTime.After(t) {
t = sn.startTime
ix = i
}
}
if ix == -1 {
return nil
}
return sl[ix]
}
// parseSnapshotName split the given string up into the various values needed
// for creating a Snapshot struct.
func parseSnapshotName(s string) (time.Time, time.Time, snapshotState, error) {
sa := strings.Split(s, "-")
var zero time.Time
if len(sa) != 3 {
return zero, zero, 0, errors.New("malformed snapshot name: " + s)
}
stime, err := strconv.ParseInt(sa[0], 10, 64)
if err != nil {
return zero, zero, 0, err
}
etime, err := strconv.ParseInt(sa[1], 10, 64)
if err != nil {
return zero, zero, 0, err
}
var state snapshotState
switch sa[2] {
case "complete":
state = stateComplete
case "incomplete":
state = stateIncomplete
case "obsolete":
state = stateObsolete
case "purging":
state = statePurging
}
if state == 0 {
return time.Unix(stime, 0), time.Unix(etime, 0), state, errors.New("could not parse state: " + s)
}
if state == stateIncomplete && etime != 0 {
return zero, zero, 0, errors.New("incomplete state but non-zero end time: " + s)
}
return time.Unix(stime, 0), time.Unix(etime, 0), state, nil
}
type snapshotListByStartTime snapshotList
func (sl snapshotListByStartTime) Len() int {
return len(sl)
}
func (sl snapshotListByStartTime) Swap(i, j int) {
sl[i], sl[j] = sl[j], sl[i]
}
func (sl snapshotListByStartTime) Less(i, j int) bool {
return sl[i].startTime.Before(sl[j].startTime)
}
// findSnapshots() reads the repository directory and returns a list of
// Snapshot pointers for all valid snapshots it could find.
func findSnapshots(cl clock) (snapshotList, error) {
snapshots := make(snapshotList, 0, 256)
dataPath := filepath.Join(config.repository, dataSubdir, "")
files, err := ioutil.ReadDir(dataPath)
if err != nil {
return nil, errors.New("Repository " + dataPath + " does not exist")
}
for _, f := range files {
if !f.IsDir() {
continue
}
stime, etime, state, err := parseSnapshotName(f.Name())
if err != nil {
log.Println(err)
continue
}
if stime.After(cl.Now()) {
log.Println("ignoring snapshot with startTime in future:", f.Name())
continue
}
sn := newSnapshot(stime, etime, state)
snapshots = append(snapshots, sn)
}
sort.Sort(snapshotListByStartTime(snapshots))
return snapshots, nil
}
// Return a new list of snapshots that fall into the given time period.
func (sl snapshotList) period(after, before time.Time) snapshotList {
slNew := make(snapshotList, 0, len(sl))
for _, sn := range sl {
if sn.startTime.After(after) && sn.startTime.Before(before) {
slNew = append(slNew, sn)
}
}
return slNew
}
// Return a list of snapshots within the given interval.
func (sl snapshotList) interval(intervals intervalList, i int, cl clock) snapshotList {
t := cl.Now()
from := t.Add(-intervals.offset(i + 1))
to := t.Add(-intervals.offset(i))
return sl.period(from, to)
}
// Return a filtered list of snapshots that match (include) or don't match
// (exclude) the given state mask.
func (sl snapshotList) state(include, exclude snapshotState) snapshotList {
slNew := make(snapshotList, 0, len(sl))
for _, sn := range sl {
if sn.matchFilter(include) && sn.matchFilter(^exclude) {
slNew = append(slNew, sn)
}
}
return slNew
}
// findDangling returns a list of obsolete or purged snapshots.
func findDangling(cl clock) snapshotList {
snapshots, err := findSnapshots(cl)
if err != nil {
log.Println(err)
}
slNew := make(snapshotList, 0, len(snapshots))
for _, sn := range snapshots.state(stateObsolete+statePurging, stateComplete) {
debugf("found dangling snapshot: %s", sn)
slNew = append(slNew, sn)
}
return slNew
}
// lastGoodFromDisk lists the snapshots in the repository and returns a pointer
// to the youngest complete snapshot.
func lastGoodFromDisk(cl clock) *snapshot {
snapshots, err := findSnapshots(cl)
if err != nil {
log.Println(err)
}
sn := snapshots.state(stateComplete, none).lastGood()
if sn == nil {
log.Println("lastgood: could not find suitable base snapshot")
}
return sn
}
// lastIncompleteFromDisk lists the snapshots in the repository and returns a pointer
// to the youngest incomplete snapshot, for possible reuse.
func lastReusableFromDisk(cl clock) *snapshot {
snapshots, err := findSnapshots(cl)
if err != nil {
log.Println(err)
}
sn := snapshots.state(stateIncomplete, none).last()
return sn
}