Skip to content

Commit

Permalink
Fix search command hangs and times out.
Browse files Browse the repository at this point in the history
The order of index construction calls was off, leading to a lock on the index before it could be constructed.

Signed-off-by: Lennard Eijsackers <[email protected]>
  • Loading branch information
Blokje5 committed Oct 31, 2020
1 parent 32ed1e0 commit 2375fc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions internal/commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (c *searchConfig) run(query string) error {
}

e, err := search.Load()
defer e.Close()

if err != nil {
return fmt.Errorf("load engine: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestMainCLI(t *testing.T) {
defer gexec.CleanupBuildArtifacts()

t.Run("we can search registries", func(t *testing.T) {
t.Skip("the search command seems to hang.")
command := exec.Command(pathToMainCLI, "search", "k8s", "-r", "./metadata/registries.yml")
session, err := gexec.Start(command, os.Stdout, os.Stdout)
session.Wait()
Expand Down
39 changes: 26 additions & 13 deletions pkg/search/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package search

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -21,10 +22,6 @@ type Engine struct {
// Load loads the engine and initializes the Index.
func Load() (*Engine, error) {
e := &Engine{}
if err := constructIndex(); err != nil {
return nil, fmt.Errorf("construct index: %w", err)
}

index, err := loadIndex()
if err != nil {
return nil, fmt.Errorf("load index: %w", err)
Expand Down Expand Up @@ -53,40 +50,56 @@ func (e *Engine) Query(query string) (*bleve.SearchResult, error) {
return e.index.Search(search)
}

func (e *Engine) Close() error {
return e.index.Close()
}

// constructIndex builds a search index
func constructIndex() error {
func constructIndex() (bleve.Index, error) {
cacheDir := cacheDirectory()
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
return setupIndexDirectory()
}

return nil
return nil, errors.New("called constructIndex while index already exists")
}

// loadIndex loads the index from disk
func loadIndex() (bleve.Index, error) {
return bleve.Open(indexDirectory())
index, err := bleve.Open(indexDirectory())
if err == bleve.ErrorIndexPathDoesNotExist {
index, err := constructIndex();
if err != nil {
return nil, fmt.Errorf("construct index: %w", err)
}

return index, nil
} else if err != nil {
return nil, fmt.Errorf("open index: %w", err)
}

return index, nil
}

// setupIndexDirectory setups the index directory
func setupIndexDirectory() error {
func setupIndexDirectory() (bleve.Index, error) {
cacheDir := cacheDirectory()
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return fmt.Errorf("make search dir: %w", err)
return nil, fmt.Errorf("make search dir: %w", err)
}

_, err := os.Create(filepath.Join(cacheDir, IndexVersion))
if err != nil {
return fmt.Errorf("create version file: %w", err)
return nil, fmt.Errorf("create version file: %w", err)
}

mapping := bleve.NewIndexMapping()
_, err = bleve.New(indexDirectory(), mapping)
index, err := bleve.New(indexDirectory(), mapping)
if err != nil {
return fmt.Errorf("creating index: %w", err)
return nil, fmt.Errorf("creating index: %w", err)
}

return nil
return index, nil
}

// cacheDirectory returns the directory to cache policy-cli configs
Expand Down

0 comments on commit 2375fc4

Please sign in to comment.