-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorager.go
270 lines (224 loc) · 5.68 KB
/
storager.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package storager
import (
"context"
"fmt"
"os"
"path"
"strings"
"sync"
"time"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/gofrs/flock"
"github.com/twmb/murmur3"
boom "github.com/tylertreat/BoomFilters"
"github.com/weedge/pkg/driver"
"github.com/weedge/pkg/safer"
"github.com/weedge/xdis-storager/config"
storagerDriver "github.com/weedge/xdis-storager/driver"
"github.com/weedge/xdis-storager/openkv"
)
var gOpts *config.StorgerOptions
// Storager core store struct for server use like redis
type Storager struct {
opts *config.StorgerOptions
// file lock
fLock *flock.Flock
// open kv store engine
odb *openkv.DB
//odb storagerDriver.IKV
// multi storager db instances on one kv store engine
dbs map[int]*DB
// dbs map lock for get and set map[int]*DB
dbLock sync.Mutex
// allow one write at same time
wLock sync.RWMutex
// allow one write commit at same time
commitLock sync.Mutex
// when set committer, use committer.Commit instead of writebatch commit for write op
committer storagerDriver.ICommitter
// counting bloom filter for db stats
cbf *boom.CountingBloomFilter
// need lock to op CBF bitset
cbfLock sync.Mutex
// ttl check
ttlCheckers []*TTLChecker
ttlCheckerCh chan *TTLChecker
wg sync.WaitGroup
quit chan struct{}
}
func New(opts *config.StorgerOptions) (store *Storager) {
store = &Storager{}
store.InitOpts(opts)
return
}
func (store *Storager) Open(ctx context.Context) (err error) {
opts := store.opts
defer func() {
if err != nil {
if e := store.Close(); e != nil {
klog.CtxErrorf(ctx, "close store err: %s", e.Error())
}
}
}()
os.MkdirAll(opts.DataDir, 0755)
store.fLock = flock.New(path.Join(opts.DataDir, "LOCK"))
ok, err := store.fLock.TryLock()
if err != nil {
return err
}
if !ok {
err = fmt.Errorf("store file had locked")
return err
}
if store.odb, err = openkv.Open(&opts.OpenkvOptions); err != nil {
return
}
store.cbf = boom.NewCountingBloomFilter(opts.CBFItmeCn, opts.CBFBucketSize, opts.CBFFpRate)
store.cbf.SetHash(murmur3.New64())
InitAllDbStats(store)
store.dbs = make(map[int]*DB, opts.Databases)
store.quit = make(chan struct{})
store.checkTTL(ctx)
return
}
func (m *Storager) Name() string {
return RegisterStoragerName
}
func (m *Storager) InitOpts(opts *config.StorgerOptions) {
if len(opts.DataDir) == 0 {
opts.DataDir = config.DefaultDataDir
}
if opts.Databases == 0 {
opts.Databases = config.DefaultDatabases
} else if opts.Databases > MaxDatabases {
opts.Databases = MaxDatabases
}
if opts.TTLCheckInterval < 0 {
opts.TTLCheckInterval = config.DefaultTTLCheckInterval
}
if opts.TTLCheckInterval > config.MaxTTLCheckInterval-config.DefaultTTLCheckInterval {
opts.TTLCheckInterval = config.MaxTTLCheckInterval - config.DefaultTTLCheckInterval
}
m.opts = opts
gOpts = opts
}
// Close close ttl check, kv store close, flock close
func (m *Storager) Close() (err error) {
if m.quit != nil {
close(m.quit)
}
m.wg.Wait()
errs := []error{}
if m.odb != nil {
errs = append(errs, m.odb.Close())
m.odb = nil
}
for _, db := range m.dbs {
errs = append(errs, db.Close())
}
errs = append(errs, m.fLock.Close())
errStrs := []string{}
for _, er := range errs {
if er != nil {
errStrs = append(errStrs, er.Error())
}
}
if len(errStrs) > 0 {
err = fmt.Errorf("errs: %s", strings.Join(errStrs, " | "))
}
return
}
// Select chooses a database.
func (m *Storager) Select(ctx context.Context, index int) (idb driver.IDB, err error) {
if index < 0 || index >= m.opts.Databases {
return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, m.opts.Databases-1)
}
m.dbLock.Lock()
db, ok := m.dbs[index]
if ok {
idb = db
m.dbLock.Unlock()
return
}
db = NewDB(m, index)
m.dbs[index] = db
db.stats = LoadDbStats(index)
// async send checker,
// if recv checkTTL tick to check,ch full, maybe block
go func() {
m.ttlCheckerCh <- db.ttlChecker
}()
idb = db
m.dbLock.Unlock()
return
}
func (m *Storager) checkTTL(ctx context.Context) {
m.ttlCheckers = make([]*TTLChecker, 0, config.DefaultDatabases)
m.ttlCheckerCh = make(chan *TTLChecker, config.DefaultDatabases)
safer.GoSafely(&m.wg, false, func() {
tick := time.NewTicker(time.Duration(m.opts.TTLCheckInterval) * time.Second)
defer tick.Stop()
for {
select {
case <-tick.C:
for _, c := range m.ttlCheckers {
c.check(ctx)
}
case c := <-m.ttlCheckerCh:
m.ttlCheckers = append(m.ttlCheckers, c)
c.check(ctx)
case <-m.quit:
return
}
}
}, nil, os.Stderr)
}
// FlushAll will clear all data
func (m *Storager) FlushAll(ctx context.Context) error {
m.wLock.Lock()
defer m.wLock.Unlock()
return m.flushAll(ctx)
}
func (m *Storager) flushAll(ctx context.Context) (err error) {
it := m.odb.NewIterator()
defer it.Close()
w := m.odb.NewWriteBatch()
defer func() {
if err != nil {
w.Rollback()
}
}()
n := 0
for it.SeekToFirst(); it.Valid(); it.Next() {
n++
if n == 10000 {
if err = w.Commit(); err != nil {
klog.CtxErrorf(ctx, "flush all commit error: %s", err.Error())
return
}
n = 0
}
w.Delete(it.RawKey())
}
if err = w.Commit(); err != nil {
klog.CtxErrorf(ctx, "flush all commit error: %s", err.Error())
return
}
return nil
}
// GetKVStore get kvstore engine which save data
func (s *Storager) GetKVStore() storagerDriver.IKV {
return s.odb
}
// SetCommitter
func (s *Storager) SetCommitter(committer storagerDriver.ICommitter) {
s.committer = committer
}
// GetRWlock noCopy return rw muxtex lock for read only
func (s *Storager) GetRWLock() *sync.RWMutex {
return &s.wLock
}
// GetCommitLock noCopy return mutex lock for commit log/data
func (s *Storager) GetCommitLock() *sync.Mutex {
return &s.commitLock
}