forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebalancer.go
66 lines (56 loc) · 1.24 KB
/
rebalancer.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
package rebalance
import (
"github.com/coreos/torus"
"github.com/coreos/torus/gc"
"github.com/coreos/torus/models"
"github.com/coreos/pkg/capnslog"
"golang.org/x/net/context"
)
var clog = capnslog.NewPackageLogger("github.com/coreos/torus", "rebalance")
type Ringer interface {
Ring() torus.Ring
UUID() string
}
type Rebalancer interface {
Tick() (int, error)
VersionStart() int
PrepVolume(*models.Volume) error
Reset() error
}
type CheckAndSender interface {
Check(ctx context.Context, peer string, refs []torus.BlockRef) ([]bool, error)
PutBlock(ctx context.Context, peer string, ref torus.BlockRef, data []byte) error
}
func NewRebalancer(r Ringer, bs torus.BlockStore, cs CheckAndSender, gc gc.GC) Rebalancer {
return &rebalancer{
r: r,
bs: bs,
cs: cs,
gc: gc,
}
}
type rebalancer struct {
r Ringer
bs torus.BlockStore
cs CheckAndSender
it torus.BlockIterator
gc gc.GC
ring torus.Ring
}
func (r *rebalancer) VersionStart() int {
if r.ring == nil {
return r.r.Ring().Version()
}
return r.ring.Version()
}
func (r *rebalancer) PrepVolume(vol *models.Volume) error {
return r.gc.PrepVolume(vol)
}
func (r *rebalancer) Reset() error {
if r.it != nil {
r.it.Close()
r.it = nil
}
r.gc.Clear()
return nil
}