shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Question] What is the intention of changing selection sort rule in v2.5.0

Open vijayuan opened this issue 2 years ago • 4 comments

Question

https://github.com/apache/shenyu/pull/3579

This change includes an implicit sort rule which may cause wrong url match for upgrading from v2.4.3. On the other hand, users may confuse with the sort rule because the url matching order may not be the same with they configed.

vijayuan avatar Sep 02 '22 00:09 vijayuan

After this change, all and selections have higher priority than all or selections.

If I config another selection with just one condition, I think there is no difference between and and or. But, not !!

vijayuan avatar Sep 02 '22 01:09 vijayuan

Can you provide a more detailed case?Thanks a lot.

KevinClair avatar Sep 02 '22 02:09 KevinClair

I think the codes under is u commented about, Yes? With selector and rule, we match the best one to execute your logic, so we can change selector and rule to do that. Is that right?

# @see org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
private SelectorData manyMatchSelector(final List<SelectorData> filterCollectors) {
        //What needs to be dealt with here is the and condition. If the number of and conditions is the same and is matched at the same time,
        // it will be sorted by the sort field.
        Map<Integer, List<Pair<Integer, SelectorData>>> collect =
                filterCollectors.stream().map(selector -> {
                    boolean match = MatchModeEnum.match(selector.getMatchMode(), MatchModeEnum.AND);
                    int sort = 0;
                    if (match) {
                        sort = selector.getConditionList().size();
                    }
                    return Pair.of(sort, selector);
                }).collect(Collectors.groupingBy(Pair::getLeft));
        Integer max = Collections.max(collect.keySet());
        List<Pair<Integer, SelectorData>> pairs = collect.get(max);
        return pairs.stream().map(Pair::getRight).min(Comparator.comparing(SelectorData::getSort)).orElse(null);
    }

damonxue avatar Sep 02 '22 07:09 damonxue

Can you provide a more detailed case?Thanks a lot.

For example, I config 2 selectors : The first one: MatchType: or conditions: 1. url match /a/** 2. url match /b/** continued: false order: 1

The second one: MatchType: and conditions: url match /** continued: false order: 1000

Now I have a request http://host:port/a/action. In version 2.4.3, the first selector is selected because the first selector's order is smaller. In version 2.5.0, the second selector is selected because and matchType have higher priority.

So,when I upgrade shenyu to 2.5.0, I have to change the MatchType of the second selector to or.

However, as a user of shenyu, I may think and and or matchTypes are same for a selector if there is just one condition in the selector.

vijayuan avatar Sep 03 '22 05:09 vijayuan

@yu199195 same question. Why use selector.getConditionList().size() as sort field?

gMan1990 avatar Aug 01 '23 07:08 gMan1990