shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Feature] Make configured sort the primary priority for selector and rule matching

Open sunnysabor opened this issue 1 week ago • 0 comments

Feature Request

Use the configured Order (sort) as the primary priority when multiple selectors or rules match a request. Use the existing condition specificity score to break ties between matching candidates with the same sort. I'd like to discuss both the precedence contract and how to introduce it safely for existing deployments.

Is your feature request related to a problem? Please describe

The Selector and Rule documentation describes a smaller Order value as higher priority for both selectors and rules. On master at 037d6f10b, AbstractShenyuPlugin.matchSelector and matchRule rank matching candidates by specificity first, then by ascending sort. The score is the number of configured conditions for AND and zero for OR.

This matters because matching has two stages. The winning selector determines which rules the gateway considers. The winning rule can determine the upstream or another plugin action, depending on the plugin. Today, adding a condition can change the winner for requests that still match both candidates, even when their configured priorities stay the same. With sort-first precedence, conditions determine which traffic a route matches, and Order gives operators explicit control over overlaps across priority levels. That makes fallback placement and reviews of routing changes easier, particularly when configurations are maintained by several teams.

Example with two enabled candidates

Candidate Sort Conditions
A 10 URI matches /orders/**
B 20 URI matches /orders/** AND header X-Region equals east

A request to /orders/123 with X-Region: east matches both. B wins today because its specificity score is higher. Under the proposed contract, A wins because it has a lower sort. If both have sort = 10, B wins on specificity. This example applies separately to two selectors in one plugin and to two rules under one selector.

Describe the solution you'd like

Apply the following precedence to the standard condition-based selector and rule matching paths:

  1. Consider enabled candidates that match the request.
  2. Prefer the lowest sort value.
  3. For equal sort values, prefer the higher existing specificity score.
  4. If both values tie, preserve the current encounter-order behavior.

For a new gateway deployment, sort-first can be adopted from the start. Operators can assign an exceptional route a lower sort and a fallback a higher one, then understand their precedence directly from the configuration. Equal priorities still use the existing specificity score. The proposal uses the current condition scoring and focuses on how the two ranking keys are ordered.

Describe alternatives you've considered

One option is to change the default in a release that explicitly documents the routing behavior change and provides migration guidance. Another is to introduce sort-first as an opt-in so existing deployments can validate and adopt it explicitly, while new deployments can choose it from day one. An opt-in would reduce upgrade risk at the cost of maintaining and documenting two matching behaviors. I'd appreciate maintainer feedback on which rollout fits ShenYu's compatibility expectations.

Additional context

Production upgrade risk. An existing gateway can select a different route after upgrading even when the incoming request and stored configuration are identical. The configurations to assess are overlapping selectors or rules where the lowest sort and the highest specificity favor different candidates. A change in selector can expose a different set of rules. A change in rule can change the upstream or another plugin action. Operators should compare the selected selector, selected rule, and resulting action for representative traffic in staging before deploying the new behavior. Where outcomes differ, the intended priority can be captured in sort and verified before rollout.

Validation. Cover both matching stages with cases for different sorts, equal sorts, ties on both keys, and disabled or non-matching candidates. Verify that cached and uncached decisions agree for the same request and configuration, including after a sort update. Document the precedence and describe the upgrade impact in the release notes.

Does sort-first reflect the intended meaning of Order at both stages, and what rollout would maintainers prefer for existing gateways?

sunnysabor avatar Nov 21 '26 01:11 sunnysabor