forked from BTrDB/btrdb-server
/
bgcleaner.go
83 lines (80 loc) · 1.66 KB
/
bgcleaner.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
package cephprovider
import (
"context"
"fmt"
"strings"
"sync"
"time"
)
func (sp *CephStorageProvider) BackgroundCleanup(uuids [][]byte) error {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
sp.bgClean(false, uuids)
wg.Done()
}()
if sp.hotPool != sp.dataPool {
wg.Add(1)
sp.bgClean(true, uuids)
wg.Done()
}
wg.Wait()
return nil
}
func (sp *CephStorageProvider) bgClean(isHot bool, uuids [][]byte) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
freed := uint64(0)
scanned := uint64(0)
go func() {
poolname := sp.dataPool
if isHot {
poolname = sp.hotPool
}
for {
if ctx.Err() != nil {
return
}
time.Sleep(30 * time.Second)
lg.Infof("[%s] BG CLEANER %d objects freed, %dk objects scanned", poolname, freed, scanned/1000)
}
}()
oidprefixes := []string{}
for _, uu := range uuids {
oidprefix := fmt.Sprintf("%032x", uu)
oidprefixes = append(oidprefixes, oidprefix)
sbprefix := fmt.Sprintf("sb%032x", uu)
oidprefixes = append(oidprefixes, sbprefix)
}
rmh, h, err := sp.getHandle(context.Background(), isHot)
if err != nil {
panic(err)
}
rmh2, h2, err := sp.getHandle(context.Background(), isHot)
if err != nil {
panic(err)
}
defer rmh.Release()
defer rmh2.Release()
lfunc := func(oid string) {
mustDelete := false
for _, pfx := range oidprefixes {
if strings.HasPrefix(oid, pfx) {
mustDelete = true
break
}
}
if mustDelete {
err := h2.Delete(oid)
if err != nil {
lg.Panicf("could not free object in BG scan: %v", err)
}
freed++
}
scanned++
}
err = h.ListObjects(lfunc)
if err != nil {
lg.Panicf("could not list objects to do delete: %v\n", err)
}
}