Now I understand the issue. Looking at the code:
- The
IsExecuting function is mentioned in the issue - it should return true if a backup has started or is running.
- Looking at the existing code in
backup_funcs.go:
IsInProgress() returns true if phase is pending, started, or running
IsDone() returns true if phase is completed or failed
CanExecuteBackup() already has logic to prevent concurrent backups but there's an issue
The problem is in the CanExecuteBackup function. Looking at it closely:
func (list *BackupList) CanExecuteBackup(backupName string) bool {
var foundRunningBackup bool
list.SortByName()
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsInProgress() {
if backupName == concurrentBackup.Name && !foundRunningBackup {
return true
}
foundRunningBackup = true
if backupName != concurrentBackup.Name {
return false
}
}
}
pendingBackups := list.GetPendingBackupNames()
if len(pendingBackups) > 0 && pendingBackups[0] != backupName {
return false
}
return true
}
And GetPendingBackupNames():
func (list BackupList) GetPendingBackupNames() []string {
pendingBackups := make([]string, 0, len(list.Items))
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsDone() {
continue
}
if !concurrentBackup.Status.IsInProgress() {
pendingBackups = append(pendingBackups, concurrentBackup.Name)
}
}
return pendingBackups
}
The issue is:
IsInProgress() includes pending, started, and running phases
- The function
GetPendingBackupNames() returns backups that are NOT done and NOT in progress - meaning they're only backups with phases like finalizing, walArchivingFailing, etc. (the ones that are neither done nor in progress)
- But the problem is: when there's no running backup, and we have pending backups, the
GetPendingBackupNames() function returns an empty list (because all pending backups ARE in progress)
Wait, let me re-read more carefully. The phases are:
pending - waiting to be started
started - now running
running - now running
finalizing - waiting for readyToUse
completed - completed
failed - failed
walArchivingFailing - wal archiving isn't properly working
So IsInProgress() returns true for pending, started, and running.
IsDone() returns true for completed and failed.
GetPendingBackupNames() returns backups that are:
- NOT done (so not completed or failed)
- NOT in progress (so not pending, started, or running)
This means it returns backups with phases like finalizing or walArchivingFailing. These aren't really "pending" backups - they're in some intermediate state.
The real issue described is:
- "Pending backups can preempt a backup that is already executing"
- "Backup scheduling must not select a pending backup while another backup is already executing"
- "When none is executing, select the oldest pending backup, using a deterministic tie-break for equal creation times"
So the issue is that we need an IsExecuting function that returns true only when a backup is actually executing (started or running), and the CanExecuteBackup logic needs to use this properly.
Looking at the issue description more carefully:
IsExecuting should return true if the backup has started or is running - meaning it should return true only for started and running phases, NOT for pending
- The scheduling logic should check
IsExecuting to determine if we can pick a pending backup
So I need to:
- Add an
IsExecuting function that returns true only for started or running phases
- Modify
CanExecuteBackup to use IsExecuting to check if any backup is executing
- When no backup is executing, select the oldest pending backup (by creation time, with name as tie-breaker)
Let me also look at GetPendingBackupNames more carefully. The issue is that this function name is misleading - it returns backups that are NOT in progress, not pending backups. The actual pending backups should be filtered by phase == pending.
Let me also look at the existing tests to understand the expected behavior:
From the tests:
When("there's a running backup", func() {
It("prevents concurrent backups", func() {
...
Expect(backupList.CanExecuteBackup("backup-1")).To(BeFalse()) // pending backup can't execute
Expect(backupList.CanExecuteBackup("backup-2")).To(BeTrue()) // running backup CAN execute
Expect(backupList.CanExecuteBackup("backup-3")).To(BeFalse()) // pending backup can't execute
})
})
So when there's a running backup, only the running one can execute. When there's no running backup, the first one (by name sort) can execute.
But the issue is about selecting the oldest pending backup, not the first alphabetically. The current code sorts by name and then picks the first one. But the issue says "select the oldest pending backup, using a deterministic tie-break for equal creation times."
So I need to:
- Create
IsExecuting function
- Modify
CanExecuteBackup to properly handle the case where no backup is executing - it should select the oldest pending backup (by creation time, with name as tie-breaker for determinism)
Let me also check if GetPendingBackupNames should be updated or if I need a new function. Looking at the current logic:
GetPendingBackupNames() actually returns backups that are NOT done AND NOT in progress. This is wrong - it should return actual pending backups.
- The
CanExecuteBackup function uses this incorrectly.
Let me think about this more carefully:
Current CanExecuteBackup logic:
- Sort by name
- Look for any backup that is in progress (pending, started, running)
- If found and it's the one we're checking and no running backup found before, return true
- If we found a running backup and it's not the one we're checking, return false
- After the loop, get "pending backups" using
GetPendingBackupNames() and check if we're the first one
The issue is:
GetPendingBackupNames() returns backups that are NOT done AND NOT in progress (like "finalizing")
- But we actually want to return backups that are in "pending" phase
- Also, when no backup is executing, we should select the oldest pending backup by creation time, not by name
I think the fix needs to:
- Add
IsExecuting() method that returns true for started/running phases
- Update the logic to:
- If any backup is executing, only allow that one to execute
- If no backup is executing, select the oldest pending backup (by creation time, name as tie-breaker)
Wait, let me re-read the issue:
"Pending backups can preempt a backup that is already executing"
So the bug is that when a backup is executing, a pending backup can be selected. Let me trace through the logic more carefully:
Consider: backup-1 is running, backup-2 is pending
list.SortByName() // backup-1, backup-2
for _, concurrentBackup := range list.Items {
// backup-1: IsInProgress() -> true (running)
if backupName == "backup-1" && !foundRunningBackup {
return true // backup-1 can execute, foundRunningBackup stays false
}
foundRunningBackup = true // now true
if backupName == "backup-2" && backupName != backup-1.Name {
return false // backup-2 can't execute
}
}
// For backup-2: returns false. Correct!
Wait, but what if backup-1 is "pending" and backup-2 is "running"?
list.SortByName() // backup-1, backup-2
for _, concurrentBackup := range list.Items {
// backup-1: IsInProgress() -> true (pending)
if backupName == "backup-1" && !foundRunningBackup {
return true // backup-1 can execute! But it's only pending, not running!
}
foundRunningBackup = true
if backupName == "backup-2" && backupName != backup-1.Name {
return false
}
}
Hmm wait, actually the logic seems correct for the scenario where backup-1 is pending and backup-2 is running:
- For backup-2: when we encounter backup-1,
foundRunningBackup is still false, and backupName == backup-1.Name is false (we're checking backup-2), so we don't return true. Then foundRunningBackup becomes true. When we encounter backup-2, backupName == backup-2.Name is true, but !foundRunningBackup is false (because foundRunningBackup is true), so we don't return true here. Then we fall through...
Wait no, let me trace more carefully:
For backup-2 (running), backup-1 (pending):
- SortByName: backup-1, backup-2
- Loop iteration 1 (backup-1, pending):
- IsInProgress() = true
- backupName ("backup-2") == "backup-1"? No
- So we skip the if block (we don't return)
- foundRunningBackup = true
- backupName ("backup-2") != "backup-1"? Yes, so return false!
OK so that case works correctly.
Let me trace backup-1 (pending) with backup-2 (running):
- SortByName: backup-1, backup-2
- Loop iteration 1 (backup-1, pending):
- IsInProgress() = true
- backupName ("backup-1") == "backup-1"? Yes
- !foundRunningBackup = !false = true
- So return true!
This is the bug! If a pending backup comes first alphabetically, it will be allowed to execute even though another backup (backup-2) is running!
So the issue is:
- The
foundRunningBackup variable is used to track whether we've seen a backup that IS in progress
- But
IsInProgress() returns true for pending backups too
- The logic
!foundRunningBackup means "haven't seen any in-progress backup yet"
- But it should mean "haven't seen any EXECUTING backup yet"
The fix is to differentiate between "in progress" (pending/started/running) and "executing" (started/running only). A pending backup should not be considered as "running" and should not set foundRunningBackup = true.
Actually wait, let me re-read the code logic:
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsInProgress() {
if backupName == concurrentBackup.Name && !foundRunningBackup {
return true
}
foundRunningBackup = true
if backupName != concurrentBackup.Name {
return false
}
}
}
The intention seems to be:
- If we find a backup that is in progress (including pending), we check if it's the one we want
foundRunningBackup tracks if we've already found a running backup
- The first "in progress" backup is allowed
- Any subsequent "in progress" backup blocks execution
But the bug is: IsInProgress() includes pending, so a pending backup can "block" subsequent ones via foundRunningBackup = true, but it can also be the first one and get through because !foundRunningBackup is still true.
The issue says we need IsExecuting() which returns true only for started/running (not pending). The fix should be:
- Add
IsExecuting() that returns true only for started/running phases
- Change the logic to check for executing backups, not in-progress backups
Let me look at what the correct fix should be:
func (list *BackupList) CanExecuteBackup(backupName string) bool {
var foundExecutingBackup bool
list.SortByCreationTime() // Need to sort by creation time
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsExecuting() { // Use IsExecuting instead of IsInProgress
if backupName == concurrentBackup.Name && !foundExecutingBackup {
return true
}
foundExecutingBackup = true
if backupName != concurrentBackup.Name {
return false
}
}
}
// No backup is executing, select the oldest pending backup
pendingBackups := list.GetOldestPendingBackupNames()
if len(pendingBackups) > 0 && pendingBackups[0] != backupName {
return false
}
return true
}
Wait, but SortByName sorts alphabetically. The issue says "select the oldest pending backup, using a deterministic tie-break for equal creation times." So we need to sort by creation time (oldest first) with name as tie-breaker.
Let me create a new sorting function and the IsExecuting method. Let me first run the existing tests to see what passes.
Actually, looking more carefully at the existing tests:
When("there's a running backup", func() {
It("prevents concurrent backups", func() {
backupList := BackupList{
Items: []Backup{
{ObjectMeta: metav1.ObjectMeta{Name: "backup-1"}}, // No phase set, defaults to empty
{ObjectMeta: metav1.ObjectMeta{Name: "backup-2"}, Status: BackupStatus{Phase: BackupPhaseRunning}},
{ObjectMeta: metav1.ObjectMeta{Name: "backup-3"}},
},
}
Expect(backupList.CanExecuteBackup("backup-1")).To(BeFalse())
Expect(backupList.CanExecuteBackup("backup-2")).To(BeTrue())
Expect(backupList.CanExecuteBackup("backup-3")).To(BeFalse())
})
})
In this test, backup-1 has no phase set (empty string). IsInProgress() returns:
func (backupStatus *BackupStatus) IsInProgress() bool {
return backupStatus.Phase == BackupPhasePending ||
backupStatus.Phase == BackupPhaseStarted ||
backupStatus.Phase == BackupPhaseRunning
}
So backup-1 (empty phase) returns false for IsInProgress().
Let me trace through:
- SortByName: backup-1, backup-2, backup-3
- Loop:
- backup-1: IsInProgress() = false (empty phase), skip
- backup-2: IsInProgress() = true (running)
- backupName "backup-1" == "backup-2"? No
- foundRunningBackup = true
- "backup-1" != "backup-2"? Yes, return false
- For backup-2: Loop iteration 1 (backup-1), skip. Loop iteration 2 (backup-2):
- backupName "backup-2" == "backup-2"? Yes
- !foundRunningBackup = !false = true... wait, foundRunningBackup is still false here
Hmm wait, for backup-2:
- backup-1: IsInProgress() = false, skip
- backup-2: IsInProgress() = true, backupName "backup-2" == "backup-2"? Yes, !foundRunningBackup = !false... wait, foundRunningBackup is false at this point because backup-1 wasn't in progress!
So for backup-2:
- backup-1: skip (not in progress)
- backup-2: in progress, is the one we're checking, !foundRunningBackup is true (no running backup seen yet)
For backup-3:
- backup-1: skip
- backup-2: in progress, not the one we're checking
- foundRunningBackup = true
- backupName "backup-3" != "backup-2"? Yes, return false ✓
OK so the tests check that backup-1, backup-2, and backup-3 all have no phase set except backup-2 which is running. But the issue is about pending backups being able to execute when another is running. Let me think of a test case:
backup-1: Phase = "pending" (or not set, defaults to empty which is NOT pending)
backup-2: Phase = "running"
backup-3: Phase = "pending"
Wait, I need to look at what phase an unset backup has. The default for BackupPhase string is empty string "", which is not equal to "pending", "started", or "running".
So in the existing tests, the "pending" backups actually have empty phase (not "pending"). That might be part of the issue - backups that are "pending" are actually in a non-phase state.
Actually, let me look at GetPendingBackupNames:
func (list BackupList) GetPendingBackupNames() []string {
pendingBackups := make([]string, 0, len(list.Items))
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsDone() {
continue
}
if !concurrentBackup.Status.IsInProgress() {
pendingBackups = append(pendingBackups, concurrentBackup.Name)
}
}
return pendingBackups
}
This returns backups that are: NOT done AND NOT in progress. So for a backup with empty phase:
- IsDone() = false (empty != completed/failed)
- IsInProgress() = false (empty != pending/started/running)
So an empty-phase backup IS included in GetPendingBackupNames(). This seems intentional - a backup with no phase set should be treated as pending.
OK so let me re-trace the bug scenario with this understanding:
Scenario: backup-1 (running), backup-2 (empty phase = "pending" per GetPendingBackupNames)
SortByName: backup-1, backup-2
for backupName = "backup-2":
- backup-1: IsInProgress() = true (running)
- backupName "backup-2" == "backup-1"? No
- foundRunningBackup = true
- "backup-2" != "backup-1"? Yes, return false ✓ Correct!
for backupName = "backup-1":
- backup-1: IsInProgress() = true (running)
- "backup-1" == "backup-1"? Yes
- !foundRunningBackup = !false = true (wait, foundRunningBackup starts as false)
- return true ✓ Correct!
OK this works correctly. What about:
Scenario: backup-1 (empty phase), backup-2 (running)
SortByName: backup-1, backup-2
for backupName = "backup-1":
- backup-1: IsInProgress() = false (empty phase)
- skip the if block
- backup-2: IsInProgress() = true (running)
- backupName "backup-1" == "backup-2"? No
- foundRunningBackup = true
- "backup-1" != "backup-2"? Yes, return false ✓ Correct!
for backupName = "backup-2":
- backup-1: IsInProgress() = false (empty phase)
- skip
- backup-2: IsInProgress() = true (running)
- "backup-2" == "backup-2"? Yes
- !foundRunningBackup = !false... wait, foundRunningBackup is still false!
- return true ✓ Correct!
OK so the scenario where it fails is:
Scenario: backup-1 (empty phase = pending), backup-2 (started or running)
- But wait, we need to check: does backup-1 with empty phase get into the for loop's
if block? No! Because IsInProgress() returns false for empty phase.
Let me think of a scenario where it fails. The !foundRunningBackup is the key. It starts as false. The first backup that is IsInProgress() will trigger the !foundRunningBackup = true.
Scenario: backup-1 (pending phase = "pending"), backup-2 (running phase)
SortByName: backup-1, backup-2
for backupName = "backup-2":
- backup-1: IsInProgress() = true (pending)
- "backup-2" == "backup-1"? No
- foundRunningBackup = true
- "backup-2" != "backup-1"? Yes, return false ✓ Correct!
for backupName = "backup-1":
- backup-1: IsInProgress() = true (pending)
- "backup-1" == "backup-1"? Yes
- !foundRunningBackup = !false = true
- return true ✗ WRONG! backup-2 is running!
YES! This is the bug! If a pending backup comes first alphabetically and we're checking if it can execute, it will return true even though another backup is running!
The fix is:
- Add
IsExecuting() that returns true only for started/running (not pending)
- Change
CanExecuteBackup to use IsExecuting() instead of IsInProgress() when checking for running/executing backups
- For selecting pending backups, use
GetPendingBackupNames() which correctly identifies them (based on NOT being done and NOT being in progress)
- Also need to sort by creation time for oldest-first selection
Wait, but there's a subtlety. The variable foundRunningBackup is tracking whether we've found a backup that "blocks" others. With the current logic, it's set for any IsInProgress() backup. But we should only set it for IsExecuting() backups.
Actually, let me reconsider. The comment says:
// CanExecuteBackup control if we can start a reconciliation loop for a certain backup.
//
// A reconciliation loop can start if:
// - there's no backup running, and if the first of the sorted list of backups
// - the current backup is running and is the first running backup of the list
So the logic should be:
- If there's a running (executing) backup, only that one can execute
- If there's no running backup, the first one in the sorted list can execute
The current code uses IsInProgress() which conflates "pending" with "running." We need to separate these.
Actually wait, let me re-read the issue:
"Pending backups can preempt a backup that is already executing"
This confirms my analysis. When a pending backup is checked before a running backup (alphabetically), it gets allowed to run because:
IsInProgress() returns true for pending
!foundRunningBackup is true (no running backup seen yet)
- So it returns true
The fix:
- Add
IsExecuting() that only returns true for started/running phases
- In
CanExecuteBackup, use IsExecuting() to check if a backup is executing (holding active resources)
- Still use
IsInProgress() or similar logic to identify pending backups
- For the pending backup selection, use creation time with name as tie-breaker
But wait, I need to think about the pending backup detection. Currently GetPendingBackupNames() returns backups that are NOT done and NOT in progress. But "pending" should really mean phase == "pending". Let me check the tests again:
It("can isolate pending backups", func() {
backupList := BackupList{
Items: []Backup{
{ObjectMeta: metav1.ObjectMeta{Name: "backup-3"}, Status: BackupStatus{Phase: BackupPhaseRunning}},
{ObjectMeta: metav1.ObjectMeta{Name: "backup-2"}},
{ObjectMeta: metav1.ObjectMeta{Name: "backup-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "backup-5"}, Status: BackupStatus{Phase: BackupPhaseCompleted}},
},
}
...
Expect(pendingBackups).To(ConsistOf("backup-1", "backup-2"))
})
OK so in this test, backup-1 and backup-2 have no phase set (empty), and they ARE considered "pending." This confirms that the current implementation treats empty phase as pending.
So the design is:
IsInProgress() returns true for pending, started, running
IsDone() returns true for completed, failed
- Everything else (empty phase, finalizing, walArchivingFailing) is neither in progress nor done
GetPendingBackupNames() returns backups that are NOT done AND NOT in progress = empty phase + finalizing + walArchivingFailing
This is a bit confusing. "Pending" backups are those that haven't started yet (empty phase or finalizing or walArchivingFailing). "In progress" backups are those that have been assigned to a pod but not completed.
Now, the IsExecuting() function should return true only for started/running, meaning the backup is actively using resources.
Let me plan the fix:
- Add
IsExecuting() method to BackupStatus:
func (backupStatus *BackupStatus) IsExecuting() bool {
return backupStatus.Phase == BackupPhaseStarted || backupStatus.Phase == BackupPhaseRunning
}
-
Modify CanExecuteBackup:
- Sort by creation time (oldest first), with name as tie-breaker
- Check if any backup is executing (using IsExecuting)
- If executing, only allow that one
- If not executing, select the oldest pending backup
-
Update GetPendingBackupNames or create a helper to get pending backups sorted by creation time.
Actually wait, let me look at how GetPendingBackupNames is used. It's called within CanExecuteBackup:
pendingBackups := list.GetPendingBackupNames()
if len(pendingBackups) > 0 && pendingBackups[0] != backupName {
return false
}
And it expects the backups to be sorted by name (since CanExecuteBackup sorts by name first). But the issue says we should sort by creation time.
Let me think about the full fix:
// IsExecuting checks if the backup is executing (started or running).
// Returns false for pending backups, finalizing, completed, failed, etc.
func (backupStatus *BackupStatus) IsExecuting() bool {
return backupStatus.Phase == BackupPhaseStarted || backupStatus.Phase == BackupPhaseRunning
}
// CanExecuteBackup controls if we can start a reconciliation loop for a certain backup.
//
// A reconciliation loop can start if:
// - there's no backup executing (started or running), and
// - the current backup is the oldest pending backup (sorted by creation time, name as tie-breaker)
// - OR the current backup is the one that is executing
func (list *BackupList) CanExecuteBackup(backupName string) bool {
list.SortByCreationTime()
// Check if any backup is currently executing
for _, concurrentBackup := range list.Items {
if concurrentBackup.Status.IsExecuting() {
return backupName == concurrentBackup.Name
}
}
// No backup is executing, select the oldest pending backup
pendingBackups := list.GetPendingBackupNames()
if len(pendingBackups) > 0 && pendingBackups[0] != backupName {
return false
}
return true
}
Wait, but there's a subtlety. After we sort by creation time and check that no backup is executing, we need the pendingBackups to be sorted by creation time too. Let me check how GetPendingBackupNames works - it just filters and appends in order of the list. So if the list is sorted by creation time, the pending backups will also be sorted by creation time.
But wait, GetPendingBackupNames iterates through list.Items in whatever order they are. So if we sort the list by creation time first, the pending backups will be in creation time order. Good.
But there's another issue: the test It("can isolate pending backups" expects that after sorting by name and filtering, pending backups are returned in a specific order. Let me check...
Actually, that test sorts by name first:
backupList.SortByName()
pendingBackups := backupList.GetPendingBackupNames()
Expect(pendingBackups).To(ConsistOf("backup-1", "backup-2"))
This doesn't test the ORDER, just the CONTENT. ConsistOf doesn't care about order. So that's fine.
Now, for the SortByName and GetPendingBackupNames to work together correctly in the current tests, the current logic in CanExecuteBackup relies on SortByName being called before the loop. After my fix, I'll call SortByCreationTime instead.
But wait, I also need to update GetPendingBackupNames to be sorted correctly. Actually, it doesn't need to be changed - it will return pending backups in the order of the list items. If the list is sorted by creation time (oldest first), then pendingBackups[0] will be the oldest pending backup.
But I also need to make sure the tie-breaker is correct. "Deterministic tie-break for equal creation times." When two backups have the same creation time, we should sort by name. Let me look at SortByReverseCreationTime:
func (list *BackupList) SortByReverseCreationTime() {
sort.Slice(list.Items, func(i, j int) bool {
return list.Items[i].CreationTimestamp.Compare(list.Items[j].CreationTimestamp.Time) > 0
})
}
This sorts in reverse order (newest first). I need to create a new function that sorts oldest first, with name as tie-breaker:
func (list *BackupList) SortByCreationTime() {
sort.Slice(list.Items, func(i, j int) bool {
cmp := list.Items[i].CreationTimestamp.Compare(list.Items[j].CreationTimestamp.Time)
if cmp != 0 {
return cmp < 0 // older first
}
return strings.Compare(list.Items[i].Name, list.Items[j].Name) < 0 // alphabetical tie-break
})
}
Actually, I realize CreationTimestamp.Compare returns -1, 0, or 1, and cmp < 0 means i is older than j. That's correct for oldest-first sorting.
Now let me also think about whether I need to update GetPendingBackupNames. Currently it's used in two places:
- Inside
CanExecuteBackup
- In the test
It("can isolate pending backups")
The test doesn't care about the order, just the content. So I don't need to change it.
But wait, there's another issue. The issue mentions that IsExecuting should:
"Returns true if the backup has started or is running. Use this to determine whether a backup holds active resources and should not be preempted."
This confirms my understanding. IsExecuting() should return true only for started/running, not pending.
Let me also check: are there any other places that use IsInProgress() that might need to be updated to use IsExecuting()?
<tool_call>
<function=bash_terminal>
<parameter=command>
cd /cloudnative-pg && grep -r "IsInProgress" --include="*.go" | head -30
</tool_call>