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

   ### Description
   
   `plugin.conf_version(conf)` hashes the conf the first time it is called, at 
request time, and caches the result. Plugin conf tables are shared by reference 
between routes, services, plugin_configs and consumers, and the core writes 
request-time fields into them: `_meta.priority` when any plugin on the route 
has a custom priority, `_skip_rewrite_in_consumer`, and `_from_consumer` on 
consumer plugins merged into a route; ai-proxy-multi writes the resolved DNS 
nodes and the picked endpoint into its instances. Whichever request reaches 
`conf_version` first decides whether those fields are in the hash, so the same 
configuration gets a different version in different workers and nodes. #14207 
is one symptom (limit-conn counters split by `_meta.priority`); 
graphql-proxy-cache has another, since its cache key embeds the version, so the 
shared nginx cache keeps duplicate entries and a PURGE only removes one of them.
   
   This PR moves that request-time state behind the conf table's metatable, the 
way `_meta.parent` already is. A single helper, `plugin.set_runtime_state(tab, 
key, value)`, writes it into the `__index` table of the table's own metatable: 
`conf._from_consumer`, `conf._meta.priority` and friends still read the same, 
but `pairs()`, `stably_encode` and JSON encoding no longer see them, so the 
version only depends on the configuration. Every writer goes through the helper:
   
   - `filter()`: the `_meta.priority` default (a priority the user configured 
is a raw field, so it still wins) and `_skip_rewrite_in_consumer`
   - `merge_consumer_route`: `_from_consumer`
   - ai-proxy-multi: `_dns_nodes`, `_nodes_ver`, `_dns_value`, 
`_resolved_endpoint`, `_resolved_host_header`, `_resolved_ssl_server_name`, 
`_healthy_dns_nodes`, including the copy it makes onto the stored resource for 
the health check timer
   - `conf_version` itself, for the cached `_version`
   
   `core.table.deepcopy` keeps the metatable, so a copy (the service conf 
copied per route, the secret-resolved copy) reads the state of the conf it was 
copied from, as it did when the fields were raw. On its first write the helper 
gives the copy its own state, starting from what it inherited, so writes to a 
copy never leak back into the original or into other copies. `core.table.clone` 
drops the metatable, so a shallow clone that is then modified no longer carries 
the original's cached `_version`.
   
   Other plugins also write into their own conf (openid-connect, cors, zipkin, 
response-rewrite, loggly at schema check, ...), but they never call 
`conf_version` on it, so they do not affect any version and are left as they 
are. google-cloud-logging writes `auth_config.scope` before its first 
`conf_version` call on every path, so its version is already consistent.
   
   There is a separate PR that removes `conf_version` from the limit-conn / 
limit-req / limit-count counter keys. The two are independent: this one makes 
`conf_version` stable for every caller (batch processors, ai-proxy-multi 
pickers, graphql-proxy-cache, ...), that one takes it out of the counter 
identity.
   
   #### 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
   - [x] 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)
   
   `t/node/plugin-conf-version.t` runs the same serverless conf on a shared 
plugin_config, a consumer and plain routes, and checks its version against a 
reference route that nothing touches: with a custom priority on one route 
sharing the plugin_config (both request orders), merged from a consumer, and on 
a key-auth route whether the first request authenticates or not. It also covers 
ai-proxy-multi with health checks, and the helper itself (reads, encoding, deep 
copies). Without the fix the priority, consumer, authentication and 
ai-proxy-multi cases fail.
   
   `docs/{en,zh}/latest/plugin-develop.md` now tell plugin authors to use 
`plugin.set_runtime_state` for request-time state on a conf.
   


-- 
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