forked from argoproj/argo-events
-
Notifications
You must be signed in to change notification settings - Fork 0
Add pre-fetch quota check for JetStream Sensor backpressure #13
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
abdulazillow
merged 9 commits into
feature/zg
from
abdula/AIP-9985-backpressure-prefetch
Jan 15, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
72dad24
AIP-9985: Add pre-fetch quota check for backpressure
abdulazillow cb62162
Add verbose logging for backpressure testing (temporary)
abdulazillow fa26732
Update poll interval docs
abdulazillow fbf5768
Clean up verbose backpressure logging
abdulazillow 01f99aa
Change default capacity ratio to 0.95 (5% buffer)
abdulazillow a733eb4
Add unit tests for backpressure
abdulazillow 8ac551f
Fix backpressure lost after reconnection
abdulazillow 12f77d8
Rename BACKPRESSURE_QUOTA_NAME to RESOURCE_QUOTA_NAME
abdulazillow 1c73eff
Add comment explaining closeCh check after backpressure wait
abdulazillow 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package sensor | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "github.com/argoproj/argo-events/metrics" | ||
| ) | ||
|
|
||
| // BackpressureWaiter checks ResourceQuota before allowing message fetch. | ||
| // This prevents fetching messages when downstream workflow capacity is exhausted, | ||
| // keeping messages safe in JetStream during backpressure conditions. | ||
| type BackpressureWaiter struct { | ||
| kubeClient kubernetes.Interface | ||
| namespace string | ||
| quotaName string | ||
| resourceName string // e.g., "count/workflows.argoproj.io" | ||
| capacityRatio float64 // e.g., 0.95 for 5% buffer | ||
| pollInterval time.Duration // How often to poll when blocked | ||
| logger *zap.SugaredLogger | ||
| metrics *metrics.Metrics | ||
| sensorName string | ||
| triggerName string | ||
| } | ||
|
|
||
| // BackpressureConfig holds configuration for BackpressureWaiter | ||
| type BackpressureConfig struct { | ||
| QuotaName string | ||
| ResourceName string // Default: "count/workflows.argoproj.io" | ||
| CapacityRatio float64 // Default: 0.95 (5% buffer) | ||
| PollInterval time.Duration // Default: 30s | ||
| SensorName string | ||
| TriggerName string | ||
| } | ||
|
|
||
| // NewBackpressureWaiter creates a new BackpressureWaiter | ||
| func NewBackpressureWaiter( | ||
| kubeClient kubernetes.Interface, | ||
| namespace string, | ||
| config BackpressureConfig, | ||
| m *metrics.Metrics, | ||
| logger *zap.SugaredLogger, | ||
| ) *BackpressureWaiter { | ||
| // Apply defaults | ||
| if config.ResourceName == "" { | ||
| config.ResourceName = "count/workflows.argoproj.io" | ||
| } | ||
| if config.CapacityRatio <= 0 || config.CapacityRatio > 1 { | ||
| config.CapacityRatio = 0.95 // 5% buffer by default | ||
| } | ||
| if config.PollInterval <= 0 { | ||
| config.PollInterval = 30 * time.Second | ||
| } | ||
|
|
||
| return &BackpressureWaiter{ | ||
| kubeClient: kubeClient, | ||
| namespace: namespace, | ||
| quotaName: config.QuotaName, | ||
| resourceName: config.ResourceName, | ||
| capacityRatio: config.CapacityRatio, | ||
| pollInterval: config.PollInterval, | ||
| logger: logger, | ||
| metrics: m, | ||
| sensorName: config.SensorName, | ||
| triggerName: config.TriggerName, | ||
| } | ||
| } | ||
|
|
||
| // HasCapacity checks if there's capacity to process more workflows. | ||
| // Returns true if used < (hard * capacityRatio), false otherwise. | ||
| func (b *BackpressureWaiter) HasCapacity(ctx context.Context) (bool, error) { | ||
| quota, err := b.kubeClient.CoreV1().ResourceQuotas(b.namespace).Get(ctx, b.quotaName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| resourceName := corev1.ResourceName(b.resourceName) | ||
| hard := quota.Status.Hard[resourceName] | ||
| used := quota.Status.Used[resourceName] | ||
|
|
||
| hardVal := hard.Value() | ||
| usedVal := used.Value() | ||
| threshold := int64(float64(hardVal) * b.capacityRatio) | ||
|
|
||
| hasCapacity := usedVal < threshold | ||
|
|
||
| // Debug logging - only visible when debug level is enabled | ||
| b.logger.Debugw("Quota check performed", | ||
| "quotaName", b.quotaName, | ||
| "resourceName", b.resourceName, | ||
| "hard", hardVal, | ||
| "used", usedVal, | ||
| "threshold", threshold, | ||
| "hasCapacity", hasCapacity, | ||
| ) | ||
|
|
||
| return hasCapacity, nil | ||
| } | ||
|
|
||
| // WaitForCapacity blocks until there's capacity available or context is cancelled. | ||
| // This should be called BEFORE fetching messages from JetStream. | ||
| func (b *BackpressureWaiter) WaitForCapacity(ctx context.Context) error { | ||
| wasBlocked := false | ||
|
|
||
| for { | ||
| hasCapacity, err := b.HasCapacity(ctx) | ||
| if err != nil { | ||
| // Fail-closed: if we can't verify quota, don't fetch | ||
| // This catches config errors (typos, RBAC issues) early | ||
| if !wasBlocked { | ||
| wasBlocked = true | ||
| if b.metrics != nil { | ||
| b.metrics.SetSensorQuotaBlocked(b.sensorName, b.triggerName, true) | ||
| } | ||
| } | ||
| b.logger.Errorw("Failed to check quota, will retry (message stays safe in JetStream)", | ||
| "error", err, | ||
| "quotaName", b.quotaName, | ||
| "pollInterval", b.pollInterval, | ||
| ) | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(b.pollInterval): | ||
| continue // Retry checking quota | ||
| } | ||
| } | ||
|
|
||
| if hasCapacity { | ||
| // Clear blocked metric if we were blocked | ||
| if wasBlocked { | ||
| b.logger.Infow("Capacity available, resuming message fetch", | ||
| "quotaName", b.quotaName, | ||
| ) | ||
| if b.metrics != nil { | ||
| b.metrics.SetSensorQuotaBlocked(b.sensorName, b.triggerName, false) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Mark as blocked on first iteration without capacity | ||
| if !wasBlocked { | ||
| wasBlocked = true | ||
| b.logger.Infow("Backpressure active, blocking message fetch until capacity available", | ||
| "quotaName", b.quotaName, | ||
| "pollInterval", b.pollInterval, | ||
| ) | ||
| if b.metrics != nil { | ||
| b.metrics.SetSensorQuotaBlocked(b.sensorName, b.triggerName, true) | ||
| } | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(b.pollInterval): | ||
| // Continue checking | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.