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
New RequestScopeHelper class to track request scopes across threads #2856
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4b7dfbf
Run concurrent requests for each test.
spericas 47e6b17
Merge branch 'master' into issue-2632
spericas 204133a
New test.
spericas 0f543fe
New helper class to manage request scope logic across threads. Update…
spericas 8762ffd
Updated test to show the problem described in https://github.com/orac…
spericas ee3f5f0
Fixed copyright.
spericas b846978
Merge branch 'master' into issue-2632
spericas File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
137 changes: 137 additions & 0 deletions
137
...lt-tolerance/src/main/java/io/helidon/microprofile/faulttolerance/RequestScopeHelper.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,137 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.microprofile.faulttolerance; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import javax.enterprise.context.control.RequestContextController; | ||
import javax.enterprise.inject.Instance; | ||
import javax.enterprise.inject.spi.CDI; | ||
|
||
import org.glassfish.jersey.internal.inject.InjectionManager; | ||
import org.glassfish.jersey.process.internal.RequestContext; | ||
import org.glassfish.jersey.process.internal.RequestScope; | ||
import org.glassfish.jersey.weld.se.WeldRequestScope; | ||
|
||
class RequestScopeHelper { | ||
|
||
enum State { | ||
CLEARED, | ||
STORED | ||
} | ||
|
||
private State state = State.CLEARED; | ||
|
||
/** | ||
* CDI's request scope controller used for activation/deactivation. | ||
*/ | ||
private RequestContextController requestController; | ||
|
||
/** | ||
* Jersey's request scope object. Will be non-null if request scope is active. | ||
*/ | ||
private RequestScope requestScope; | ||
|
||
/** | ||
* Jersey's request scope object. | ||
*/ | ||
private RequestContext requestContext; | ||
|
||
/** | ||
* Jersey's injection manager. | ||
*/ | ||
private InjectionManager injectionManager; | ||
|
||
/** | ||
* Store request context information from the current thread. State | ||
* related to Jersey and CDI to handle {@code @Context} and {@code @Inject} | ||
* injections. | ||
*/ | ||
void saveScope() { | ||
if (state == State.STORED) { | ||
throw new IllegalStateException("Request scope state already stored"); | ||
} | ||
// CDI scope | ||
Instance<RequestContextController> rcc = CDI.current().select(RequestContextController.class); | ||
if (rcc.isResolvable()) { | ||
requestController = rcc.get(); | ||
} | ||
// Jersey scope | ||
injectionManager = WeldRequestScope.actualInjectorManager.get(); // thread local | ||
try { | ||
requestScope = CDI.current().select(RequestScope.class).get(); | ||
requestContext = requestScope.referenceCurrent(); | ||
} catch (Exception e) { | ||
// Ignored, Jersey request scope not active | ||
} finally { | ||
state = State.STORED; | ||
} | ||
} | ||
|
||
/** | ||
* Wraps a supplier into another supplier that actives the request scope | ||
* before calling it. | ||
* | ||
* @param supplier supplier to wrap | ||
* @return wrapped supplier | ||
*/ | ||
FtSupplier<Object> wrapInScope(FtSupplier<Object> supplier) { | ||
if (state != State.STORED) { | ||
throw new IllegalStateException("Request scope state never stored"); | ||
} | ||
if (requestScope != null) { // Jersey and CDI | ||
return () -> requestScope.runInScope(requestContext, | ||
(Callable<?>) (() -> { | ||
InjectionManager old = WeldRequestScope.actualInjectorManager.get(); | ||
try { | ||
requestController.activate(); | ||
WeldRequestScope.actualInjectorManager.set(injectionManager); | ||
return supplier.get(); | ||
} catch (Throwable t) { | ||
throw t instanceof Exception ? ((Exception) t) : new RuntimeException(t); | ||
} finally { | ||
requestController.deactivate(); | ||
WeldRequestScope.actualInjectorManager.set(old); | ||
} | ||
})); | ||
} else if (requestController != null) { // CDI only | ||
return () -> { | ||
try { | ||
requestController.activate(); | ||
return supplier.get(); | ||
} finally { | ||
requestController.deactivate(); | ||
} | ||
}; | ||
} else { | ||
return supplier; | ||
} | ||
} | ||
|
||
/** | ||
* Clears internal state saved by calling {@link #saveScope()}. | ||
*/ | ||
void clearScope() { | ||
if (requestContext != null) { | ||
requestContext.release(); | ||
requestContext = null; | ||
} | ||
requestScope = null; | ||
requestController = null; | ||
injectionManager = null; | ||
state = State.CLEARED; | ||
} | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
...al/request-scope/src/main/java/io/helidon/tests/functional/requestscope/Application1.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,35 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.tests.functional.requestscope; | ||
|
||
import java.util.Set; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.ws.rs.core.Application; | ||
|
||
/** | ||
* This functional test requires having two application subclasses. | ||
* See: https://github.com/oracle/helidon/issues/2632#issuecomment-796831904 | ||
*/ | ||
@ApplicationScoped | ||
class Application1 extends Application { | ||
|
||
@Override | ||
public Set<Class<?>> getClasses() { | ||
return Set.of(Service1.class, Service2.class, Service3.class); | ||
} | ||
} |
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.
Would we ever legitimately instantiate a helper without also saving the scope right away?
If not, maybe fold the save work into the constructor? Or make the constructor private and use a static factory method that instantiates the helper and then invokes its
saveScope
method?That would not work if there are ever cases where a single helper instance is created and then reused through several
save
andclear
cycles.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 don't have a use case for saving/clearing/saving yet, so it is a bit more generic than necessary. Having said that, I think it easier to read with an explicit call to
saveScope
.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.
My concern was that correct usage requires me to remember to both instantiate the helper and invoke
saveScope
explicitly. This is internal so I'm not overly worried.