forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.go
198 lines (177 loc) · 4.56 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
/*
Copyright 2014 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package gc defines a generic garbage collector.
package gc
import (
"errors"
"fmt"
"camlistore.org/pkg/context"
"camlistore.org/pkg/syncutil"
)
const buffered = 32 // arbitrary
// Item is something that exists that may or may not survive a GC collection.
type Item interface{}
// A Collector performs a garbage collection.
type Collector struct {
// World specifies a World that should be stopped before a
// collection and started again after.
World World
Marker Marker
Roots Enumerator
Sweeper Enumerator
ItemEnumerator ItemEnumerator
Deleter Deleter
}
type Marker interface {
// Mark marks that an item should exist.
// It must be safe for calls from concurrent goroutines.
Mark(Item) error
// IsMarked returns whether the item is marked.
// It must be safe for calls from concurrent goroutines.
IsMarked(Item) (bool, error)
}
// World defines the thing that should be stopped before GC and started after.
type World interface {
Stop() error
Start() error
}
type Deleter interface {
// Delete deletes an item that was deemed unreachable via
// the garbage collector.
// It must be safe for calls from concurrent goroutines.
Delete(Item) error
}
// Enumerator enumerates items.
type Enumerator interface {
// Enumerate enumerates items (which items depends on usage)
// and sends them to the provided channel. Regardless of return
// value, the channel should be closed.
//
// If the provided context is closed, Enumerate should return
// with an error (typically context.ErrCanceled)
Enumerate(*context.Context, chan<- Item) error
}
// ItemEnumerator enumerates all the edges out from an item.
type ItemEnumerator interface {
// EnumerateItme is like Enuerator's Enumerate, but specific
// to the provided item.
EnumerateItem(*context.Context, Item, chan<- Item) error
}
// ctx will be canceled on failure
func (c *Collector) markItem(ctx *context.Context, it Item, isRoot bool) error {
if !isRoot {
marked, err := c.Marker.IsMarked(it)
if err != nil {
return err
}
if marked {
return nil
}
}
if err := c.Marker.Mark(it); err != nil {
return err
}
ch := make(chan Item, buffered)
var grp syncutil.Group
grp.Go(func() error {
return c.ItemEnumerator.EnumerateItem(ctx, it, ch)
})
grp.Go(func() error {
for it := range ch {
if err := c.markItem(ctx, it, false); err != nil {
return err
}
}
return nil
})
if err := grp.Err(); err != nil {
ctx.Cancel()
return err
}
return nil
}
// Collect performs a garbage collection.
func (c *Collector) Collect(ctx *context.Context) (err error) {
if c.World == nil {
return errors.New("no World")
}
if c.Marker == nil {
return errors.New("no Marker")
}
if c.Roots == nil {
return errors.New("no Roots")
}
if c.Sweeper == nil {
return errors.New("no Sweeper")
}
if c.ItemEnumerator == nil {
return errors.New("no ItemEnumerator")
}
if c.Deleter == nil {
return errors.New("no Deleter")
}
if err := c.World.Stop(); err != nil {
return err
}
defer func() {
startErr := c.World.Start()
if err == nil {
err = startErr
}
}()
// Mark.
roots := make(chan Item, buffered)
markCtx := ctx.New()
var marker syncutil.Group
marker.Go(func() error {
defer markCtx.Cancel()
for it := range roots {
if err := c.markItem(markCtx, it, true); err != nil {
return err
}
}
return nil
})
marker.Go(func() error {
return c.Roots.Enumerate(markCtx, roots)
})
if err := marker.Err(); err != nil {
return fmt.Errorf("Mark failure: %v", err)
}
// Sweep.
all := make(chan Item, buffered)
sweepCtx := ctx.New()
var sweeper syncutil.Group
sweeper.Go(func() error {
return c.Sweeper.Enumerate(sweepCtx, all)
})
sweeper.Go(func() error {
defer sweepCtx.Done()
for it := range all {
ok, err := c.Marker.IsMarked(it)
if err != nil {
return err
}
if !ok {
if err := c.Deleter.Delete(it); err != nil {
return err
}
}
}
return nil
})
if err := sweeper.Err(); err != nil {
return fmt.Errorf("Sweep failure: %v", err)
}
return nil
}