|
23 | 23 | import org.apache.shenyu.common.dto.RuleData; |
24 | 24 | import org.apache.shenyu.common.dto.SelectorData; |
25 | 25 | import org.apache.shenyu.common.dto.convert.rule.canary.CanaryConfig; |
| 26 | +import org.apache.shenyu.common.dto.convert.rule.sticky.DeterministicKeyConfig; |
26 | 27 | import org.apache.shenyu.common.dto.convert.rule.sticky.StickySessionConfig; |
27 | 28 | import org.apache.shenyu.common.dto.convert.rule.impl.DivideRuleHandle; |
28 | 29 | import org.apache.shenyu.common.enums.HttpRetryBackoffSpecEnum; |
|
49 | 50 | import org.apache.shenyu.plugin.divide.canary.CanaryDecisionService; |
50 | 51 | import org.apache.shenyu.plugin.divide.canary.DefaultCanaryDecisionService; |
51 | 52 | import org.apache.shenyu.plugin.divide.handler.DividePluginDataHandler; |
| 53 | +import org.apache.shenyu.loadbalancer.factory.LoadBalancerFactory; |
| 54 | +import org.apache.shenyu.loadbalancer.entity.LoadBalanceData; |
| 55 | +import org.apache.shenyu.plugin.divide.sticky.DeterministicKeyExtractor; |
52 | 56 | import org.apache.shenyu.plugin.divide.sticky.StickySessionAffinity; |
53 | 57 | import org.slf4j.Logger; |
54 | 58 | import org.slf4j.LoggerFactory; |
@@ -86,6 +90,8 @@ public class DividePlugin extends AbstractShenyuPlugin { |
86 | 90 |
|
87 | 91 | private final StickySessionAffinity stickySessionAffinity = new StickySessionAffinity(); |
88 | 92 |
|
| 93 | + private final DeterministicKeyExtractor deterministicKeyExtractor = new DeterministicKeyExtractor(); |
| 94 | + |
89 | 95 | public DividePlugin() { |
90 | 96 | this(new DefaultCanaryDecisionService()); |
91 | 97 | } |
@@ -365,9 +371,41 @@ private Upstream resolveWithAffinity(final ServerWebExchange exchange, final Rul |
365 | 371 | exchange.getResponse().setStatusCode(HttpStatus.SERVICE_UNAVAILABLE); |
366 | 372 | return null; |
367 | 373 | } |
| 374 | + // a configured deterministic key maps a binding-less request onto the admitted |
| 375 | + // candidates by the consistent hash ring; it is key hashing, not a binding — |
| 376 | + // no separate cookie is written for it and a missing key falls back to the |
| 377 | + // algorithm (#8037). The key is a sub-mode of the sticky configuration: it is |
| 378 | + // evaluated inside the affinity path, so sticky itself must be active (enabled |
| 379 | + // with usable binding secrets), which keeps one configuration surface. |
| 380 | + final Upstream keyed = resolveWithDeterministicKey(exchange, sticky, ruleHandle, breakeredUpstreamList); |
| 381 | + if (Objects.nonNull(keyed)) { |
| 382 | + return keyed; |
| 383 | + } |
368 | 384 | return LoadbalancerUtils.getForExchange(breakeredUpstreamList, ruleHandle.getLoadBalance(), exchange); |
369 | 385 | } |
370 | 386 |
|
| 387 | + private Upstream resolveWithDeterministicKey(final ServerWebExchange exchange, final StickySessionConfig sticky, |
| 388 | + final DivideRuleHandle ruleHandle, final List<Upstream> breakeredUpstreamList) { |
| 389 | + final DeterministicKeyConfig keyConfig = Objects.nonNull(sticky) ? sticky.getKey() : null; |
| 390 | + if (Objects.isNull(keyConfig) || !keyConfig.isConfigured()) { |
| 391 | + return null; |
| 392 | + } |
| 393 | + final String key = deterministicKeyExtractor.extract(exchange, keyConfig); |
| 394 | + if (Objects.isNull(key)) { |
| 395 | + return null; |
| 396 | + } |
| 397 | + final LoadBalanceData data = new LoadBalanceData(); |
| 398 | + data.setHashKey(key); |
| 399 | + final Upstream upstream = LoadBalancerFactory.selector(breakeredUpstreamList, LoadBalanceEnum.HASH.getName(), data); |
| 400 | + if (Objects.nonNull(upstream) && P2C.equals(ruleHandle.getLoadBalance())) { |
| 401 | + // the hash pick skips the P2C selection, which normally reserves the |
| 402 | + // attempt: the same accounting must apply or the count drifts down |
| 403 | + upstream.getInflight().incrementAndGet(); |
| 404 | + upstream.setLastPicked(System.currentTimeMillis()); |
| 405 | + } |
| 406 | + return upstream; |
| 407 | + } |
| 408 | + |
371 | 409 | private DivideRuleHandle buildRuleHandle(final RuleData rule) { |
372 | 410 | return DividePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule)); |
373 | 411 | } |
|
0 commit comments