Skip to content

Commit

Permalink
fix: check module existence & add in the same txn
Browse files Browse the repository at this point in the history
This is to prevent a race condition
  • Loading branch information
radeksimko committed Aug 2, 2022
1 parent 7bde727 commit 8b32233
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
27 changes: 21 additions & 6 deletions internal/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ func (s *ModuleStore) Add(modPath string) error {
txn := s.db.Txn(true)
defer txn.Abort()

err := s.add(txn, modPath)
if err != nil {
return err
}
txn.Commit()

return nil
}

func (s *ModuleStore) add(txn *memdb.Txn, modPath string) error {
// TODO: Introduce Exists method to Txn?
obj, err := txn.First(s.tableName, "id", modPath)
if err != nil {
Expand All @@ -252,7 +262,6 @@ func (s *ModuleStore) Add(modPath string) error {
return err
}

txn.Commit()
return nil
}

Expand Down Expand Up @@ -318,19 +327,25 @@ func (s *ModuleStore) ModuleByPath(path string) (*Module, error) {
return mod, nil
}

func (s *ModuleStore) Exists(path string) (bool, error) {
txn := s.db.Txn(false)
func (s *ModuleStore) AddIfNotExists(path string) error {
txn := s.db.Txn(true)
defer txn.Abort()

_, err := moduleByPath(txn, path)
if err != nil {
if IsModuleNotFound(err) {
return false, nil
err := s.add(txn, path)
if err != nil {
return err
}
txn.Commit()
return nil
}

return false, err
return err
}

return true, nil
return nil
}

func (s *ModuleStore) ModuleCalls(modPath string) (tfmod.ModuleCalls, error) {
Expand Down
13 changes: 3 additions & 10 deletions internal/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ type PathStore interface {
}

type ModuleStore interface {
Exists(dir string) (bool, error)
Add(dir string) error
AddIfNotExists(dir string) error
}

func NewWalker(fs fs.ReadDirFS, pathStore PathStore, modStore ModuleStore, walkFunc WalkFunc) *Walker {
Expand Down Expand Up @@ -190,20 +189,14 @@ func (w *Walker) walk(ctx context.Context, dir document.DirHandle) error {
dirIndexed = true
w.logger.Printf("found module %s", dir)

exists, err := w.modStore.Exists(dir.Path())
err := w.modStore.AddIfNotExists(dir.Path())
if err != nil {
return err
}
if !exists {
err := w.modStore.Add(dir.Path())
if err != nil {
return err
}
}

ids, err := w.walkFunc(ctx, dir)
if err != nil {
w.collectError(err)
w.collectError(fmt.Errorf("walkFunc: %w", err))
}
w.collectJobIds(ids)
continue
Expand Down

0 comments on commit 8b32233

Please sign in to comment.