-
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
Properly support CompletionStage as a return type in caching extension #24894
Conversation
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.
Thank you @geoand!
I'm not done reviewing the tests but first I have a few comments about the runtime code.
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
Outdated
Show resolved
Hide resolved
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
Outdated
Show resolved
Hide resolved
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
Outdated
Show resolved
Hide resolved
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
Outdated
Show resolved
Hide resolved
Thanks for the feedback.
I'll have a look tomorrow as I am taking the day off today
…On Wed, Apr 13, 2022, 12:30 Gwenneg Lepage ***@***.***> wrote:
***@***.**** commented on this pull request.
Thank you @geoand <https://github.com/geoand>!
I'm not done reviewing the tests but first I have a few comments about the
runtime code.
------------------------------
In
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
<#24894 (comment)>:
> @@ -27,6 +28,7 @@ public abstract class CacheInterceptor {
private static final Logger LOGGER = Logger.getLogger(CacheInterceptor.class);
private static final String PERFORMANCE_WARN_MSG = "Cache key resolution based on reflection calls. Please create a GitHub issue in the Quarkus repository, the maintainers might be able to improve your application performance.";
+ protected static final String UNHANDLED_ASYNC_RETURN_TYPE_MSG = "Unhandled async type";
⬇️ Suggested change
- protected static final String UNHANDLED_ASYNC_RETURN_TYPE_MSG = "Unhandled async type";
+ protected static final String UNHANDLED_ASYNC_RETURN_TYPE_MSG = "Unhandled async return type";
------------------------------
In
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
<#24894 (comment)>:
> + if (Uni.class.isAssignableFrom(returnType)) {
+ return ReturnType.Uni;
+ }
+ if (CompletionStage.class.isAssignableFrom(returnType)) {
+ return ReturnType.CompletionStage;
+ }
+ return ReturnType.NonAsync;
+ }
+
+ protected Uni<?> asyncInvocationResultToUni(Object invocationResult, ReturnType returnType) {
+ if (returnType == ReturnType.Uni) {
+ return (Uni<?>) invocationResult;
+ } else if (returnType == ReturnType.CompletionStage) {
+ return Uni.createFrom().completionStage((CompletionStage<?>) invocationResult);
+ } else {
+ throw new IllegalStateException(UNHANDLED_ASYNC_RETURN_TYPE_MSG);
This could throw a CacheException instead of the ISE.
------------------------------
In
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
<#24894 (comment)>:
> + return (Uni<?>) invocationResult;
+ } else if (returnType == ReturnType.CompletionStage) {
+ return Uni.createFrom().completionStage((CompletionStage<?>) invocationResult);
+ } else {
+ throw new IllegalStateException(UNHANDLED_ASYNC_RETURN_TYPE_MSG);
+ }
+ }
+
+ protected Object createAsyncResult(Uni<Object> cacheValue, ReturnType returnType) {
+ if (returnType == ReturnType.Uni) {
+ return cacheValue;
+ }
+ if (returnType == ReturnType.CompletionStage) {
+ return cacheValue.subscribeAsCompletionStage();
+ }
+ throw new IllegalStateException(UNHANDLED_ASYNC_RETURN_TYPE_MSG);
This could throw a CacheException instead of the ISE.
------------------------------
In
extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java
<#24894 (comment)>:
> - return Uni.class.isAssignableFrom(invocationContext.getMethod().getReturnType());
+ protected static ReturnType determineReturnType(Class<?> returnType) {
+ if (Uni.class.isAssignableFrom(returnType)) {
+ return ReturnType.Uni;
+ }
+ if (CompletionStage.class.isAssignableFrom(returnType)) {
+ return ReturnType.CompletionStage;
+ }
+ return ReturnType.NonAsync;
+ }
+
+ protected Uni<?> asyncInvocationResultToUni(Object invocationResult, ReturnType returnType) {
+ if (returnType == ReturnType.Uni) {
+ return (Uni<?>) invocationResult;
+ } else if (returnType == ReturnType.CompletionStage) {
+ return Uni.createFrom().completionStage((CompletionStage<?>) invocationResult);
I'm not sure @CacheResult#lockTimeout will work if the Uni is created
from the CompletionStage here. Delaying the CompletionStage computation
(with a Supplier<CompletionStage>) until the Uni is resolved may be
necessary:
⬇️ Suggested change
- return Uni.createFrom().completionStage((CompletionStage<?>) invocationResult);
+ return Uni.createFrom().completionStage(() -> (CompletionStage<?>) invocationContext.proceed());
(the real code would need a proper Supplier and not that lambda 😄)
—
Reply to this email directly, view it on GitHub
<#24894 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABBMDP6DOGRA44UTO23RZULVE2H47ANCNFSM5TH3REAA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Comments addressed |
This stuff will likely need to be rewritten for #24894, but it's best to separate the two as this one addresses a clear bug, while the Kotlin corroutine support is a feature request |
I should be able to finish this review later today, sorry for the delay 😃 |
Not a problem at all :) |
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.
Sorry for the delay 😃
I noticed something wrong with CacheResult#lockTimeout
and Uni
while reviewing this PR, so I spent some time confirming that there is indeed a bug in that area. CompletionStage
is affected in a similar way. I'll create an issue about it or a PR to fix it depending on how much time I have this week.
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
...he/deployment/src/test/java/io/quarkus/cache/test/runtime/CompletionStageReturnTypeTest.java
Outdated
Show resolved
Hide resolved
I hope I didn't break a test with the variables names changes 😄 |
Fixes: quarkusio#23816 Co-authored-by: Gwenneg Lepage <[email protected]>
Thanks a lot for the review, suggestions applied and commits squashed :) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
LGTM
Fixes: #23816