Skip to content

Commit

Permalink
fix: more accurate storage metrics after zot restart
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Dodon <[email protected]>
  • Loading branch information
adodon2go committed Oct 30, 2023
1 parent f34af3c commit f77064d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
ext.EnableMetricsExtension(c.Config, c.Log, c.Config.Storage.RootDirectory)
ext.EnableSearchExtension(c.Config, c.StoreController, c.MetaDB, taskScheduler, c.CveScanner, c.Log)
}
// runs once if metrics are enabled
if c.Config.IsMetricsEnabled() {
c.StoreController.DefaultStore.PopulateStorageMetrics(time.Duration(0), taskScheduler)
}

if c.Config.Storage.SubPaths != nil {
for route, storageConfig := range c.Config.Storage.SubPaths {
Expand All @@ -396,6 +400,10 @@ func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
substore := c.StoreController.SubStore[route]
if substore != nil {
substore.RunDedupeBlobs(time.Duration(0), taskScheduler)

if c.Config.IsMetricsEnabled() {
substore.PopulateStorageMetrics(time.Duration(0), taskScheduler)
}
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions pkg/storage/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"path"
"strings"
"time"

"github.com/docker/distribution/registry/storage/driver"
godigest "github.com/opencontainers/go-digest"
Expand All @@ -18,6 +20,7 @@ import (

zerr "zotregistry.io/zot/errors"
zcommon "zotregistry.io/zot/pkg/common"
"zotregistry.io/zot/pkg/extensions/monitoring"
zlog "zotregistry.io/zot/pkg/log"
"zotregistry.io/zot/pkg/scheduler"
storageConstants "zotregistry.io/zot/pkg/storage/constants"
Expand Down Expand Up @@ -1052,3 +1055,76 @@ func (dt *dedupeTask) DoWork(ctx context.Context) error {

return err
}

type StorageMetricsInitGenerator struct {
ImgStore storageTypes.ImageStore
done bool
Metrics monitoring.MetricServer
lastRepo string
nextRun time.Time
rand *rand.Rand
Log zlog.Logger
}

func (gen *StorageMetricsInitGenerator) getRandomDelay() int {
maxDelay := 30

return gen.rand.Intn(maxDelay)
}

func (gen *StorageMetricsInitGenerator) Next() (scheduler.Task, error) {
if gen.lastRepo == "" && gen.nextRun.IsZero() {
gen.rand = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) //nolint: gosec
}

delay := gen.getRandomDelay()

gen.nextRun = time.Now().Add(time.Duration(delay) * time.Second)

repo, err := gen.ImgStore.GetNextRepository(gen.lastRepo)
if err != nil {
return nil, err
}

Check warning on line 1087 in pkg/storage/common/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/common/common.go#L1086-L1087

Added lines #L1086 - L1087 were not covered by tests

gen.Log.Debug().Str("repo", repo).Int("randomDelay", delay).Msg("StorageMetricsInitGenerator")

if repo == "" {
gen.done = true

return nil, nil
}
gen.lastRepo = repo

return NewStorageMetricsTask(gen.ImgStore, gen.Metrics, repo), nil

Check warning on line 1098 in pkg/storage/common/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/common/common.go#L1096-L1098

Added lines #L1096 - L1098 were not covered by tests
}

func (gen *StorageMetricsInitGenerator) IsDone() bool {
return gen.done
}

func (gen *StorageMetricsInitGenerator) IsReady() bool {
return true
}

func (gen *StorageMetricsInitGenerator) Reset() {
gen.lastRepo = ""
gen.done = false
}

type smTask struct {
imgStore storageTypes.ImageStore
metrics monitoring.MetricServer
repo string
}

func NewStorageMetricsTask(imgStore storageTypes.ImageStore, metrics monitoring.MetricServer, repo string,
) *smTask {
return &smTask{imgStore, metrics, repo}

Check warning on line 1122 in pkg/storage/common/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/common/common.go#L1121-L1122

Added lines #L1121 - L1122 were not covered by tests
}

func (smt *smTask) DoWork(ctx context.Context) error {
// run task
monitoring.SetStorageUsage(smt.metrics, smt.imgStore.RootDir(), smt.repo)

return nil

Check warning on line 1129 in pkg/storage/common/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/common/common.go#L1125-L1129

Added lines #L1125 - L1129 were not covered by tests
}
10 changes: 10 additions & 0 deletions pkg/storage/imagestore/imagestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,16 @@ func (is *ImageStore) RunDedupeBlobs(interval time.Duration, sch *scheduler.Sche
sch.SubmitGenerator(generator, interval, scheduler.MediumPriority)
}

func (is *ImageStore) PopulateStorageMetrics(interval time.Duration, sch *scheduler.Scheduler) {
generator := &common.StorageMetricsInitGenerator{
ImgStore: is,
Metrics: is.metrics,
Log: is.log,
}

sch.SubmitGenerator(generator, interval, scheduler.LowPriority)
}

type blobStream struct {
reader io.Reader
closer io.Closer
Expand Down
1 change: 1 addition & 0 deletions pkg/storage/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ImageStore interface { //nolint:interfacebloat
RunDedupeForDigest(digest godigest.Digest, dedupe bool, duplicateBlobs []string) error
GetNextDigestWithBlobPaths(repos []string, lastDigests []godigest.Digest) (godigest.Digest, []string, error)
GetAllBlobs(repo string) ([]string, error)
PopulateStorageMetrics(interval time.Duration, sch *scheduler.Scheduler)
}

type Driver interface { //nolint:interfacebloat
Expand Down
7 changes: 7 additions & 0 deletions pkg/test/mocks/image_store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type MockedImageStore struct {
GetAllBlobsFn func(repo string) ([]string, error)
CleanupRepoFn func(repo string, blobs []godigest.Digest, removeRepo bool) (int, error)
PutIndexContentFn func(repo string, index ispec.Index) error
RunStorageMetricsFn func(interval time.Duration, sch *scheduler.Scheduler)
}

func (is MockedImageStore) Lock(t *time.Time) {
Expand Down Expand Up @@ -405,3 +406,9 @@ func (is MockedImageStore) PutIndexContent(repo string, index ispec.Index) error

return nil
}

func (is MockedImageStore) PopulateStorageMetrics(interval time.Duration, sch *scheduler.Scheduler) {
if is.RunStorageMetricsFn != nil {
is.RunStorageMetricsFn(interval, sch)
}
}

0 comments on commit f77064d

Please sign in to comment.