You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Maps should immediately and consistently load cached styles + tiles when a device is in airplane mode, even if the cached data might be beyond the Expires header timestamp.
Actual behavior
App calls -[MGLMapView setStyleURL:], and despite the app having loaded a map style + tiles recently (multiple times in the past 24 hours), the map disappears and the user is presented with a blank black or grey screen.
The only resolution has been to turn off airplane mode and reconnect to the internet (note: the style does not automatically reload once reachability is gained, the app must call -[MGLMapView setStyleURL:] to trigger a refresh).
This has happened multiple times in the wilderness where having a reliable map is absolutely essential. In these cases where no internet connection is available to reload the style, any navigation apps powered by Mapbox become useless.
Steps to trigger behavior
This is difficult to replicate in isolation and it usually takes a day or so to appear since it seems related to expired cache headers. Might be able to fake some requests with short cache headers, and then restart a test app in airplane mode. Let me know if you have any ideas :/.
The cache (by design) does not make any guarantees about the availability of resources that aren't in an offline region. If it's critical for your map to work offline, you really need to use the offline features, and not rely on ambient caching.
That said, it's possible there is some case where cache eviction happens when it doesn't need to. We'd probably need a reproducible case to track that down though.
What triggers cache eviction? It seems like it might be too aggressive in this case and shouldn't happen while there's no internet reachability since it's unlikely that new items will be added to the cache.
I'll be looking at a map one minute, switch to a different map style, then the next minute switch back and the map will be gone.
Cache eviction is currently triggered when deleting an offline region, and when adding a new resource to the cache. I can't think of any way for the latter to happen if there's no network connection.
Looked through the logic in offline_database.cpp and what I think is happening here is not an issue with caching while offline, but rather a premature eviction of a style's resources when the cache is close to its 50mb limit. Being offline simply highlights the fact that these resources no longer exist.
However, in the case of a "full" cache database, there are generally only a handful of resources (generally a style json, sprite sheet, 1-2 font files, and the tile source url) compared to a large number of tiles. With the iOS test app I can fill the 50mb cache with 9 resources and just over 1000 tiles.
As a result, every time the cache needs to be evicted when loading new tiles, generally all of the style-related resources are removed, when really only a handful of old tiles need to be evicted.
I think the logic here should be updated to either only evict tiles, or only evict resources in a worst-case scenario.
I could see that helping in some, perhaps most, cases, although it would harm others -- for instance if you have cached multiple styles, but only recently used one, you would want to prefer the tiles required by the recent style over the glyphs and sprites of the older styles.
I'm still not clear on how eviction is happening in an offline scenario at all. Do you have a theory for how that happens?
We could adjust the eviction queries so that the access times are equally weighted between the two. One way would be to first obtain a "cut-off" time over all rows:
SELECTMAX(accessed)
FROM (
SELECT accessed
FROM resources
LEFT JOIN region_resources
ON resource_id =resources.idWHERE resource_id IS NULLUNION ALLSELECT accessed
FROM tiles
LEFT JOIN region_tiles
ON tile_id =tiles.idWHERE tile_id IS NULL
)
ORDER BY accessed ASCLIMIT50
Then use that to delete from individual tables:
DELETEFROM resources
WHERE id IN (
SELECT id FROM resources
LEFT JOIN region_resources
ON resource_id =resources.idWHERE resource_id IS NULLAND accessed < ?1)
DELETEFROM tiles
WHERE id IN (
SELECT id FROM tiles
LEFT JOIN region_tiles
ON tile_id =tiles.idWHERE tile_id IS NULLAND accessed < ?1)
The max function was being run before the ORDER BY clauses, so had to move that up:
SELECTmax(accessed)
FROM (
SELECT accessed
FROM resources
LEFT JOIN region_resources
ON resource_id =resources.idWHERE resource_id IS NULLUNION ALLSELECT accessed
FROM tiles
LEFT JOIN region_tiles
ON tile_id =tiles.idWHERE tile_id IS NULLORDER BY accessed ASCLIMIT50
);
Query plan looks reasonable:
2|0|0|SCAN TABLE resources USING COVERING INDEX resources_accessed
2|1|1|SEARCH TABLE region_resources USING COVERING INDEX region_resources_resource_id (resource_id=?)
3|0|0|SCAN TABLE tiles USING COVERING INDEX tiles_accessed
3|1|1|SEARCH TABLE region_tiles USING COVERING INDEX region_tiles_tile_id (tile_id=?)
1|0|0|COMPOUND SUBQUERIES 2 AND 3 (UNION ALL)
0|0|0|SEARCH SUBQUERY 1
FWIW, moving the sorting & limiting to subqueries has little effect on the query plan and execution time:
SELECTmax(accessed)
FROM (
SELECT*FROM (
SELECT accessed
FROM resources
LEFT JOIN region_resources
ON resource_id =resources.idWHERE resource_id IS NULLORDER BY accessed ASCLIMIT50
)
UNION ALLSELECT*FROM (
SELECT accessed
FROM tiles
LEFT JOIN region_tiles
ON tile_id =tiles.idWHERE tile_id IS NULLORDER BY accessed ASCLIMIT50
)
ORDER BY accessed ASCLIMIT50
);
An app with multiple map styles and a full ambient cache (~50mb of tiles and resources) was being used in areas with low cell coverage, and airplane mode was being toggled occasionally to check for coverage.
If the internet became reachable, new tiles were requested and eviction occurred on the full cache, which would evict the 50 least recently used tiles and the 50 least recently used style resources—i.e. all of the style resources being used by the app.
When the app returned to airplane mode, none of the style resources existed any more and when the map was reloaded no maps would display.
Yup, was wondering the same before I changed it. According to the spec, ORDER BY is applied to the entire result set of UNION ALL. In fact, SQL doesn't allow you to place another ORDER BY before the UNION without wrapping in a subquery.
In a compound SELECT statement, only the last or right-most simple SELECT may have an ORDER BY clause. That ORDER BY clause will apply across all elements of the compound.
Activity
jfirebaugh commentedon Jan 17, 2017
The cache (by design) does not make any guarantees about the availability of resources that aren't in an offline region. If it's critical for your map to work offline, you really need to use the offline features, and not rely on ambient caching.
That said, it's possible there is some case where cache eviction happens when it doesn't need to. We'd probably need a reproducible case to track that down though.
ericrwolfe commentedon Jan 17, 2017
What triggers cache eviction? It seems like it might be too aggressive in this case and shouldn't happen while there's no internet reachability since it's unlikely that new items will be added to the cache.
I'll be looking at a map one minute, switch to a different map style, then the next minute switch back and the map will be gone.
jfirebaugh commentedon Jan 17, 2017
Cache eviction is currently triggered when deleting an offline region, and when adding a new resource to the cache. I can't think of any way for the latter to happen if there's no network connection.
ericrwolfe commentedon Jan 17, 2017
Looked through the logic in
offline_database.cppand what I think is happening here is not an issue with caching while offline, but rather a premature eviction of a style's resources when the cache is close to its 50mb limit. Being offline simply highlights the fact that these resources no longer exist.The
evictfunction inoffline_database.cppremoves the last 50 resources and the last 50 tiles from the database on each loop untilused_size() + etc... < maximumCacheSize.However, in the case of a "full" cache database, there are generally only a handful of resources (generally a style json, sprite sheet, 1-2 font files, and the tile source url) compared to a large number of tiles. With the iOS test app I can fill the 50mb cache with 9 resources and just over 1000 tiles.
As a result, every time the cache needs to be evicted when loading new tiles, generally all of the style-related resources are removed, when really only a handful of old tiles need to be evicted.
I think the logic here should be updated to either only evict tiles, or only evict resources in a worst-case scenario.
jfirebaugh commentedon Jan 18, 2017
I could see that helping in some, perhaps most, cases, although it would harm others -- for instance if you have cached multiple styles, but only recently used one, you would want to prefer the tiles required by the recent style over the glyphs and sprites of the older styles.
I'm still not clear on how eviction is happening in an offline scenario at all. Do you have a theory for how that happens?
jfirebaugh commentedon Jan 18, 2017
We could adjust the eviction queries so that the access times are equally weighted between the two. One way would be to first obtain a "cut-off" time over all rows:
Then use that to delete from individual tables:
We should look at the query plans for these to make sure they are reasonable.
ericrwolfe commentedon Jan 18, 2017
The max function was being run before the
ORDER BYclauses, so had to move that up:Query plan looks reasonable:
FWIW, moving the sorting & limiting to subqueries has little effect on the query plan and execution time:
[-][ios, core] Previously loaded maps disappear while offline (airplane mode)[/-][+][ios, core] Cached resources evicted prematurely[/+]ericrwolfe commentedon Jan 18, 2017
Summary of what was happening:
An app with multiple map styles and a full ambient cache (~50mb of tiles and resources) was being used in areas with low cell coverage, and airplane mode was being toggled occasionally to check for coverage.
If the internet became reachable, new tiles were requested and eviction occurred on the full cache, which would evict the 50 least recently used tiles and the 50 least recently used style resources—i.e. all of the style resources being used by the app.
When the app returned to airplane mode, none of the style resources existed any more and when the map was reloaded no maps would display.
ericrwolfe commentedon Jan 18, 2017
Yup, was wondering the same before I changed it. According to the spec,
ORDER BYis applied to the entire result set ofUNION ALL. In fact, SQL doesn't allow you to place anotherORDER BYbefore theUNIONwithout wrapping in a subquery.