This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
/
gc.go
86 lines (72 loc) · 1.7 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
// Package gc provides the Torus interface for how garbage collection is implemented.
// Volumes implement a garbage collector and the controller in this package runs
// them.
package gc
import (
"github.com/alternative-storage/torus"
"github.com/alternative-storage/torus/models"
"github.com/coreos/pkg/capnslog"
"golang.org/x/net/context"
)
var clog = capnslog.NewPackageLogger("github.com/alternative-storage/torus", "gc")
type controller struct {
gcs []GC
}
type GC interface {
PrepVolume(*models.Volume) error
IsDead(torus.BlockRef) bool
Clear()
}
type INodeFetcher interface {
GetINode(context.Context, torus.INodeRef) (*models.INode, error)
}
func NewGCController(srv *torus.Server, inodes INodeFetcher) GC {
var gcs []GC
for k, v := range gcFuncs {
clog.Debugf("creating %s gc", k)
gc, err := v(srv, inodes)
if err != nil {
clog.Errorf("cannot create gc %s: %v", k, err)
continue
}
gcs = append(gcs, gc)
}
return &controller{
gcs: gcs,
}
}
func (c *controller) PrepVolume(vol *models.Volume) error {
n := 0
for _, x := range c.gcs {
n++
err := x.PrepVolume(vol)
if err != nil {
return err
}
}
return nil
}
func (c *controller) IsDead(ref torus.BlockRef) bool {
for _, x := range c.gcs {
if x.IsDead(ref) {
return true
}
}
return false
}
func (c *controller) Clear() {
for _, x := range c.gcs {
x.Clear()
}
}
type CreateGCFunc func(srv *torus.Server, inodes INodeFetcher) (GC, error)
var gcFuncs map[string]CreateGCFunc
func RegisterGC(name string, newFunc CreateGCFunc) {
if gcFuncs == nil {
gcFuncs = make(map[string]CreateGCFunc)
}
if _, ok := gcFuncs[name]; ok {
panic("gc: attempted to register GC " + name + " twice")
}
gcFuncs[name] = newFunc
}