orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Deadlock in a call cycle of client triggering grain publishing to stream consumer by client

Open sergeybykov opened this issue 10 years ago • 6 comments

Reported by @bwanner on Gitter.

I've a question about Orleans Streams: Implemented a client subscribing with an IGrainObserver and IAsyncObserver to a IGrain1. Now the client calls IGrain1.TriggerMessage() which sends an item over a stream to the client. This call only succeeds if the Grain1 is marked reentrant. Any thoughts about that? Tried to debug but could not figure out why reentrancy on the grain side should be an issue here ... Here is the most important code: https://gist.github.com/bwanner/daa1c302d6c0c3532391 - if interested I can provide a sample.

namespace DemoHost { public class ClientObserver : IClientObserver { public Task OnNextAsync(int item, StreamSequenceToken token = null) { Console.WriteLine(item); return Task.FromResult(true); } public Task OnCompletedAsync() { return Task.FromResult(true); } public Task OnErrorAsync(Exception ex) { throw new NotImplementedException(); } } public interface IClientObserver : IGrainObserver, IAsyncObserver { } }

namespace DemoGrains { [Reentrant] public class Grain1 : Grain, IGrain1 { private IAsyncStream _stream; public override Task OnActivateAsync() { _stream = GetStreamProvider("SMS").GetStream(this.GetPrimaryKey(), "Grain1Scope"); return base.OnActivateAsync(); } public async Task<StreamSubscriptionHandle> SubscribeAsync(IAsyncObserver observer) { return await _stream.SubscribeAsync(observer); } public async Task<StreamSubscriptionHandle> SubscribeAsync(IAsyncObserver observer, StreamSequenceToken token, StreamFilterPredicate filterFunc = null, object filterData = null) { return await _stream.SubscribeAsync(observer, token, filterFunc, filterData); } public async Task TriggerMessage() { await _stream.OnNextAsync(42); } } public interface IGrain1 : IGrainWithGuidKey, IAsyncObservable { Task TriggerMessage(); } }

var grain1 = GrainClient.GrainFactory.GetGrain(Guid.NewGuid()); var clientObserver = new ClientObserver(); var clientObserverRef = await GrainClient.GrainFactory.CreateObjectReference(clientObserver); await grain1.SubscribeAsync(clientObserverRef); await grain1.TriggerMessage(); // Never succeeds if Grain1 is not reentrant!

IGrainObserver seems extraneous to me here but that should be irrelevant. I don't think [Reentrant] should be required here, but maybe I'm wrong.

sergeybykov avatar Feb 12 '16 17:02 sergeybykov

How that is even considered legal? Grain subscribes a client observer? Grain should be allowed to subscribe only its local observer. Client should subscribe itself. We don't support "subscribe on behalf of another remote entity".

The right thing to do is this line in the grain: return await _stream.SubscribeAsync(observer); to throw illegal op exception - grain can not subscribe ANOTHER REMOTE observer. It does not throw now, and what comes out is an unpredictable mess. The fact that it somehow does work with reentrant is a puzzle, coincidence, but does not really matter.

gabikliot avatar Feb 12 '16 18:02 gabikliot

@gabikliot I see what you mean, the way streams work makes sense now. It might make sense to check if the passed observer to StreamConsumer.SubscribeAsync() is not a GrainReference and throw then.

I found the documentation http://dotnet.github.io/orleans/Orleans-Streams/ in section "Programming APIs" a little confusing: Both examples are calling GetStream<T>() within their scope without obtaining any StreamProvider prior to that. Is that documentation presenting old code? If so, I will create a PR for that.

bwanner avatar Feb 12 '16 19:02 bwanner

I somehow overlooked the critical pieces in the code. Thanks, @gabikliot, for spotting it.

@bwanner We always welcome improvements to the docs. Just submit a PR against https://github.com/dotnet/orleans/tree/gh-pages.

sergeybykov avatar Feb 12 '16 20:02 sergeybykov

I'll close this one because the issue was due to misunderstanding.

sergeybykov avatar Feb 12 '16 20:02 sergeybykov

I actually thought more about this, and how come it worked with reentrant. In fact, it is amazing that it worked and testifies to the fact that Orleans abstractions work correctly, even if we did not even think they will. The grain subscribes. It subscribes itself, not the client observer. But as a local subscription callback instead of giving a local POCO, it gives a reference to a remote object. And it just works! The grain subscribes - that is, the grain will appear in pub sub, and the msg from the producer is sent to the grain. But once it hits the grain (the grain extension we install), it is delivered to the callback object, which in this case is a grain reference, and the grain reference just works like it should work - sends the msg to the client observer. So it does not work accidentally, it works by design.

Now, there is a separate question from the API perspective if we should allow that. That is a separate discussion. On the one hand, you one may claim it is confusing and we better limit those things. But on the other hand it opens more possibilities. I am not opinioned on that question. This can be usefull for example in some hybrid streams/observers setup when client is not using stream, but grain does. Client subscribes with observer to a grain, like in the traditional pre-streams time, while the grain receives its input via stream. Instead of writing an explicit app code in the grain to take from stream and push into observer, this way allows to do it automatically. The runtime "pipelines" the input stream into the client observer. Rx-like....

From the runtime capabilities perspective (what the runtime CAN support, not from the perspective of what should be exposed in the prog. model), I think this is awesome and demonstrates the power of our abstraction (the grain reference is really virtual) and its implementation.


Now totally unrelated to client observer is the question of reentrant. You got a deadlock since the grain both produces and subscribes: producer awaits the call, which locks the grain, so the input msg cannot even be delivered to the grain extension. Same would happen if you did self grain call, unrelated to streams.

gabikliot avatar Feb 13 '16 04:02 gabikliot

Great explanation! The use case you mention for hybrid streams/observers are very valid and might safe developers some work. However, I don't think this (rarely used, I assume) advantage justifies this use of the API:

By digging through the code I found (as you mentioned) that ObserveAsync() triggers the creation of an object of type StreamConsumerExtension within the caller's scope that is then made addressable to Orleans and is used for consuming the stream. From a user's perspective considering the API it is not obvious that the caller becomes part of that subscription if it subscribes another observer on-behalf. In order to have a simple and clear meaning on the API, I would limit subscription of streams to two parties at once.

With the current implementation this would result in denying on-behalf subscription as in the PR.

bwanner avatar Feb 13 '16 07:02 bwanner