orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Serialization behavior differs between test runs

Open bwanner opened this issue 10 years ago • 4 comments

This might be related to #1536 opened 4 weeks ago.

Edit: See post below for updated details

A unit test creates two grains g1 and g2, where g2 is connected to a stream populated by g1. This stream transfers objects of type MyObject. MyObject contains one event that is marked as [field: NonSerialized].

When g1 is sending objects of type MyObject to the stream sometimes the events are still connected and sometimes not, while they should always be disconnected.

Is this an issue with TestingSiloHost or a general issue (Update: It is a general issue)? This inconsistent behavior makes Orleans unusable for my purposes right now. Let me know if I can be of any help fixing that issue.

Reproduction project for 1.1.3 is available at https://github.com/bwanner/Orleans-Event-Serialization

bwanner avatar Apr 05 '16 11:04 bwanner

Was able to narrow down the issue by further simplifying the sample project: https://github.com/bwanner/Orleans-Event-Serialization

There are two grains of type MyGrain that create a POCO MyObject on initialization. The grains are called g1 and g2 from now on.

[Serializable]
public class MyObject
{
    [NonSerialized]
    public string MyString; // This should never be transferred!

    public bool StringNull => MyString == null;
}

Testcase 1 (TestSendViaMethodCall): g1 sends its instance of MyObject to g2 via await g2.ReceiveMyObject(MyObject). It is then validated in g2 that MyObject.MyString is null. This test case always succeeds.

Testcase 2 (TestSendViaStream): g1 sends its instance of MyObject to g2 via a stream that g2 has previously subscribed to. It is then validated in g2 that MyObject.MyString is null. MyObject.MyString is never null if g1 and g2 are both placed on the same silo. I.e. if the testcase is started with option StartSecondary = false the testcase never succeeds. If the secondary silo is started, the testcase succeeds sometimes. This behavior is not wanted since the content should be independent of grain placement.

Inside the class StreamConsumerExtensionCollection I saw code making the streamed item immutable. Can anybody provide information on how streaming is supposed to work for the local and remote case? Maybe @gabikliot

bwanner avatar Apr 07 '16 13:04 bwanner

SMS extension indeed wraps items into an Immutable at https://github.com/dotnet/orleans/blob/master/src/Orleans/Streams/SimpleMessageStream/SimpleMessageStreamProducerExtension.cs#L186 to avoid extra deep copying. Looks like this is causing an unexpected consequence of passing the field marked as [NonSerialized].

It's an interesting question what is the right thing to do here.

Do the wrapping only for truly remote messages? That would go against location transparency. Somehow communicate that wrapping shouldn't be used for classes with [NonSerialized]? That seems too error prone. Make wrapping globally configurable, so that it could be turned off?

sergeybykov avatar Apr 08 '16 18:04 sergeybykov

I agree that location transparency is a core priority. Can we be more strict about that and treat streaming similar to grain method calls where items are not implicitly assumed Immutable? Neither stream source nor stream receiver placement should have an impact on streamed values. Thus, I would suggest to go with consistent behavior: Always provide a copy of an object following the rules of serialization to the stream receiver.

While the current implementation might provide some performance benefit, the implicit treatment of streamed values as Immutable breaks grain isolation: Two (or more) grains can hold a reference to the same object. Modifying this object that is not explicitly marked as Immutable might lead to race conditions. Also it violates foundations of the actor model: Each object is owned by its particular grain and can be modified/used without thinking about concurrency while communication only happens via message passing. Preconditions mentioned in the docs for using Immutable are not guaranteed by streaming as well:

For the purposes of the Immutable<> class, immutability is a rather strict statement: the contents of the data item will not be modified in any way that could change the item’s semantic meaning, or that would interfere with another thread simultaneously accessing the item.

All these things considered, my preferred option would be to just remove immutability as the default behavior from Simple Message Streams. Any thoughts?

bwanner avatar Apr 09 '16 11:04 bwanner

I think we should just allow additional config parameter to SMS Stream provider: ENFORCE_ITEMS_IMMUTABILITY (or something similar). As for default value, I think actually treating stream data as immutable by default (as opposite to RPC args) is the right choice here.

gabikliot avatar Apr 10 '16 02:04 gabikliot