geosolutions-it / imageio-ext

Additional plugins and extension for the standard Java ImageIO library
Other
139 stars 80 forks source link

COG cache broken #288

Open 11904212 opened 10 months ago

11904212 commented 10 months ago

Hi, As soon as i turn on caching by specifying:

  var useCache = true;
  var cogUri = new BasicAuthURI(url, useCache); 
  var input = new CogSourceSPIProvider(
                  cogUri,
                  new CogImageReaderSpi(),
                  new CogImageInputStreamSpi(),
                  HttpRangeReader.class.getName()
  );
  ...

i get some NullPointerException caused by CachingCogImageInputStream. I have added some tests to CogHTTPReadOnlineTest to demonstrate the problem.

I tried to fix these problems by keeping the header field of CachingCogImageInputStream up to date and my tests passed.

However, upon closer inspection of the current implementation of CachingCogImageInputStream, I noticed that it relies heavily on the cache and will most likely break when parts of the cache are evicted. For example: Tile is cached when readRanges(.) is called -> ehcache decides to remove that tile -> call of read(...) tries to get the tile from the cache -> error

since a TODO in the code already noted that the implementation never worked well, i thought i'd give it a shot: CachingCogImageInputStream extends DefaultCogImageInputStream and all the cache related stuff happens in readRanges(.) If the tiles are in cache the will be copied, else we fetch them and add them to cache. The downside of this solution is that we have to merge the cached tile-chunks into continuous-chunks because the read(.) method expects it that way. Which might raise concerns about memory management?

Please let me know your thoughts on this.