-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Ensure that failed unis are not cached #39762
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
.../deployment/src/test/java/io/quarkus/cache/test/runtime/UniReturnTypeWithFailureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.cache.test.runtime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.cache.CacheResult; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.impl.NoStackTraceException; | ||
|
||
public class UniReturnTypeWithFailureTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot((jar) -> jar.addClass(CachedService.class)); | ||
|
||
@Inject | ||
CachedService cachedService; | ||
|
||
@Test | ||
void testCacheResult() { | ||
assertThrows(NoStackTraceException.class, () -> cachedService.cacheResult("k1").await().indefinitely()); | ||
assertEquals(1, cachedService.getCacheResultInvocations()); | ||
assertEquals("", cachedService.cacheResult("k1").await().indefinitely()); | ||
assertEquals(2, cachedService.getCacheResultInvocations()); | ||
assertEquals("", cachedService.cacheResult("k1").await().indefinitely()); | ||
assertEquals(2, cachedService.getCacheResultInvocations()); | ||
} | ||
|
||
@ApplicationScoped | ||
static class CachedService { | ||
|
||
private volatile int cacheResultInvocations; | ||
|
||
@CacheResult(cacheName = "test-cache") | ||
public Uni<String> cacheResult(String key) { | ||
cacheResultInvocations++; | ||
if (cacheResultInvocations == 1) { | ||
return Uni.createFrom().failure(new NoStackTraceException("dummy")); | ||
} | ||
return Uni.createFrom().item(() -> new String()); | ||
} | ||
|
||
public int getCacheResultInvocations() { | ||
return cacheResultInvocations; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering backporting this one and I have a question: my understanding is that we invalidate the cache key if we have an error?
I would expect us to not store anything in this case? And thus we wouldn't have to invalidate the cache? I'm especially worried in the case of concurrent accesses because I wouldn't expect us to invalidate a cache entry that could have been stored from another thread.
Now it's reactive code so I'm not understanding exactly what it does but this concerns me a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your understanding is correct, but given the API, I am not sure how it can be done differently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah my problem is that again IIUC we have a very short period of time when the error is cached so another thread can get it.
Also we have a short period of time when we might invalidate an actually valid key but this is probably more theoretical (as I wouldn't expect another thread to store a cache entry) and less problematic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's certainly true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gwenneg could we have some feedback from you here? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gsmet, you are right. There is a chance to have concurrent access and replace a correct value. Reactive or not does not change anything here.
For Redis, we could imagine using a Redis transaction (be careful, it is not a database transaction). For others, I'm not sure. We could imagine adding an atomic operation doing this.