Skip to content

Add support for Actor Reentrancy - #545

Closed
halspang wants to merge 3 commits into
dapr:masterfrom
halspang:reentrancy
Closed

halspang wants to merge 3 commits into
dapr:masterfrom
halspang:reentrancy

Conversation

@halspang

@halspang halspang commented May 14, 2021 •

Copy link
Copy Markdown
Contributor

Description

This adds the new Reentrancy field to the actor runtime config
and perserves the Dapr-Reentrancy-Id header which is required for
all reentrant calls.

Future Updates

  • Only functions in HTTP calls/needs support for gRPC

Issue reference

Please reference the issue this PR will close: #533

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

@halspang
halspang marked this pull request as draft May 14, 2021 16:03
String actorId,
String methodName,
byte[] jsonPayload,
Map<String, String> headers) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the gRPC implementation is still in progress for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the problem here is that the protobuf doesn't appear to expose Metadata to us, so I think this change might require a protobuf update.

this.runtimeContext = runtimeContext;
this.actorMethods = new ActorMethodInfoMap(runtimeContext.getActorTypeInformation().getInterfaces());
this.activeActors = Collections.synchronizedMap(new HashMap<>());
this.actorReentrancyContext = new ActorReentrancyContext();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reentrancy context is per method, not?

Still, I don't think this will work because it would need a way to garbage collect. Imagine that the call stack failed, there would need to be a way to understand that the call stack is over and release the id.

I like the idea of providing the context via an argument to the Actor's method is Reentrancy annotation is present. This way, it remains a stateless solution.

The preferred solution would still be to have this context passed through via Mono. I think we should move this to the next release since it will require more investigation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reentrancy context is per method, not?

The context is per actor id/type combo.

I like the idea of providing the context via an argument to the Actor's method is Reentrancy annotation is present. This way, it remains a stateless solution.

I avoided passing it through via a method because we originally wanted to have the passthrough happen without downstream actor's having to touch it. I don't think we can achieve that just by passing it through so we'd have to make that call about experience.

Still, I don't think this will work because it would need a way to garbage collect. Imagine that the call stack failed, there would need to be a way to understand that the call stack is over and release the id.

This is actually one of the few places where Reactor was able to help us. On the original call into the actor we can setup a terminal event (currently we just have onSuccess) that clears the ID. This comes in both success and error flavors. If the actual Java runtime crashes, the data is cleared anyway, so I think this is fine.

The preferred solution would still be to have this context passed through via Mono. I think we should move this to the next release since it will require more investigation.

As for passing the context through via Mono's SubscriberContext, I don't think we're actually using Reactor in a way that this is possible. The SubscriberContext as we need it is available in a single call chain. Since our chain involves a method that can call into other methods (thus creating a nested mono), we lose access to the original pipeline and context in the nested monos.

I agree this change has gotten a lot more complex than anticipated and we shouldn't try and rush it into the release on the day of code freeze.

@halspang
halspang force-pushed the reentrancy branch 7 times, most recently from 04d253d to 79ed286 Compare June 18, 2021 18:58
This adds the new Reentrancy field to the actor runtime config
and perserves the Dapr-Reentrancy-Id header which is required for
all reentrant calls.

dapr#533
This commit adds unit tests and associated changes for supporting
Actor Reentrancy in the Java SDK.
@halspang halspang changed the title [DRAFT] Add support for Actor Reentrancy Add support for Actor Reentrancy Jun 25, 2021
@halspang
halspang requested a review from artursouza June 25, 2021 18:20
@halspang
halspang marked this pull request as ready for review June 25, 2021 18:21
@halspang
halspang force-pushed the reentrancy branch 3 times, most recently from 017224c to 2c695b4 Compare June 28, 2021 22:19
@codecov

codecov Bot commented Jun 28, 2021

Copy link
Copy Markdown

Codecov Report

Merging #545 (053bc9a) into master (0e5e311) will increase coverage by 0.61%.
The diff coverage is 89.37%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #545      +/-   ##
============================================
+ Coverage     80.75%   81.36%   +0.61%     
- Complexity      986     1058      +72     
============================================
  Files            88       94       +6     
  Lines          3039     3183     +144     
  Branches        332      353      +21     
============================================
+ Hits           2454     2590     +136     
- Misses          419      422       +3     
- Partials        166      171       +5     
Impacted Files Coverage Δ
...c/main/java/io/dapr/actors/client/ActorClient.java 56.52% <0.00%> (-2.57%) ⬇️
.../java/io/dapr/actors/client/ActorProxyBuilder.java 70.96% <42.85%> (-9.04%) ⬇️
...ain/java/io/dapr/actors/runtime/AbstractActor.java 86.66% <57.14%> (+1.33%) ⬆️
...main/java/io/dapr/actors/runtime/ActorRuntime.java 59.52% <80.00%> (+2.76%) ⬆️
...ain/java/io/dapr/actors/client/ActorProxyImpl.java 85.71% <83.33%> (-2.66%) ⬇️
...main/java/io/dapr/actors/runtime/ActorManager.java 82.40% <90.00%> (+0.58%) ⬆️
...ctors/runtime/reentrancy/ReentrancyContextKey.java 90.00% <90.00%> (ø)
...ors/runtime/reentrancy/ActorReentrancyContext.java 96.29% <96.29%> (ø)
...ain/java/io/dapr/actors/client/DaprGrpcClient.java 93.75% <100.00%> (+0.20%) ⬆️
...ain/java/io/dapr/actors/client/DaprHttpClient.java 100.00% <100.00%> (ø)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e5e311...053bc9a. Read the comment docs.

@artursouza artursouza left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw some key points that we need to address before proceeding with this change.

@skyao @xiazuojie can we get some help from Alibaba in using Mono's context here?

* @param invocationContext Additional information associated with this invocation.
* @return Asynchronous result with the Actor's response.
*/
Mono<byte[]> invoke(String actorType,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to avoid adding this public method? This is a one way street and we cannot take it out afterwards. I am concerned because the new parameter is a workaround for the feature and not really needed to invoke the actor method if we could properly set the context.

* @return this
*/
public ActorProxyBuilder<T> withActorRuntime(ActorRuntime actorRuntime) {
if (actorRuntime == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really required because the code handles null in the build() method.

* @param actorClient Dapr client for Actor APIs.
*/
ActorProxyImpl(String actorType, ActorId actorId, DaprObjectSerializer serializer, ActorClient actorClient) {
this(actorType, actorId, serializer, actorClient, ActorRuntime.getInstance());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit odd because it adds a dependency between the client (ActorProxy) and the server (ActorRuntime). An actor client does not need to register any of the actors in the runtime.

String actorId,
String methodName,
byte[] jsonPayload,
ActorInvocationContext invocationContext) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that reentrancy still remain to be implemented for gRPC?

import java.util.HashMap;
import java.util.Map;

public class ActorInvocationContext {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a lot like the Context from Mono.

}).thenReturn(true);
}

Mono<Optional<String>> getReentrancyId(String actorId, String actorType) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use Mono if values are "static".

.flatMap(r -> actor.onPostActorMethodInternal(context).thenReturn(r))
.onErrorMap(throwable -> {
actor.rollback();
actor.rollback(context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Does it break telemetry?

* @param reentrancyId Id used to match reentrant requests.
* @return Response for the actor method.
*/
public Mono<byte[]> invoke(String actorTypeName,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, another public method that will force us to keep supporting this, while passing context as a param instead of using Mono's context.

@danp11 danp11 Jul 16, 2021 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi all,

This PR is a beutiful one 👍 and is coming with some super nice additions to the actors.

However, it looks like it is a bit stale atm. @halspang @skyao @xiazuojie , any chance that you
can look into Arthurs comments?

Thanks in advance and have a nice weekend!

From a Dapr Java-SDK user :-)

/Dan

@johnou

johnou commented Jan 26, 2022

Copy link
Copy Markdown

ping @halspang

@pravinpushkar

Copy link
Copy Markdown
Contributor

@halspang If you are not planning to work on this right now then feel free to assign it to me. I would like to continue on this.

@cicoyle

cicoyle commented Dec 21, 2023

Copy link
Copy Markdown
Contributor

gentle ping - @halspang

@halspang

Copy link
Copy Markdown
Contributor Author

This hasn't been a priority for a while. It was discussed offline, but never here that others would take over this work. I'm not sure where that ended up or if they need this PR open anymore. Given how old this is, it's probably best to close the PR and start a new one with whoever decided to take this over.

@cicoyle

cicoyle commented Dec 21, 2023

Copy link
Copy Markdown
Contributor

@pravinpushkar - if you are interested in adding this change to the java-sdk, please open up a follow up PR with the changes for this. I think we can close this one given @halspang's comment.

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.

Add support for actor reentrancy

6 participants