Skip to content

Commit

Permalink
chore(bolt): add option to skip fsync for test purposes (influxdata#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac authored Jul 24, 2020
1 parent 089ccc0 commit 1b6d9ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
24 changes: 22 additions & 2 deletions bolt/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,33 @@ type KVStore struct {
path string
db *bolt.DB
log *zap.Logger

noSync bool
}

type KVOption func(*KVStore)

// WithNoSync WARNING: this is useful for tests only
// this skips fsyncing on every commit to improve
// write performance in exchange for no guarantees
// that the db will persist.
func WithNoSync(s *KVStore) {
s.noSync = true
}

// NewKVStore returns an instance of KVStore with the file at
// the provided path.
func NewKVStore(log *zap.Logger, path string) *KVStore {
return &KVStore{
func NewKVStore(log *zap.Logger, path string, opts ...KVOption) *KVStore {
store := &KVStore{
path: path,
log: log,
}

for _, opt := range opts {
opt(store)
}

return store
}

// Open creates boltDB file it doesn't exists and opens it otherwise.
Expand All @@ -56,6 +74,8 @@ func (s *KVStore) Open(ctx context.Context) error {
}
s.db = db

db.NoSync = s.noSync

s.log.Info("Resources opened", zap.String("path", s.path))
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func NewTestBoltStore(t *testing.T) (kv.SchemaStore, func(), error) {
ctx := context.Background()
logger := zaptest.NewLogger(t)
path := f.Name()
s := bolt.NewKVStore(logger, path)

// skip fsync to improve test performance
s := bolt.NewKVStore(logger, path, bolt.WithNoSync)
if err := s.Open(context.Background()); err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tenant/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewTestBoltStore(t *testing.T) (kv.SchemaStore, func(), error) {
f.Close()

path := f.Name()
s := bolt.NewKVStore(zaptest.NewLogger(t), path)
s := bolt.NewKVStore(zaptest.NewLogger(t), path, bolt.WithNoSync)
if err := s.Open(context.Background()); err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 1b6d9ab

Please sign in to comment.