-
Notifications
You must be signed in to change notification settings - Fork 199
/
storageunit.go
410 lines (338 loc) · 9.95 KB
/
storageunit.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package storageUnit
import (
"encoding/base64"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/hashing/blake2b"
"github.com/ElrondNetwork/elrond-go-core/hashing/fnv"
"github.com/ElrondNetwork/elrond-go-core/hashing/keccak"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/fifocache"
"github.com/ElrondNetwork/elrond-go/storage/leveldb"
"github.com/ElrondNetwork/elrond-go/storage/lrucache"
"github.com/ElrondNetwork/elrond-go/storage/memorydb"
)
var _ storage.Storer = (*Unit)(nil)
// CacheType represents the type of the supported caches
type CacheType string
// DBType represents the type of the supported databases
type DBType string
// HasherType represents the type of the supported hash functions
type HasherType string
// LRUCache is currently the only supported Cache type
const (
LRUCache CacheType = "LRU"
SizeLRUCache CacheType = "SizeLRU"
FIFOShardedCache CacheType = "FIFOSharded"
)
var log = logger.GetOrCreate("storage/storageUnit")
// LvlDB currently the only supported DBs
// More to be added
const (
LvlDB DBType = "LvlDB"
LvlDBSerial DBType = "LvlDBSerial"
MemoryDB DBType = "MemoryDB"
)
const (
// Keccak is the string representation of the keccak hashing function
Keccak HasherType = "Keccak"
// Blake2b is the string representation of the blake2b hashing function
Blake2b HasherType = "Blake2b"
// Fnv is the string representation of the fnv hashing function
Fnv HasherType = "Fnv"
)
const minimumSizeForLRUCache = 1024
// UnitConfig holds the configurable elements of the storage unit
type UnitConfig struct {
CacheConf CacheConfig
DBConf DBConfig
}
// CacheConfig holds the configurable elements of a cache
type CacheConfig struct {
Name string
Type CacheType
SizeInBytes uint64
SizeInBytesPerSender uint32
Capacity uint32
SizePerSender uint32
Shards uint32
}
// String returns a readable representation of the object
func (config *CacheConfig) String() string {
bytes, err := json.Marshal(config)
if err != nil {
log.Error("CacheConfig.String()", "err", err)
}
return string(bytes)
}
// DBConfig holds the configurable elements of a database
type DBConfig struct {
FilePath string
Type DBType
BatchDelaySeconds int
MaxBatchSize int
MaxOpenFiles int
}
// Unit represents a storer's data bank
// holding the cache and persistence unit
type Unit struct {
lock sync.RWMutex
persister storage.Persister
cacher storage.Cacher
}
// Put adds data to both cache and persistence medium
func (u *Unit) Put(key, data []byte) error {
u.lock.Lock()
defer u.lock.Unlock()
u.cacher.Put(key, data, len(data))
err := u.persister.Put(key, data)
if err != nil {
u.cacher.Remove(key)
return err
}
return err
}
// PutInEpoch will call the Put method as this storer doesn't handle epochs
func (u *Unit) PutInEpoch(key, data []byte, _ uint32) error {
return u.Put(key, data)
}
// GetOldestEpoch will return an error that signals that the oldest epoch fetching is not available
func (u *Unit) GetOldestEpoch() (uint32, error) {
return 0, storage.ErrOldestEpochNotAvailable
}
// Close will close unit
func (u *Unit) Close() error {
u.cacher.Clear()
err := u.persister.Close()
if err != nil {
log.Error("cannot close storage unit persister", "error", err)
return err
}
return nil
}
// RangeKeys can iterate over the persisted (key, value) pairs calling the provided handler
func (u *Unit) RangeKeys(handler func(key []byte, value []byte) bool) {
u.persister.RangeKeys(handler)
}
// Get searches the key in the cache. In case it is not found,
// it further searches it in the associated database.
// In case it is found in the database, the cache is updated with the value as well.
func (u *Unit) Get(key []byte) ([]byte, error) {
u.lock.Lock()
defer u.lock.Unlock()
v, ok := u.cacher.Get(key)
var err error
if !ok {
// not found in cache
// search it in second persistence medium
v, err = u.persister.Get(key)
if err != nil {
return nil, err
}
buff, okAssertion := v.([]byte)
if !okAssertion {
return nil, fmt.Errorf("key: %s is not a byte slice", base64.StdEncoding.EncodeToString(key))
}
// if found in persistence unit, add it in cache
u.cacher.Put(key, v, len(buff))
}
return v.([]byte), nil
}
// GetFromEpoch will call the Get method as this storer doesn't handle epochs
func (u *Unit) GetFromEpoch(key []byte, _ uint32) ([]byte, error) {
return u.Get(key)
}
// GetBulkFromEpoch will call the Get method for all keys as this storer doesn't handle epochs
func (u *Unit) GetBulkFromEpoch(keys [][]byte, _ uint32) (map[string][]byte, error) {
retMap := make(map[string][]byte)
for _, key := range keys {
value, err := u.Get(key)
if err != nil {
log.Warn("cannot get key from unit",
"key", key,
"error", err.Error(),
)
continue
}
retMap[string(key)] = value
}
return retMap, nil
}
// Has checks if the key is in the Unit.
// It first checks the cache. If it is not found, it checks the db
func (u *Unit) Has(key []byte) error {
u.lock.RLock()
defer u.lock.RUnlock()
has := u.cacher.Has(key)
if has {
return nil
}
return u.persister.Has(key)
}
// SearchFirst will call the Get method as this storer doesn't handle epochs
func (u *Unit) SearchFirst(key []byte) ([]byte, error) {
return u.Get(key)
}
// RemoveFromCurrentEpoch removes the data associated to the given key from both cache and persistence medium
func (u *Unit) RemoveFromCurrentEpoch(key []byte) error {
return u.Remove(key)
}
// Remove removes the data associated to the given key from both cache and persistence medium
func (u *Unit) Remove(key []byte) error {
u.lock.Lock()
defer u.lock.Unlock()
u.cacher.Remove(key)
err := u.persister.Remove(key)
return err
}
// ClearCache cleans up the entire cache
func (u *Unit) ClearCache() {
u.cacher.Clear()
}
// DestroyUnit cleans up the cache, and the db
func (u *Unit) DestroyUnit() error {
u.lock.Lock()
defer u.lock.Unlock()
u.cacher.Clear()
return u.persister.Destroy()
}
// IsInterfaceNil returns true if there is no value under the interface
func (u *Unit) IsInterfaceNil() bool {
return u == nil
}
// NewStorageUnit is the constructor for the storage unit, creating a new storage unit
// from the given cacher and persister.
func NewStorageUnit(c storage.Cacher, p storage.Persister) (*Unit, error) {
if check.IfNil(p) {
return nil, storage.ErrNilPersister
}
if check.IfNil(c) {
return nil, storage.ErrNilCacher
}
sUnit := &Unit{
persister: p,
cacher: c,
}
return sUnit, nil
}
// NewStorageUnitFromConf creates a new storage unit from a storage unit config
func NewStorageUnitFromConf(cacheConf CacheConfig, dbConf DBConfig) (*Unit, error) {
var cache storage.Cacher
var db storage.Persister
var err error
defer func() {
if err != nil && db != nil {
_ = db.Destroy()
}
}()
if dbConf.MaxBatchSize > int(cacheConf.Capacity) {
return nil, storage.ErrCacheSizeIsLowerThanBatchSize
}
cache, err = NewCache(cacheConf)
if err != nil {
return nil, err
}
argDB := ArgDB{
DBType: dbConf.Type,
Path: dbConf.FilePath,
BatchDelaySeconds: dbConf.BatchDelaySeconds,
MaxBatchSize: dbConf.MaxBatchSize,
MaxOpenFiles: dbConf.MaxOpenFiles,
}
db, err = NewDB(argDB)
if err != nil {
return nil, err
}
return NewStorageUnit(cache, db)
}
// NewCache creates a new cache from a cache config
func NewCache(config CacheConfig) (storage.Cacher, error) {
storage.MonitorNewCache(config.Name, config.SizeInBytes)
cacheType := config.Type
capacity := config.Capacity
shards := config.Shards
sizeInBytes := config.SizeInBytes
var cacher storage.Cacher
var err error
switch cacheType {
case LRUCache:
if sizeInBytes != 0 {
return nil, storage.ErrLRUCacheWithProvidedSize
}
cacher, err = lrucache.NewCache(int(capacity))
case SizeLRUCache:
if sizeInBytes < minimumSizeForLRUCache {
return nil, fmt.Errorf("%w, provided %d, minimum %d",
storage.ErrLRUCacheInvalidSize,
sizeInBytes,
minimumSizeForLRUCache,
)
}
cacher, err = lrucache.NewCacheWithSizeInBytes(int(capacity), int64(sizeInBytes))
case FIFOShardedCache:
cacher, err = fifocache.NewShardedCache(int(capacity), int(shards))
if err != nil {
return nil, err
}
// add other implementations if required
default:
return nil, storage.ErrNotSupportedCacheType
}
if err != nil {
return nil, err
}
return cacher, nil
}
// ArgDB is a structure that is used to create a new storage.Persister implementation
type ArgDB struct {
DBType DBType
Path string
BatchDelaySeconds int
MaxBatchSize int
MaxOpenFiles int
}
// NewDB creates a new database from database config
func NewDB(argDB ArgDB) (storage.Persister, error) {
var db storage.Persister
var err error
for i := 0; i < common.MaxRetriesToCreateDB; i++ {
switch argDB.DBType {
case LvlDB:
db, err = leveldb.NewDB(argDB.Path, argDB.BatchDelaySeconds, argDB.MaxBatchSize, argDB.MaxOpenFiles)
case LvlDBSerial:
db, err = leveldb.NewSerialDB(argDB.Path, argDB.BatchDelaySeconds, argDB.MaxBatchSize, argDB.MaxOpenFiles)
case MemoryDB:
db = memorydb.New()
default:
return nil, storage.ErrNotSupportedDBType
}
if err == nil {
return db, nil
}
// TODO: extract this in a parameter and inject it
time.Sleep(common.SleepTimeBetweenCreateDBRetries)
}
if err != nil {
return nil, err
}
return db, nil
}
// NewHasher will return a hasher implementation form the string HasherType
func (h HasherType) NewHasher() (hashing.Hasher, error) {
switch h {
case Keccak:
return keccak.NewKeccak(), nil
case Blake2b:
return blake2b.NewBlake2b(), nil
case Fnv:
return fnv.NewFnv(), nil
default:
return nil, storage.ErrNotSupportedHashType
}
}