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

[ios, core] Cached resources evicted prematurely #7755

Closed

Description

@ericrwolfe

Platform: iOS
Mapbox SDK version: 3.4.0-beta.7

Expected behavior

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 :/.

/cc @1ec5, @jfirebaugh

Activity

  1. added
    iOS
    on Jan 17, 2017
  2. jfirebaugh commented on Jan 17, 2017

    @jfirebaugh
    Contributor

    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.

  3. ericrwolfe commented on Jan 17, 2017

    @ericrwolfe
    ContributorAuthor

    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.

  4. jfirebaugh commented on Jan 17, 2017

    @jfirebaugh
    Contributor

    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.

  5. ericrwolfe commented on Jan 17, 2017

    @ericrwolfe
    ContributorAuthor

    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.

    The evict function in offline_database.cpp removes the last 50 resources and the last 50 tiles from the database on each loop until used_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.

  6. jfirebaugh commented on Jan 18, 2017

    @jfirebaugh
    Contributor

    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?

  7. jfirebaugh commented on Jan 18, 2017

    @jfirebaugh
    Contributor

    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:

    SELECT MAX(accessed)
    FROM (
      SELECT accessed
      FROM resources
      LEFT JOIN region_resources
      ON resource_id = resources.id
      WHERE resource_id IS NULL
      UNION ALL
      SELECT accessed
      FROM tiles
      LEFT JOIN region_tiles
      ON tile_id = tiles.id
      WHERE tile_id IS NULL
    )
    ORDER BY accessed ASC LIMIT 50

    Then use that to delete from individual tables:

    DELETE FROM resources
    WHERE id IN (
      SELECT id FROM resources
      LEFT JOIN region_resources
      ON resource_id = resources.id
      WHERE resource_id IS NULL
      AND accessed < ?1)
    
    DELETE FROM tiles 
    WHERE id IN (
      SELECT id FROM tiles
      LEFT JOIN region_tiles
      ON tile_id = tiles.id
      WHERE tile_id IS NULL
      AND accessed < ?1)

    We should look at the query plans for these to make sure they are reasonable.

  8. ericrwolfe commented on Jan 18, 2017

    @ericrwolfe
    ContributorAuthor

    The max function was being run before the ORDER BY clauses, so had to move that up:

    SELECT max(accessed) 
    FROM ( 
        SELECT accessed 
        FROM resources 
        LEFT JOIN region_resources 
        ON resource_id = resources.id 
        WHERE resource_id IS NULL 
      UNION ALL 
        SELECT accessed 
        FROM tiles 
        LEFT JOIN region_tiles 
        ON tile_id = tiles.id 
        WHERE tile_id IS NULL 
      ORDER BY accessed ASC LIMIT 50 
    );

    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:

    SELECT max(accessed) 
    FROM ( 
      SELECT *
      FROM (
        SELECT accessed 
        FROM resources 
        LEFT JOIN region_resources 
        ON resource_id = resources.id 
        WHERE resource_id IS NULL 
        ORDER BY accessed ASC LIMIT 50 
      )
      UNION ALL
      SELECT * 
      FROM ( 
        SELECT accessed 
        FROM tiles 
        LEFT JOIN region_tiles 
        ON tile_id = tiles.id 
        WHERE tile_id IS NULL 
        ORDER BY accessed ASC LIMIT 50 
      )
      ORDER BY accessed ASC LIMIT 50
    );
  9. changed the title [-][ios, core] Previously loaded maps disappear while offline (airplane mode)[/-] [+][ios, core] Cached resources evicted prematurely[/+] on Jan 18, 2017
  10. ericrwolfe commented on Jan 18, 2017

    @ericrwolfe
    ContributorAuthor

    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.

  11. self-assigned this
    on Jan 18, 2017
  12. ericrwolfe commented on Jan 18, 2017

    @ericrwolfe
    ContributorAuthor

    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.

    Compound Select

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugiOSoffline

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions