Skip to content

Commit aa7e487

Browse files
committed
feat: add lastarchivedwaltime to the recoverywindow
1 parent 316828c commit aa7e487

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
159159

160160
## Tool Versions
161161
KUSTOMIZE_VERSION ?= v5.4.3
162-
CONTROLLER_TOOLS_VERSION ?= v0.16.1
162+
CONTROLLER_TOOLS_VERSION ?= v0.19.0
163163
ENVTEST_VERSION ?= release-0.19
164164
GOLANGCI_LINT_VERSION ?= v1.64.8
165165

api/v1/objectstore_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type RecoveryWindow struct {
9494

9595
// The last failed backup time
9696
LastFailedBackupTime *metav1.Time `json:"lastFailedBackupTime,omitempty"`
97+
98+
// The last time a WAL file was successfully archived by this plugin
99+
LastArchivedWALTime *metav1.Time `json:"lastArchivedWALTime,omitempty"`
97100
}
98101

99102
// +kubebuilder:object:root=true

api/v1/zz_generated.deepcopy.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/barmancloud.cnpg.io_objectstores.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,11 @@ spec:
671671
restored.
672672
format: date-time
673673
type: string
674+
lastArchivedWALTime:
675+
description: The last time a WAL file was successfully archived
676+
by this plugin
677+
format: date-time
678+
type: string
674679
lastFailedBackupTime:
675680
description: The last failed backup time
676681
format: date-time

internal/cnpgi/common/wal.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ import (
3838
walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals"
3939
"github.com/cloudnative-pg/machinery/pkg/log"
4040
apierrors "k8s.io/apimachinery/pkg/api/errors"
41+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4142
"k8s.io/apimachinery/pkg/types"
43+
"k8s.io/client-go/util/retry"
44+
"k8s.io/utils/ptr"
4245
"sigs.k8s.io/controller-runtime/pkg/client"
4346

4447
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
@@ -220,6 +223,21 @@ func (w WALServiceImplementation) Archive(
220223
}
221224
}
222225

226+
// Update the last archived WAL time in the ObjectStore status
227+
contextLogger.Debug("Updating last archived WAL time", "serverName", configuration.ServerName)
228+
if err := setLastArchivedWALTime(
229+
ctx,
230+
w.Client,
231+
configuration.GetBarmanObjectKey(),
232+
configuration.ServerName,
233+
time.Now(),
234+
); err != nil {
235+
// Log the error but don't fail the archive operation
236+
contextLogger.Error(err, "Error updating last archived WAL time in ObjectStore status")
237+
} else {
238+
contextLogger.Debug("Successfully updated last archived WAL time")
239+
}
240+
223241
return &wal.WALArchiveResult{}, nil
224242
}
225243

@@ -509,3 +527,30 @@ func isEndOfWALStream(results []barmanRestorer.Result) bool {
509527

510528
return false
511529
}
530+
531+
// SetLastArchivedWALTime sets the last archived WAL time in the
532+
// passed object store, for the passed server name.
533+
func setLastArchivedWALTime(
534+
ctx context.Context,
535+
c client.Client,
536+
objectStoreKey client.ObjectKey,
537+
serverName string,
538+
lastArchivedWALTime time.Time,
539+
) error {
540+
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
541+
var objectStore barmancloudv1.ObjectStore
542+
543+
if err := c.Get(ctx, objectStoreKey, &objectStore); err != nil {
544+
return err
545+
}
546+
recoveryWindow := objectStore.Status.ServerRecoveryWindow[serverName]
547+
recoveryWindow.LastArchivedWALTime = ptr.To(metav1.NewTime(lastArchivedWALTime))
548+
549+
if objectStore.Status.ServerRecoveryWindow == nil {
550+
objectStore.Status.ServerRecoveryWindow = make(map[string]barmancloudv1.RecoveryWindow)
551+
}
552+
objectStore.Status.ServerRecoveryWindow[serverName] = recoveryWindow
553+
554+
return c.Status().Update(ctx, &objectStore)
555+
})
556+
}

internal/cnpgi/instance/metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var (
5151
firstRecoverabilityPointMetricName = buildFqName("first_recoverability_point")
5252
lastAvailableBackupTimestampMetricName = buildFqName("last_available_backup_timestamp")
5353
lastFailedBackupTimestampMetricName = buildFqName("last_failed_backup_timestamp")
54+
lastArchivedWALTimestampMetricName = buildFqName("last_archived_wal_timestamp")
5455
)
5556

5657
func (m metricsImpl) GetCapabilities(
@@ -97,6 +98,11 @@ func (m metricsImpl) Define(
9798
Help: "The last failed backup as a unix timestamp",
9899
ValueType: &metrics.MetricType{Type: metrics.MetricType_TYPE_GAUGE},
99100
},
101+
{
102+
FqName: lastArchivedWALTimestampMetricName,
103+
Help: "The last archived WAL timestamp as a unix timestamp",
104+
ValueType: &metrics.MetricType{Type: metrics.MetricType_TYPE_GAUGE},
105+
},
100106
},
101107
}, nil
102108
}
@@ -136,13 +142,18 @@ func (m metricsImpl) Collect(
136142
FqName: lastFailedBackupTimestampMetricName,
137143
Value: 0,
138144
},
145+
{
146+
FqName: lastArchivedWALTimestampMetricName,
147+
Value: 0,
148+
},
139149
},
140150
}, nil
141151
}
142152

143153
var firstRecoverabilityPoint float64
144154
var lastAvailableBackup float64
145155
var lastFailedBackup float64
156+
var lastArchivedWAL float64
146157
if x.FirstRecoverabilityPoint != nil {
147158
firstRecoverabilityPoint = float64(x.FirstRecoverabilityPoint.Unix())
148159
}
@@ -152,6 +163,9 @@ func (m metricsImpl) Collect(
152163
if x.LastFailedBackupTime != nil {
153164
lastFailedBackup = float64(x.LastFailedBackupTime.Unix())
154165
}
166+
if x.LastArchivedWALTime != nil {
167+
lastArchivedWAL = float64(x.LastArchivedWALTime.Unix())
168+
}
155169

156170
return &metrics.CollectMetricsResult{
157171
Metrics: []*metrics.CollectMetric{
@@ -167,6 +181,10 @@ func (m metricsImpl) Collect(
167181
FqName: lastFailedBackupTimestampMetricName,
168182
Value: lastFailedBackup,
169183
},
184+
{
185+
FqName: lastArchivedWALTimestampMetricName,
186+
Value: lastArchivedWAL,
187+
},
170188
},
171189
}, nil
172190
}

0 commit comments

Comments
 (0)