Repository navigation
Description
Hi !
If ATS receives a POST request to 'fresh cache existing' URL, we have CACHE_LOOKUP_HIT_FRESH status. But ATS will never return such fresh cache unless proxy.config.http.cache.post_method is 1, regardless if POST request (that should be always sent to the backend) succeeds or not. I believe the status should be something else for such 'fresh cache exists but it will not be served' case, by the following reason.
[background]
I am writing a Lua plugin script, that modifies response body. To do that, I want uncompressed response from the backend, thus I use compress plugin, later than lua plugin in plugin.config, to 'remove Accept-Encoding sent to the backend' and 'compress response from ATS'.
- plugin.config
tslua.so modify_body.lua
compress.so compress.config
- modify_body.lua
function do_global_read_response()
ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, modify_body)
end
function do_global_cache_lookup_complete()
if ts.http.get_cache_lookup_status() == TS_LUA_CACHE_LOOKUP_HIT_FRESH then
ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, modify_body)
end
end
function modify_body(data, end_of_stream)
...
end- compress.config
cache false
remove-accept-encoding true
compressible-content-type text/*
In case of GET request having fresh cache,
- (lua) cache_lookup_complete; register transform hook to modify body, because of CACHE_LOOKUP_HIT_FRESH status
- (compress) cache_lookup_complete: register transform hook to compress response, because of CACHE_LOOKUP_HIT_FRESH status
trafficserver/plugins/compress/compress.cc
Lines 884 to 891 in d0a44f8
| case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: { | |
| int obj_status; | |
| if (TS_ERROR != TSHttpTxnCacheLookupStatusGet(txnp, &obj_status) && (TS_CACHE_LOOKUP_HIT_FRESH == obj_status)) { | |
| if (hc != nullptr) { | |
| info("handling compression of cached object"); | |
| if (transformable(txnp, false, hc, &compress_type, &algorithms)) { | |
| compress_transform_add(txnp, hc, compress_type, algorithms); |
So transform is invoked 1. -> 2. order, that is good.
In case of GET/POST request without fresh cache,
- (lua) cache_lookup_complete; do nothing, because of non-CACHE_LOOKUP_HIT_FRESH status
- (compress) cache_lookup_complete: do nothing, because of non-CACHE_LOOKUP_HIT_FRESH status
- (lua) read_response; register transform hook to modify body
- (compress) read_response: register transform hook to compress response
So transform is invoked 3. -> 4. order, that is good.
in case of POST request having fresh cache, the following will happen,
- (lua) cache_lookup_complete; register transform hook to modify body, because of CACHE_LOOKUP_HIT_FRESH status, but it is not used as we will anyway have another response from the backend
- (compress) cache_lookup_complete: register transform hook to compress response, because of CACHE_LOOKUP_HIT_FRESH status
- (lua) read_response; register transform hook to modify body
- (compress) read_response: compress plugin does NOT register transform hook, if it is done in cache_lookup_complete stage.
So transform is invoked 2. -> 3. order for the final response, that is NOT good, i.e. for the 'response from the backend to this POST request', compress plugin's compress transform is invoked first, thus Lua script's modify_body function gets compressed result.
(also compress plugin does nothing in send_request timing if transform is registered in cache_lookup_complete timing, remove-access-encoding true is not effective for this case)
To avoid this issue, I patched compress plugin so that it does NOT register transform hook in cache_lookup_complete timing, in case of POST request. But similar issue can happen for any plugin and it breaks expected hook timing order by plugin order in plugin.config.
This is why I would like to propose another status than CACHE_LOOKUP_HIT_FRESH for 'fresh cache exists but it will not be served' case.
Thanks in advance !
Kazuhiko
Activity
SolidWallOfCode commentedon Nov 30, 2021
Does this happen in only
POSTis used for the URL? Or does it require having done aGETpreviously that was cached? The question is, how does aPOSTyield a hit fresh ifPOSTrequests aren't cached?bneradt commentedon Nov 30, 2021
@fdiary : can you please write an autest for this? If not, can you provide a set of curl commands that would reproduce this so that someone else can write the autest for it.
fdiary commentedon Nov 30, 2021
@SolidWallOfCode
Yes.
Imagine we have the following chronological requests in the same URL :
And the issue here is in 3. case above, i.e. compress plugin implementation does not assume "CACHE_LOOKUP_HIT_FRESH but fetch from the origin server" case.
fdiary commentedon Nov 30, 2021
@bneradt Let me first show the minimal example of the following issue :
Now let's show the result of the 4 requests as written in the previous comment :
As you can see, CACHE_LOOKUP_HIT_FRESH is not a proper enough condition to know 'ATS will return a cached response'. But I'm wondering what should be the way to go.
we should have another state than
CACHE_LOOKUP_HIT_FRESH?each plugin should implement the same logic as
does_method_require_cache_copy_deletion?trafficserver/proxy/http/HttpTransact.cc
Lines 727 to 733 in 420935b
we should introduce another hook like
READ_CACHE_RESPONSE_HOOKwhen we are really sending a cache response ?bneradt commentedon Dec 1, 2021
Thank you @fdiary for these details. This is helpful. I'll start by working on a test for this.
bneradt commentedon Dec 2, 2021
I've created and added the following plugin to the cache-request-method.test.py test. It reproduces the issue by printing the cache status which it sees:
The test is updated with:
Running the test gives this output:
The test does the following:
Thus requests 3 and 6 reproduce this problem described in this ticket.
bneradt commentedon Dec 2, 2021
Now that we've reproduced this with a test, we'll have to consider how to handle this. I'm a little leery about changing this
cache_lookup_resultvariable because both the HttpSM.cc and HttpTransact.cc make use of thecache_lookup_resultin a way that looks potentially sensitive. I don't want to break things in a non-obvious way. I'll tinker around a bit and see what our options are.TSHttpTxnCacheLookupStatusGet: handle cannot respond cases
bneradt commentedon Dec 2, 2021
@fdiary : I created #8545 for this. For these situations where there is a cached resource but the request methods don't line up, the patch will cause
TSHttpTxnCacheLookupStatusGetto now return aTS_CACHE_LOOKUP_MISSinstead of aTS_CACHE_LOOKUP_HIT_FRESH. I think this will address your situation, but can you please confirm this?fdiary commentedon Dec 3, 2021
@bneradt Thanks for your great effort !
Yes, exactly. The change in #8545 will solve this issue, without any change on existing plugins. If there isn't any bad side effect with it, it should be the best solution, in my opinion.
TSHttpTxnCacheLookupStatusGet: handle cannot respond cases (#8545)
TSHttpTxnCacheLookupStatusGet: handle cannot respond cases (#8545)
Revert fixes for apache#8539
Revert fixes for #8539 (#8637)
bneradt commentedon Jan 31, 2022
It has proven tricky to both reliably fix this issue without introducing other issues. My first patch, while stable, got broken by a change in the way things are ordered in the HttpSM, while my second patch, while presumably more reliable wrt this issue, introduced crashes in some situations. I'll try to swing back around to this issue later. For now I backed out both patches to ensure stability in 9.2.x and master.
Revert fixes for #8539 (#8637)
Merge remote-tracking branch 'asf/9.2.x' into 9.2.x