Run backup post hooks inside ItemBlock synchronously

Run backup post hooks inside ItemBlock synchronously as the ItemBlocks are handled asynchronously

Fixes #8516

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
pull/8694/head
Wenkai Yin(尹文开) 2025-02-17 13:27:41 +08:00
parent c0c4407657
commit 7aa8040c09
4 changed files with 6 additions and 21 deletions

View File

@ -0,0 +1 @@
Run backup post hooks inside ItemBlock synchronously

View File

@ -70,7 +70,6 @@ type HookTracker struct {
hookExecutedCnt int
// hookErrs records hook execution errors if any.
hookErrs []HookErrInfo
AsyncItemBlocks *sync.WaitGroup
}
// NewHookTracker creates a hookTracker instance.
@ -78,7 +77,6 @@ func NewHookTracker() *HookTracker {
return &HookTracker{
lock: &sync.RWMutex{},
tracker: make(map[hookKey]hookStatus),
AsyncItemBlocks: &sync.WaitGroup{},
}
}
@ -143,8 +141,6 @@ func (ht *HookTracker) Record(podNamespace, podName, container, source, hookName
// Stat returns the number of attempted hooks and failed hooks
func (ht *HookTracker) Stat() (hookAttemptedCnt int, hookFailedCnt int) {
ht.AsyncItemBlocks.Wait()
ht.lock.RLock()
defer ht.lock.RUnlock()

View File

@ -759,8 +759,7 @@ func (kb *kubernetesBackupper) backupItemBlock(itemBlock *BackupItemBlock) []sch
if len(postHookPods) > 0 {
itemBlock.Log.Debug("Executing post hooks")
itemBlock.itemBackupper.hookTracker.AsyncItemBlocks.Add(1)
go kb.handleItemBlockPostHooks(itemBlock, postHookPods)
kb.handleItemBlockPostHooks(itemBlock, postHookPods)
}
return grList
@ -798,7 +797,6 @@ func (kb *kubernetesBackupper) handleItemBlockPreHooks(itemBlock *BackupItemBloc
// The hooks cannot execute until the PVBs to be processed
func (kb *kubernetesBackupper) handleItemBlockPostHooks(itemBlock *BackupItemBlock, hookPods []itemblock.ItemBlockItem) {
log := itemBlock.Log
defer itemBlock.itemBackupper.hookTracker.AsyncItemBlocks.Done()
// the post hooks will not execute until all PVBs of the item block pods are processed
if err := kb.waitUntilPVBsProcessed(kb.podVolumeContext, log, itemBlock, hookPods); err != nil {

View File

@ -3995,17 +3995,7 @@ func TestBackupWithHooks(t *testing.T) {
require.NoError(t, h.backupper.Backup(h.log, req, backupFile, nil, tc.actions, nil))
if tc.wantHookExecutionLog != nil {
// as the post hook execution in async way, check the existence rather than the exact order
assert.Equal(t, len(tc.wantHookExecutionLog), len(podCommandExecutor.HookExecutionLog))
m := map[string]struct{}{}
for _, entry := range podCommandExecutor.HookExecutionLog {
m[entry.String()] = struct{}{}
}
for _, entry := range tc.wantHookExecutionLog {
_, exist := m[entry.String()]
assert.True(t, exist)
}
assert.Equal(t, tc.wantHookExecutionLog, podCommandExecutor.HookExecutionLog)
}
assertTarballContents(t, backupFile, append(tc.wantBackedUp, "metadata/version")...)
})