Contribution idea: Proper support for F#
I'd be interested in contributing here.
Great! There are at least two problems here we are aware of.
- @johnazariah has an example of the "filthy hack" he had to do to work around what appears an incompatibility between F# async and TPL custom schedulers - https://gist.github.com/johnazariah/527287a190a14a73b35d#file-callergrain-fs. There is already a suggestion there from @vasily-kirichenko - https://github.com/fsprojects/fsharpx/blob/7246e314e3fdeae07f3465b9126f2bc22faa0cd5/src/FSharpx.Core/Async.fs#L43-L45.
- The bigger issue people have been pointing to is the fact that today you need to use interfaces, which is not natural for F#. The thought here is that a better 'binding' to F# is needed in the runtime. Being F# illiterate, I have very little idea what the right 'binding' should look like. We are open to all ideas.
@sergeybykov - thanks for raising the async issue @vasily-kirichenko - thanks for the fix - i'll try it out
My thoughts on the second issue are as follows:
-
The issue of idiomatic F# over Orleans is quite orthogonal to just the async support. My take on idiomatic F# is that the functional programming model prefers stateless functions operating on data which can be pipelined successively between functions, whilst the Orleans programming model has well encapsulated state and well defined semantics for updating internal state. I'm not sure how we are going to get a solution on this level that is both idiomatically F# and idiomatically Orleans.
-
Digging a level deeper, I think the Orleans runtime has the makings of a streaming data provider, and with judicious use of stateless functions and stateful objects, we might be able to write distributed F# programs that take advantage of the scalability and reliability afforded by the Orleans runtime platform. (There is probably a lot to be gained on this front by looking at competing platforms like Apache Spark, which provide a Scala-based functional programming model over a highly-available redundant data storage layer)
We can also take guidance how existing distributed compute frameworks are surfaced
mbrace and Vagabond and FsPickler
One thing they all have in common is using Computation Expressions to hide all the boiler plate code. Taking place of the code Gen on the C# side.
Another issue is, there is already a concept of an Actors in F# as MailboxProcessors. Ideally we would map, Interface methods to a DU.
For example the Game Grain could be mapped to
type gamegrain =
| AddPlayer of Guid
| GetState of AsyncReplyChannel
| GetMoves of AsyncReplyChannel
| SetName of string
| ....
@TonyAbell +1 :)
@TonyAbell
One thing they all have in common is using Computation Expressions to hide all the boiler plate code. Taking place of the code Gen on the C# side.
Ye, but that requires having a non-static way to define an actors. Something which Orleans is currently lacking. You will need to define an interface first using C# and that immediately knocks-off F# fans :)
Another issue is, there is already a concept of an Actors in F# as MailboxProcessors. Ideally we would map, Interface methods to a DU.
'MailboxProcessor' and Akka's actor API - have a uniform interface. I'm not sure what will be the point of creating a mapper which will map from uniform interface to a non-uniform and vice versa. You either have one or another.
DU - is a Message. You do Message Handler selection yourself (via pattern matching). Which means that all of your functional grain interfaces will ever have 2 fully generic methods, at max. Something like this:
Task ReceiveTell(TRequest m);
Task<TResponse> ReceiveAsk(TRequest m);
Which raises a point: why you need to define any interfaces at all?!?
@johnazariah We have tried Async.AwaitTask and it works perfectly. We've also introduced Async.AwaitVoidTask to await Tasks, that don't return results. And it covers the whole F# TPL-based async story. That is all.
I believe @alan-geller also had some thoughts about F# integration.
In the filthy hack code, on which scheduler does the last Async.StartAsTask run? If my memory serves me well (well, it can fail too), it doesn't run in the Orleans scheduler and according to documentation there isn't a way to specify a scheduler using Async like this.
@yevhen , @veikkoeeva that's precisely the issue - the Async.StartAsTask runs on a different scheduler as does the async {} monad, so making a grain call another grain (asynchronously) in F# does not work out of the box if you use the idiomatic async {} monad to sequence the calls...
@yevhen I think that having a way to create ad-hoc actor implementations (a-la mbrace, akka,net) and having a way to dynamically distribute them over the Orleans run-time so that you get the best of both worlds looks like the desired outcome! For this, there will have to be a way to dynamically create Orleans objects (with or without interfaces to define the capabilities) and ship them over the runtime...
maybe we could implement an orleansasync computational expression with the desired behavior.
@TonyAbell that's the simplest fix I could think of for the first issue, but perhaps vasily's solution is equally effective! :)
@johnazariah
I think that having a way to create ad-hoc actor implementations (a-la mbrace, akka,net) and having a way to dynamically distribute them over the Orleans run-time so that you get the best of both worlds looks like the desired outcome! For this, there will have to be a way to dynamically create Orleans objects (with or without interfaces to define the capabilities) and ship them over the runtime...
We're working on this :) ...
@yevhen :+1:
@johnazariah have you looked into ActorFx? Maybe what you're looking for lies in the intersection between ActorFx and Orleans. The model can certainly be hosted within Orleans, with a little finagling. @sergeybykov, @gabikliot & I were talking about this a few days ago, actually.
We could have a generic, delegating implementation of an interface and provide a Become or SetImplementation method on it which takes a runtime implementation of its interface - certainly we would need to come up with a solid design, but it would be a heck of a lot of fun to work on :)
@reubenbond I feel another learning session coming on :)
ActorFx seems dead or forgotten as the last check-in was Mar 19, 2014
How would you compare/contrast the ActorFx / m-brace frameworks.
I'm not familiar enough with m-brace (@johnazariah knows more), but ActorFx provides a system for dynamically creating actors at runtime using a variety of languages (.NET, JavaScript, Python, afaik).
Actors can have their state automatically replicated (I believe they replicate the state of each actor after each method invocation, but I don't know). They are observable (actors can subscribe to other actors). And they are referenced by name.
It may not be active any more, but there are some concepts and possibly code which can be lifted & shifted into Orleans - perhaps as an optional dependency.
M-Brace looks like a distributed dataflow system, so maybe it's more similar to EventHubs than Orleans. Orleans could provide this model using steams as edges & grains as nodes.
Does M-Brace checkpoint flows, or are they restarted from scratch when there's a failure?
Does M-Brace checkpoint flows, or are they restarted from scratch when there's a failure?
Taking a quick look at the docs it looks like the flows aren't explicitly checkpointed. From the Programming Model page
Clearly, one of the child computations will fail on account of an invalid url, creating an exception. In general, uncaught exceptions bubble up through Cloud.Parallel triggering cancellation of all outstanding child computations (just like Async.Parallel).
The interesting bit here is that the exception handling clause will almost certainly be executed in a different machine than the one in which it was originally thrown. This is due to the interpreted nature of the monadic skeleton of cloud workflows, which allows exceptions, environments, closures to be passed around worker machines in a seemingly transparent manner.
The MBrace manual goes deeper into details on what happens upon node failures or exceptions.
An important feature supported by cloud workflows is exception handling. Exceptions can be raised and caught in cloud workflows just as in any other piece of F# or .NET code.
cloud {
try
let! x = cloud { return 1 / 0 }
return x + y
with :? System.DivideByZeroException as e ->
// log and reraise
do! Cloud.Logf "error: %O" e
return raise e
}
This snippet will execute in the anticipated fashion, recording a message to the log before completing the computation by re-raising the exception. It is, however, the distributed nature of MBrace that makes its exception handling mechanism particularly interesting. With cloud workflows, the symbolic execution stack winds across multiple machines. Thus as exceptions are raised, they are passed around multiple nodes before they are, if ever, returned to the user as a failed computation result.
This is a good demonstration of what is one of the strengths of MBrace. Error handling in particular and computation state in general have a global and hierarchical scope rather than one that is fragmented and localized. This is achieved thanks to symbolic and distributed interpretation of what is known as monadic trampolines[10, 11], also known as the “monadic skeleton” of a cloud workflow.
Having this mind, I was a few weeks ago thinking about MBrace and using Recalled to checkpoint calculations. There's a video of Recalled (1:15:55 onwards) and I was quite impressed in Helsinki F# meetup of the performance that saturated the memory bus when doing (arbitrary) checkpointed calculations. Recalled is imporessive, as is Hopac (of which @vasily-kirichenko knows more) on which it's built on.
As an aside, it would be interesting to combine MBrace, Recalled and something like FSCL (OpenCL accelerated computing, say, with FPGAs) and with a quick look it's like Spark on steroids, only in F# and .NET. Naturally with Orleans (somehow).