From ac3fbdaf977dd6935dea513116575fc8b685d306 Mon Sep 17 00:00:00 2001 From: Francesco Canovai Date: Thu, 17 Apr 2025 12:10:22 +0200 Subject: [PATCH 1/2] fix: improve logs on parallel wal archiving Differentiate between the WAL archived and the ones pre-archived by the parallel workers. Signed-off-by: Francesco Canovai --- pkg/walarchive/cmd.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/pkg/walarchive/cmd.go b/pkg/walarchive/cmd.go index f3be5f3e..273b96bb 100644 --- a/pkg/walarchive/cmd.go +++ b/pkg/walarchive/cmd.go @@ -114,26 +114,31 @@ func (archiver *BarmanArchiver) ArchiveList( walStatus.StartTime = time.Now() walStatus.Err = archiver.Archive(ctx, walNames[walIndex], options) walStatus.EndTime = time.Now() - if walStatus.Err == nil && walIndex != 0 { - walStatus.Err = archiver.Touch(walNames[walIndex]) - } - elapsedWalTime := walStatus.EndTime.Sub(walStatus.StartTime) - if walStatus.Err != nil { - contextLog.Warning( + walContextLog := contextLog.WithValues( + "walName", walStatus.WalName, + "startTime", walStatus.StartTime, + "endTime", walStatus.EndTime, + "elapsedWalTime", walStatus.EndTime.Sub(walStatus.StartTime), + ) + + switch { + case walStatus.Err != nil: + walContextLog.Warning( "Failed archiving WAL: PostgreSQL will retry", - "walName", walStatus.WalName, - "startTime", walStatus.StartTime, - "endTime", walStatus.EndTime, - "elapsedWalTime", elapsedWalTime, "error", walStatus.Err) - } else { - contextLog.Info( - "Archived WAL file", - "walName", walStatus.WalName, - "startTime", walStatus.StartTime, - "endTime", walStatus.EndTime, - "elapsedWalTime", elapsedWalTime) + + case walIndex == 0: + walContextLog.Info("Archived WAL file") + + default: + if err := archiver.Touch(walNames[walIndex]); err != nil { + walContextLog.Warning( + "WAL file pre-archived, but it could not be added to the spool. PostgreSQL will retry", + "error", err) + } else { + walContextLog.Info("Pre-archived WAL file (parallel)") + } } waitGroup.Done() From 22b69d482cbd3cd32fa577bdb5ee224c81cb9a44 Mon Sep 17 00:00:00 2001 From: Armando Ruocco Date: Fri, 18 Apr 2025 13:28:41 +0200 Subject: [PATCH 2/2] refactor: improve clarity and streamline logic Signed-off-by: Armando Ruocco --- pkg/walarchive/cmd.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/pkg/walarchive/cmd.go b/pkg/walarchive/cmd.go index 273b96bb..da539323 100644 --- a/pkg/walarchive/cmd.go +++ b/pkg/walarchive/cmd.go @@ -109,11 +109,15 @@ func (archiver *BarmanArchiver) ArchiveList( for idx := range walNames { waitGroup.Add(1) go func(walIndex int) { + defer waitGroup.Done() + + result[walIndex] = WALArchiverResult{ + WalName: walNames[walIndex], + StartTime: time.Now(), + Err: archiver.Archive(ctx, walNames[walIndex], options), + EndTime: time.Now(), + } walStatus := &result[walIndex] - walStatus.WalName = walNames[walIndex] - walStatus.StartTime = time.Now() - walStatus.Err = archiver.Archive(ctx, walNames[walIndex], options) - walStatus.EndTime = time.Now() walContextLog := contextLog.WithValues( "walName", walStatus.WalName, @@ -122,26 +126,26 @@ func (archiver *BarmanArchiver) ArchiveList( "elapsedWalTime", walStatus.EndTime.Sub(walStatus.StartTime), ) - switch { - case walStatus.Err != nil: + if walStatus.Err != nil { walContextLog.Warning( "Failed archiving WAL: PostgreSQL will retry", "error", walStatus.Err) + return + } - case walIndex == 0: + if walIndex == 0 { walContextLog.Info("Archived WAL file") + return + } - default: - if err := archiver.Touch(walNames[walIndex]); err != nil { - walContextLog.Warning( - "WAL file pre-archived, but it could not be added to the spool. PostgreSQL will retry", - "error", err) - } else { - walContextLog.Info("Pre-archived WAL file (parallel)") - } + if err := archiver.Touch(walNames[walIndex]); err != nil { + walContextLog.Warning( + "WAL file pre-archived, but it could not be added to the spool. PostgreSQL will retry", + "error", err) + return } - waitGroup.Done() + walContextLog.Info("Pre-archived WAL file (parallel)") }(idx) }