Skip to content

Commit 950364b

Browse files
authored
fix: check for empty WAL archive during WAL archiving (#458)
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 <[email protected]>
1 parent 3fa8072 commit 950364b

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

internal/cnpgi/common/check.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package common
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudnative-pg/barman-cloud/pkg/archiver"
7+
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
8+
"github.com/cloudnative-pg/machinery/pkg/log"
9+
)
10+
11+
// CheckBackupDestination checks if the backup destination is suitable
12+
// to archive WALs
13+
func CheckBackupDestination(
14+
ctx context.Context,
15+
barmanConfiguration *cnpgv1.BarmanObjectStoreConfiguration,
16+
barmanArchiver *archiver.WALArchiver,
17+
serverName string,
18+
) error {
19+
contextLogger := log.FromContext(ctx)
20+
contextLogger.Info(
21+
"Checking backup destination with barman-cloud-wal-archive",
22+
"serverName", serverName)
23+
24+
// Get WAL archive options
25+
checkWalOptions, err := barmanArchiver.BarmanCloudCheckWalArchiveOptions(
26+
ctx, barmanConfiguration, serverName)
27+
if err != nil {
28+
log.Error(err, "while getting barman-cloud-wal-archive options")
29+
return err
30+
}
31+
32+
// Check if we're ok to archive in the desired destination
33+
return barmanArchiver.CheckWalArchiveDestination(ctx, checkWalOptions)
34+
}

internal/cnpgi/common/wal.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
1414
barmanRestorer "github.com/cloudnative-pg/barman-cloud/pkg/restorer"
1515
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
16+
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
1617
"github.com/cloudnative-pg/cnpg-i/pkg/wal"
18+
"github.com/cloudnative-pg/machinery/pkg/fileutils"
1719
walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals"
1820
"github.com/cloudnative-pg/machinery/pkg/log"
1921
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -125,18 +127,36 @@ func (w WALServiceImplementation) Archive(
125127
return nil, err
126128
}
127129

130+
emptyWalArchiveFile := path.Join(w.PGDataPath, metadata.CheckEmptyWalArchiveFile)
128131
arch, err := archiver.New(
129132
ctx,
130133
envArchive,
131134
w.SpoolDirectory,
132135
w.PGDataPath,
133-
path.Join(w.PGDataPath, metadata.CheckEmptyWalArchiveFile),
136+
emptyWalArchiveFile,
134137
)
135138
if err != nil {
136139
return nil, err
137140
}
138141

139-
// Step 2: check if this WAL file has not been already archived
142+
// Step 2: Check if the archive location is safe to perform archiving
143+
checkFileExisting, err := fileutils.FileExists(emptyWalArchiveFile)
144+
if err != nil {
145+
return nil, fmt.Errorf("while checking for empty wal archive check file %q: %w", emptyWalArchiveFile, err)
146+
}
147+
148+
if utils.IsEmptyWalArchiveCheckEnabled(&configuration.Cluster.ObjectMeta) && checkFileExisting {
149+
if err := CheckBackupDestination(
150+
ctx,
151+
&objectStore.Spec.Configuration,
152+
arch,
153+
configuration.ServerName,
154+
); err != nil {
155+
return nil, err
156+
}
157+
}
158+
159+
// Step 3: check if this WAL file has not been already archived
140160
var isDeletedFromSpool bool
141161
isDeletedFromSpool, err = arch.DeleteFromSpool(baseWalName)
142162
if err != nil {
@@ -151,7 +171,7 @@ func (w WALServiceImplementation) Archive(
151171
return nil, nil
152172
}
153173

154-
// Step 3: gather the WAL files names to archive
174+
// Step 4: gather the WAL files names to archive
155175
options, err := arch.BarmanCloudWalArchiveOptions(ctx, &objectStore.Spec.Configuration, configuration.ServerName)
156176
if err != nil {
157177
return nil, err

internal/cnpgi/restore/restore.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,9 @@ func (impl *JobHookImpl) checkBackupDestination(
266266
}
267267
}
268268

269-
// Get WAL archive options
270-
checkWalOptions, err := walArchiver.BarmanCloudCheckWalArchiveOptions(
271-
ctx, barmanConfiguration, serverName)
272-
if err != nil {
273-
log.Error(err, "while getting barman-cloud-wal-archive options")
274-
return err
275-
}
276-
277269
// Check if we're ok to archive in the desired destination
278270
if utils.IsEmptyWalArchiveCheckEnabled(&cluster.ObjectMeta) {
279-
return walArchiver.CheckWalArchiveDestination(ctx, checkWalOptions)
271+
return common.CheckBackupDestination(ctx, barmanConfiguration, walArchiver, serverName)
280272
}
281273

282274
return nil

0 commit comments

Comments
 (0)