Skip to content

Commit

Permalink
fix: parallelize inventory verification
Browse files Browse the repository at this point in the history
Inventory verification is an expensive step and can be parallelized.

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed Feb 7, 2024
1 parent b4fae2e commit c69d3f9
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions pkg/fs/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/rs/zerolog/log"
"sigs.k8s.io/bom/pkg/spdx"
Expand Down Expand Up @@ -92,7 +94,8 @@ func Verify(input, inventory, missing string) error {

mdoc := bom.NewDocument("", "")
mdoc.Name = "missing-files-document"
mcount := 0
var mcount atomic.Uint64

Check failure on line 97 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / lint

declarations should never be cuddled (wsl)

Check failure on line 97 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / build (1.20, linux, amd64)

declarations should never be cuddled (wsl)
var wg sync.WaitGroup

Check failure on line 98 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / lint

declarations should never be cuddled (wsl)

Check failure on line 98 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / build (1.20, linux, amd64)

declarations should never be cuddled (wsl)

for _, entry := range inv.Entries {
mode, err := strconv.ParseInt(entry.Mode, 8, 32)
Expand All @@ -106,26 +109,35 @@ func Verify(input, inventory, missing string) error {
continue
}

if err := checkBOM(input, entry.Path); err != nil {
log.Error().Err(err).Str("path", entry.Path).Msg("inventory verify failed")
mcount++
sfile := spdx.NewFile()
sfile.SetEntity(
&spdx.Entity{
Name: entry.Path,
Checksum: map[string]string{"SHA256": strings.Split(entry.Checksum, ":")[1]},
},
)

if err := mdoc.AddFile(sfile); err != nil {
log.Error().Err(err).Msg("unable to add file to package")

return err
wg.Add(1)

go func(entry Entry) error {
defer wg.Done()
if err := checkBOM(input, entry.Path); err != nil {

Check failure on line 116 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)

Check failure on line 116 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / build (1.20, linux, amd64)

if statements should only be cuddled with assignments (wsl)
log.Error().Err(err).Str("path", entry.Path).Msg("inventory verify failed")
mcount.Add(1)
sfile := spdx.NewFile()
sfile.SetEntity(
&spdx.Entity{
Name: entry.Path,
Checksum: map[string]string{"SHA256": strings.Split(entry.Checksum, ":")[1]},
},
)

if err := mdoc.AddFile(sfile); err != nil {
log.Error().Err(err).Msg("unable to add file to package")

return err
}
}
}

return nil
}(entry)

Check failure on line 135 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)

Check failure on line 135 in pkg/fs/verify.go

View workflow job for this annotation

GitHub Actions / build (1.20, linux, amd64)

Error return value is not checked (errcheck)
}

if mcount != 0 {
wg.Wait()

if mcount.Load() != 0 {
if missing != "" {
if err := bom.WriteDocument(mdoc, missing); err != nil {
log.Error().Err(err).Str("path", missing).Msg("unable to writing missing entries")
Expand Down

0 comments on commit c69d3f9

Please sign in to comment.