Skip to content

Continue http cache work - #18676

Merged
bors-servo merged 7 commits into
servo:masterfrom
gterzian:continue_http_cache_work
Nov 21, 2017
Merged

bors-servo merged 7 commits into
servo:masterfrom
gterzian:continue_http_cache_work

Conversation

@gterzian

@gterzian gterzian commented Sep 29, 2017 •

Copy link
Copy Markdown
Member

Work in progress, and not quite worth a review yet. (Continuation of #4117)

TODO

  • cache metadata (find some subset ofnet_traits::Metadata that can be shared across threads, it seems the problem is mainly stuff inside hyper::header in the headers field)

  • determine which other fields of a Response need to be cached, so a full and valid one can be returned upon a cache hit.

  • determine how to best share the cache across fetch threads (inside HttpState like I tried now?)

  • Spend more time reading the spec and make sure the cache follows it where it matters.

  • Make the current wpt tests pass.

  • More...


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix Implement an HTTP cache #12972 (github issue number if applicable).
  • There are tests for these changes OR
  • These changes do not require tests because _____

This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @KiChjang: components/net/http_loader.rs, components/net/resource_thread.rs, components/net/lib.rs, components/net/http_cache.rs

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify net code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 29, 2017
@jdm jdm removed the S-awaiting-review There is new code that needs to be reviewed. label Sep 29, 2017
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 1, 2017
@gterzian

gterzian commented Oct 4, 2017 •

Copy link
Copy Markdown
Member Author

Ok there is still a lot of work to be done, however I now have all the tests in tests/wpt/web-platform-tests/fetch/http-cache/status.html passing, so I guess it's a milestone of sorts.

I would like to ask for initial feedback on one early design choice, relating to "how to plug the cache into fetch". Let me sketch the current design:

  1. Fetch A starts for resource A.
  2. http_network_or_cache_fetch misses the cache, and starts a network fetch.
  3. The network fetch immediatly returns a Response A, with a body of type ResponseBody::Receiving.
  4. Resonse A is cached.
  5. Fetch B starts for resource A.
  6. Cache hit returns Response A, with a body still in ResponseBody::Receiving.
  7. Fetch A receives the body of Response A from the network fetch thread, via DoneChannel.
  8. Fetch A receives the full body and Response now has a body of type ResponseBody::Done.
  9. Fetch A updates the cache with Response A, the cache only updates it's cached resource with the new body data.
  10. The cache, using the same DoneChannel that the network fetch would normally use, updates Fetch B by sending the whole body in one chunk.
  11. Fetch B now also has Response A with a full body, and updates the cache, not knowing whether the update via the channel came from the network or from the cache. The cache ignores this update(since the cached resource already has a body of type ResponseBody::Done).
  12. Fetch C starts for resource A, a full response is immediatly returned from the cache, network fetch is skipped, and DoneChannel is None(this case is handled within wait_for_response).

I tried a fully sync cache operation first, however the tests were failing and I'm pretty sure it was because since the fetch is async, the cache would simply always miss since the full response hadn't been received yet by the time the second request testing for a cache hit came in.
So I had to introduce the async element by returning a response immediately and updating the body using the channel. Re-using the network fetch channel seemed to make sense in the context of 'network_or_cache_fetch', essentially it's transparent from the main fetch code perspective.
The cache is only updated once the full body has been received, in the main fetch code(I'm not sure if doing it in chunks would be better). I tried updating from within the fetch thread, but it created some issues about sharing the request which isn't thread-safe and is necessary to update the cache for a given resource.

How does the above look?

@jdm jdm mentioned this pull request Oct 4, 2017
@gterzian
gterzian force-pushed the continue_http_cache_work branch 2 times, most recently from 0052e73 to df3f9d6 Compare October 5, 2017 13:25
Comment thread components/net/http_cache.rs Outdated
},
_ => (),
let has_no_cache_directive = directives.iter().any(|directive| {
if let header::CacheDirective::NoCache = *directive {

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.

Why does header::CacheDirective::NoCache == *directive not work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it does work, thanks!

Comment thread components/net/http_loader.rs Outdated
}

fn is_no_store_cache(headers: &Headers) -> bool {
println!("debug headers {:?}", headers);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@KiChjang In the context of the http-cache/cc-request.html tests, it seems that the request headers set in the tests don't come through to the headers in the request inside network_or_cache_fetch. It seems that some basic headers are set, but not the ones the test is adding. Could this be a bug in the tests, or is some part of the fetch code dealing with request headers not implemented yet?

 "debug headers Headers { Accept: */*"
", Accept-Language: en-US, en; q=0.5"
", Referer: http://web-platform.test:8000/fetch/http-cache/cc-request.html"
", User-Agent: Servo/wptrunner"
", }"

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks, I'll check it out...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed in f4f7b9f

The headers were not being copied from the dom Request to the net Request...

@jdm

jdm commented Oct 7, 2017

Copy link
Copy Markdown
Member

I'm going to take more time on Monday, but I'll say that in general your description above looks like what I was expecting. For the purposes of merging the first implementation of the PR, I would like to have a plan for how we'll get to simultaneously updating other cache listeners while the original response is still ongoing, because it will be an important use case for the media stack that we're working towards integrating.

@gterzian

gterzian commented Oct 7, 2017

Copy link
Copy Markdown
Member Author

@jdm OK.

Do you mean that the media stack would be listening on a channel other than the DoneChannel used by the fetch method? If so, perhaps we could store another vec of Sender, for the media stack, which would be updated in a similar way to this.

The fetch thread already does something similar by sending the response chunks over to the devtools in addition to the fetch method, see https://github.com/gterzian/servo/blob/929084c584ccf4513fe7dfb6f65a0a305d831131/components/net/http_loader.rs#L1107

I guess the integration would need to be done as well within the fetch thread, not just the cache?

Current the devtool chan is stored on FetchContext, perhaps that could be the place to store a media stack channel?

@jdm

jdm commented Oct 7, 2017

Copy link
Copy Markdown
Member

What I meant is that if a media element starts fetching a 100mb file, then seeks to some other point and causes a new request to retrieve the cached response, we don't want to have to wait until the original request is complete before we service the second request.

@gterzian

gterzian commented Oct 8, 2017

Copy link
Copy Markdown
Member Author

@jdm Thanks for the explanation. I'm wondering if this doesn't take us into the territory of range requests and caching partial content...

@jdm

jdm commented Oct 8, 2017

Copy link
Copy Markdown
Member

It definitely does. My goal in this work is to ensure that we think about how those fit in with this design before merging it to ensure that we don't have to fundamentally change things later to support them.

Comment thread components/net/http_cache.rs Outdated
request_headers: request.headers
.iter()
.filter(|header| {
match header.name().to_lowercase().as_ref() {

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.

I don't think you need the as_ref call here if you have the ref keyword in the line above for header.

@gterzian gterzian Oct 9, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried |ref header], it gave

expected type `std::string::String`
              found type `&'static str`

and also tried putting match &header.name().to_lowercase(), which would give

expected type `&std::string::String`
              found type `&'static str`

@gterzian

gterzian commented Oct 9, 2017

Copy link
Copy Markdown
Member Author

Ok, regarding integration with the media stack:

  1. The way the cache currently updates responses follows the model of the fetch worker thread(the difference being that for now the update is done in one chunk when the full response has been received, but it could be adapted to chunks as well), so it can grow along with it I guess.
  2. If range requests fit the model of the media element(I still have to check out the spec), then we can implement support for that in the cache, there is a suite of tests covering those.

Besides this, currently the entire http_cache test suite is passing, with the exception of the partial requests bits. I tried to clean up the code a little bit, still somewhat rough but ready for a first review I guess.

I can work on the partial requests next weekend.

@gterzian
gterzian force-pushed the continue_http_cache_work branch from 6dbd07c to 251f7da Compare October 9, 2017 09:14
@jdm

jdm commented Oct 9, 2017

Copy link
Copy Markdown
Member

Amazing!

@gterzian
gterzian force-pushed the continue_http_cache_work branch from 215de4d to 698b6cd Compare October 9, 2017 14:38
@gterzian

gterzian commented Oct 11, 2017 •

Copy link
Copy Markdown
Member Author

I found a couple of subtle bugs, a lot actually relied on the fact that the headers were part of the key, and while that gave us some cache logic "for free", it prevented some edge cases(for example not caching a response with Vary: "*", for which I'll add tests later).

A cache key containing only the url, actually follows the spec more closely:

   The primary cache key consists of the request method and target URI.
   However, since HTTP caches in common use today are typically limited
   to caching responses to GET, many caches simply decline other methods
   and use only the URI as the primary cache key.

https://tools.ietf.org/html/rfc7234#section-4.1

I also realized that the entire body updating via the done channel was unecessary, in fact that code didn't work, and it was hidden by the fact that the headers in the key would prevent a match, so in fact the buggy code was just never called...

I do wonder how the response body is propagated without this, perhaps thanks to the call to mem::update in the fetch worker thread?

In any case, switched to using a ServoUrl as cachekey(not stripping fragments yet), and storing a Vec of resources per key.

Some tests were only passing because the request headers were different. Also fixed the Vary calculation, again it didn't actually work, but that was hidden by the headers in they key that would prevent matches.

Almost all tests are passing again, except a few regarding invalidation, I think I just need to debug the function and adapt it to fit the latest...

@gterzian
gterzian force-pushed the continue_http_cache_work branch 3 times, most recently from 7831544 to b249f2d Compare October 11, 2017 10:26
@gterzian

Copy link
Copy Markdown
Member Author

Ok, all tests are passing again...

@gterzian
gterzian force-pushed the continue_http_cache_work branch 2 times, most recently from b661946 to 7fd61fa Compare October 12, 2017 07:56
@gterzian

gterzian commented Oct 13, 2017 •

Copy link
Copy Markdown
Member Author

I'm puzzled as to why the unit test fetch::test_fetch_redirect_updates_method started to hang after 2185bf2,
seems there is some weird interaction between the cache, entry invalidation, and redirects. Fixed it for now, but not sure what the original problem was...

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 19, 2017
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit dcc3adf with merge 1aa5415...

bors-servo pushed a commit that referenced this pull request Nov 19, 2017
Continue http cache work

<!-- Please describe your changes on the following line: -->

Work in progress, and not quite worth a review yet. (Continuation of #4117)

TODO

- [ ] cache metadata (find some subset of`net_traits::Metadata` that can be shared across threads, it seems the problem is mainly stuff inside `hyper::header` in the `headers` field)

- [ ] determine which other fields of a `Response` need to be cached, so a full and valid one can be returned upon a cache hit.

- [ ] determine how to best share the cache across fetch threads (inside HttpState like I tried now?)

- [ ] Spend more time reading the spec and make sure the cache follows it where it matters.

- [ ] Make the current wpt tests pass.

- [ ] More...

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #12972  (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18676)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-css

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Nov 19, 2017
@KiChjang

Copy link
Copy Markdown
Contributor

Uh oh:

  ▶ Unexpected subtest result in /_mozilla/mozilla/http-cache.html:
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 200 response.
  │   → assert_equals: Response status expected 200 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 203 response.
  │   → assert_equals: Response status expected 203 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 204 response.
  │   → assert_equals: Response status expected 204 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 299 response.
  │   → assert_equals: Response status expected 299 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 400 response.
  │   → assert_equals: Response status expected 400 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 410 response.
  │   → assert_equals: Response status expected 410 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 499 response.
  │   → assert_equals: Response status expected 499 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 500 response.
  │   → assert_equals: Response status expected 500 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 502 response.
  │   → assert_equals: Response status expected 502 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 503 response.
  │   → assert_equals: Response status expected 503 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 504 response.
  │   → assert_equals: Response status expected 504 but got 404
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 599 response.
  │   → assert_equals: Response status expected 599 but got 404
  │ 
  └ make_test/</<.code/<@http://web-platform.test:8000/fetch/http-cache/http-cache.js:161:19

  ▶ Unexpected subtest result in /_mozilla/mozilla/http-cache.html:
  │ FAIL [expected PASS] HTTP cache, when disabled, does not cache a 404 response.
  │   → assert_equals: Response body expected "NTk0MjIzOTMzNjI1LjU5NjY=" but got "{\"error\": {\"message\": \"\", \"code\": 404}}"
  │ 
  └ make_test/</<.code/<@http://web-platform.test:8000/fetch/http-cache/http-cache.js:182:17

@gterzian
gterzian force-pushed the continue_http_cache_work branch from dcc3adf to ff62d4f Compare November 19, 2017 04:59
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Nov 19, 2017
@gterzian

gterzian commented Nov 19, 2017 •

Copy link
Copy Markdown
Member Author

I had to copy http-cache.js over from the wpt folder to the mozilla one, and change the url to correctly point to the server.
Should I perhaps also copy the server http-cache.py across as well so those two test suites are completely isolated?
Any licence/attribution necessary when copying across like that?

@gterzian
gterzian force-pushed the continue_http_cache_work branch from ff62d4f to 6196a6e Compare November 19, 2017 05:52
@jdm

jdm commented Nov 20, 2017

Copy link
Copy Markdown
Member

I think this is fine as it is.
@bors-servo: r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 6196a6e has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 20, 2017
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 6196a6e with merge e2bc0f0...

bors-servo pushed a commit that referenced this pull request Nov 21, 2017
Continue http cache work

<!-- Please describe your changes on the following line: -->

Work in progress, and not quite worth a review yet. (Continuation of #4117)

TODO

- [ ] cache metadata (find some subset of`net_traits::Metadata` that can be shared across threads, it seems the problem is mainly stuff inside `hyper::header` in the `headers` field)

- [ ] determine which other fields of a `Response` need to be cached, so a full and valid one can be returned upon a cache hit.

- [ ] determine how to best share the cache across fetch threads (inside HttpState like I tried now?)

- [ ] Spend more time reading the spec and make sure the cache follows it where it matters.

- [ ] Make the current wpt tests pass.

- [ ] More...

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #12972  (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18676)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: jdm
Pushing e2bc0f0 to master...

@bors-servo
bors-servo merged commit 6196a6e into servo:master Nov 21, 2017
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 21, 2017
@gterzian

Copy link
Copy Markdown
Member Author

thanks for your help @jdm and @KiChjang

@jdm

jdm commented Nov 21, 2017

Copy link
Copy Markdown
Member

Thanks for tackling this work!

@Manishearth

Copy link
Copy Markdown
Member

Wooo!

I added the aborted flag on Response, btw. Would you be able to do a followup to check that in the cache (or tell me where approximately it needs to be added)? Thanks!

@gterzian

Copy link
Copy Markdown
Member Author

@Manishearth great, I can open a new PR to check for the aborted flag...

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.

Implement an HTTP cache

6 participants