Orleans default serializer doesn't handle Linq enumerables
I have a grain method with IEnumerable of type T as an argument. In another grain, when I call that method with something along these lines:
grainref.MyMethod(SomeEnumerable.Skip(x));
I receive a no copier found exception for System.Linq.Enumerable.
Is there a way we could get orleans to understand how to serialize these enumerable types in linq, even though they aren't marked as serializable? If I swap over to using List
Is System.Linq.Enumerable serializable in .NET? If not, I am not sure it is correct/idiomatic to try to serialize it, even if we could (we sure could).
IEnumerable has the ability to iterate over a collection. That collection can support lazy-evaluation, so it makes sense that an IEnumerable cannot be serialized.
If you want to return the collection of data by calling a grain method you can do so by returning a list type. An alternative is to use Orleans Streams instead if the ability to enumerate without creating a list is important to you.
Yep, these are all valid arguments.
I see this as mostly an ease of use question, sacrificing some correctness for usability.
Would it be reasonable to serialize enumerables using the same method as a some solidified class such as an array? I have a number of points where I really only care that I'm passing some enumerable from one grain to another, and the requirement of calling .ToList() just feels like it should be unnecessary. In a way, it would behave in a similar fashion to how we can pass around IGrain interface references where there is some invisible backing class the user is not aware of nor cares about. A number of serialization libraries have this behavior and really, how is orleans' default serialization any different (formatting aside)? In fact, if people are using the json serializer, then they will probably get this behavior.
It's a pretty minor pain, so if this seems like an unreasonable solution then so be it.
Yes, to me that .ToList() feels like it's redundant as well.
However, once you start treating IEnumerable as an array type, likely at one point someone will open an issue and report that returning an IEnumerable is not behaving as expected.
For example if you return Enumerable.Range(0,200) from a grain call - one might expect evaluation as well on the receivers side, which at least in this example could be possible as well.
I personally try to avoid IEnumerable in distributed environments as much as I can because it has no clearly-defined meaning when working across many systems.
Perhaps I'm missing something, but if we did serialize the Enumerable.Range(...) as an array, wouldn't the behavior be the same on the receiver side? Assume they truly are treating the resulting value as just an IEnumerable? I think it's entirely reasonable to say that they can only expect the behavior of the interface, anything else is an implementation detail.
The behavior of the interface is to expose an enumerator. That's not the same as exposing a list of objects. That enumerator can be serializable or not, it's an implementation detail. If a specific implementation of IEnumerable is marked with the [Serializable]serializable attribute, such as a List, serializing that type should work.
Take an IEnumerable that provides access to a result set from a database. It exposes an enumerator that uses a database connection to (lazy) retrieve items from the database. Transferring an IEnumerable across grains would mean transferring the ability to obtain an enumerator that is connected to a database and ready to query and iterate over a result set.
I'll admit that your lazy evaluation example certainly exposes weaknesses in the solution I've provided. If orleans did provide some default implementation for serializing ienumerables, we would need to be explicit that it involves evaluating the enumerable as part of the serialization process. I think this would still provide desirable behavior for a number of use cases (linq being the gorilla for me), and would just require fair warning about the implications.
It's probably less pain to make an explicit call to ToList() each time than to make that behavior implicit and document it. I see Orleans as an actor framework that provides basic functionality, customizing the meaning of IEnumerable across the application is not what is supposed to do. Just my personal thoughts :)
Are there any additional downsides to the current implementation except the need to call ToList() each time?
I agree with you. This is what I called non idiomatic. We don't want to overload the meaning of ienumerable.
Ha, after shedding so much virtual ink over this, no it's mostly just about .ToList(). I just know that it gets repetitive when writing a lot of grain methods that pass enumerables around. I think I'll close this for now, since I think it's safe to say there's some contention and it's not a big deal :)