Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Implement Cache-Control: must-revalidate support #9670

Merged
merged 1 commit into from
Aug 8, 2017

Conversation

kkaefer
Copy link
Member

@kkaefer kkaefer commented Aug 1, 2017

We already had a half-finished implementation, in particular, we already parsed the header and had (incorrect) tests for it.

This PR adds support for indicating that revalidation is required to the database scheme (updating it to v6).

Fixes #9103

@kkaefer kkaefer requested a review from jfirebaugh August 1, 2017 17:12
@kkaefer kkaefer added Core The cross-platform C++ core, aka mbgl offline labels Aug 1, 2017
@@ -126,6 +127,14 @@ void OfflineDatabase::migrateToVersion5() {
db->exec("PRAGMA user_version = 5");
}

void OfflineDatabase::migrateToVersion6() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit worried about this migration in combination with #9666. Any idea how long it will take if the database contains large offline regions?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Before I opted to use ALTER TABLE, I looked up the SQLite docs and they say this:

The execution time of the ALTER TABLE command is independent of the amount of data in the table. The ALTER TABLE command runs as quickly on a table with 10 million rows as it does on a table with 1 row.

I also conducted a test with a database that was ~300 MB in size and contained 6000 tiles. The query execution time the SQLite3 command line showed was < 1 millisecond.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great!

// v5.db is a v5 database, migrated from v2, v3 & v4.

deleteFile("test/fixtures/offline_database/migrated.db");
writeFile("test/fixtures/offline_database/migrated.db", util::read_file("test/fixtures/offline_database/v5.db"));
Copy link
Contributor

Choose a reason for hiding this comment

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

v5.db should be checked in. It's currently ignored in .gitignore. I like the rename of the output to migrated.db -- that means we can just migrated.db and not v*.db.

@kkaefer
Copy link
Member Author

kkaefer commented Aug 1, 2017

@jfirebaugh another thing I considered was to add a generic "flags" field that we can use to store more (bitfield) metadata in the future. E.g. I think we should add support for caching 404 responses, and we don't currently have a field for storing that. Optionally, I also considered repurposing the compressed fields and adding more flags to it. However, having the existing name be compressed would make for very awkward code.

@kkaefer kkaefer force-pushed the cache-control-must-revalidate branch 2 times, most recently from 408b142 to 4c1fc5e Compare August 1, 2017 20:00
@kkaefer
Copy link
Member Author

kkaefer commented Aug 2, 2017

There's one remaining bug before this can be merged:

When a file is cached, but expired, and has must-revalidate set, we're not responding with the outdated file. Instead, we're waiting for the revalidation request to complete. However, when that revalidation request sends a 304 Not Modified response, we also don't reply with the actual data, so in those situations, implementations will never get the actual data, only the information that the data hasn't been changed.

@kkaefer kkaefer added the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Aug 2, 2017
@kkaefer kkaefer force-pushed the cache-control-must-revalidate branch from 4c1fc5e to e34e5c4 Compare August 7, 2017 10:43
@kkaefer
Copy link
Member Author

kkaefer commented Aug 7, 2017

Fixed the edge case described in the previous comment.

@kkaefer kkaefer removed the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Aug 7, 2017
@kkaefer
Copy link
Member Author

kkaefer commented Aug 7, 2017

@jfirebaugh Can you please re-review?

@kkaefer kkaefer merged commit f0a7c45 into master Aug 8, 2017
@kkaefer kkaefer deleted the cache-control-must-revalidate branch August 8, 2017 10:40
kkaefer added a commit that referenced this pull request Sep 22, 2017
In #9670, we implemented support for the Cache-Control: must-revalidate header. While we now respect this header and don't show resources that are stale and have this header set, the optional cache request never completes. This means we're also never going to try to actually get a fresh tile and just never show this tile anymore.

This commit fixes this by responding with a Not Found error when the resource is unusable (= stale and has must-revalidate set). Since we actually still have the data (but can't use it due to caching rules), we're responding with the data as well. To avoid a second cache hit, tile_loader_impl.hpp now passes on the data from the Optional to the Required request so that it can be reused when we get a 304 Not Modified response from the server.
kkaefer added a commit that referenced this pull request Sep 25, 2017
In #9670, we implemented support for the Cache-Control: must-revalidate header. While we now respect this header and don't show resources that are stale and have this header set, the optional cache request never completes. This means we're also never going to try to actually get a fresh tile and just never show this tile anymore.

This commit fixes this by responding with a Not Found error when the resource is unusable (= stale and has must-revalidate set). Since we actually still have the data (but can't use it due to caching rules), we're responding with the data as well. To avoid a second cache hit, tile_loader_impl.hpp now passes on the data from the Optional to the Required request so that it can be reused when we get a 304 Not Modified response from the server.
@tekbob27
Copy link

tekbob27 commented Dec 7, 2017

Is there any possibility that these changes will cause startup to fail on iOS if cache.db (DefaultFileSource) is not found? I am seeing an issue where my app crashes at startup with an unknown error when trying to read cach.db (DefaultFileSource), if cache.db does not exist.

@kkaefer
Copy link
Member Author

kkaefer commented Dec 8, 2017

@tekbob27 if you are seeing an issue with Mapbox GL, please open a new ticket on this issue tracker and make sure to describe the version of Mapbox GL, platform etc. you're using.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Core The cross-platform C++ core, aka mbgl offline
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants