mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-20 06:54:32 +00:00
Track running backup count via BackupTracker
This avoids an unnecessary apiserver List call when the backup reconciler is already at capacity. Signed-off-by: Scott Seago <sseago@redhat.com>
This commit is contained in:
@@ -25,45 +25,90 @@ import (
|
||||
|
||||
// BackupTracker keeps track of in-progress backups.
|
||||
type BackupTracker interface {
|
||||
// Add informs the tracker that a backup is ReadyToStart.
|
||||
AddReadyToStart(ns, name string)
|
||||
// Add informs the tracker that a backup is in progress.
|
||||
Add(ns, name string)
|
||||
// Delete informs the tracker that a backup is no longer in progress.
|
||||
// Add informs the tracker that a backup has moved beyond InProgress
|
||||
AddPostProcessing(ns, name string)
|
||||
// Delete informs the tracker that a backup has reached a terminal state.
|
||||
Delete(ns, name string)
|
||||
// Contains returns true if the tracker is tracking the backup.
|
||||
// Contains returns true if backup is InProgress or post-InProgress
|
||||
Contains(ns, name string) bool
|
||||
// RunningCount returns the number of backups which are ReadyToStart or InProgress
|
||||
RunningCount() int
|
||||
}
|
||||
|
||||
type backupTracker struct {
|
||||
lock sync.RWMutex
|
||||
backups sets.Set[string]
|
||||
lock sync.RWMutex
|
||||
readyToStartBackups sets.Set[string]
|
||||
inProgressBackups sets.Set[string]
|
||||
postProgressBackups sets.Set[string]
|
||||
}
|
||||
|
||||
// NewBackupTracker returns a new BackupTracker.
|
||||
func NewBackupTracker() BackupTracker {
|
||||
return &backupTracker{
|
||||
backups: sets.New[string](),
|
||||
readyToStartBackups: sets.New[string](),
|
||||
inProgressBackups: sets.New[string](),
|
||||
postProgressBackups: sets.New[string](),
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *backupTracker) AddReadyToStart(ns, name string) {
|
||||
bt.lock.Lock()
|
||||
defer bt.lock.Unlock()
|
||||
|
||||
bt.readyToStartBackups.Insert(backupTrackerKey(ns, name))
|
||||
}
|
||||
|
||||
func (bt *backupTracker) Add(ns, name string) {
|
||||
bt.lock.Lock()
|
||||
defer bt.lock.Unlock()
|
||||
|
||||
bt.backups.Insert(backupTrackerKey(ns, name))
|
||||
key := backupTrackerKey(ns, name)
|
||||
bt.readyToStartBackups.Delete(key)
|
||||
bt.inProgressBackups.Insert(key)
|
||||
}
|
||||
|
||||
func (bt *backupTracker) AddPostProcessing(ns, name string) {
|
||||
bt.lock.Lock()
|
||||
defer bt.lock.Unlock()
|
||||
|
||||
key := backupTrackerKey(ns, name)
|
||||
bt.readyToStartBackups.Delete(key)
|
||||
bt.inProgressBackups.Delete(key)
|
||||
bt.postProgressBackups.Insert(key)
|
||||
}
|
||||
|
||||
func (bt *backupTracker) Delete(ns, name string) {
|
||||
bt.lock.Lock()
|
||||
defer bt.lock.Unlock()
|
||||
|
||||
bt.backups.Delete(backupTrackerKey(ns, name))
|
||||
key := backupTrackerKey(ns, name)
|
||||
bt.readyToStartBackups.Delete(key)
|
||||
bt.inProgressBackups.Delete(key)
|
||||
bt.postProgressBackups.Delete(key)
|
||||
}
|
||||
|
||||
// Contains returns true if backup is InProgress or post-InProgress
|
||||
// ignores ReadyToStart, since this is used to determine whether
|
||||
// a backup is in progress and thus not able to be deleted now.
|
||||
func (bt *backupTracker) Contains(ns, name string) bool {
|
||||
bt.lock.RLock()
|
||||
defer bt.lock.RUnlock()
|
||||
|
||||
return bt.backups.Has(backupTrackerKey(ns, name))
|
||||
key := backupTrackerKey(ns, name)
|
||||
return bt.inProgressBackups.Has(key) || bt.postProgressBackups.Has(key)
|
||||
}
|
||||
|
||||
// RunningCount returns the number of backups which are ReadyToStart or InProgress
|
||||
// used by queue controller to determine whether a new backup can be started.
|
||||
func (bt *backupTracker) RunningCount() int {
|
||||
bt.lock.RLock()
|
||||
defer bt.lock.RUnlock()
|
||||
|
||||
return bt.inProgressBackups.Len() + bt.readyToStartBackups.Len()
|
||||
}
|
||||
|
||||
func backupTrackerKey(ns, name string) string {
|
||||
|
||||
Reference in New Issue
Block a user