nic-6443 opened a new pull request, #14239:
URL: https://github.com/apache/apisix/pull/14239

   ### Description
   
   limit-conn, limit-req and limit-count put `conf_version(conf)`, a hash of 
the whole plugin configuration, into every counter key. That made counter 
identity depend on anything that ends up in the configuration table: #14207 
shows one plugin config split into two Redis counters because some workers 
cached a hash taken after `_meta.priority` was added at runtime and others 
before. It also means every configuration update, even a new `rejected_msg`, 
silently starts all counters over.
   
   The hash came in back when the stored value was the remaining quota with the 
limit baked in at write time. That's no longer true: the stored state is the 
consumed count (local and Redis), the open connections, or the leaky-bucket 
excess, and the threshold is applied when a request is checked. So this PR 
removes `conf_version` from counter identity. A counter is now identified by 
the owning resource (`_meta.parent.resource_key`), the resolved key, the 
workflow action id (`_vid`), and for a limit-count fixed window the window 
length:
   
   ```
   limit-conn / limit-req:     <resource_key>:<key>            
<resource_key>#<vid>:<key>
   limit-count fixed window:   <resource_key>:<time_window>:<key>   
<resource_key>#<vid>:<time_window>:<key>
   limit-count sliding window: <resource_key>:<key>            
<resource_key>#<vid>:<key>
   ```
   
   The `#<vid>` tag sits before the first `:`, so an ordinary key can't collide 
with a workflow action's key (ordinary key `tenant:1` vs action 1 with key 
`tenant` used to produce the same string once the hash is gone). The window 
length makes a `time_window` change start a new fixed window; a sliding window 
counter already carries its window id. `group` keys are unchanged. This covers 
everything built on these three: graphql-limit-count, ai-rate-limiting, stream 
limit-conn and the workflow actions.
   
   The hash was also masking two problems that show up once a counter survives 
an update, fixed here as well:
   
   - limit-conn Redis ran `EXPIRE key key_ttl` on every admission with the 
current `key_ttl`, so lowering `key_ttl` expired leases admitted under the old 
value. The expiry is now only extended for a positive `key_ttl`; a non-positive 
one keeps today's behaviour. limit-req Redis had the same pattern with its 
derived ttl (`ceil(burst/rate) + 1`): a faster-draining configuration deleted a 
bucket that still held excess from the slower one. Its expiry is now only 
extended too.
   - limit-conn Redis stored the lease id in `ngx.ctx.limit_conn_req_ids[key]`, 
so two rules resolving to the same key overwrote each other's id; the release 
then removed the second lease twice and the first one leaked until `key_ttl`. 
This is independent of the hash and already happens on master. The lease id now 
lives on the limiter object, which serves exactly one reservation.
   
   One more thing the hash was doing silently: ai-rate-limiting builds one 
limit-count configuration per LLM instance, and an instance without its own 
entry in `instances` falls back to the top-level `limit`/`time_window` with the 
same `_vid` (`ai-rate-limiting#global`). Those configurations only differed in 
the per-instance quota header names, so their hashes, and therefore their 
counters, were different. That per-instance counter is what the existing 
fallback tests rely on (`ai-rate-limiting.t` TEST 14 and TEST 20), so the 
`_vid` is now the instance name to keep it.
   
   With delayed sync (`sync_interval`), a node judges requests on the quota it 
synced last, so after a `count` change the new limit applies from that node's 
next sync, on the same consumption. The test covers that explicitly.
   
   #14238 keeps runtime state out of the `conf_version` hash itself. The two 
are independent: that one fixes every other user of the hash, this one stops 
rate-limit counters from depending on it at all, so either is useful without 
the other.
   
   Compatibility: this is a behaviour change. A configuration update no longer 
resets the counters of these plugins, and a lowered threshold applies to what 
was already consumed (for example lowering `count` below the current 
consumption rejects the next request). Since the key format changes, existing 
counters (shared dict and Redis) start over once when nodes are upgraded; old 
Redis keys expire on their own TTL. During a rolling upgrade, old and new nodes 
sharing a Redis counter use different keys until all nodes run the new version. 
The docs of the three plugins (en and zh) describe the new behaviour.
   
   Tests: `t/plugin/limit-count-conf-update.t`, `limit-conn-conf-update.t` and 
`limit-req-conf-update.t` update the configuration in place for the local, 
redis and redis-cluster policies (plus delayed sync for limit-count) and check 
that a threshold change applies to the current state, an unrelated change 
(`rejected_msg`, `_meta.filter`) keeps it, a `time_window` change starts a new 
window, and a workflow action keeps its own counter. They also cover the 
extend-only expiry for limit-conn and limit-req Redis and the two-rule lease 
release. Except the workflow cases, which pass on master because the hash kept 
those keys apart and now guard the new encoding, they all fail on master. The 
existing tests that matched the key text are updated to the new format.
   
   #### Which issue(s) this PR fixes:
   
   Fixes #14207
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [x] I have updated the documentation to reflect this change
   - [ ] I have verified that this change is backward compatible (If not, 
please discuss on the [APISIX mailing 
list](https://github.com/apache/apisix/tree/master#community) first)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to