mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
fix periodic background tasks - gc and scrub
Signed-off-by: Andreea-Lupu <andreealupu1470@yahoo.com>
This commit is contained in:
committed by
Ramkumar Chinchani
parent
d0b52612a2
commit
081ba0b2f2
+128
-12
@@ -20,6 +20,7 @@ import (
|
||||
"zotregistry.io/zot/errors"
|
||||
"zotregistry.io/zot/pkg/api/config"
|
||||
ext "zotregistry.io/zot/pkg/extensions"
|
||||
extconf "zotregistry.io/zot/pkg/extensions/config"
|
||||
"zotregistry.io/zot/pkg/extensions/monitoring"
|
||||
"zotregistry.io/zot/pkg/log"
|
||||
"zotregistry.io/zot/pkg/storage"
|
||||
@@ -350,23 +351,13 @@ func (c *Controller) Shutdown() {
|
||||
}
|
||||
|
||||
func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
|
||||
// Enable running garbage-collect periodically for DefaultStore
|
||||
if c.Config.Storage.GC && c.Config.Storage.GCInterval != 0 {
|
||||
c.StoreController.DefaultStore.RunGCPeriodically(c.Config.Storage.GCInterval)
|
||||
}
|
||||
|
||||
// Enable extensions if extension config is provided for DefaultStore
|
||||
if c.Config != nil && c.Config.Extensions != nil {
|
||||
ext.EnableExtensions(c.Config, c.Log, c.Config.Storage.RootDirectory)
|
||||
}
|
||||
|
||||
if c.Config.Storage.SubPaths != nil {
|
||||
for route, storageConfig := range c.Config.Storage.SubPaths {
|
||||
// Enable running garbage-collect periodically for subImageStore
|
||||
if storageConfig.GC && storageConfig.GCInterval != 0 {
|
||||
c.StoreController.SubStore[route].RunGCPeriodically(storageConfig.GCInterval)
|
||||
}
|
||||
|
||||
for _, storageConfig := range c.Config.Storage.SubPaths {
|
||||
// Enable extensions if extension config is provided for subImageStore
|
||||
if c.Config != nil && c.Config.Extensions != nil {
|
||||
ext.EnableExtensions(c.Config, c.Log, storageConfig.RootDirectory)
|
||||
@@ -382,6 +373,131 @@ func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
|
||||
}
|
||||
|
||||
if c.Config.Extensions != nil {
|
||||
ext.EnableScrubExtension(c.Config, c.StoreController, c.Log)
|
||||
ext.EnableScrubExtension(c.Config, c.Log, false, nil, "")
|
||||
}
|
||||
|
||||
go StartPeriodicTasks(c.StoreController.DefaultStore, c.StoreController.SubStore, c.Config.Storage.SubPaths,
|
||||
c.Config.Storage.GC, c.Config.Storage.GCInterval, c.Config.Extensions, c.Log)
|
||||
}
|
||||
|
||||
func StartPeriodicTasks(defaultStore storage.ImageStore, subStore map[string]storage.ImageStore,
|
||||
subPaths map[string]config.StorageConfig, gcEnabled bool, gcInterval time.Duration,
|
||||
extensions *extconf.ExtensionConfig, log log.Logger,
|
||||
) {
|
||||
// start periodic gc and/or scrub for DefaultStore
|
||||
StartPeriodicTasksForImageStore(defaultStore, gcEnabled, gcInterval, extensions, log)
|
||||
|
||||
for route, storageConfig := range subPaths {
|
||||
// Enable running garbage-collect or/and scrub periodically for subImageStore
|
||||
StartPeriodicTasksForImageStore(subStore[route], storageConfig.GC, storageConfig.GCInterval, extensions, log)
|
||||
}
|
||||
}
|
||||
|
||||
func StartPeriodicTasksForImageStore(imageStore storage.ImageStore, configGC bool, configGCInterval time.Duration,
|
||||
extensions *extconf.ExtensionConfig, log log.Logger,
|
||||
) {
|
||||
scrubInterval := time.Duration(0)
|
||||
gcInterval := time.Duration(0)
|
||||
|
||||
gc := false
|
||||
scrub := false
|
||||
|
||||
if configGC && configGCInterval != 0 {
|
||||
gcInterval = configGCInterval
|
||||
gc = true
|
||||
}
|
||||
|
||||
if extensions != nil && extensions.Scrub != nil && extensions.Scrub.Interval != 0 {
|
||||
scrubInterval = extensions.Scrub.Interval
|
||||
scrub = true
|
||||
}
|
||||
|
||||
interval := minPeriodicInterval(scrub, gc, scrubInterval, gcInterval)
|
||||
if interval == time.Duration(0) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Msg(fmt.Sprintf("Periodic interval for %s set to %s", imageStore.RootDir(), interval))
|
||||
|
||||
var lastGC, lastScrub time.Time
|
||||
|
||||
for {
|
||||
log.Info().Msg(fmt.Sprintf("Starting periodic background tasks for %s", imageStore.RootDir()))
|
||||
|
||||
// Enable running garbage-collect or/and scrub periodically for imageStore
|
||||
RunBackgroundTasks(imageStore, gc, scrub, log)
|
||||
|
||||
log.Info().Msg(fmt.Sprintf("Finishing periodic background tasks for %s", imageStore.RootDir()))
|
||||
|
||||
if gc {
|
||||
lastGC = time.Now()
|
||||
}
|
||||
|
||||
if scrub {
|
||||
lastScrub = time.Now()
|
||||
}
|
||||
|
||||
time.Sleep(interval)
|
||||
|
||||
if !lastGC.IsZero() && time.Since(lastGC) >= gcInterval {
|
||||
gc = true
|
||||
}
|
||||
|
||||
if !lastScrub.IsZero() && time.Since(lastScrub) >= scrubInterval {
|
||||
scrub = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RunBackgroundTasks(imgStore storage.ImageStore, gc, scrub bool, log log.Logger) {
|
||||
repos, err := imgStore.GetRepositories()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg(fmt.Sprintf("error while running background task for %s", imgStore.RootDir()))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if gc {
|
||||
start := time.Now()
|
||||
|
||||
// run gc for this repo
|
||||
imgStore.RunGCRepo(repo)
|
||||
|
||||
elapsed := time.Since(start)
|
||||
log.Info().Msg(fmt.Sprintf("gc for %s executed in %s", repo, elapsed))
|
||||
time.Sleep(1 * time.Minute)
|
||||
}
|
||||
|
||||
if scrub {
|
||||
start := time.Now()
|
||||
|
||||
// run scrub for this repo
|
||||
ext.EnableScrubExtension(nil, log, true, imgStore, repo)
|
||||
|
||||
elapsed := time.Since(start)
|
||||
log.Info().Msg(fmt.Sprintf("scrub for %s executed in %s", repo, elapsed))
|
||||
time.Sleep(1 * time.Minute)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func minPeriodicInterval(scrub, gc bool, scrubInterval, gcInterval time.Duration) time.Duration {
|
||||
if scrub && gc {
|
||||
if scrubInterval <= gcInterval {
|
||||
return scrubInterval
|
||||
}
|
||||
|
||||
return gcInterval
|
||||
}
|
||||
|
||||
if scrub {
|
||||
return scrubInterval
|
||||
}
|
||||
|
||||
if gc {
|
||||
return gcInterval
|
||||
}
|
||||
|
||||
return time.Duration(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user