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

Invalidate Glide disk cache when new images are received #6432

Merged
merged 5 commits into from
Sep 30, 2024

Conversation

grzesiek2010
Copy link
Member

@grzesiek2010 grzesiek2010 commented Sep 26, 2024

Closes #6419

Why is this the best possible solution? Were any other approaches considered?

According to Glide's documentation, we need to assist Glide in determining when cached images should be invalidated. There are several options to achieve this, and I chose to base it on the last modification date of the image file which seems to be simple and reliable. Now uses MD5 hash because timestamp updates with each form update.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

This pull request should just fix the problem with cached images described in the issue.
Please note that we also support SVG images, which are handled differently from other image types, so it’s important to test them as well. For example, I recently forgot to apply this fix to SVG images: link to PR, but I've now included it.

Do we need any specific form for testing your changes? If so, please attach one.

I used this one:
imgCacheTest.zip

Does this change require updates to documentation? If so, please file an issue here and include the link below.

No.

Before submitting this PR, please make sure you have:

  • added or modified tests for any new or changed behavior
  • run ./gradlew connectedAndroidTest (or ./gradlew testLab) and confirmed all checks still pass
  • added a comment above any new strings describing it for translators
  • added any new strings with date formatting to DateFormatsTest
  • verified that any code or assets from external sources are properly credited in comments and/or in the about file.
  • verified that any new UI elements use theme colors. UI Components Style guidelines

@grzesiek2010 grzesiek2010 force-pushed the COLLECT-6419 branch 2 times, most recently from 13620b7 to 0398a4b Compare September 26, 2024 16:57
Copy link
Member

@lognaturel lognaturel left a comment

Choose a reason for hiding this comment

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

Those images are pretty big and will forever increase the size of the repo. Could you please replace them with really small images? I don't think they need to be more than 1x1 squares of different colors. You'll need to rebase the commit introducing the images.

Are you sure that update time is stable across updates without changes to media files? While troubleshooting the original issue I noticed that I was always getting updated timestamps for all media files. I didn't investigate why but it seems to me like there might be an issue there.

@grzesiek2010
Copy link
Member Author

Are you sure that update time is stable across updates without changes to media files? While troubleshooting the original issue I noticed that I was always getting updated timestamps for all media files. I didn't investigate why but it seems to me like there might be an issue there.

I thought I was sure, but you're right. Each updated form creates a new directory, so even though we try to optimize the process by not downloading media attachments we already have, we still have to copy the existing ones from the old location to the new one, which changes the timestamp. This isn't a bug, but it forces Glide to load a new image (not ideal, but manageable). I've updated the fix to use an MD5 hash instead.

@lognaturel
Copy link
Member

lognaturel commented Sep 30, 2024

@getodk/testers I imagine you already have a good protocol for testing this area but just in case you don't have it captured formally anywhere, I think it's important to use as slow of a device as you can and as big of an image as you can. In addition to checking correctness, let's do a brief check of performance. I imagine this will be slightly slower but it shouldn't be very noticeable.

@grzesiek2010 grzesiek2010 merged commit 4a70306 into getodk:master Sep 30, 2024
6 checks passed
@lognaturel lognaturel changed the title Improved caching images with Glide Invalidate Glide disk cache when new images are received Sep 30, 2024
@lognaturel
Copy link
Member

Each updated form creates a new directory, so even though we try to optimize the process by not downloading media attachments we already have, we still have to copy the existing ones from the old location to the new one, which changes the timestamp. This isn't a bug

This feels surprising to me. I think it may also explain some unexpected behavior a user recently reported -- they were seeing multiple different form directories with full media even though the form version/definition had never changed. That suggests to me that there might be an issue with cleanup. Is there a reason we can't do the form attachment updating in place if the form definition hasn't changed?

@grzesiek2010
Copy link
Member Author

This feels surprising to me. I think it may also explain some unexpected behavior a user recently reported -- they were seeing multiple different form directories with full media even though the form version/definition had never changed.

That's not the scenario I have described I believe because:

Is there a reason we can't do the form attachment updating in place if the form definition hasn't changed?

It's not possible to update attachments without bumping the form version right? At least it's not possible with Central.

@lognaturel
Copy link
Member

lognaturel commented Sep 30, 2024

It's not possible to update attachments without bumping the form version right? At least it's not possible with Central.

There are two cases in which it's possible using Central: drafts (that's how this issue was discovered) and Entities. The user issue was reported for a form with Entities. They noticed a few unexpected directories with Entity Lists from different points in time. Given what we've described here, it seems possible that they weren't cleaned up correctly for some reason.

Other servers do more liberally allow updating form attachments without changing the form version.

@grzesiek2010
Copy link
Member Author

I've tested drafts, and it's true that all files end up with updated timestamps. We have a mechanism in place to prevent downloading media files that are already available. This was originally implemented for form updates, as we would have encountered the issue with drafts otherwise. The mechanism searches for existing media files in other directories created for the same form (but older versions) before downloading a new one. If a matching file is found, it’s copied to the new location instead of being re-downloaded. Unfortunately, it's not optimized, so if a new directory isn't created (such as in the case of drafts or entities) the file is copied regardless.

@WKobus
Copy link

WKobus commented Oct 2, 2024

Tested with Success!

Verified on device with Android 10,14

Verified cases:

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

Successfully merging this pull request may close these issues.

Images are cached incorrectly, probably by glide
3 participants