Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent requests for the same resource can result in cache misses #24166

Closed
asajeffrey opened this issue Sep 10, 2019 · 8 comments
Closed

Concurrent requests for the same resource can result in cache misses #24166

asajeffrey opened this issue Sep 10, 2019 · 8 comments
Labels

Comments

@asajeffrey
Copy link
Member

@asajeffrey asajeffrey commented Sep 10, 2019

If two GET requests are processed in quick succession, then the second one can end up being a cache miss, since the code to check whether the cache has been hit uses a read lock at

if let Ok(http_cache) = context.state.http_cache.read() {
whereas the code to insert into the cache uses a write lock at
if let Ok(mut http_cache) = context.state.http_cache.write() {

@asajeffrey
Copy link
Member Author

@asajeffrey asajeffrey commented Sep 10, 2019

@asajeffrey
Copy link
Member Author

@asajeffrey asajeffrey commented Sep 10, 2019

This came up in the context of #24148 which makes concurrent GETs more likely.

@asajeffrey
Copy link
Member Author

@asajeffrey asajeffrey commented Sep 10, 2019

The matching spec issue: whatwg/fetch#932

@gterzian
Copy link
Member

@gterzian gterzian commented Sep 10, 2019

That's interesting.

it might be the point that our simple "lock around a hashmap" approach to the cache shows its limitations.

We could handle this quite easily if the cache was message-passing based, where the cache ran in a worker thread, and each fetch thread would send a message to ask for a cached entry, and block on the reply. The cache could then simply let the fetch block if it knows there is another one doing the fetch at the same time.

I guess it could be done with locks(and condvars?) without an additional cache worker thread, but that might be much more complicated...

@jdm
Copy link
Member

@jdm jdm commented Sep 10, 2019

I don't think a message-passing-based approach would solve this issue - the spec describes two separate operations (check the cache & store the response) which happen at different times, so unless we add a marker entry to the cache which means "something else has requested this URL and there is no response yet" we're going to always run into this problem.

@gterzian
Copy link
Member

@gterzian gterzian commented Sep 10, 2019

so unless we add a marker entry to the cache which means "something else has requested this URL and there is no response yet" we're going to always run into this problem

I agree. What I mean is that it would be easier to add such a marker, as it would any other "state" in addition to the cached entries, if the cache where to run in it's own thread and handle messages, versus adding more shared-state to the current structure.

@gterzian
Copy link
Member

@gterzian gterzian commented Sep 10, 2019

Ok so I think one idea from https://github.com/servo/servo/pull/23553/files could be useful, which is to have fetches operate on entries, versus the cache as a whole.

Because a problem with the current structure is that we're operation on a RwLock<HttpCache>, which means it's going to be pretty hard to hide any sort of condvar based logic inside of it, since you'd need to acquire the outer lock first, and to tie that one to a condvar would require changing the whole structure of HttpState.

Instead, if fetches operate on entries, we can put a condvar inside of it, tie it to a new mutex around a "pending fetch" flag, and just have every other fetch using the same entry block on it, without affecting ALL fetches.

(And we don't need all the fine-grained locking from that PR, just the entries concept).

@gterzian
Copy link
Member

@gterzian gterzian commented Sep 11, 2019

I will take a look at using entries, and a condvar.

bors-servo added a commit that referenced this issue Sep 12, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 13, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 13, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 15, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 16, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 16, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 16, 2019
Http cache: introduce cache entries, prevent cache misses when resource will be fetched

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

---
<!-- 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 #___ (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/24203)
<!-- Reviewable:end -->
@gterzian gterzian mentioned this issue Sep 29, 2019
0 of 5 tasks complete
bors-servo added a commit that referenced this issue Sep 29, 2019
Http cache: wait on pending stores

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

---
<!-- 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 #___ (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/24318)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 30, 2019
Http cache: wait on pending stores

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

---
<!-- 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 #___ (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/24318)
<!-- Reviewable:end -->
bors-servo added a commit that referenced this issue Sep 30, 2019
Http cache: wait on pending stores

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

---
<!-- 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 #___ (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/24318)
<!-- Reviewable:end -->
@bors-servo bors-servo closed this in c1401f5 Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.