-
Notifications
You must be signed in to change notification settings - Fork 2
/
garbage_collector.go
153 lines (116 loc) · 2.84 KB
/
garbage_collector.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
package crabfs
import (
"context"
"sync"
"time"
"github.com/golang/protobuf/proto"
pb "github.com/runletapp/crabfs/protos"
"github.com/GustavoKatel/asyncutils/event"
cid "github.com/ipfs/go-cid"
ipfsDatastore "github.com/ipfs/go-datastore"
ipfsDatastoreQuery "github.com/ipfs/go-datastore/query"
ipfsBlockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/runletapp/crabfs/interfaces"
)
var _ interfaces.GarbageCollector = &garbageCollectorImpl{}
type garbageCollectorImpl struct {
interval time.Duration
scheduleCh chan interface{}
ctx context.Context
ds ipfsDatastore.Batching
bs ipfsBlockstore.Blockstore
locker *sync.RWMutex
event event.Event
}
// GarbageCollectorNew basic implementation of blockstore garbage collector
func GarbageCollectorNew(ctx context.Context, interval time.Duration, ds ipfsDatastore.Batching, bs ipfsBlockstore.Blockstore) (interfaces.GarbageCollector, error) {
gc := &garbageCollectorImpl{
ctx: ctx,
interval: interval,
scheduleCh: make(chan interface{}, 1),
ds: ds,
bs: bs,
locker: &sync.RWMutex{},
event: event.NewEvent(false),
}
return gc, nil
}
func (gc *garbageCollectorImpl) Start() error {
go gc.background()
return nil
}
func (gc *garbageCollectorImpl) Schedule() error {
gc.scheduleCh <- nil
return nil
}
func (gc *garbageCollectorImpl) background() {
ticker := time.NewTicker(gc.interval)
for {
select {
case <-gc.ctx.Done():
ticker.Stop()
return
case <-ticker.C:
gc.Collect()
case <-gc.scheduleCh:
gc.Collect()
}
}
}
func (gc *garbageCollectorImpl) Collect() error {
// Check if there's another collect operation going on
if gc.event.IsSet() {
return nil
}
gc.event.Set()
defer gc.event.Reset()
// Lock for read/write, making sure no other operation is happening during collecting
gc.locker.Lock()
defer gc.locker.Unlock()
query := ipfsDatastoreQuery.Query{
Prefix: "/crabfs/",
}
results, err := gc.ds.Query(query)
if err != nil {
return err
}
usedBlocks := map[cid.Cid]interface{}{}
var record pb.DHTNameRecord
var recordValue pb.CrabObject
for result := range results.Next() {
if err := proto.Unmarshal(result.Value, &record); err != nil {
continue
}
if err := proto.Unmarshal(record.Data, &recordValue); err != nil {
continue
}
for _, blockMeta := range recordValue.Blocks {
cid, _ := cid.Cast(blockMeta.Cid)
usedBlocks[cid] = nil
}
}
ch, err := gc.bs.AllKeysChan(gc.ctx)
if err != nil {
return err
}
for key := range ch {
if _, prs := usedBlocks[key]; prs {
// Block is used, next
continue
}
err := gc.bs.DeleteBlock(key)
if err != nil {
return err
}
}
gcDs, ok := gc.ds.(ipfsDatastore.GCDatastore)
if ok {
if err := gcDs.CollectGarbage(); err != nil {
return err
}
}
return nil
}
func (gc *garbageCollectorImpl) Locker() sync.Locker {
return gc.locker.RLocker()
}