Forwarding failed while unsubscribing from stream. [Question]
Hi, I am trying to unsubscribe from a stream explicitly from OnDeactivateAsync() of grain. I am also doing stream subscription through a method call and not in OnActivateAsync. Sometimes I have noticed following exception:
Error calling grain's OnDeactivateAsync() method - Grain type = GrainType Activation = [Activation: S[IP]:[Port]:grainxxxxxxxx #GrainType=GrainType Placement=RandomPlacement State=Deactivating] Exc level 0: Orleans.Runtime.OrleansException: Forwarding failed: tried to forward message Request S[IP]:[Port]:grainxxxxxxxxxx->S[IP]:[Port]:grainxxxxxxxxxxx #7635441[ForwardCount=2]: global::Orleans.Streams.IPubSubRendezvousGrain:UnregisterConsumer() for 2 times after Finished Destroy Activation to invalid activation. Rejecting now. at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Orleans.Streams.StreamConsumer`1.
d__16.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at GrainType. d__11.MoveNext()
My question is; if there is an exception in OnDeactivateAsync() will the grain deactivation will fail and stream will not be unsubscribed? I have also encountered "Subscription not installed on grain" exception multiple time. So does it mean I need to call ResumeAsync() on subscription handle from OnActivateAsync() to make sure recovery even though I am trying to UnSubscribe from stream in OnDeactivateAsync(). I am using Orleans 1.4.1. Thanks
I have also encountered "Subscription not installed on grain" exception multiple time. So does it mean I need to call ResumeAsync() on subscription handle from OnActivateAsync() to make sure recovery even though I am trying to UnSubscribe from stream in OnDeactivateAsync().
Yes. You need to subscribe/resume first, then you can UnSubscribe. Even though subscription can live through different activation, the grain extension required by UnSubscribe call need to be installed on each activation. So you need to call subscribe/resume first to install that grain extension.
Thanks for the reply @xiazen Need clarification on:
Orleans.Streams.IPubSubRendezvousGrain:UnregisterConsumer() for 2 times after Finished Destroy Activation to invalid activation. Rejecting now.
Does this mean that grain is deactivated but unsubscribe to stream failed.
That means grain deactivated regardless. But the response of unsubscribe call cannot be forwarded to the deactivated grain. The unsubscribe may or may not fail, which I cannot assess based on the logs. But the forwarding response failed.
Can you describe how your deactivation logic looks like? do you call deactivate explicitly? do you await unsubscribe call in OnDeactivateAsync ?
Hi @xiazen PFB:
public override async Task OnDeactivateAsync()
{
await UnSubscribeAsync();
await base.OnDeactivateAsync();
}
public async Task UnSubscribeAsync()
{
if (_subscriberHandle != null)
{
await _subscriberHandle.UnsubscribeAsync();
_subscriberHandle = null;
}
}
Have you called DeactivateOnIdle(); for this grain to explicitly deactivate it somewhere in your application?
Hi @xiazen,
We are not calling DeactivateOnIdle() also the grain state is not persisted. _subscriberHandle mentioned in the code above is just a private field in grain and is set when grain subscribe to stream through a method call.
Hi @xiazen
What I have noticed is that such exceptions are getting logged on Silo restarts. Now I do not need old subscriptions so to heal the grain I am resuming the old subscription on activation and then unsubscribing it.
As per your comments above, do I have to call the ResumeAsync first or I can directly call UnSubscribeAsync? I saw the Orleans code, it is calling method BindExtensionLazy in UnSubscribeAsync..
StreamConsumer.cs
Then why do we need to Resume first, also, GetAllSubscriptions returning existing subscription, so the grain extension should alread be installed. Just curious!
var newHandle = await streamSubscriptionHandle.ResumeAsync(OnStreamMessageReceivedAsync);
await newHandle.UnsubscribeAsync();
I apologize for false information. You are right. Calling UnSubscribeAsync will install the extension. You don't need to call resumeAsync first and then UnSubscribe, as long as the handle is valid. And you can always call GetAllSubscriptions, and then Unsubscribe on the returned handles.
I took your "Subscription not installed on grain" exception as "Extension not installed on grain", which leads to my conclusion. Just curious, is "Subscription not installed on grain" the exact word? Since Orleans shouldn't log this if UnSubscribe failed. Just want to debug what is the failure cause.
Hi @xiazen Sorry, It was "Extension not installed on grain.". Since the grain was reactivating from failed state, and stream were sending messages, exception were raised as there were no Resume. However, now I am unsubscribing from stream in OnActivateAsync, hope it will not lead to any race condition where message handler will still be called. I was not able to find a way to check if handle is still valid.
GetAllSubscriptions should be returning valid handles. If you are unsure about the handle, you can always use that to refresh your stored handles. But yeah, it is unfortunate that currently there isn't a way to determine the handle is valid or not, to my knowledge.
Thanks @xiazen for your help.