Skip to content

Re-extract cached resource when its source content changes#6562

Open
TimurRakhmatullin86 wants to merge 1 commit into
spring-projects:mainfrom
TimurRakhmatullin86:fix/transformers-stale-onnx-cache
Open

Re-extract cached resource when its source content changes#6562
TimurRakhmatullin86 wants to merge 1 commit into
spring-projects:mainfrom
TimurRakhmatullin86:fix/transformers-stale-onnx-cache

Conversation

@TimurRakhmatullin86

Copy link
Copy Markdown
Contributor

Fixes gh-6219

ResourceCacheService copies a resource into the local cache only when the target file does not yet exist, so a resource that is replaced in place keeps its previously extracted (stale) copy forever.

When a classpath: resource is packaged inside a fat jar its URI scheme resolves to jar rather than classpath, so it is not excluded from caching and gets extracted to the temp cache. The cache path is derived only from the resource location, which is stable across builds. Rebuilding the jar with a different model.onnx therefore reuses the old extracted file, and TransformersEmbeddingModel keeps serving the previous model's embedding dimensions until the temp directory is deleted by hand (e.g. a 768-dim model is used to query a 384-dim index).

This refreshes the cached copy when the original resource's content length differs from the cached file, or when the original has been modified more recently than the cached copy. Both are cheap metadata reads; when either is unavailable the existing cached copy is kept, preserving current behavior for resources that cannot report size/last-modified.

A regression test reproduces an in-place source change and asserts the cache is refreshed. It fails without the fix and passes with it.

ResourceCacheService copied a resource into the local cache only when the
target file did not yet exist, so a resource replaced in place kept its
previously extracted (stale) copy. A classpath resource packaged in a fat
jar resolves to a "jar" URI scheme rather than "classpath", so it is
cached; rebuilding the jar with a different model.onnx then reuses the old
extracted file and the wrong embedding dimensions are served until the temp
cache is deleted by hand.

Refresh the cached copy when the original resource's size differs from the
cached file, or when the original has been modified more recently, and add a
regression test covering an in-place source change.

Fixes spring-projectsgh-6219

Signed-off-by: Timur Rakhmatullin <174210871+TimurRakhmatullin86@users.noreply.github.com>
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.

TransformersEmbeddingModel serves stale ONNX model from temp cache when running from fat jar

2 participants