-
Notifications
You must be signed in to change notification settings - Fork 5
Fix dependency resolution order and reduce transient reconciliation errors #208
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
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
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
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
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,95 @@ | ||
| /* | ||
| Copyright 2024. | ||
|
|
||
| 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 utils | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| const ( | ||
| RetryCountAnnotation = "prefect.io/retry-count" | ||
| MaxRetryAttempts = 10 | ||
| ) | ||
|
|
||
| // CalculateBackoffDelay returns a progressive delay based on retry attempts | ||
| // Uses exponential backoff: 15s, 30s, 60s, 120s (max) | ||
| func CalculateBackoffDelay(attempts int) time.Duration { | ||
| delays := []time.Duration{ | ||
| 15 * time.Second, | ||
| 30 * time.Second, | ||
| 60 * time.Second, | ||
| 120 * time.Second, | ||
| } | ||
|
|
||
| if attempts >= len(delays) { | ||
| return delays[len(delays)-1] // Max delay | ||
| } | ||
|
|
||
| return delays[attempts] | ||
| } | ||
|
|
||
| // GetRetryCount retrieves the current retry count from object annotations | ||
| func GetRetryCount(obj client.Object) int { | ||
| if obj.GetAnnotations() == nil { | ||
| return 0 | ||
| } | ||
|
|
||
| countStr, exists := obj.GetAnnotations()[RetryCountAnnotation] | ||
| if !exists { | ||
| return 0 | ||
| } | ||
|
|
||
| count, err := strconv.Atoi(countStr) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| return count | ||
| } | ||
|
|
||
| // IncrementRetryCount increments the retry count annotation on an object | ||
| func IncrementRetryCount(obj client.Object) { | ||
| if obj.GetAnnotations() == nil { | ||
| obj.SetAnnotations(make(map[string]string)) | ||
| } | ||
|
|
||
| currentCount := GetRetryCount(obj) | ||
| newCount := currentCount + 1 | ||
|
|
||
| annotations := obj.GetAnnotations() | ||
| annotations[RetryCountAnnotation] = strconv.Itoa(newCount) | ||
| obj.SetAnnotations(annotations) | ||
| } | ||
|
|
||
| // ResetRetryCount clears the retry count annotation on successful operations | ||
| func ResetRetryCount(obj client.Object) { | ||
| if obj.GetAnnotations() == nil { | ||
| return | ||
| } | ||
|
|
||
| annotations := obj.GetAnnotations() | ||
| delete(annotations, RetryCountAnnotation) | ||
| obj.SetAnnotations(annotations) | ||
| } | ||
|
|
||
| // ShouldStopRetrying determines if we've exceeded max retry attempts | ||
| func ShouldStopRetrying(obj client.Object) bool { | ||
| return GetRetryCount(obj) >= MaxRetryAttempts | ||
| } |
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,112 @@ | ||
| /* | ||
| Copyright 2024. | ||
|
|
||
| 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 utils | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| prefectiov1 "github.com/PrefectHQ/prefect-operator/api/v1" | ||
| ) | ||
|
|
||
| var _ = Describe("Backoff utilities", func() { | ||
| Describe("CalculateBackoffDelay", func() { | ||
| It("should return progressive delays", func() { | ||
| testCases := []struct { | ||
| attempts int | ||
| expected time.Duration | ||
| }{ | ||
| {0, 15 * time.Second}, | ||
| {1, 30 * time.Second}, | ||
| {2, 60 * time.Second}, | ||
| {3, 120 * time.Second}, | ||
| {4, 120 * time.Second}, // Max delay | ||
| {10, 120 * time.Second}, // Still max delay | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| delay := CalculateBackoffDelay(tc.attempts) | ||
| Expect(delay).To(Equal(tc.expected), "Attempt %d should have delay %v", tc.attempts, tc.expected) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| Describe("Retry count management", func() { | ||
| var workPool *prefectiov1.PrefectWorkPool | ||
|
|
||
| BeforeEach(func() { | ||
| workPool = &prefectiov1.PrefectWorkPool{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-pool", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| It("should start with zero retry count", func() { | ||
| count := GetRetryCount(workPool) | ||
| Expect(count).To(Equal(0)) | ||
| }) | ||
|
|
||
| It("should increment retry count", func() { | ||
| IncrementRetryCount(workPool) | ||
| Expect(GetRetryCount(workPool)).To(Equal(1)) | ||
|
|
||
| IncrementRetryCount(workPool) | ||
| Expect(GetRetryCount(workPool)).To(Equal(2)) | ||
| }) | ||
|
|
||
| It("should reset retry count", func() { | ||
| IncrementRetryCount(workPool) | ||
| IncrementRetryCount(workPool) | ||
| Expect(GetRetryCount(workPool)).To(Equal(2)) | ||
|
|
||
| ResetRetryCount(workPool) | ||
| Expect(GetRetryCount(workPool)).To(Equal(0)) | ||
| }) | ||
|
|
||
| It("should detect when to stop retrying", func() { | ||
| for i := 0; i < MaxRetryAttempts-1; i++ { | ||
| IncrementRetryCount(workPool) | ||
| Expect(ShouldStopRetrying(workPool)).To(BeFalse()) | ||
| } | ||
|
|
||
| IncrementRetryCount(workPool) | ||
| Expect(ShouldStopRetrying(workPool)).To(BeTrue()) | ||
| }) | ||
|
|
||
| It("should handle missing annotations gracefully", func() { | ||
| workPool.SetAnnotations(nil) | ||
| count := GetRetryCount(workPool) | ||
| Expect(count).To(Equal(0)) | ||
|
|
||
| IncrementRetryCount(workPool) | ||
| Expect(GetRetryCount(workPool)).To(Equal(1)) | ||
| }) | ||
|
|
||
| It("should handle invalid annotation values", func() { | ||
| workPool.SetAnnotations(map[string]string{ | ||
| RetryCountAnnotation: "invalid", | ||
| }) | ||
| count := GetRetryCount(workPool) | ||
| Expect(count).To(Equal(0)) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
(non-blocking thought): I wonder if either of these packages would help here to avoid writing our own backoff logic.
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.
That's smart, captured it here: #210