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 3d220fa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/onsi/gomega v1.7.0
github.com/open-policy-agent/conftest v0.21.0
github.com/pkg/profile v1.5.0
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.5.0 h1:042Buzk+NhDI+DeSAA62RwJL8VAuZUMQZUjCsRz1Mug=
github.com/pkg/profile v1.5.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
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
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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.")
// 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 3d220fa

Please sign in to comment.