Streams do not continue working after silo failover
Hi,
I have a test project with a single grain that receives a number and returns the previous number via a stream while also persisting it to the grain's state (backed by SQL Server). The client connects to a silo via the SystemStore (again SQL Server).
The issue I am having is with failover of the silos when running two silos (A & B) on two machines (one is a VM). When the client first connects the stream works as expected and data is returned to the client. When I shutdown the silo with the active grain (silo A) it will normally successfully failover to silo B, however, on restarting silo A and shutting down silo B the grains activate ok as silo A receives the messages from the client but the stream back to the client is not re-established and the client never receives data back until I restart everything. I have tested this using the observer pattern as well and I get the same result. Could someone point me in the right direction of what I am doing wrong? Config and code below.
Thank you in advance, Rich
Server config
<SystemStore SystemStoreType ="SqlServer" DeploymentId="SimpleOrleans" DataConnectionString="Data Source=server;Initial Catalog=Orleans;User Id=user;Password=password" AdoInvariant="System.Data.SqlClient" /> <Liveness LivenessType="SqlServer" /> <StorageProviders> <Provider Type="Orleans.StorageProviders.SimpleSQLServerStorage.SimpleSQLServerStorage" Name="GrainStore" ConnectionString="Data Source=server;Initial Catalog=Orleans;User Id=user;Password=password" UseJsonFormat="false" /> <Provider Type="Orleans.StorageProviders.SimpleSQLServerStorage.SimpleSQLServerStorage" Name="PubSubStore" ConnectionString="Data Source=server;Initial Catalog=Orleans;User Id=user;Password=password" UseJsonFormat="false" /> </StorageProviders> <StreamProviders> <Provider Type="Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProvider" Name="SMSProvider" FireAndForgetDelivery="False"/> </StreamProviders>
Client config
<ClientConfiguration xmlns="urn:orleans"> <SystemStore SystemStoreType ="SqlServer" DeploymentId="SimpleOrleans" DataConnectionString="Data Source=server;Initial Catalog=Orleans;User Id=user;Password=password" AdoInvariant="System.Data.SqlClient" /> <GatewayProvider ProviderType="SqlServer"/> <StreamProviders> <Provider Type="Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProvider" Name="SMSProvider" FireAndForgetDelivery="False"/> </StreamProviders> </ClientConfiguration>
Client stream code
var streamProvider = GrainClient.GetStreamProvider("SMSProvider"); var stream = streamProvider.GetStream<int>(Guid.Empty, "NumberData"); var handle = stream.SubscribeAsync((number, token) =>{/* this is never called after silo failover */});
Grain stream code
// the client does manage to call this after silo failover var streamProvider = this.GetStreamProvider("SMSProvider"); var stream = streamProvider.GetStream<int>(Guid.Empty, "NumberData"); await this.stream.OnNextAsync(number);
Which stream provider are you using?
Streaming between the client and cluster should continue working as long as the client is connected to the Cluster.
To be clear, the following should work.
- Start Client and Cluster with silos A, and B
- Kill Silo A
- Start Silo A, A joins cluster
- Kill Silo B
- Start Silo B, B joins cluster
However, if the client loses connection to the silo, streaming would not recover without a client restart. For instance, if in step 3, one started Silo A and killed Silo B at the same time, the client could lose connection to silo be before it can connect to silo A. In that case it would have lost all connections to the cluster, and streaming would not recover without a client restart.
Most likely cause of what your seeing is that A, while started, had not joined the cluster before B was killed.
I am using SimpleMessageStreamProvider.
The silos are reconnecting to the cluster fine as the calls to the grains still work (client to grain). The only thing that fails is to re-establish the stream from the grain back to the client (grain to client).
A client that lost connection to a cluster can reestablish a connection and continue sending grain calls. Streams however, may not survive. Streaming is built on grain calls, but it's not the same. For client streams to survive Silo failures within a cluster, the client must maintain connectivity with the cluster. This is why I specified that Silo A must rejoin the cluster before Silo B is killed,
Do you see a warning with errorcode 103409, line "Consumer {0} on stream {1} is no longer active - permanently removing Consumer." in the logs?
The most common cause of this warning is that a client has become unreachable by the cluster (loses connectivity with the cluster) causing a stream consumer on that client to become unreachable. This error removes that consumer from the system.
I do have that warning so that most likely is it. I do wait for the silo to say it is connected each time but waiting a bit longer and so far so good on my testing. I will take this into consideration for the design of the final system. Thanks for the detailed information.
Another thing you may want to consider is that the client is less likely to lose connectivity with the cluster if there are more silos. If there were 3 silos in the deployment, the original tests would have worked fine, it was just that the last silo in the cluster was being killed before the other rejoined, causing the client to lose connection with the cluster as a whole.
That is a good point. Three silos is a good recommendation to clients as a minimum for a resilient system.
Jason, is this the bug we discussed before, in the context of https://github.com/dotnet/orleans/pull/1429 and beyond? "too eagerly unsunscribing client consumers"?
@gabikliot, Yes. Thanks for providing the reference. After refreshing my memory from the thread, I think the discussion that led to the current state is in the related "Client Stream Producers are not being removed" #1421, Specifically the conversation from Mar 28 through Mar 30.
Yes, exactly.
@jason-bragg - I've just stumbled upon this after debugging my system for why Orleans Clients don't handle events. So it turns out that after a full cluster restart, the clients lost the subscription. I have to say that I wasn't expecting this. I would expect the Client's to be able to resubscribe automatically (as the reconnection to Orleans cluster is done automatically).
In my system, I have Orleans clients with many different dynamic streams subscriptions. So it's not trivial to resubscribe.
After digging and conversing with @ashkan-saeedi-mazdeh I think my approach to address this would be:
- One centralized manager for the Orleans clients subscriptions.
- Handle GrainClient event
ClusterConnectionLost, wait for reconnection, then resubscribe to all streams. - I'll use it to also to gracefully handle the unsubscriptions when the client is down.
I actually believe this should be managed automatically in the Orleans client. It should greatly simplify the work with client subscriptions. Maybe also add some details in the streams API doc for 'Recovering From Failures' for the client side.
Thanks!
To add my 2 cents. The only way that I see that can make this automatic (as I told @shlomiw as well) is to have clients which identify themselves with the silo using an ID so the stream subscriptions belong to the client ID and can be stored.
Then the issue is that when a client stops to be available, the detection of this should tell the streaming system in Orleans to stop sending the messages to it and when it comes back online it should notify it so the data are sent to the client with the same ID. I don't see it as a good way of doing it since the client might need a totally different set of streams in almost all cases and it is not easy to bind the actual streams needed to the client ID. So my conclusion is that docs should be improved to at least mention some general algorithms that users of Orleans can use to recover from these failures and resubscribe.
One scenario which is impossible to fix using any automatic approach for resubscription is,
- Client C1 with Id ID1 (or even worth without it) connects to Silo S1
- Some players [P1...P10] connect to it and it subscribes for their streams
- C1 disconnect from S1 and the whole cluster due to a machine restart.
- C1 comes back online and connect to S1 or S2
- A partially different set of players connect to it, [P7,P9,P33...P50]
Then how an automatic resubscription manager should know that streams for some old players are not needed?
So as far as I see it this is impossible to be automated unless each ID subscribes to a set of fix streams and for that case writing the resubscription logic is not particularly hard either.
Sorry for having such a long 2 cents :)
My current main usage is that I'm using Orleans Streams to send general system events to all the Orleans Clients services. i.e - clear cache of objects, etc. (I also have Player's streams to send them messages)
When I'm deploying a new version for the silo (shutting down Orleans), I'm losing these subscriptions, and the clients no longer receive these events. So I'd say that the same way the Client reconnects to Orleans, I would expect it to resubscribe these streams.
I agree that we should address this in the docs.
The most common, and simple, pattern (which I'm aware of) for handling this case is to terminate (restart?) the client on ClusterConnectionLost. The assumption is that a system needs some recovery logic for when clients fail anyway, so leveraging that error handling to resolve loss of connection to the cluster should be practical.
As for a longer term fix, this is on our radar, the problem is simply resources and priorities. This issue is simply not common enough to prioritize the work above other initiatives. We'd be happy to work with the community to address this through community contributions, but I think their is a general awareness that the solution for this may not be trivial.
Orleans Client can be any service in the system that also talks to the grains. If the Orleans cluster fails - many other services don't need to restart, maybe do some recovery logic regarding the cluster.
I've currently implemented a streams subscription manager for the Clients which listens to ClusterConnectionLost and managing the resubscribe when the cluster is available again. Works fine so far :)
In the docs the streams are presented under Programming Model section:
Following the philosophy of Orleans virtual actors, Orleans streams are virtual. That is, a stream always exists. It is not explicitly created or destroyed, and it can never fail.
When one read this paragraph one expect reliability :) (this is why I was surprised)
Anyway, I understand all about priorities, and you guys are doing an amazing job!
@shlomiw , you are 100% correct in expecting reliability in this case. This is simply a corner case bug, which had a lower priority of fixing, as @jason-bragg wrote.
Regarding usage of ClusterConnectionLost. Can you also, in addition to using this event to detect potential stream loss, do the following:
periodically, very infrequently, publish some dummy test event to those system streams and check that it arrives. If not, re-subsricribe. This will make this combined logic more robust.
@jason-bragg , should we keep this issue open (it is now closed)?
@gabikliot - thanks for the heads up, this is important so I could trust my system events infra.
@jason-bragg / @ReubenBond - I've upgraded to 1.5.0-rc. Wanted to ditch the GrainClient for IClusterClient, so my Silo generic infra code would be the same as the Clients code.
But now I don't have access to ClusterConnectionLost , which important for the clients streams resubscriptions logic (see discussion above).
I've checked the source and I see the usage of:
client.ServiceProvider.GetRequiredService<OutsideRuntimeClient>() which exposes this event. But OutsideRuntimeClient is internal.
What am I missing?
(btw - I'm currently using hacky Reflection to catch this illusive event)
EDIT - I've accidentally just found ClientBuilder.AddClusterConnectionLostHandler()!! Wow! ok, so everything is alright. I hope you'll update the docs soon with ClientBuilder, could have saved me some time :) cheers
Any news about this issue?
I face a similar problem with Orleans 2.2 and I don't really know how to solve it in a robust way.
Without doing anything special, if I kill a Silo and restart it, sometimes the streams subscriptions continue working on the client and sometimes not. No idea why.
By the way I try to detect the connection lost by using clientBuilder.AddClusterConnectionLostHandler(handler)
But the handler is almost never called. And for a completly unknown reason, the only few times where it is called is when a debugger is attached. I tried a lot of times without the debugger and nothing happens
Edit: I opened a new issue #5310 for my problem as ResumeAsync() doesn't seem to work