@cryptotaxi247 / kubo / commits / 70eccb03e

gc: collect all errors during ColoredSet phase

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Feb 19, 2017 at 22:47 UTC 70eccb03e01518dd5a6c3372698320f088298f7d
1 file changed +40 -13
pin/gc/gc.go
+40 -13
@@ -1,6 +1,7 @@
1 package gc
2
3 import (
4 + "bytes"
5 "context"
6
7 bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
@@ -28,9 +29,9 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
29
30 ls = ls.GetOfflineLinkService()
31
31 - gcs, err := ColoredSet(ctx, pn, ls, bestEffortRoots)
32 - if err != nil {
33 - return nil, err
32 + gcs, errs := ColoredSet(ctx, pn, ls, bestEffortRoots)
33 + if errs != nil {
34 + return nil, &UnsafeToContinueError{errs}
35 }
36
37 keychan, err := bs.AllKeysChan(ctx)
@@ -83,35 +84,61 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots
84 return nil
85 }
86
86 -func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffortRoots []*cid.Cid) (*cid.Set, error) {
87 +func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffortRoots []*cid.Cid) (*cid.Set, []error) {
88 // KeySet currently implemented in memory, in the future, may be bloom filter or
89 // disk backed to conserve memory.
90 gcs := cid.NewSet()
90 - err := Descendants(ctx, ls.GetLinks, gcs, pn.RecursiveKeys())
91 + var errors []error
92 + getLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
93 + links, err := ls.GetLinks(ctx, cid)
94 + if err != nil {
95 + errors = append(errors, err)
96 + }
97 + return links, nil
98 + }
99 + err := Descendants(ctx, getLinks, gcs, pn.RecursiveKeys())
100 if err != nil {
92 - return nil, err
101 + errors = append(errors, err)
102 }
103
104 bestEffortGetLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
105 links, err := ls.GetLinks(ctx, cid)
97 - if err == dag.ErrNotFound {
98 - err = nil
106 + if err != nil && err != dag.ErrNotFound {
107 + errors = append(errors, err)
108 }
100 - return links, err
109 + return links, nil
110 }
111 err = Descendants(ctx, bestEffortGetLinks, gcs, bestEffortRoots)
112 if err != nil {
104 - return nil, err
113 + errors = append(errors, err)
114 }
115
116 for _, k := range pn.DirectKeys() {
117 gcs.Add(k)
118 }
119
111 - err = Descendants(ctx, ls.GetLinks, gcs, pn.InternalPins())
120 + err = Descendants(ctx, getLinks, gcs, pn.InternalPins())
121 if err != nil {
113 - return nil, err
122 + errors = append(errors, err)
123 + }
124 +
125 + if errors != nil {
126 + return nil, errors
127 + } else {
128 + return gcs, nil
129 }
130 +}
131
116 - return gcs, nil
132 +type UnsafeToContinueError struct {
133 + Errors []error
134 +}
135 +
136 +func (e *UnsafeToContinueError) Error() string {
137 + var buf bytes.Buffer
138 + for _, err := range e.Errors {
139 + buf.WriteString(err.Error())
140 + buf.WriteString("\n")
141 + }
142 + buf.WriteString("aborting due to previous errors")
143 + return buf.String()
144 }