Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Fix KVStore reopening of BucketManager's DBInstances. #545

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/storage/database/openablekvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s *openableKVStore) instance() kvstore.KVStore {
parent := s.topParent()

if parent.dbInstance.isClosed.Load() {
parent.dbInstance.Lock()
defer parent.dbInstance.Unlock()

parent.dbInstance.Open()
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/storage/prunable/bucket_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ func (b *BucketManager) Get(epoch iotago.EpochIndex, realm kvstore.Realm) (kvsto
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 all KVStores so that they can't be reopened by components that store references to them (e.g., StateDiff)
b.openDBs.ForEach(func(epoch iotago.EpochIndex, db *database.DBInstance) bool {
db.Lock()

return true
})
}

func (b *BucketManager) Unlock() {
b.mutex.Unlock()
b.openDBsCacheMutex.Unlock()

b.openDBs.ForEach(func(epoch iotago.EpochIndex, db *database.DBInstance) bool {
db.Unlock()

return true
})
}

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()
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/prunable/prunable.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func Clone(source *Prunable, dbConfig database.Config, apiProvider iotago.APIPro
source.semiPermanentDB.Lock()
defer source.semiPermanentDB.Unlock()

source.prunableSlotStore.mutex.Lock()
defer source.prunableSlotStore.mutex.Unlock()
source.prunableSlotStore.Lock()
defer source.prunableSlotStore.Unlock()

// Close forked prunable storage before copying its contents.
source.semiPermanentDB.CloseWithoutLocking()
source.prunableSlotStore.Shutdown()
source.prunableSlotStore.CloseWithoutLocking()

// Copy the storage on disk to new location.
if err := copydir.Copy(source.prunableSlotStore.dbConfig.Directory, dbConfig.Directory); err != nil {
Expand Down
Loading