Skip to content

Commit

Permalink
Make MutexMap shards configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Nov 25, 2020
1 parent 61557ac commit 5002d77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions storage/badger_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ type BadgerStorage struct {
encoder *Encoder
compress bool

writer *utils.MutexMap
writer *utils.MutexMap
writerShards int

// Track the closed status to ensure we exit garbage
// collection when the db closes.
Expand Down Expand Up @@ -197,12 +198,16 @@ func NewBadgerStorage(
closed: make(chan struct{}),
pool: NewBufferPool(),
compress: true,
writer: utils.NewMutexMap(utils.DefaultShards),
writerShards: utils.DefaultShards,
}
for _, opt := range storageOptions {
opt(b)
}

// Initialize utis.MutexMap used to track granular
// write transactions.
b.writer = utils.NewMutexMap(b.writerShards)

db, err := badger.Open(b.badgerOptions)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrDatabaseOpenFailed, err)
Expand Down
10 changes: 10 additions & 0 deletions storage/badger_storage_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ func WithCustomSettings(settings badger.Options) BadgerOption {
b.badgerOptions = settings
}
}

// WithWriterShards overrides the default shards used
// in the writer utils.MutexMap. It is recommended
// to set this value to your write concurrency to prevent
// lock contention.
func WithWriterShards(shards int) BadgerOption {
return func(b *BadgerStorage) {
b.writerShards = shards
}
}

0 comments on commit 5002d77

Please sign in to comment.