-
Notifications
You must be signed in to change notification settings - Fork 2
/
group_storage.go
179 lines (138 loc) · 4.18 KB
/
group_storage.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
package rbstor
import (
"context"
"time"
iface "github.com/lotus-web3/ribs"
"golang.org/x/xerrors"
)
const MaxLocalGroupCount = 64 // todo user config
func (r *rbs) createGroup(ctx context.Context) (iface.GroupKey, *Group, error) {
if err := r.ensureSpaceForGroup(ctx); err != nil {
return 0, nil, xerrors.Errorf("ensure space for group: %w", err)
}
selectedGroup, err := r.db.CreateGroup()
if err != nil {
return iface.UndefGroupKey, nil, xerrors.Errorf("creating group: %w", err)
}
g, err := r.openGroup(ctx, selectedGroup, 0, 0, 0, iface.GroupStateWritable, true)
if err != nil {
return iface.UndefGroupKey, nil, xerrors.Errorf("opening group: %w", err)
}
return selectedGroup, g, nil
}
func (r *rbs) openGroup(ctx context.Context, group iface.GroupKey, blocks, bytes, jbhead int64, state iface.GroupState, create bool) (*Group, error) {
g, err := OpenGroup(ctx, r.db, r.index, &r.staging, group, blocks, bytes, jbhead, r.root, state, create)
if err != nil {
return nil, xerrors.Errorf("opening group: %w", err)
}
if state == iface.GroupStateWritable {
r.writableGroups[group] = g
}
r.openGroups[group] = g
return g, nil
}
func (r *rbs) withWritableGroup(ctx context.Context, prefer iface.GroupKey, cb func(group *Group) error) (selectedGroup iface.GroupKey, err error) {
r.lk.Lock()
defer r.lk.Unlock()
r.writeLk.Lock()
defer r.writeLk.Unlock()
defer func() {
if err != nil || selectedGroup == iface.UndefGroupKey {
return
}
// if the group was filled, drop it from writableGroups and start finalize
if r.writableGroups[selectedGroup].state != iface.GroupStateWritable {
delete(r.writableGroups, selectedGroup)
r.tasks <- task{
tt: taskTypeFinalize,
group: selectedGroup,
}
}
}()
// todo prefer
for g, grp := range r.writableGroups {
return g, cb(grp)
}
// no writable groups, try to open one
selectedGroup = iface.UndefGroupKey
{
var blocks, bytes, jbhead int64
var state iface.GroupState
selectedGroup, blocks, bytes, jbhead, state, err = r.db.GetWritableGroup()
if err != nil {
return iface.UndefGroupKey, xerrors.Errorf("finding writable groups: %w", err)
}
if selectedGroup != iface.UndefGroupKey {
g, err := r.openGroup(ctx, selectedGroup, blocks, bytes, jbhead, state, false)
if err != nil {
return iface.UndefGroupKey, xerrors.Errorf("opening group: %w", err)
}
return selectedGroup, cb(g)
}
}
// no writable groups, create one
selectedGroup, g, err := r.createGroup(ctx)
if err != nil {
return iface.UndefGroupKey, xerrors.Errorf("creating group: %w", err)
}
return selectedGroup, cb(g)
}
func (r *rbs) withReadableGroup(ctx context.Context, group iface.GroupKey, cb func(group *Group) error) (err error) {
r.lk.Lock()
// todo prefer
if r.openGroups[group] != nil {
r.lk.Unlock()
return cb(r.openGroups[group])
}
// not open, open it
blocks, bytes, jbhead, state, err := r.db.OpenGroup(group)
if err != nil {
r.lk.Unlock()
return xerrors.Errorf("getting group metadata: %w", err)
}
g, err := r.openGroup(ctx, group, blocks, bytes, jbhead, state, false)
if err != nil {
r.lk.Unlock()
return xerrors.Errorf("opening group: %w", err)
}
r.resumeGroup(group)
r.lk.Unlock()
return cb(g)
}
func (r *rbs) ensureSpaceForGroup(ctx context.Context) error {
localCount, err := r.db.CountNonOffloadedGroups()
if err != nil {
return xerrors.Errorf("counting non-offloaded groups: %w", err)
}
if localCount < MaxLocalGroupCount {
return nil
}
var offloadCandidate iface.GroupKey
for {
offloadCandidate, err = r.db.GetOffloadCandidate()
if err != nil {
return xerrors.Errorf("getting offload candidate: %w", err)
}
if offloadCandidate != iface.UndefGroupKey {
break
}
log.Errorw("no offload candidate, waiting for space", "localCount", localCount)
// wait 1 min, then try again
r.lk.Unlock()
select {
case <-ctx.Done():
r.lk.Lock()
return ctx.Err()
case <-time.After(time.Minute):
}
r.lk.Lock()
}
log.Errorw("local space full, offloading group", "group", offloadCandidate)
// release read side
r.lk.Unlock()
defer r.lk.Lock()
return r.withReadableGroup(ctx, offloadCandidate, func(g *Group) error {
err := g.offloadStaging()
return err
})
}