Skip to content

Commit 2a10118

Browse files
committed
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 <[email protected]>
1 parent 3fa8072 commit 2a10118

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

internal/cnpgi/common/check.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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/cloudnative-pg/pkg/utils"
9+
"github.com/cloudnative-pg/machinery/pkg/log"
10+
)
11+
12+
// CheckBackupDestination checks if the backup destination is suitable
13+
// to archive WALs
14+
func CheckBackupDestination(
15+
ctx context.Context,
16+
cluster *cnpgv1.Cluster,
17+
barmanConfiguration *cnpgv1.BarmanObjectStoreConfiguration,
18+
barmanArchiver *archiver.WALArchiver,
19+
serverName string,
20+
) error {
21+
contextLogger := log.FromContext(ctx)
22+
23+
contextLogger.Info(
24+
"Checking backup destination with barman-cloud-wal-archive",
25+
"clusterName", cluster.Name,
26+
"serverName", serverName)
27+
28+
// Get WAL archive options
29+
checkWalOptions, err := barmanArchiver.BarmanCloudCheckWalArchiveOptions(
30+
ctx, barmanConfiguration, serverName)
31+
if err != nil {
32+
log.Error(err, "while getting barman-cloud-wal-archive options")
33+
return err
34+
}
35+
36+
// Check if we're ok to archive in the desired destination
37+
if utils.IsEmptyWalArchiveCheckEnabled(&cluster.ObjectMeta) {
38+
return barmanArchiver.CheckWalArchiveDestination(ctx, checkWalOptions)
39+
}
40+
41+
return nil
42+
}

internal/cnpgi/common/wal.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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"
1718
walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals"
1819
"github.com/cloudnative-pg/machinery/pkg/log"
@@ -136,7 +137,20 @@ func (w WALServiceImplementation) Archive(
136137
return nil, err
137138
}
138139

139-
// Step 2: check if this WAL file has not been already archived
140+
// Step 2: Check if the archive location is safe to perform archiving
141+
if utils.IsEmptyWalArchiveCheckEnabled(&configuration.Cluster.ObjectMeta) {
142+
if err := CheckBackupDestination(
143+
ctx,
144+
configuration.Cluster,
145+
&objectStore.Spec.Configuration,
146+
arch,
147+
configuration.ServerName,
148+
); err != nil {
149+
return nil, err
150+
}
151+
}
152+
153+
// Step 3: check if this WAL file has not been already archived
140154
var isDeletedFromSpool bool
141155
isDeletedFromSpool, err = arch.DeleteFromSpool(baseWalName)
142156
if err != nil {
@@ -151,7 +165,7 @@ func (w WALServiceImplementation) Archive(
151165
return nil, nil
152166
}
153167

154-
// Step 3: gather the WAL files names to archive
168+
// Step 4: gather the WAL files names to archive
155169
options, err := arch.BarmanCloudWalArchiveOptions(ctx, &objectStore.Spec.Configuration, configuration.ServerName)
156170
if err != nil {
157171
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, cluster, barmanConfiguration, walArchiver, serverName)
280272
}
281273

282274
return nil

0 commit comments

Comments
 (0)