Skip to content

Commit 57f6efb

Browse files
committed
[ISSUE #8208] Observe actual selector and rule matching decisions without reevaluation
1 parent bac845f commit 57f6efb

5 files changed

Lines changed: 862 additions & 0 deletions

File tree

‎shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
import org.apache.shenyu.plugin.base.cache.BaseDataCache;
3636
import org.apache.shenyu.plugin.base.cache.MatchDataCache;
3737
import org.apache.shenyu.plugin.base.condition.strategy.MatchStrategyFactory;
38+
import org.apache.shenyu.plugin.base.match.SelectorRuleMatchObservation;
39+
import org.apache.shenyu.plugin.base.match.SelectorRuleMatchObservation.CacheResolution;
40+
import org.apache.shenyu.plugin.base.match.SelectorRuleMatchObservation.Decision;
41+
import org.apache.shenyu.plugin.base.match.SelectorRuleMatchObserver;
42+
import org.apache.shenyu.plugin.base.match.SelectorRuleMatchObserverHolder;
3843
import org.slf4j.Logger;
3944
import org.slf4j.LoggerFactory;
4045
import org.springframework.web.server.ServerWebExchange;
@@ -87,54 +92,86 @@ public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginCh
8792
PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(pluginName);
8893
// early exit
8994
if (Objects.isNull(pluginData) || !pluginData.getEnabled()) {
95+
reportDecision(Objects.isNull(pluginData) ? Decision.NO_PLUGIN_DATA : Decision.PLUGIN_DISABLED,
96+
null, null, false, CacheResolution.NOT_CONSULTED, CacheResolution.NOT_CONSULTED);
9097
return chain.execute(exchange);
9198
}
9299
final String path = getRawPath(exchange);
93100
List<SelectorData> selectors = BaseDataCache.getInstance().obtainSelectorData(pluginName);
94101
if (CollectionUtils.isEmpty(selectors)) {
102+
reportDecision(Decision.NO_SELECTOR, null, null, false,
103+
CacheResolution.NOT_CONSULTED, CacheResolution.NOT_CONSULTED);
95104
return handleSelectorIfNull(pluginName, exchange, chain);
96105
}
97106
SelectorData selectorData = obtainSelectorDataCacheIfEnabled(path);
107+
CacheResolution selectorCache = Objects.nonNull(selectorData) ? CacheResolution.POSITIVE_HIT
108+
: cacheResolution(selectorMatchConfig.getCache().getEnabled());
98109
// handle Selector
99110
if (Objects.nonNull(selectorData) && StringUtils.isBlank(selectorData.getId())) {
111+
reportDecision(Decision.NO_SELECTOR, null, null, false,
112+
CacheResolution.NEGATIVE_HIT, CacheResolution.NOT_CONSULTED);
100113
return handleSelectorIfNull(pluginName, exchange, chain);
101114
}
102115
if (Objects.isNull(selectorData)) {
103116
selectorData = defaultMatchSelector(exchange, selectors, path);
104117
if (Objects.isNull(selectorData)) {
118+
reportDecision(Decision.SELECTOR_UNMATCHED, null, null, false,
119+
selectorCache, CacheResolution.NOT_CONSULTED);
105120
return handleSelectorIfNull(pluginName, exchange, chain);
106121
}
107122
}
108123
printLog(selectorData, pluginName);
109124
if (!selectorData.getContinued()) {
110125
// if continued, not match rules
126+
reportDecision(Decision.SELECTOR_NOT_CONTINUED, selectorData, null, true,
127+
selectorCache, CacheResolution.NOT_CONSULTED);
111128
return doExecute(exchange, chain, selectorData, defaultRuleData(selectorData));
112129
}
130+
return matchRuleAndExecute(exchange, chain, pluginName, selectors, path, selectorData, selectorCache);
131+
}
132+
133+
private Mono<Void> matchRuleAndExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain,
134+
final String pluginName, final List<SelectorData> selectors,
135+
final String path, final SelectorData selectorData,
136+
final CacheResolution selectorCache) {
113137
List<RuleData> rules = BaseDataCache.getInstance().obtainRuleData(selectorData.getId());
114138
if (CollectionUtils.isEmpty(rules)) {
139+
reportDecision(Decision.NO_RULE, selectorData, null, false,
140+
selectorCache, CacheResolution.NOT_CONSULTED);
115141
return handleRuleIfNull(pluginName, exchange, chain);
116142
}
117143
if (selectorData.getType() == SelectorTypeEnum.FULL_FLOW.getCode()) {
118144
RuleData rule = lastEnabledRule(rules);
119145
if (Objects.isNull(rule)) {
146+
reportDecision(Decision.NO_RULE, selectorData, null, false,
147+
selectorCache, CacheResolution.NOT_CONSULTED);
120148
return handleRuleIfNull(pluginName, exchange, chain);
121149
}
122150
printLog(rule, pluginName);
151+
reportDecision(Decision.FULL_FLOW_RULE, selectorData, rule, false,
152+
selectorCache, CacheResolution.NOT_CONSULTED);
123153
return doExecute(exchange, chain, selectorData, rule);
124154
}
125155
// consult the bounded per-path match cache first when it is enabled.
126156
// on a cache miss, match the current rule list with the default strategy.
127157
RuleData ruleData = obtainRuleDataCacheIfEnabled(path);
158+
CacheResolution ruleCache = Objects.nonNull(ruleData) ? CacheResolution.POSITIVE_HIT
159+
: cacheResolution(ruleMatchConfig.getCache().getEnabled());
128160
if (Objects.nonNull(ruleData) && Objects.isNull(ruleData.getId())) {
161+
reportDecision(Decision.NO_RULE, selectorData, null, false,
162+
selectorCache, CacheResolution.NEGATIVE_HIT);
129163
return handleRuleIfNull(pluginName, exchange, chain);
130164
}
131165
if (Objects.isNull(ruleData)) {
132166
ruleData = defaultMatchRule(exchange, selectors, rules, path);
133167
if (Objects.isNull(ruleData)) {
168+
reportDecision(Decision.RULE_UNMATCHED, selectorData, null, false,
169+
selectorCache, ruleCache);
134170
return handleRuleIfNull(pluginName, exchange, chain);
135171
}
136172
}
137173
printLog(ruleData, pluginName);
174+
reportDecision(Decision.SELECTED, selectorData, ruleData, false, selectorCache, ruleCache);
138175
return doExecute(exchange, chain, selectorData, ruleData);
139176
}
140177

@@ -147,6 +184,41 @@ private static RuleData lastEnabledRule(final List<RuleData> rules) {
147184
}
148185
return null;
149186
}
187+
188+
private static CacheResolution cacheResolution(final boolean cacheEnabled) {
189+
return cacheEnabled ? CacheResolution.EVALUATED : CacheResolution.NOT_CONSULTED;
190+
}
191+
192+
/**
193+
* Report one matching decision to the registered observer, if any.
194+
*
195+
* <p>The summary is built only when an observer is registered, so the un-instrumented
196+
* path performs a single null read and allocates nothing. The report never re-runs a
197+
* predicate: it only names the branch just taken and the ids already in hand.</p>
198+
*
199+
* @param decision the decision reason
200+
* @param selector the selected selector, or null
201+
* @param rule the selected rule, or null
202+
* @param defaultRule whether the rule is the synthetic default rule
203+
* @param selectorCache how the selector stage resolved its cache
204+
* @param ruleCache how the rule stage resolved its cache
205+
*/
206+
private void reportDecision(final Decision decision, final SelectorData selector, final RuleData rule,
207+
final boolean defaultRule, final CacheResolution selectorCache,
208+
final CacheResolution ruleCache) {
209+
SelectorRuleMatchObserver observer = SelectorRuleMatchObserverHolder.get();
210+
if (Objects.isNull(observer)) {
211+
return;
212+
}
213+
try {
214+
final String ruleId = defaultRule ? Constants.DEFAULT_RULE : (Objects.isNull(rule) ? null : rule.getId());
215+
observer.onMatchDecision(SelectorRuleMatchObservation.of(named(), decision,
216+
Objects.isNull(selector) ? null : selector.getId(), ruleId,
217+
defaultRule, selectorCache, ruleCache));
218+
} catch (RuntimeException ignored) {
219+
// a broken observer must never influence the request outcome
220+
}
221+
}
150222

151223
protected String getRawPath(final ServerWebExchange exchange) {
152224
return RequestUrlUtils.getRewrittenRawPath(exchange);
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shenyu.plugin.base.match;
19+
20+
/**
21+
* Immutable, bounded summary of one real selector/rule matching decision taken by
22+
* {@code AbstractShenyuPlugin}.
23+
*
24+
* <p>The record is emitted from the decision branches themselves, so it never re-runs a
25+
* predicate, consumes a request body or touches a cache. A {@link CacheResolution} of
26+
* {@link CacheResolution#POSITIVE_HIT} or {@link CacheResolution#NEGATIVE_HIT} states only
27+
* that the result came from the per-path match cache: it does <b>not</b> claim that any
28+
* condition was evaluated for this request, and no winner reason is recomputed for cached
29+
* decisions. A {@link Decision#SELECTED} outcome means matching succeeded, not that the
30+
* subsequent business or authorization handling succeeded.</p>
31+
*
32+
* <p>Instances carry plugin identity, the selected ids and the decision reason only. They
33+
* never contain request paths, credentials, condition payloads or bodies, so they are safe
34+
* to forward to metric labels or log pipelines.</p>
35+
*/
36+
public final class SelectorRuleMatchObservation {
37+
38+
private final String pluginName;
39+
40+
private final Decision decision;
41+
42+
private final String selectorId;
43+
44+
private final String ruleId;
45+
46+
private final boolean defaultRule;
47+
48+
private final CacheResolution selectorCache;
49+
50+
private final CacheResolution ruleCache;
51+
52+
private SelectorRuleMatchObservation(final String pluginName, final Decision decision, final String selectorId,
53+
final String ruleId, final boolean defaultRule,
54+
final CacheResolution selectorCache, final CacheResolution ruleCache) {
55+
this.pluginName = pluginName;
56+
this.decision = decision;
57+
this.selectorId = selectorId;
58+
this.ruleId = ruleId;
59+
this.defaultRule = defaultRule;
60+
this.selectorCache = selectorCache;
61+
this.ruleCache = ruleCache;
62+
}
63+
64+
/**
65+
* Build an immutable observation of one matching decision.
66+
*
67+
* @param pluginName the plugin that took the decision
68+
* @param decision the decision reason
69+
* @param selectorId the selected selector id, or null when none was selected
70+
* @param ruleId the selected rule id, or null when none was selected
71+
* @param defaultRule whether the rule is the synthetic default rule
72+
* @param selectorCache how the selector stage resolved its cache
73+
* @param ruleCache how the rule stage resolved its cache
74+
* @return the observation
75+
*/
76+
public static SelectorRuleMatchObservation of(final String pluginName, final Decision decision, final String selectorId,
77+
final String ruleId, final boolean defaultRule,
78+
final CacheResolution selectorCache, final CacheResolution ruleCache) {
79+
return new SelectorRuleMatchObservation(pluginName, decision, selectorId, ruleId, defaultRule, selectorCache, ruleCache);
80+
}
81+
82+
/**
83+
* Gets the plugin name.
84+
*
85+
* @return the plugin name
86+
*/
87+
public String getPluginName() {
88+
return pluginName;
89+
}
90+
91+
/**
92+
* Gets the decision reason.
93+
*
94+
* @return the decision
95+
*/
96+
public Decision getDecision() {
97+
return decision;
98+
}
99+
100+
/**
101+
* Gets the selected selector id.
102+
*
103+
* @return the selector id, or null when no selector was selected
104+
*/
105+
public String getSelectorId() {
106+
return selectorId;
107+
}
108+
109+
/**
110+
* Gets the selected rule id.
111+
*
112+
* @return the rule id, or null when no rule was selected
113+
*/
114+
public String getRuleId() {
115+
return ruleId;
116+
}
117+
118+
/**
119+
* Whether the selected rule is the synthetic default rule.
120+
*
121+
* @return true when the rule was synthesized, not selected from the rule list
122+
*/
123+
public boolean isDefaultRule() {
124+
return defaultRule;
125+
}
126+
127+
/**
128+
* Gets the selector stage cache resolution.
129+
*
130+
* @return the selector cache resolution
131+
*/
132+
public CacheResolution getSelectorCache() {
133+
return selectorCache;
134+
}
135+
136+
/**
137+
* Gets the rule stage cache resolution.
138+
*
139+
* @return the rule cache resolution
140+
*/
141+
public CacheResolution getRuleCache() {
142+
return ruleCache;
143+
}
144+
145+
/**
146+
* The reason a matching decision ended where it did.
147+
*
148+
* <p>The values name the branch of the decision tree, so a cache-sourced outcome is
149+
* distinguishable from a freshly evaluated one without ever re-deriving a winner.</p>
150+
*/
151+
public enum Decision {
152+
153+
/**
154+
* A selector and a rule were selected and the plugin's doExecute will run. This is
155+
* a matching success only; it says nothing about the business outcome.
156+
*/
157+
SELECTED,
158+
159+
/**
160+
* The selector matched but does not continue to rule selection; the synthetic
161+
* default rule is handed to doExecute (defaultRule is true).
162+
*/
163+
SELECTOR_NOT_CONTINUED,
164+
165+
/**
166+
* A full-flow selector supplied the rule (the last enabled rule) without any
167+
* condition evaluation for this request.
168+
*/
169+
FULL_FLOW_RULE,
170+
171+
/**
172+
* The plugin has no configuration data in the cache at all.
173+
*/
174+
NO_PLUGIN_DATA,
175+
176+
/**
177+
* The plugin configuration exists but is disabled.
178+
*/
179+
PLUGIN_DISABLED,
180+
181+
/**
182+
* No selector was selected: the selector list is empty, the negative match cache
183+
* recorded no match for this path, or live matching found no candidate.
184+
*/
185+
NO_SELECTOR,
186+
187+
/**
188+
* Live selector matching evaluated the candidate set and found no candidate.
189+
*/
190+
SELECTOR_UNMATCHED,
191+
192+
/**
193+
* No rule was selected: the rule list is empty, the negative match cache recorded
194+
* no match for this path, or live matching found no enabled rule.
195+
*/
196+
NO_RULE,
197+
198+
/**
199+
* Live rule matching evaluated the candidate set and found no candidate.
200+
*/
201+
RULE_UNMATCHED
202+
}
203+
204+
/**
205+
* How one matching stage resolved against the per-path match cache.
206+
*
207+
* <p>A hit value declares the source only; the conditions behind a cached decision were
208+
* not re-evaluated for the current request and no per-request predicate ran.</p>
209+
*/
210+
public enum CacheResolution {
211+
212+
/**
213+
* The stage never consulted the cache (disabled cache, or the stage was not reached).
214+
*/
215+
NOT_CONSULTED,
216+
217+
/**
218+
* The stage found a matching entry in the cache and used it.
219+
*/
220+
POSITIVE_HIT,
221+
222+
/**
223+
* The stage found the cache's negative (no-match) entry for this path.
224+
*/
225+
NEGATIVE_HIT,
226+
227+
/**
228+
* The cache was consulted but missed, so the stage matched live. Conditions were
229+
* evaluated only in this state (and in the hit-free unmatch paths that report the
230+
* corresponding UNMATCHED decisions).
231+
*/
232+
EVALUATED
233+
}
234+
}

0 commit comments

Comments
 (0)