-
Notifications
You must be signed in to change notification settings - Fork 1
fix(controller): retry the auto-snapshot RD bookkeeping stamp on update conflict #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+208
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
internal/controller/autosnapshot_stamp_conflict_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /* | ||
| Copyright 2026 Cozystack contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
|
|
||
| blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1" | ||
| ) | ||
|
|
||
| // TestAutoSnapshotStampSurvivesRDUpdateConflict pins the bookkeeping | ||
| // half of the auto-snapshot tick against the reconciler race observed | ||
| // in the L-integration stack (TestGroupG/AutoSnapshotPeriodicTick): | ||
| // the RD object the runnable stamps comes from Tick's List, and the | ||
| // Snapshot create wakes reconcilers that update the RD concurrently — | ||
| // so the stamp's bare Update hits a 409 Conflict. Pre-fix the conflict | ||
| // aborted the stamp (Tick swallows per-RD errors), leaving NextAutoId | ||
| // and the last-at annotation unset: the next tick re-derived the SAME | ||
| // id every interval and only the createAutoSnapshot AlreadyExists | ||
| // short-circuit kept the loop from duplicating snapshots. The stamp | ||
| // must instead re-read the RD fresh and retry the conflict away. | ||
| func TestAutoSnapshotStampSurvivesRDUpdateConflict(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| scheme := runtime.NewScheme() | ||
| if err := blockstoriov1alpha1.AddToScheme(scheme); err != nil { | ||
| t.Fatalf("AddToScheme: %v", err) | ||
| } | ||
|
|
||
| rd := &blockstoriov1alpha1.ResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "rd-stamp-conflict"}, | ||
| Spec: blockstoriov1alpha1.ResourceDefinitionSpec{ | ||
| Props: map[string]string{PropAutoSnapshotRunEvery: "1"}, | ||
| }, | ||
| } | ||
|
|
||
| // Model the racing reconciler: the FIRST RD Update lands on a | ||
| // stale resourceVersion and 409s; the retry (against a fresh | ||
| // read) succeeds. | ||
| conflicted := false | ||
| cli := fake.NewClientBuilder(). | ||
| WithScheme(scheme). | ||
| WithObjects(rd). | ||
| WithInterceptorFuncs(interceptor.Funcs{ | ||
| Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { | ||
| _, isRD := obj.(*blockstoriov1alpha1.ResourceDefinition) | ||
| if isRD && !conflicted { | ||
| conflicted = true | ||
|
|
||
| return apierrors.NewConflict( | ||
| schema.GroupResource{Group: "blockstor.cozystack.io", Resource: "resourcedefinitions"}, | ||
| obj.GetName(), | ||
| nil, | ||
| ) | ||
| } | ||
|
|
||
| return c.Update(ctx, obj, opts...) | ||
| }, | ||
| }). | ||
| Build() | ||
|
|
||
| runnable := &AutoSnapshotRunnable{Client: cli} | ||
|
|
||
| if err := runnable.Tick(context.Background()); err != nil { | ||
| t.Fatalf("Tick: %v", err) | ||
| } | ||
|
|
||
| if !conflicted { | ||
| t.Fatalf("interceptor never fired — the test no longer models the stamp Update") | ||
| } | ||
|
|
||
| var updated blockstoriov1alpha1.ResourceDefinition | ||
| if err := cli.Get(context.Background(), types.NamespacedName{Name: "rd-stamp-conflict"}, &updated); err != nil { | ||
| t.Fatalf("re-fetch RD: %v", err) | ||
| } | ||
|
|
||
| if got := updated.Spec.Props[PropAutoSnapshotNextID]; got != "2" { | ||
| t.Errorf("NextAutoId after conflicted stamp: got %q, want \"2\"", got) | ||
| } | ||
|
|
||
| if updated.Annotations[AnnotationAutoSnapshotLastAt] == "" { | ||
| t.Errorf("AnnotationAutoSnapshotLastAt unset after conflicted stamp; the tick would re-fire and re-derive the same id") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build integration | ||
|
|
||
| /* | ||
| Copyright 2026 Cozystack contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package harness | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestScaledTimeoutStretchesOnCI pins the CI budget stretch: the | ||
| // per-group convergence constants are tuned for dev machines, and the | ||
| // Integration lane rotate-flaked on GitHub runners until Eventually | ||
| // budgets were scaled there (GroupI / GroupJ 30s timeouts). | ||
| func TestScaledTimeoutStretchesOnCI(t *testing.T) { | ||
| t.Setenv("CI", "true") | ||
|
|
||
| if got := scaledTimeout(30 * time.Second); got != 90*time.Second { | ||
| t.Fatalf("scaledTimeout on CI: got %s, want 90s", got) | ||
| } | ||
|
|
||
| t.Setenv("CI", "") | ||
|
|
||
| if got := scaledTimeout(30 * time.Second); got != 30*time.Second { | ||
| t.Fatalf("scaledTimeout off CI: got %s, want 30s", got) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the previous implementation,
stampRDAfterCreatemodified the passed-inrdobject in-place. With the new retry logic,rdis left unmodified because the updates are applied to a localfreshvariable. Although the current caller does not rely on the updated fields ofrdafter this call, leaving the passed-in pointer stale can easily lead to subtle bugs in the future if the caller or subsequent logic is extended to userd.To prevent this, update the dereferenced
rdpointer with thefreshobject upon a successful API update.