Skip to content

Commit b7c9690

Browse files
mnenciaarmru
andauthored
fix(slots): cleanup replication slots on replicas when features disabled (#9381)
When both replicationSlots.highAvailability.enabled and replicationSlots.synchronizeReplicas.enabled are disabled, the Replicator now performs cleanup by calling reconcile() before continuing the loop, ensuring user-defined replication slots are properly removed from replica pods. This change also adds webhook validation to prevent users from removing the synchronizeReplicas configuration section while it is still enabled. This ensures that cleanup logic can properly execute before the configuration is removed, preventing orphaned replication slots on replicas. Closes #9380 Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Armando Ruocco <[email protected]> Co-authored-by: Armando Ruocco <[email protected]>
1 parent c8b3cff commit b7c9690

3 files changed

Lines changed: 170 additions & 26 deletions

File tree

‎internal/management/controller/slots/runner/runner.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ func (sr *Replicator) Start(ctx context.Context) error {
7272
case <-ticker.C:
7373
}
7474

75-
// If replication is disabled stop the timer,
76-
// the process will resume through the wakeUp channel if necessary
75+
// If replication is disabled, stop the timer and perform cleanup
7776
if config == nil || !config.GetEnabled() {
7877
ticker.Stop()
79-
// we set updateInterval to 0 to make sure the Ticker will be reset
78+
// We set updateInterval to 0 to make sure the Ticker will be reset
8079
// if the feature is enabled again
8180
updateInterval = 0
81+
// Perform cleanup before continuing
82+
if config != nil {
83+
if err := sr.reconcile(ctx, config); err != nil {
84+
contextLog.Warning("cleanup during reconcile when features are disabled", "err", err)
85+
}
86+
}
8287
continue
8388
}
8489

‎internal/webhook/v1/cluster_webhook.go‎

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,33 +2273,54 @@ func (v *ClusterCustomValidator) validateReplicationSlotsChange(r, old *apiv1.Cl
22732273
newReplicationSlots := r.Spec.ReplicationSlots
22742274
oldReplicationSlots := old.Spec.ReplicationSlots
22752275

2276-
if oldReplicationSlots == nil || oldReplicationSlots.HighAvailability == nil ||
2277-
!oldReplicationSlots.HighAvailability.GetEnabled() {
2276+
var errs field.ErrorList
2277+
2278+
if oldReplicationSlots == nil {
22782279
return nil
22792280
}
22802281

2281-
var errs field.ErrorList
2282+
// Validate HighAvailability changes
2283+
if oldReplicationSlots.HighAvailability.GetEnabled() {
2284+
// When disabling, we should check that the prefix is not removed and doesn't change
2285+
// to properly execute the cleanup logic
2286+
if newReplicationSlots == nil || newReplicationSlots.HighAvailability == nil {
2287+
path := field.NewPath("spec", "replicationSlots")
2288+
if newReplicationSlots != nil {
2289+
path = path.Child("highAvailability")
2290+
}
2291+
errs = append(errs,
2292+
field.Invalid(
2293+
path,
2294+
nil,
2295+
fmt.Sprintf("Cannot remove %v section while highAvailability is enabled", path)),
2296+
)
2297+
} else if oldReplicationSlots.HighAvailability.SlotPrefix != newReplicationSlots.HighAvailability.SlotPrefix {
2298+
errs = append(errs,
2299+
field.Invalid(
2300+
field.NewPath("spec", "replicationSlots", "highAvailability", "slotPrefix"),
2301+
newReplicationSlots.HighAvailability.SlotPrefix,
2302+
"Cannot change replication slot prefix while highAvailability is enabled"),
2303+
)
2304+
}
2305+
}
22822306

2283-
// when disabling we should check that the prefix it's not removed, and it doesn't change to
2284-
// properly execute the cleanup logic
2285-
if newReplicationSlots == nil || newReplicationSlots.HighAvailability == nil {
2286-
path := field.NewPath("spec", "replicationSlots")
2287-
if newReplicationSlots != nil {
2288-
path = path.Child("highAvailability")
2307+
// Validate SynchronizeReplicas changes
2308+
// When synchronizeReplicas is enabled, we need to ensure users disable it before removing the configuration
2309+
// to allow the cleanup logic to properly remove user-defined replication slots from replicas
2310+
if oldReplicationSlots.SynchronizeReplicas.GetEnabled() {
2311+
if newReplicationSlots == nil || newReplicationSlots.SynchronizeReplicas == nil {
2312+
path := field.NewPath("spec", "replicationSlots")
2313+
if newReplicationSlots != nil {
2314+
path = path.Child("synchronizeReplicas")
2315+
}
2316+
errs = append(errs,
2317+
field.Invalid(
2318+
path,
2319+
nil,
2320+
fmt.Sprintf("Cannot remove %v section while synchronizeReplicas is enabled. "+
2321+
"Disable synchronizeReplicas first to allow cleanup of user-defined replication slots on replicas", path)),
2322+
)
22892323
}
2290-
errs = append(errs,
2291-
field.Invalid(
2292-
path,
2293-
nil,
2294-
fmt.Sprintf("Cannot remove %v section while highAvailability is enabled", path)),
2295-
)
2296-
} else if oldReplicationSlots.HighAvailability.SlotPrefix != newReplicationSlots.HighAvailability.SlotPrefix {
2297-
errs = append(errs,
2298-
field.Invalid(
2299-
field.NewPath("spec", "replicationSlots", "highAvailability", "slotPrefix"),
2300-
newReplicationSlots.HighAvailability.SlotPrefix,
2301-
"Cannot change replication slot prefix while highAvailability is enabled"),
2302-
)
23032324
}
23042325

23052326
return errs

‎internal/webhook/v1/cluster_webhook_test.go‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3546,6 +3546,9 @@ var _ = Describe("validation of replication slots configuration", func() {
35463546
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
35473547
Enabled: ptr.To(false),
35483548
},
3549+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3550+
Enabled: ptr.To(false),
3551+
},
35493552
},
35503553
},
35513554
}
@@ -3557,6 +3560,9 @@ var _ = Describe("validation of replication slots configuration", func() {
35573560
Enabled: ptr.To(true),
35583561
SlotPrefix: "_test_",
35593562
},
3563+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3564+
Enabled: ptr.To(true),
3565+
},
35603566
}
35613567

35623568
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(BeEmpty())
@@ -3581,7 +3587,7 @@ var _ = Describe("validation of replication slots configuration", func() {
35813587
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(HaveLen(1))
35823588
})
35833589

3584-
It("prevents removing the replication slot section when replication slots are enabled", func() {
3590+
It("prevents removing the replication slot section when highAvailability is enabled", func() {
35853591
oldCluster := &apiv1.Cluster{
35863592
Spec: apiv1.ClusterSpec{
35873593
ImageName: versions.DefaultImageName,
@@ -3590,6 +3596,30 @@ var _ = Describe("validation of replication slots configuration", func() {
35903596
Enabled: ptr.To(true),
35913597
SlotPrefix: "_test_",
35923598
},
3599+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3600+
Enabled: ptr.To(false),
3601+
},
3602+
},
3603+
},
3604+
}
3605+
oldCluster.Default()
3606+
3607+
newCluster := oldCluster.DeepCopy()
3608+
newCluster.Spec.ReplicationSlots = nil
3609+
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(HaveLen(1))
3610+
})
3611+
3612+
It("prevents removing the replication slot section when synchronizeReplicas is enabled", func() {
3613+
oldCluster := &apiv1.Cluster{
3614+
Spec: apiv1.ClusterSpec{
3615+
ImageName: versions.DefaultImageName,
3616+
ReplicationSlots: &apiv1.ReplicationSlotsConfiguration{
3617+
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
3618+
Enabled: ptr.To(false),
3619+
},
3620+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3621+
Enabled: ptr.To(true),
3622+
},
35933623
},
35943624
},
35953625
}
@@ -3600,6 +3630,28 @@ var _ = Describe("validation of replication slots configuration", func() {
36003630
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(HaveLen(1))
36013631
})
36023632

3633+
It("prevents removing the replication slot section when both features are enabled", func() {
3634+
oldCluster := &apiv1.Cluster{
3635+
Spec: apiv1.ClusterSpec{
3636+
ImageName: versions.DefaultImageName,
3637+
ReplicationSlots: &apiv1.ReplicationSlotsConfiguration{
3638+
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
3639+
Enabled: ptr.To(true),
3640+
SlotPrefix: "_test_",
3641+
},
3642+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3643+
Enabled: ptr.To(true),
3644+
},
3645+
},
3646+
},
3647+
}
3648+
oldCluster.Default()
3649+
3650+
newCluster := oldCluster.DeepCopy()
3651+
newCluster.Spec.ReplicationSlots = nil
3652+
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(HaveLen(2))
3653+
})
3654+
36033655
It("allows disabling the replication slots", func() {
36043656
oldCluster := &apiv1.Cluster{
36053657
Spec: apiv1.ClusterSpec{
@@ -3663,6 +3715,72 @@ var _ = Describe("validation of replication slots configuration", func() {
36633715
Expect(errors).To(BeEmpty())
36643716
})
36653717

3718+
It("prevents removing the synchronizeReplicas section when synchronizeReplicas is enabled", func() {
3719+
oldCluster := &apiv1.Cluster{
3720+
Spec: apiv1.ClusterSpec{
3721+
ImageName: versions.DefaultImageName,
3722+
ReplicationSlots: &apiv1.ReplicationSlotsConfiguration{
3723+
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
3724+
Enabled: ptr.To(false),
3725+
},
3726+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3727+
Enabled: ptr.To(true),
3728+
},
3729+
},
3730+
},
3731+
}
3732+
oldCluster.Default()
3733+
3734+
newCluster := oldCluster.DeepCopy()
3735+
newCluster.Spec.ReplicationSlots.SynchronizeReplicas = nil
3736+
errs := v.validateReplicationSlotsChange(newCluster, oldCluster)
3737+
Expect(errs).To(HaveLen(1))
3738+
Expect(errs[0].Error()).To(ContainSubstring("Cannot remove"))
3739+
Expect(errs[0].Error()).To(ContainSubstring("synchronizeReplicas"))
3740+
})
3741+
3742+
It("allows disabling the synchronizeReplicas feature", func() {
3743+
oldCluster := &apiv1.Cluster{
3744+
Spec: apiv1.ClusterSpec{
3745+
ImageName: versions.DefaultImageName,
3746+
ReplicationSlots: &apiv1.ReplicationSlotsConfiguration{
3747+
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
3748+
Enabled: ptr.To(false),
3749+
},
3750+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3751+
Enabled: ptr.To(true),
3752+
},
3753+
},
3754+
},
3755+
}
3756+
oldCluster.Default()
3757+
3758+
newCluster := oldCluster.DeepCopy()
3759+
newCluster.Spec.ReplicationSlots.SynchronizeReplicas.Enabled = ptr.To(false)
3760+
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(BeEmpty())
3761+
})
3762+
3763+
It("allows removing synchronizeReplicas section when synchronizeReplicas is disabled", func() {
3764+
oldCluster := &apiv1.Cluster{
3765+
Spec: apiv1.ClusterSpec{
3766+
ImageName: versions.DefaultImageName,
3767+
ReplicationSlots: &apiv1.ReplicationSlotsConfiguration{
3768+
HighAvailability: &apiv1.ReplicationSlotsHAConfiguration{
3769+
Enabled: ptr.To(false),
3770+
},
3771+
SynchronizeReplicas: &apiv1.SynchronizeReplicasConfiguration{
3772+
Enabled: ptr.To(false),
3773+
},
3774+
},
3775+
},
3776+
}
3777+
oldCluster.Default()
3778+
3779+
newCluster := oldCluster.DeepCopy()
3780+
newCluster.Spec.ReplicationSlots.SynchronizeReplicas = nil
3781+
Expect(v.validateReplicationSlotsChange(newCluster, oldCluster)).To(BeEmpty())
3782+
})
3783+
36663784
It("returns no errors when synchronizeLogicalDecoding is disabled", func() {
36673785
cluster := &apiv1.Cluster{
36683786
Spec: apiv1.ClusterSpec{

0 commit comments

Comments
 (0)