Skip to content

feat: add trigger weight for load distribution#3893

Open
yugannkt wants to merge 3 commits intoargoproj:masterfrom
yugannkt:feat/trigger-weight
Open

feat: add trigger weight for load distribution#3893
yugannkt wants to merge 3 commits intoargoproj:masterfrom
yugannkt:feat/trigger-weight

Conversation

@yugannkt
Copy link
Copy Markdown
Contributor

Title

feat: add trigger weight for load distribution (#3800)

Description

This PR adds a weight field to triggers that enables distributing events across multiple triggers based on configured weights. This addresses the feature request in #3800.

Problem

Users want to route events to different triggers based on percentages - for example, send 30% of events to one trigger and 70% to another. This enables use cases like A/B testing, canary deployments, and load distribution.

Solution

Added a weight field (0-100) to the Trigger struct that uses hash-based routing for deterministic event distribution.

Example usage:

apiVersion: argoproj.io/v1alpha1
kind: Sensor
spec:
  triggers:
    - template:
        name: primary-trigger
      weight: 70    # Receives 70% of events
    - template:
        name: canary-trigger
      weight: 30    # Receives 30% of events

Key Features

Feature Description
Deterministic routing Uses hash of event ID - same event always routes to same trigger
Multi-replica safe Works correctly across multiple sensor replicas
Backward compatible weight: 0 or omitted = trigger always executes (existing behavior)
Mutually exclusive Weighted triggers are mutually exclusive - exactly one executes per event
Flexible Non-weighted triggers can coexist and always execute

Testing

  • Unit tests for shouldExecuteByWeight() function
  • Unit tests for hashEventIDs() function
  • Unit tests for weight validation
  • All existing tests pass

Fixes #3800

This adds a weight field to triggers that enables distributing events
across multiple triggers based on configured weights.

Key features:
- Weight field (0-100) on Trigger struct
- Hash-based routing using event ID for deterministic distribution
- Weighted triggers are mutually exclusive (one executes per event)
- Non-weighted triggers (weight=0) always execute
- Works correctly with multiple sensor replicas

Use cases:
- A/B testing trigger implementations
- Canary deployments
- Load distribution across backends

Fixes argoproj#3800

Signed-off-by: Yugan <yugannkt@gmail.com>
@yugannkt yugannkt requested a review from whynowy as a code owner January 24, 2026 15:00
// Returns true if the trigger should execute, false otherwise.
// The second return value indicates if weight-based routing is enabled for this trigger.
func shouldExecuteByWeight(trigger v1alpha1.Trigger, allTriggers []v1alpha1.Trigger, eventHash uint64) (bool, bool) {
// If weight is 0 or not set, weight-based routing is disabled for this trigger
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think shouldExecuteByWeight doesn't need to return two bool.

When weightEnabled=false, shouldExecute is always true (the function returns (true, false) in those cases). So the condition weightEnabled && !shouldExecute is functionally equivalent to just !shouldExecute.

weightedTriggers = append(weightedTriggers, TriggerWeightRange{
TriggerName: t.Template.Name,
StartRange: totalWeight,
EndRange: totalWeight + t.Weight,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can t.Template be nil ?

// Find if this trigger's range contains the calculated position
for _, wr := range weightedTriggers {
if wr.TriggerName == trigger.Template.Name {
if position >= wr.StartRange && position < wr.EndRange {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here also, can trigger.Template be `nil´ ?

for _, c := range id {
hash ^= uint64(c)
hash *= 1099511628211
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use all of this? the standard lib has hash/fnv lib.

@whynowy whynowy requested a review from eduardodbr April 3, 2026 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add trigger weight to route events

2 participants