forked from sahib/brig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.go
211 lines (171 loc) · 4.41 KB
/
gc.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
package core
import (
log "github.com/Sirupsen/logrus"
"github.com/sahib/brig/catfs/db"
ie "github.com/sahib/brig/catfs/errors"
n "github.com/sahib/brig/catfs/nodes"
h "github.com/sahib/brig/util/hashlib"
)
// GarbageCollector implements a small mark & sweep garbage collector.
// It exists more for the sake of fault tolerance than it being an
// essential part of brig. This is different from the ipfs garbage collector.
type GarbageCollector struct {
lkr *Linker
kv db.Database
notifier func(nd n.Node) bool
markMap map[string]struct{}
}
// NewGarbageCollector will return a new GC, operating on `lkr` and `kv`.
// It will call `kc` on every collected node.
func NewGarbageCollector(lkr *Linker, kv db.Database, kc func(nd n.Node) bool) *GarbageCollector {
return &GarbageCollector{
lkr: lkr,
kv: kv,
notifier: kc,
}
}
func (gc *GarbageCollector) markMoveMap(key []string) error {
walker := func(key []string) error {
data, err := gc.kv.Get(key...)
if err != nil {
return err
}
node, _, err := gc.lkr.parseMoveMappingLine(string(data))
if err != nil {
return err
}
if node != nil {
gc.markMap[node.TreeHash().B58String()] = struct{}{}
}
return nil
}
return gc.kv.Keys(walker, key...)
}
func (gc *GarbageCollector) mark(cmt *n.Commit, recursive bool) error {
if cmt == nil {
return nil
}
root, err := gc.lkr.DirectoryByHash(cmt.Root())
if err != nil {
return err
}
gc.markMap[cmt.TreeHash().B58String()] = struct{}{}
err = n.Walk(gc.lkr, root, true, func(child n.Node) error {
gc.markMap[child.TreeHash().B58String()] = struct{}{}
return nil
})
if err != nil {
return err
}
parent, err := cmt.Parent(gc.lkr)
if err != nil {
return err
}
if recursive && parent != nil {
parentCmt, ok := parent.(*n.Commit)
if !ok {
return ie.ErrBadNode
}
return gc.mark(parentCmt, recursive)
}
return nil
}
func (gc *GarbageCollector) sweep(key []string) (int, error) {
removed := 0
return removed, gc.lkr.AtomicWithBatch(func(batch db.Batch) (bool, error) {
sweeper := func(key []string) error {
b58Hash := key[len(key)-1]
if _, ok := gc.markMap[b58Hash]; !ok {
hash, err := h.FromB58String(b58Hash)
if err != nil {
return err
}
node, err := gc.lkr.NodeByHash(hash)
if err != nil {
return err
}
if node == nil {
return nil
}
// Allow the gc caller to check if he really
// wants to delete this node.
if gc.notifier != nil && !gc.notifier(node) {
return nil
}
// Actually get rid of the node:
gc.lkr.MemIndexPurge(node)
batch.Erase(key...)
removed++
}
return nil
}
return hintRollback(gc.kv.Keys(sweeper, key...))
})
}
func (gc *GarbageCollector) findAllMoveLocations(head *n.Commit) ([][]string, error) {
locations := [][]string{
{"stage", "moves"},
}
for {
parent, err := head.Parent(gc.lkr)
if err != nil {
return nil, err
}
if parent == nil {
break
}
parentCmt, ok := parent.(*n.Commit)
if !ok {
return nil, ie.ErrBadNode
}
head = parentCmt
location := []string{"moves", head.TreeHash().B58String()}
locations = append(locations, location)
}
return locations, nil
}
// Run will trigger a GC run. If `allObjects` is false,
// only the staging commit will be checked. Otherwise
// all objects in the key value store.
func (gc *GarbageCollector) Run(allObjects bool) error {
gc.markMap = make(map[string]struct{})
head, err := gc.lkr.Status()
if err != nil {
return err
}
if err := gc.mark(head, allObjects); err != nil {
return err
}
// Staging might contain moved files that are not reachable anymore,
// but still are referenced by the move mapping.
// Keep them for now, they will die most likely on MakeCommit()
moveMapLocations := [][]string{
{"stage", "moves"},
}
if allObjects {
moveMapLocations, err = gc.findAllMoveLocations(head)
if err != nil {
return err
}
}
for _, location := range moveMapLocations {
if err := gc.markMoveMap(location); err != nil {
return err
}
}
removed, err := gc.sweep([]string{"stage", "objects"})
if err != nil {
log.Debugf("removed %d unreachable staging objects.", removed)
}
if allObjects {
removed, err = gc.sweep([]string{"objects"})
if err != nil {
return err
}
if removed > 0 {
log.Warningf("removed %d unreachable permanent objects.", removed)
log.Warningf("this might indiciate a bug in catfs somewhere.")
}
}
return nil
}