Skip to content

Commit 3a98167

Browse files
committed
indexer: Cache scanner table
During the indexing cycle many operations try to read information from the scanner table, this can occasionally lead to resource contention depending on the DB. This reads the table into memory when the indexer store is instanciated and uses when asked for scanner info. Signed-off-by: crozzy <[email protected]>
1 parent 49f1591 commit 3a98167

13 files changed

+237
-290
lines changed

datastore/postgres/distributionsbylayer.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ var (
3939

4040
func (s *IndexerStore) DistributionsByLayer(ctx context.Context, hash claircore.Digest, scnrs indexer.VersionedScanners) ([]*claircore.Distribution, error) {
4141
const (
42-
selectScanner = `
43-
SELECT id
44-
FROM scanner
45-
WHERE name = $1
46-
AND version = $2
47-
AND kind = $3;
48-
`
4942
query = `
5043
SELECT dist.id,
5144
dist.name,
@@ -68,17 +61,9 @@ func (s *IndexerStore) DistributionsByLayer(ctx context.Context, hash claircore.
6861
return []*claircore.Distribution{}, nil
6962
}
7063

71-
// get scanner ids
72-
scannerIDs := make([]int64, len(scnrs))
73-
for i, scnr := range scnrs {
74-
start := time.Now()
75-
err := s.pool.QueryRow(ctx, selectScanner, scnr.Name(), scnr.Version(), scnr.Kind()).
76-
Scan(&scannerIDs[i])
77-
if err != nil {
78-
return nil, fmt.Errorf("failed to retrieve distribution ids for scanner %q: %w", scnr, err)
79-
}
80-
distributionByLayerCounter.WithLabelValues("selectScanner").Add(1)
81-
distributionByLayerDuration.WithLabelValues("selectScanner").Observe(time.Since(start).Seconds())
64+
scannerIDs, err := s.selectScanners(scnrs)
65+
if err != nil {
66+
return nil, err
8267
}
8368

8469
start := time.Now()

datastore/postgres/filesbylayer.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ var (
3838

3939
func (s *IndexerStore) FilesByLayer(ctx context.Context, hash claircore.Digest, scnrs indexer.VersionedScanners) ([]claircore.File, error) {
4040
const (
41-
selectScanner = `
42-
SELECT id
43-
FROM scanner
44-
WHERE name = $1
45-
AND version = $2
46-
AND kind = $3;
47-
`
4841
query = `
4942
SELECT file.path, file.kind
5043
FROM file_scanartifact
@@ -59,17 +52,9 @@ func (s *IndexerStore) FilesByLayer(ctx context.Context, hash claircore.Digest,
5952
return []claircore.File{}, nil
6053
}
6154

62-
// get scanner ids
63-
scannerIDs := make([]int64, len(scnrs))
64-
for i, scnr := range scnrs {
65-
start := time.Now()
66-
err := s.pool.QueryRow(ctx, selectScanner, scnr.Name(), scnr.Version(), scnr.Kind()).
67-
Scan(&scannerIDs[i])
68-
filesByLayerCounter.WithLabelValues("selectScanner").Add(1)
69-
filesByLayerDuration.WithLabelValues("selectScanner").Observe(time.Since(start).Seconds())
70-
if err != nil {
71-
return nil, fmt.Errorf("failed to retrieve file ids for scanner %q: %w", scnr, err)
72-
}
55+
scannerIDs, err := s.selectScanners(scnrs)
56+
if err != nil {
57+
return nil, err
7358
}
7459

7560
start := time.Now()

0 commit comments

Comments
 (0)