Skip to content

Commit

Permalink
peapod: Optimize read-only mode
Browse files Browse the repository at this point in the history
It makes no sense to repeatedly begin writeable BoltDB transactions and
start flushing routine when underlying BoltDB instance is read-only.

Require root bucket to be presented in the BoltDB instance in read-only
configuration.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Aug 11, 2023
1 parent 2ff8cc8 commit 0438519
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
33 changes: 26 additions & 7 deletions pkg/local_object_storage/peapod/peapod.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type batch struct {
// Peapod is a single low-level key/value database optimized to work with big
// number of stored units.
type Peapod struct {
readOnly bool

bolt *bbolt.DB

currentBatchMtx sync.RWMutex
Expand All @@ -52,14 +54,29 @@ func New(path string, perm fs.FileMode, readOnly bool) (*Peapod, error) {
return nil, fmt.Errorf("open BoltDB instance: %w", err)
}

if !readOnly {
err = db.Update(func(tx *bbolt.Tx) error {
_, err = tx.CreateBucketIfNotExists(rootBucket)
return err
if readOnly {
err = db.View(func(tx *bbolt.Tx) error {
if tx.Bucket(rootBucket) == nil {
return errMissingRootBucket
}
return nil
})
if err != nil {
return nil, fmt.Errorf("create root bucket in BoltDB instance: %w", err)
return nil, fmt.Errorf("check root bucket presence in BoltDB instance: %w", err)
}

return &Peapod{
readOnly: true,
bolt: db,
}, nil
}

err = db.Update(func(tx *bbolt.Tx) error {
_, err = tx.CreateBucketIfNotExists(rootBucket)
return err
})
if err != nil {
return nil, fmt.Errorf("create root bucket in BoltDB instance: %w", err)
}

res := &Peapod{
Expand All @@ -77,8 +94,10 @@ func New(path string, perm fs.FileMode, readOnly bool) (*Peapod, error) {

// Close syncs data and closes the database.
func (x *Peapod) Close() error {
close(x.chClose)
<-x.chFlushDone
if !x.readOnly {
close(x.chClose)
<-x.chFlushDone
}
return x.bolt.Close()
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/local_object_storage/peapod/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (x *Peapod) Delete(ctx context.Context, addr oid.Address) error {
}

func (x *Peapod) batch(ctx context.Context, fBktRoot func(bktRoot *bbolt.Bucket) error) error {
if x.readOnly {
return bbolt.ErrDatabaseReadOnly
}

x.currentBatchMtx.RLock()

currentBatch := x.currentBatch
Expand Down

0 comments on commit 0438519

Please sign in to comment.