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

Timed based addon cache #1389

Merged
merged 7 commits into from
Jun 20, 2024
Merged

Timed based addon cache #1389

merged 7 commits into from
Jun 20, 2024

Conversation

nirs
Copy link
Member

@nirs nirs commented May 12, 2024

Improve the caching logic to refresh the cache automatically base on cached item age. This ensures that we always use fresh cache (< 2 days), so new changes in addons resources are used automatically and transparently for developers and CI.

The time based logic also avoids the issue of having to clear the cache to trigger a refresh of the cache, eliminating refreshes during builds. We can run now drenv cache envfile from a daily cron job, to update the cached resources every day.

The time based logic also allows retries on drenv cache errors. We can run a simple retry loop running drenv cache every 5 minutes until it succeeds. Cached items newer than 12 hours are skipped automatically.

Fixes #1435

@nirs nirs force-pushed the drenv-cache branch 2 times, most recently from 260373d to ed0c730 Compare May 15, 2024 08:44
@nirs nirs marked this pull request as ready for review May 15, 2024 08:44
@nirs nirs requested review from ShyamsundarR and BenamarMk May 15, 2024 08:44
@nirs nirs marked this pull request as draft May 15, 2024 09:26
@nirs nirs marked this pull request as ready for review May 15, 2024 09:26
@nirs nirs force-pushed the drenv-cache branch 4 times, most recently from 9049863 to 4def0e9 Compare May 15, 2024 11:19
@nirs nirs marked this pull request as draft May 20, 2024 19:02
@nirs nirs marked this pull request as draft May 20, 2024 19:02
@nirs nirs marked this pull request as ready for review May 20, 2024 19:28
@nirs nirs force-pushed the drenv-cache branch 3 times, most recently from 74d9a06 to 31cc35a Compare May 22, 2024 14:17
@nirs nirs linked an issue Jun 3, 2024 that may be closed by this pull request
@nirs nirs force-pushed the drenv-cache branch 2 times, most recently from f6aa183 to a05f35a Compare June 9, 2024 20:35
nirs added 4 commits June 11, 2024 01:03
And remove the timing of the operation, deleting a directory is fast
local operation.

Signed-off-by: Nir Soffer <[email protected]>
Called to warn about a missing addon.

Signed-off-by: Nir Soffer <[email protected]>
Previously we had to do:

    path = cache.patch(key)
    cache.fetch(kustomziation_dir, key)

Now we do both operations in one call:

    path = cache.fetch(kustomziation_dir, key)

Thanks: Raghavendra Talur <[email protected]>
Signed-off-by: Nir Soffer <[email protected]>
Extract the a _fetch() helper to be used by new public APIs.

Signed-off-by: Nir Soffer <[email protected]>
nirs added 3 commits June 11, 2024 01:03
Preparing for renaming "fetch" to "cache" - the cache() function will
shadow the cache module. We can rename the imports or the function to
avoid this. Renaming the import is bad since it makes it harder to
understand the code. Renaming the functions is good since it helps to
understand what is a command function.

Signed-off-by: Nir Soffer <[email protected]>
Previously to update the cache you had to clear the cache and fetch.
This leaves a window where a build may use uncached value and fail.  A
better way is to refresh the cache periodically *without* clearing the
cache, ensuring that builds will always use cached values.

To support time based refresh, we fetch data only if the cached item is
too old or missing. Upon successful fetch, the item age is set to zero,
so the next access to the cache will skip the fetch.

We use 2 threshold for refreshing the cache:

- REFRESH_THRSHOLD: 12 hours
- FETCH_THRESHOLD: 48 hours

This allows refreshing the cache once per day (e.g. from a cron job). On
errors, we can rerun the refresh until the refresh succeeds. Fresh items
are skipped during the refresh.

If refreshing the cache failed, jobs will trigger a refresh after 48
hours. The longer threshold ensures that this will never happen unless
the refresh job is broken.

The `fetch` command was renamed to `cache`. This describe better the
purpose of the command. The hook was update as well to match the command
name.

The `drenv.cache` module provides now 2 operations:

- cache.refresh() - rebuild the cache if older than
  cache.REFRESH_THRESHOLD (12 hours). Will is used in the `cache` hook.

- cache.get() - rebuild the cache if older than cache.FETCH_THRESHOLD
  (48 hours) and return the path to the cached value. Will is used in
  `start` hooks to get a cached value.

Fixes: RamenDR#1435
Signed-off-by: Nir Soffer <[email protected]>
Add script and crontab sample:

- `scripts/refresh-cache`: can be run from a cron job to refresh the
  cache.
- `scripts/refresh-cache.crontab`: sample user crontab for refreshing
  the cache daily.

Signed-off-by: Nir Soffer <[email protected]>
@ShyamsundarR ShyamsundarR merged commit 6f6b057 into RamenDR:main Jun 20, 2024
16 checks passed
nirs added a commit to nirs/ramen that referenced this pull request Jun 20, 2024
We change the cache API in RamenDR#1389 but it was merge after the argocd was
added.

- Use cache.get() instead of removed cache.fetch()
- Change current directory for start hook was missing in RamenDR#1421, not sure
  how the cache worked without it.

Signed-off-by: Nir Soffer <[email protected]>
ShyamsundarR pushed a commit that referenced this pull request Jun 20, 2024
We change the cache API in #1389 but it was merge after the argocd was
added.

- Use cache.get() instead of removed cache.fetch()
- Change current directory for start hook was missing in #1421, not sure
  how the cache worked without it.

Signed-off-by: Nir Soffer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

drenv cache is on by default
2 participants