Skip to content

Commit

Permalink
feat(filelock): add filelock for dirPath so that other process will n…
Browse files Browse the repository at this point in the history
…ot be able to read/write to the dir
  • Loading branch information
manosriram committed May 18, 2024
1 parent dab9636 commit 5a58dff
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build:
clean:
rm -rf ./nimbusdb_temp* benchmark/nimbusdb_temp*
rm -rf ~/nimbusdb/test_data
mkdir -p ~/nimbusdb/test_data

test:
go test -v -failfast
Expand Down
23 changes: 22 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"syscall"
"time"

"github.com/gofrs/flock"
"github.com/google/btree"
"github.com/hashicorp/golang-lru/v2/expirable"
utils "github.com/manosriram/nimbusdb/utils"
Expand Down Expand Up @@ -67,6 +68,7 @@ func NewDb(dirPath string, opts ...*Options) *Db {
segments: segments,
opts: &Options{
ShouldWatch: false,
Flock: opts[0].Flock,
},
}

Expand Down Expand Up @@ -482,6 +484,7 @@ func (db *Db) createActiveDatafile(dirPath string) error {
}

func (db *Db) handleInterrupt() {
defer db.opts.Flock.Close()
terminateSignal := make(chan os.Signal, 1)
signal.Notify(terminateSignal, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
for {
Expand Down Expand Up @@ -509,10 +512,27 @@ func Open(opts *Options) (*Db, error) {

dirPath = utils.JoinPaths(home, DefaultDataDir)
}

var builder strings.Builder
_, err := fmt.Fprintf(&builder, "%s.%s", dirPath, FlockSuffix)
if err != nil {
return nil, err
}

flock := flock.New(builder.String())
tryLock, err := flock.TryLock()
if err != nil {
return nil, err
}
if !tryLock {
return nil, ERROR_DIRPATH_ALREADY_IN_USE
}
opts.Flock = flock

db := NewDb(dirPath, opts)
go db.handleInterrupt()

err := os.MkdirAll(dirPath, os.ModePerm)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -554,6 +574,7 @@ func Open(opts *Options) (*Db, error) {
// Closes the database. Closes the file pointer used to read/write the activeDataFile.
// Closes all file inactiveDataFile pointers and marks them as closed.
func (db *Db) Close() error {
defer db.opts.Flock.Unlock()
err := db.closeActiveDataFileReader()
if err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions examples/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

const (
// DirPath = "/Users/manosriram/nimbusdb/test_data"
DirPath = "./dd/"
DirPath = "/Users/manosriram/nimbusdb/test_data"
)

func watchKeyChange(ch chan nimbusdb.WatcherEvent) {
Expand All @@ -32,7 +31,10 @@ func watchKeyChange(ch chan nimbusdb.WatcherEvent) {
}

func main() {
d, _ := nimbusdb.Open(&nimbusdb.Options{Path: DirPath, WatchQueueSize: 10})
d, err := nimbusdb.Open(&nimbusdb.Options{Path: DirPath, WatchQueueSize: 10})
if err != nil {
log.Fatalf("error opening dirpath: %s\n", err.Error())
}
defer d.Close()

ch, _ := d.NewWatch()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
Expand All @@ -19,6 +21,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

flock "github.com/gofrs/flock"
"github.com/segmentio/ksuid"
)

Expand All @@ -28,6 +29,7 @@ const (
TempDataFilePattern = "*.dfile"
TempInactiveDataFilePattern = "*.idfile"
DefaultDataDir = "nimbusdb"
FlockSuffix = "flock"

DatafileThreshold = 1 * MB
BlockSize = 32 * KB
Expand Down Expand Up @@ -67,6 +69,7 @@ var (
ERROR_DATA_FILE_WRITER_NOT_CLOSED = errors.New("data file writer is not closed")
ERROR_DATA_FILE_READER_NOT_OPEN = errors.New("data file reader is not open")
ERROR_DATA_FILE_WRITER_NOT_OPEN = errors.New("data file writer is not open")
ERROR_DIRPATH_ALREADY_IN_USE = errors.New("dirpath already in use")
)

var (
Expand Down Expand Up @@ -95,6 +98,7 @@ type Options struct {
Path string
ShouldWatch bool
WatchQueueSize int
Flock *flock.Flock
}

type KeyValuePair struct {
Expand Down

0 comments on commit 5a58dff

Please sign in to comment.