-
Notifications
You must be signed in to change notification settings - Fork 12
/
bucket_manager.go
321 lines (251 loc) · 9.24 KB
/
bucket_manager.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
package prunable
import (
"os"
"github.com/zyedidia/generic/cache"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/ioutils"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/storage/database"
iotago "github.com/iotaledger/iota.go/v4"
)
type BucketManager struct {
openDBsCache *cache.Cache[iotago.EpochIndex, *database.DBInstance]
openDBsCacheMutex syncutils.RWMutex
openDBs *shrinkingmap.ShrinkingMap[iotago.EpochIndex, *database.DBInstance]
lastPrunedEpoch *model.EvictionIndex[iotago.EpochIndex]
lastPrunedMutex syncutils.RWMutex
dbConfig database.Config
errorHandler func(error)
dbSizes *shrinkingmap.ShrinkingMap[iotago.EpochIndex, int64]
optsMaxOpenDBs int
mutex syncutils.RWMutex
}
func NewBucketManager(dbConfig database.Config, errorHandler func(error), opts ...options.Option[BucketManager]) *BucketManager {
return options.Apply(&BucketManager{
optsMaxOpenDBs: 5,
dbConfig: dbConfig,
errorHandler: errorHandler,
openDBs: shrinkingmap.New[iotago.EpochIndex, *database.DBInstance](),
dbSizes: shrinkingmap.New[iotago.EpochIndex, int64](),
lastPrunedEpoch: model.NewEvictionIndex[iotago.EpochIndex](),
}, opts, func(m *BucketManager) {
// We use an LRU cache to try closing unnecessary databases.
m.openDBsCache = cache.New[iotago.EpochIndex, *database.DBInstance](m.optsMaxOpenDBs)
//nolint:revive
m.openDBsCache.SetEvictCallback(func(baseIndex iotago.EpochIndex, db *database.DBInstance) {
db.Close()
})
})
}
// IsTooOld checks if the index is in a pruned epoch.
func (b *BucketManager) IsTooOld(epoch iotago.EpochIndex) (isTooOld bool) {
b.lastPrunedMutex.RLock()
defer b.lastPrunedMutex.RUnlock()
return epoch < b.lastPrunedEpoch.NextIndex()
}
func (b *BucketManager) Get(epoch iotago.EpochIndex, realm kvstore.Realm) (kvstore.KVStore, error) {
if b.IsTooOld(epoch) {
return nil, ierrors.WithMessagef(database.ErrEpochPruned, "epoch %d", epoch)
}
kv := newBucketedKVStore(b, b.getDBInstance(epoch).KVStore())
return lo.PanicOnErr(kv.WithExtendedRealm(realm)), nil
}
func (b *BucketManager) Lock() {
// Lock b.mutex so that a new DBInstance is not created
b.mutex.Lock()
// Lock b.openDBsCacheMutex so that DBInstance is not retrieved from cache
b.openDBsCacheMutex.Lock()
// Lock access to all KVStores so that they can't be reopened by components that store references to them (e.g., StateDiff)
b.openDBs.ForEach(func(_ iotago.EpochIndex, db *database.DBInstance) bool {
db.LockAccess()
return true
})
}
func (b *BucketManager) Unlock() {
b.openDBs.ForEach(func(_ iotago.EpochIndex, db *database.DBInstance) bool {
db.UnlockAccess()
return true
})
b.openDBsCacheMutex.Unlock()
b.mutex.Unlock()
}
func (b *BucketManager) CloseWithoutLocking() {
b.openDBs.ForEach(func(epoch iotago.EpochIndex, db *database.DBInstance) bool {
db.CloseWithoutLocking()
b.openDBsCache.Remove(epoch)
return true
})
}
func (b *BucketManager) Shutdown() {
b.openDBsCacheMutex.Lock()
defer b.openDBsCacheMutex.Unlock()
b.openDBs.ForEach(func(epoch iotago.EpochIndex, db *database.DBInstance) bool {
db.Shutdown()
b.openDBsCache.Remove(epoch)
b.openDBs.Delete(epoch)
return true
})
}
// TotalSize returns the size of the prunable storage containing all db instances.
func (b *BucketManager) TotalSize() int64 {
// Sum up all the evicted databases
var sum int64
b.dbSizes.ForEach(func(_ iotago.EpochIndex, i int64) bool {
sum += i
return true
})
// Add up all the open databases
b.openDBs.ForEach(func(key iotago.EpochIndex, _ *database.DBInstance) bool {
size, err := dbPrunableDirectorySize(b.dbConfig.Directory, key)
if err != nil {
b.errorHandler(ierrors.Wrapf(err, "dbPrunableDirectorySize failed for epoch %d in directory %s", key, b.dbConfig.Directory))
}
sum += size
return true
})
return sum
}
func (b *BucketManager) BucketSize(epoch iotago.EpochIndex) (int64, error) {
size, exists := b.dbSizes.Get(epoch)
if exists {
return size, nil
}
_, exists = b.openDBs.Get(epoch)
if !exists {
return 0, nil
// TODO: this should be fixed by https://github.com/iotaledger/iota.go/pull/480
// return 0, ierrors.Errorf("bucket does not exists: %d", epoch)
}
size, err := dbPrunableDirectorySize(b.dbConfig.Directory, epoch)
if err != nil {
return 0, ierrors.Wrapf(err, "dbPrunableDirectorySize failed for epoch %d in directory %s", epoch, b.dbConfig.Directory)
}
return size, nil
}
func (b *BucketManager) LastPrunedEpoch() (epoch iotago.EpochIndex, hasPruned bool) {
b.lastPrunedMutex.RLock()
defer b.lastPrunedMutex.RUnlock()
return b.lastPrunedEpoch.Index()
}
func (b *BucketManager) RestoreFromDisk() (lastPrunedEpoch iotago.EpochIndex) {
b.lastPrunedMutex.Lock()
defer b.lastPrunedMutex.Unlock()
dbInfos := getSortedDBInstancesFromDisk(b.dbConfig.Directory)
// There are no dbInstances on disk -> nothing to restore.
if len(dbInfos) == 0 {
return
}
// Set the maxPruned epoch to the baseEpoch-1 of the oldest dbInstance.
// Leave the lastPrunedEpoch at the default value if the oldest dbInstance is at baseEpoch 0, which is not pruned yet.
if dbInfos[0].baseEpoch > 0 {
lastPrunedEpoch = dbInfos[0].baseEpoch - 1
b.lastPrunedEpoch.MarkEvicted(lastPrunedEpoch)
}
// Open all the dbInstances (perform health checks) and add them to the openDBs cache. Also fills the dbSizes map (when evicted from the cache).
for _, dbInfo := range dbInfos {
b.getDBInstance(dbInfo.baseEpoch)
}
return
}
// getDBInstance returns the DB instance for the given epochIndex or creates a new one if it does not yet exist.
// DBs are created as follows where each db is located in m.basedir/<starting epochIndex>/
//
// epochIndex 0 -> db 0
// epochIndex 1 -> db 1
// epochIndex 2 -> db 2
func (b *BucketManager) getDBInstance(epoch iotago.EpochIndex) *database.DBInstance {
// Lock global mutex to prevent closing and copying storage data on disk during engine switching.
b.mutex.RLock()
defer b.mutex.RUnlock()
// Try to retrieve the DBInstance from cache in the first step.
// This puts the DBInstance at the front of the LRU cache.
b.openDBsCacheMutex.Lock()
if db, exists := b.openDBsCache.Get(epoch); exists {
b.openDBsCacheMutex.Unlock()
return db
}
b.openDBsCacheMutex.Unlock()
openedCallback := func(d *database.DBInstance) {
b.openDBsCacheMutex.Lock()
defer b.openDBsCacheMutex.Unlock()
// Mark the db as used in the cache.
b.openDBsCache.Put(epoch, d)
// Remove the cached db size since we will open the db.
b.dbSizes.Delete(epoch)
}
// check if exists again, as another goroutine might have created it in parallel
db, created := b.openDBs.GetOrCreate(epoch, func() *database.DBInstance {
return database.NewDBInstance(b.dbConfig.WithDirectory(dbPathFromIndex(b.dbConfig.Directory, epoch)), openedCallback)
})
if created {
// Call openedCallback here instead of inside NewDBInstance to avoid a deadlock due to
// locking `b.openDBs` and `b.openDBsCacheMutex` in different order.
openedCallback(db)
}
return db
}
func (b *BucketManager) Prune(epoch iotago.EpochIndex) error {
b.lastPrunedMutex.Lock()
defer b.lastPrunedMutex.Unlock()
if epoch < lo.Return1(b.lastPrunedEpoch.Index()) {
return ierrors.WithMessagef(database.ErrNoPruningNeeded, "epoch %d is already pruned", epoch)
}
b.DeleteBucket(epoch)
b.lastPrunedEpoch.MarkEvicted(epoch)
return nil
}
// DeleteBucket deletes directory that stores the data for the given bucket and returns boolean
// flag indicating whether a directory for that bucket existed.
func (b *BucketManager) DeleteBucket(epoch iotago.EpochIndex) (deleted bool) {
b.openDBsCacheMutex.Lock()
defer b.openDBsCacheMutex.Unlock()
// Check if the directory exists
exists, isDir, err := ioutils.PathExists(dbPathFromIndex(b.dbConfig.Directory, epoch))
if err != nil {
panic(err)
}
if !exists {
return false
}
if !isDir {
panic(ierrors.Errorf("given path is a file instead of a directory %s", dbPathFromIndex(b.dbConfig.Directory, epoch)))
}
db, exists := b.openDBs.Get(epoch)
if exists {
db.Shutdown()
b.openDBsCache.Remove(epoch)
b.openDBs.Delete(epoch)
}
if err := os.RemoveAll(dbPathFromIndex(b.dbConfig.Directory, epoch)); err != nil {
panic(err)
}
// Delete the db size since we pruned the whole directory
b.dbSizes.Delete(epoch)
return true
}
// PruneSlots prunes the data of all slots in the range [from, to] in the given epoch.
func (b *BucketManager) PruneSlots(epoch iotago.EpochIndex, startPruneRange iotago.SlotIndex, endPruneRange iotago.SlotIndex) error {
epochStore := b.getDBInstance(epoch).KVStore()
for slot := startPruneRange; slot <= endPruneRange; slot++ {
if err := epochStore.DeletePrefix(slot.MustBytes()); err != nil {
return ierrors.Wrapf(err, "error while clearing slot %d in bucket for epoch %d", slot, epoch)
}
}
_ = b.Flush()
return nil
}
func (b *BucketManager) Flush() error {
var innerErr error
b.openDBs.ForEach(func(_ iotago.EpochIndex, db *database.DBInstance) bool {
if err := db.KVStore().Flush(); err != nil {
innerErr = err
}
return true
})
return innerErr
}