Skip to content

http::cache: Adds serving HEAD requests from cache. - #15910

Merged
jmarantz merged 7 commits into
envoyproxy:mainfrom
ekiziv:head-requests
May 5, 2021
Merged

jmarantz merged 7 commits into
envoyproxy:mainfrom
ekiziv:head-requests

Conversation

@ekiziv

@ekiziv ekiziv commented Apr 10, 2021

Copy link
Copy Markdown
Contributor

Signed-off-by: Eleonora Kiziv [email protected]

Commit Message: Adds serving HEAD requests from cache. Does not add storing HEAD responses in cache.
Additional Description: Relevant issue: #10020
Risk Level: Low
Testing: Unit, integration

@ekiziv
ekiziv requested a review from jmarantz as a code owner April 10, 2021 00:14
@repokitteh-read-only

Copy link
Copy Markdown

Hi @ekiziv, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

🐱

Caused by: #15910 was opened by ekiziv.

see: more, trace.

@rojkov rojkov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've left some comments regarding coding style.

You may also want to automate running format checks before git push with ./support/bootstrap.

Comment on lines 21 to 23
// Note that if a request is not cacheable according to 'isCacheableRequest'
// then its response is also not cacheable.
// Therefore, isCacheableRequest, isCacheableResponse and CacheFilter::request_allows_inserts_

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The references to isCacheableRequest here should be updated to CanServeRequestFromCache.

// request cache-control headers only decide whether
// validation is required and whether the response can be cached.
static bool isCacheableRequest(const Http::RequestHeaderMap& headers);
static bool CanServeRequestFromCache(const Http::RequestHeaderMap& headers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functions we use camelCased names. Envoy's coding style diverges from Google's here.


TEST_P(CacheIntegrationTest, ServeHeadFromCacheAfterGetRequest) {
useAccessLog("%RESPONSE_FLAGS% %RESPONSE_CODE_DETAILS%");
simTime().setSystemTime(std::chrono::hours(1));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: perhaps this line needs the same comment as in the cases above for consistency:

// Set system time to cause Envoy's cached formatted time to match time on this thread.


TEST_P(CacheIntegrationTest, ServeGetFromUpstreamAfterHeadRequest) {
useAccessLog("%RESPONSE_FLAGS% %RESPONSE_CODE_DETAILS%");
simTime().setSystemTime(std::chrono::hours(1));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

return Http::FilterHeadersStatus::Continue;
}
if (!CacheabilityUtils::isCacheableRequest(headers)) {
if (!CacheabilityUtils::CanServeRequestFromCache(headers)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canServeRequestFromCache per envoy style guide

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

response_decoder->waitForEndStream();
EXPECT_TRUE(response_decoder->complete());
EXPECT_THAT(response_decoder->headers(), IsSupersetOfHeaders(response_headers));
EXPECT_TRUE(response_decoder->headers().get(Http::Headers::get().Age).empty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: this is all over this file in pre-existing code, but if you compare a string to "" with EXPECT_EQ, you'll get more potentially actionable info in the test log if it fails.

Feel free to either ignore this or fix all of them :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion. I tried following it, however, turns out that response_decoder->headers().get(Http::Headers::get().Age) returns an Envoy::Http::HeaderMap::GetResult, which contains an absl::InlinedVector<HeaderEntry*, 1>. Therefore, as far as I understand, empty() is to check for an empty list and not an empty string.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah in that case compare size to 0, or better yet compare the vector to the expected empty vector using the gtest macros for that.

simTime().setSystemTime(std::chrono::hours(1));
initializeFilter(default_config);

const Http::TestRequestHeaderMapImpl request_headers = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high level question on the test structure. There are a lot of common (or maybe mostly common) stanzas of code in all your new tests. Would it be worth factoring these out into common helper methods in the test-class? The benefit is that by factoring out the common boilerplate, it might be easier to scan over the tests and see what the differences are.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored the tests slightly, nevertheless ensuring that I always EXPECT in the tests rather than in the helper methods to make it really clear what is being tested. Let me know what you think!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better, and I agree. Thank you!

…n_test.cc to reduce code duplication.

Signed-off-by: Eleonora Kiziv <[email protected]>
}

Http::TestResponseHeaderMapImpl httpResponseHeadersForBody(
std::string body, std::string cache_control = "public,max-age=3600",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass by absl::string_view or const std::string&

}

IntegrationStreamDecoderPtr sendHeaderOnlyRequestAwaitResponse(
Http::TestRequestHeaderMapImpl headers, std::function<void()> simulate_upstream = []() {}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const Http::TestRequestHeaderMapImpl&

return response_decoder;
}

std::function<void()> simulateUpstreamResponse(Http::TestResponseHeaderMapImpl headers,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const refs

simTime().setSystemTime(std::chrono::hours(1));
initializeFilter(default_config);

const Http::TestRequestHeaderMapImpl request_headers = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better, and I agree. Thank you!

response_decoder->waitForEndStream();
EXPECT_TRUE(response_decoder->complete());
EXPECT_THAT(response_decoder->headers(), IsSupersetOfHeaders(response_headers));
EXPECT_TRUE(response_decoder->headers().get(Http::Headers::get().Age).empty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah in that case compare size to 0, or better yet compare the vector to the expected empty vector using the gtest macros for that.

@jmarantz

Copy link
Copy Markdown
Contributor

Also you need to fix format before CI runs.

/wait

@jmarantz jmarantz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great modulo one nit.

@envoyproxy/senior-maintainers

}

std::function<void()> simulateUpstreamResponse(const Http::TestResponseHeaderMapImpl& headers,
const std::string* body) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Try OptRef<std::string>, found in include/envoy/common/optref.h for a more Envoy-idiomatic way of doing this.

@jmarantz

jmarantz commented May 3, 2021

Copy link
Copy Markdown
Contributor

/assign-from @envoyproxy/senior-maintainers

@repokitteh-read-only

Copy link
Copy Markdown

@envoyproxy/senior-maintainers assignee is @htuch

🐱

Caused by: a #15910 (comment) was created by @jmarantz.

see: more, trace.

// Stop the encoding stream until the cached response is fetched & added to the encoding stream.
return Http::FilterHeadersStatus::StopIteration;
if (is_head_request_) {
// Return since head requests are not cached

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove double spacing after Return, capitalize HEAD

}

if (lookup_result_->content_length_ > 0) {
if (filter_state_ == FilterState::EncodeServingFromCache && is_head_request_) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage is actually fine in this PR, but more generally, this filter is missing a bunch of coverage, e.g. the red in https://storage.googleapis.com/envoy-pr/a8f4dde/coverage/source/extensions/filters/http/cache/cache_filter.cc.gcov.html.

@ekiziv @toddmgreer @jmarantz it would be great if we can get this filter up to full coverage, doesn't seem to be huge amount of extra testing needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will address this in the followup PR! :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, thanks!

*decoder_callbacks_, headers);
return Http::FilterHeadersStatus::Continue;
}
if (!CacheabilityUtils::isCacheableRequest(headers)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Eleonora Kiziv <[email protected]>
htuch
htuch previously approved these changes May 5, 2021

@htuch htuch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but needs main merge! Thanks.

@jmarantz
jmarantz merged commit efa7c88 into envoyproxy:main May 5, 2021
@ekiziv
ekiziv deleted the head-requests branch May 5, 2021 13:56
gokulnair pushed a commit to gokulnair/envoy that referenced this pull request May 6, 2021
* http::cache: Adds serving HEAD requests from cache.

Relevant issue: envoyproxy#10020

Signed-off-by: Eleonora Kiziv <[email protected]>

Commit Message: Adds serving HEAD requests from cache. Does not add storing HEAD responses in cache.
Additional Description: Relevant issue: envoyproxy#10020
Risk Level: Low
Testing: Unit, integration

Signed-off-by: Eleonora Kiziv <[email protected]>
Signed-off-by: Gokul Nair <[email protected]>
gokulnair pushed a commit to gokulnair/envoy that referenced this pull request May 6, 2021
* http::cache: Adds serving HEAD requests from cache.

Relevant issue: envoyproxy#10020

Signed-off-by: Eleonora Kiziv <[email protected]>

Commit Message: Adds serving HEAD requests from cache. Does not add storing HEAD responses in cache.
Additional Description: Relevant issue: envoyproxy#10020
Risk Level: Low
Testing: Unit, integration

Signed-off-by: Eleonora Kiziv <[email protected]>
Signed-off-by: Gokul Nair <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants