-
Notifications
You must be signed in to change notification settings - Fork 199
/
leveldbSerial.go
343 lines (280 loc) · 6.82 KB
/
leveldbSerial.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package leveldb
import (
"bytes"
"context"
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/closing"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var _ storage.Persister = (*SerialDB)(nil)
// SerialDB holds a pointer to the leveldb database and the path to where it is stored.
type SerialDB struct {
*baseLevelDb
path string
maxBatchSize int
batchDelaySeconds int
sizeBatch int
batch storage.Batcher
mutBatch sync.RWMutex
dbAccess chan serialQueryer
cancel context.CancelFunc
mutClosed sync.Mutex
closed bool
closer core.SafeCloser
}
// NewSerialDB is a constructor for the leveldb persister
// It creates the files in the location given as parameter
func NewSerialDB(path string, batchDelaySeconds int, maxBatchSize int, maxOpenFiles int) (s *SerialDB, err error) {
err = os.MkdirAll(path, rwxOwner)
if err != nil {
return nil, err
}
if maxOpenFiles < 1 {
return nil, storage.ErrInvalidNumOpenFiles
}
options := &opt.Options{
// disable internal cache
BlockCacheCapacity: -1,
OpenFilesCacheCapacity: maxOpenFiles,
}
db, err := openLevelDB(path, options)
if err != nil {
return nil, fmt.Errorf("%w for path %s", err, path)
}
bldb := &baseLevelDb{
db: db,
}
ctx, cancel := context.WithCancel(context.Background())
dbStore := &SerialDB{
baseLevelDb: bldb,
path: path,
maxBatchSize: maxBatchSize,
batchDelaySeconds: batchDelaySeconds,
sizeBatch: 0,
dbAccess: make(chan serialQueryer),
cancel: cancel,
closed: false,
closer: closing.NewSafeChanCloser(),
}
dbStore.batch = NewBatch()
go dbStore.batchTimeoutHandle(ctx)
go dbStore.processLoop(ctx)
runtime.SetFinalizer(dbStore, func(db *SerialDB) {
_ = db.Close()
})
log.Debug("opened serial level db persister", "path", path)
return dbStore, nil
}
func (s *SerialDB) batchTimeoutHandle(ctx context.Context) {
for {
select {
case <-time.After(time.Duration(s.batchDelaySeconds) * time.Second):
err := s.putBatch()
if err != nil {
log.Warn("leveldb serial putBatch", "error", err.Error())
continue
}
case <-ctx.Done():
log.Debug("batchTimeoutHandle - closing", "path", s.path)
return
}
}
}
func (s *SerialDB) updateBatchWithIncrement() error {
s.mutBatch.Lock()
s.sizeBatch++
if s.sizeBatch < s.maxBatchSize {
s.mutBatch.Unlock()
return nil
}
s.mutBatch.Unlock()
err := s.putBatch()
return err
}
// Put adds the value to the (key, val) storage medium
func (s *SerialDB) Put(key, val []byte) error {
if s.isClosed() {
return storage.ErrSerialDBIsClosed
}
s.mutBatch.RLock()
err := s.batch.Put(key, val)
s.mutBatch.RUnlock()
if err != nil {
return err
}
return s.updateBatchWithIncrement()
}
// Get returns the value associated to the key
func (s *SerialDB) Get(key []byte) ([]byte, error) {
if s.isClosed() {
return nil, storage.ErrSerialDBIsClosed
}
s.mutBatch.RLock()
data := s.batch.Get(key)
s.mutBatch.RUnlock()
if data != nil {
if bytes.Equal(data, []byte(removed)) {
return nil, storage.ErrKeyNotFound
}
return data, nil
}
ch := make(chan *pairResult)
req := &getAct{
key: key,
resChan: ch,
}
err := s.tryWriteInDbAccessChan(req)
if err != nil {
return nil, err
}
result := <-ch
close(ch)
if result.err == leveldb.ErrNotFound {
return nil, storage.ErrKeyNotFound
}
if result.err != nil {
return nil, result.err
}
return result.value, nil
}
// Has returns nil if the given key is present in the persistence medium
func (s *SerialDB) Has(key []byte) error {
if s.isClosed() {
return storage.ErrSerialDBIsClosed
}
s.mutBatch.RLock()
data := s.batch.Get(key)
s.mutBatch.RUnlock()
if data != nil {
if bytes.Equal(data, []byte(removed)) {
return storage.ErrKeyNotFound
}
return nil
}
ch := make(chan error)
req := &hasAct{
key: key,
resChan: ch,
}
err := s.tryWriteInDbAccessChan(req)
if err != nil {
return err
}
result := <-ch
close(ch)
return result
}
func (s *SerialDB) tryWriteInDbAccessChan(req serialQueryer) error {
select {
case s.dbAccess <- req:
return nil
case <-s.closer.ChanClose():
return storage.ErrSerialDBIsClosed
}
}
// putBatch writes the Batch data into the database
func (s *SerialDB) putBatch() error {
s.mutBatch.Lock()
dbBatch, ok := s.batch.(*batch)
if !ok {
s.mutBatch.Unlock()
return storage.ErrInvalidBatch
}
s.sizeBatch = 0
s.batch = NewBatch()
s.mutBatch.Unlock()
ch := make(chan error)
req := &putBatchAct{
batch: dbBatch,
resChan: ch,
}
err := s.tryWriteInDbAccessChan(req)
if err != nil {
return err
}
result := <-ch
close(ch)
return result
}
func (s *SerialDB) isClosed() bool {
s.mutClosed.Lock()
isClosed := s.closed
s.mutClosed.Unlock()
return isClosed
}
// Close closes the files/resources associated to the storage medium
func (s *SerialDB) Close() error {
s.mutClosed.Lock()
defer s.mutClosed.Unlock()
if s.closed {
return nil
}
//calling close on the SafeCloser instance should be the last instruction called
//(just to close some go routines started as edge cases that would otherwise hang)
defer s.closer.Close()
s.closed = true
_ = s.putBatch()
s.cancel()
return s.db.Close()
}
// Remove removes the data associated to the given key
func (s *SerialDB) Remove(key []byte) error {
if s.isClosed() {
return storage.ErrSerialDBIsClosed
}
s.mutBatch.Lock()
_ = s.batch.Delete(key)
s.mutBatch.Unlock()
return s.updateBatchWithIncrement()
}
// Destroy removes the storage medium stored data
func (s *SerialDB) Destroy() error {
log.Debug("serialDB.Destroy", "path", s.path)
//calling close on the SafeCloser instance should be the last instruction called
//(just to close some go routines started as edge cases that would otherwise hang)
defer s.closer.Close()
s.mutBatch.Lock()
s.batch.Reset()
s.sizeBatch = 0
s.mutBatch.Unlock()
s.mutClosed.Lock()
s.closed = true
s.cancel()
err := s.db.Close()
s.mutClosed.Unlock()
if err != nil {
return err
}
err = os.RemoveAll(s.path)
return err
}
// DestroyClosed removes the already closed storage medium stored data
func (s *SerialDB) DestroyClosed() error {
err := os.RemoveAll(s.path)
if err != nil {
log.Error("error destroy closed", "error", err, "path", s.path)
}
return err
}
func (s *SerialDB) processLoop(ctx context.Context) {
for {
select {
case queryer := <-s.dbAccess:
queryer.request(s)
case <-ctx.Done():
log.Debug("processLoop - closing the leveldb process loop", "path", s.path)
return
}
}
}
// IsInterfaceNil returns true if there is no value under the interface
func (s *SerialDB) IsInterfaceNil() bool {
return s == nil
}