forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.go
177 lines (150 loc) · 4.45 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
package gc
import (
"context"
"errors"
"fmt"
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
dag "github.com/ipfs/go-ipfs/merkledag"
pin "github.com/ipfs/go-ipfs/pin"
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
)
// Result represents an incremental output from a garbage collection
// run. It contains either an error, or the cid of a removed object.
type Result struct {
KeyRemoved *cid.Cid
Error error
}
// GC performs a mark and sweep garbage collection of the blocks in the blockstore
// first, it creates a 'marked' set and adds to it the following:
// - all recursively pinned blocks, plus all of their descendants (recursively)
// - bestEffortRoots, plus all of its descendants (recursively)
// - all directly pinned blocks
// - all blocks utilized internally by the pinner
//
// The routine then iterates over every block in the blockstore and
// deletes any block that is not found in the marked set.
//
func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.Pinner, bestEffortRoots []*cid.Cid) <-chan Result {
unlocker := bs.GCLock()
ls = ls.GetOfflineLinkService()
output := make(chan Result, 128)
go func() {
defer close(output)
defer unlocker.Unlock()
gcs, err := ColoredSet(ctx, pn, ls, bestEffortRoots, output)
if err != nil {
output <- Result{Error: err}
return
}
keychan, err := bs.AllKeysChan(ctx)
if err != nil {
output <- Result{Error: err}
return
}
errors := false
loop:
for {
select {
case k, ok := <-keychan:
if !ok {
break loop
}
if !gcs.Has(k) {
err := bs.DeleteBlock(k)
if err != nil {
errors = true
output <- Result{Error: &CannotDeleteBlockError{k, err}}
//log.Errorf("Error removing key from blockstore: %s", err)
// continue as error is non-fatal
continue loop
}
select {
case output <- Result{KeyRemoved: k}:
case <-ctx.Done():
break loop
}
}
case <-ctx.Done():
break loop
}
}
if errors {
output <- Result{Error: ErrCannotDeleteSomeBlocks}
}
}()
return output
}
func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots []*cid.Cid) error {
for _, c := range roots {
set.Add(c)
// EnumerateChildren recursively walks the dag and adds the keys to the given set
err := dag.EnumerateChildren(ctx, getLinks, c, set.Visit)
if err != nil {
return err
}
}
return nil
}
// ColoredSet computes the set of nodes in the graph that are pinned by the
// pins in the given pinner.
func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffortRoots []*cid.Cid, output chan<- Result) (*cid.Set, error) {
// KeySet currently implemented in memory, in the future, may be bloom filter or
// disk backed to conserve memory.
errors := false
gcs := cid.NewSet()
getLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
links, err := ls.GetLinks(ctx, cid)
if err != nil {
errors = true
output <- Result{Error: &CannotFetchLinksError{cid, err}}
}
return links, nil
}
err := Descendants(ctx, getLinks, gcs, pn.RecursiveKeys())
if err != nil {
errors = true
output <- Result{Error: err}
}
bestEffortGetLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
links, err := ls.GetLinks(ctx, cid)
if err != nil && err != dag.ErrNotFound {
errors = true
output <- Result{Error: &CannotFetchLinksError{cid, err}}
}
return links, nil
}
err = Descendants(ctx, bestEffortGetLinks, gcs, bestEffortRoots)
if err != nil {
errors = true
output <- Result{Error: err}
}
for _, k := range pn.DirectKeys() {
gcs.Add(k)
}
err = Descendants(ctx, getLinks, gcs, pn.InternalPins())
if err != nil {
errors = true
output <- Result{Error: err}
}
if errors {
return nil, ErrCannotFetchAllLinks
}
return gcs, nil
}
var ErrCannotFetchAllLinks = errors.New("garbage collection aborted: could not retrieve some links")
var ErrCannotDeleteSomeBlocks = errors.New("garbage collection incomplete: could not delete some blocks")
type CannotFetchLinksError struct {
Key *cid.Cid
Err error
}
func (e *CannotFetchLinksError) Error() string {
return fmt.Sprintf("could not retrieve links for %s: %s", e.Key, e.Err)
}
type CannotDeleteBlockError struct {
Key *cid.Cid
Err error
}
func (e *CannotDeleteBlockError) Error() string {
return fmt.Sprintf("could not remove %s: %s", e.Key, e.Err)
}