fix: check for empty WAL archive during WAL archiving

In the in-tree barman-cloud implementation, the check for an empty WAL
archive is performed both immediately after the restore process and when the
first WAL file is archived.

Previously, the plugin-based implementation only performed this check after
restore, skipping it during archiving of the first WAL. This patch restores
parity with the in-tree behavior by ensuring the check is also performed
during WAL archiving.

Closes: #457

Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
This commit is contained in:
Leonardo Cecchi 2025-08-01 18:26:04 +02:00
parent 3fa8072e30
commit 46d4d4630e
3 changed files with 58 additions and 12 deletions

View File

@ -0,0 +1,34 @@
package common
import (
"context"
"github.com/cloudnative-pg/barman-cloud/pkg/archiver"
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/machinery/pkg/log"
)
// CheckBackupDestination checks if the backup destination is suitable
// to archive WALs
func CheckBackupDestination(
ctx context.Context,
barmanConfiguration *cnpgv1.BarmanObjectStoreConfiguration,
barmanArchiver *archiver.WALArchiver,
serverName string,
) error {
contextLogger := log.FromContext(ctx)
contextLogger.Info(
"Checking backup destination with barman-cloud-wal-archive",
"serverName", serverName)
// Get WAL archive options
checkWalOptions, err := barmanArchiver.BarmanCloudCheckWalArchiveOptions(
ctx, barmanConfiguration, serverName)
if err != nil {
log.Error(err, "while getting barman-cloud-wal-archive options")
return err
}
// Check if we're ok to archive in the desired destination
return barmanArchiver.CheckWalArchiveDestination(ctx, checkWalOptions)
}

View File

@ -13,7 +13,9 @@ import (
barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
barmanRestorer "github.com/cloudnative-pg/barman-cloud/pkg/restorer"
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
"github.com/cloudnative-pg/cnpg-i/pkg/wal"
"github.com/cloudnative-pg/machinery/pkg/fileutils"
walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals"
"github.com/cloudnative-pg/machinery/pkg/log"
apierrors "k8s.io/apimachinery/pkg/api/errors"
@ -125,18 +127,36 @@ func (w WALServiceImplementation) Archive(
return nil, err
}
emptyWalArchiveFile := path.Join(w.PGDataPath, metadata.CheckEmptyWalArchiveFile)
arch, err := archiver.New(
ctx,
envArchive,
w.SpoolDirectory,
w.PGDataPath,
path.Join(w.PGDataPath, metadata.CheckEmptyWalArchiveFile),
emptyWalArchiveFile,
)
if err != nil {
return nil, err
}
// Step 2: check if this WAL file has not been already archived
// Step 2: Check if the archive location is safe to perform archiving
checkFileExisting, err := fileutils.FileExists(emptyWalArchiveFile)
if err != nil {
return nil, fmt.Errorf("while checking for empty wal archive check file %q: %w", emptyWalArchiveFile, err)
}
if utils.IsEmptyWalArchiveCheckEnabled(&configuration.Cluster.ObjectMeta) && checkFileExisting {
if err := CheckBackupDestination(
ctx,
&objectStore.Spec.Configuration,
arch,
configuration.ServerName,
); err != nil {
return nil, err
}
}
// Step 3: check if this WAL file has not been already archived
var isDeletedFromSpool bool
isDeletedFromSpool, err = arch.DeleteFromSpool(baseWalName)
if err != nil {
@ -151,7 +171,7 @@ func (w WALServiceImplementation) Archive(
return nil, nil
}
// Step 3: gather the WAL files names to archive
// Step 4: gather the WAL files names to archive
options, err := arch.BarmanCloudWalArchiveOptions(ctx, &objectStore.Spec.Configuration, configuration.ServerName)
if err != nil {
return nil, err

View File

@ -266,17 +266,9 @@ func (impl *JobHookImpl) checkBackupDestination(
}
}
// Get WAL archive options
checkWalOptions, err := walArchiver.BarmanCloudCheckWalArchiveOptions(
ctx, barmanConfiguration, serverName)
if err != nil {
log.Error(err, "while getting barman-cloud-wal-archive options")
return err
}
// Check if we're ok to archive in the desired destination
if utils.IsEmptyWalArchiveCheckEnabled(&cluster.ObjectMeta) {
return walArchiver.CheckWalArchiveDestination(ctx, checkWalOptions)
return common.CheckBackupDestination(ctx, barmanConfiguration, walArchiver, serverName)
}
return nil