1

This may be a duplicate.

Thus, I have made some progress. However, I find it challenging to interpret the reference documentation from the C# API to the desired Akka.FSharp API.

Is there an example of sending messages between actors using "Akkling.Cluster.Sharding"?

As of now, I am only able to send messages from my client program and not an actor.

let consumer (actor:Actor<_>) msg = 
    printfn "\n%A received %A" (actor.Self.Path.ToStringWithAddress()) (box msg) |> string |> ignored

let system1 = System.create "cluster-system" (configurePort 2551)
let shardRegion1 = spawnSharded id system1 "printer" <| props (actorOf2 consumer)

shardRegion1 <! ("shard-1", "entity-1", "hello world 1")

The code above works. However, it only works with strings as messages. I am still struggling to get actors to send messages to each other with various typed messages.

NOTE:

I got the Akka.Persistence.SqlServer plugin working.

However, I am not clear on how to retrofit the following setup within Akkling.Cluster.Sharding:

open Akka.FSharp
let clusterHostActor =
    spawn system1 nodeName <| fun (inbox: Actor<_>) -> 
        let cluster = Cluster.Get system1
        cluster.Subscribe(inbox.Self, [| typeof<ClusterEvent.IClusterDomainEvent> |])
        inbox.Defer(fun () -> cluster.Unsubscribe(inbox.Self))
        let rec messageLoop () = 
            actor {
                let! message = inbox.Receive()                        
                match box message with
                | :? ClusterEvent.MemberUp      as event -> printfn "Member %s Joined the Cluster at %O" event.Member.Address.Host DateTime.Now
                                                            let sref = select (event.Member.Address.ToString() + "/user/listener") inbox
                                                            sref <! "Hello from clusterHostActor"
                | :? ClusterEvent.MemberRemoved as event -> printfn "Member %s Left the Cluster at %O"   event.Member.Address.Host DateTime.Now
                | other ->                                  printfn "Cluster Received event %O at %O" other DateTime.Now

                return! messageLoop()
            }
        messageLoop()

Specifically, I was under the impression that a shard-region is required within a sharded cluster system in order to send messages back and forth between actors.

As someone that is new to this paradigm, I am struggling to create a simple "hello world" type messaging program between two actors using the sharding functionality.

Any suggestions?

Community
  • 1
  • 1
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118

1 Answers1

1

If you want for your shard node to be valid host/container for sharded actors, it must be running shard region associated with that type of an actor. All messages sent to sharded actors are sent through a shardRegion reference.

In the first example snippet, you've shown that, string messages are the only valid message types probably because your consumer behavior takes a string as the only valid message type.

As you may see in spawnSharded definition, it takes 4 parameters. What matters here is the first one, which is a function that is used to resolve all of the info required by the sharding plugin in order to route the message to a valid actor/entity. This method returns a tuple where:

  • First element is an identifier of shard, target entity lives in.
  • Second element is an identifier of entity itself in scope of its shard. So, to uniquely identify entity among all other entities within a cluster, it must provide a unique shard-id/entity-id pair.
  • Third parameter is an actual message, that is going to be sent to an entity. Its type should match the type of a recursive loop function input used as your actor behavior - in case you've shown, it would probably be a plain object.

Since in the example, this message resolver function is id (identity), we send a tuple directly to shard region. You may change that function to whatever else you want to specify something custom.

PS: if you have more questions, an Akka.NET gitter channel is the place when you can find help.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
Bartosz Sypytkowski
  • 7,463
  • 19
  • 36