akka.net icon indicating copy to clipboard operation
akka.net copied to clipboard

Akka.Cluster.Singleton: should singleton creation match what we do with `ShardRegion`s?

Open Aaronontheweb opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

In Akka.Cluster.Sharding, if you are creating a ShardRegion that can only be hosted on nodes with role foo from a node that does not have a role of foo, you get a ShardRegionProxy back in exchange.

Akka.Cluster.Tools could work the same way with SingletonManagers and SingletonProxys, potentially. I think it may not be 100% feasible without creating some type of ClusterSingleton extension for the ActorSystem to handle this, similarly to how ShardRegions are handled. But would this make the creation of singletons easier and less error-prone for end users?

Worth noting, this is exactly how we do it inside Akka.Hosting right now - although we always start a singleton + proxy at the same time on each node anyway:

https://github.com/akkadotnet/Akka.Hosting/blob/db3d82780fc29445cbfbf826e029012bfb314057/src/Akka.Cluster.Hosting/AkkaClusterHostingExtensions.cs#L319-L356

Describe alternatives you've considered

Maybe just sticking with Akka.Cluster.Hosting for this.

Aaronontheweb avatar Jul 08 '22 15:07 Aaronontheweb

I have tried this with Akka.Hosting, but I still get an exception if I try to start a singleton on a node that doesn't have the matching role.

[12:10:26 ERR] Error while creating actor instance of type Akka.Cluster.Tools.Singleton.ClusterSingletonManager with 3 args: (Akka.Actor.Props,<PoisonPill>,Akka.Cluster.Tools.Singleton.ClusterSingletonManagerSettings)
[akka://akkacluster/user/counter-job#982673914]: Akka.Actor.ActorInitializationException: Exception during creation
 ---> System.TypeLoadException: Error while creating actor instance of type Akka.Cluster.Tools.Singleton.ClusterSingletonManager with 3 args: (Akka.Actor.Props,<PoisonPill>,Akka.Cluster.Tools.Singleton.ClusterSingletonManagerSettings)
 ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentException: This cluster member [akka.tcp://[email protected]:5055] doesn't have the role [counter-job-host]
   at Akka.Cluster.Tools.Singleton.ClusterSingletonManager..ctor(Props singletonProps, Object terminationMessage, ClusterSingletonManagerSettings settings)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Akka.Actor.Props.ActivatorProducer.Produce()
   at Akka.Actor.Props.NewActor()
   --- End of inner exception stack trace ---
   at Akka.Actor.Props.NewActor()
   at Phobos.Actor.PhobosActorCell.CreateNewActorInstance()
   at Akka.Actor.ActorCell.<>c__DisplayClass116_0.<NewActor>b__0()
   at Akka.Actor.ActorCell.UseThreadContext(Action action)
   at Akka.Actor.ActorCell.NewActor()
   at Akka.Actor.ActorCell.Create(Exception failure)
   --- End of inner exception stack trace ---
   at Akka.Actor.ActorCell.Create(Exception failure)
   at Akka.Actor.ActorCell.SysMsgInvokeAll(EarliestFirstSystemMessageList messages, Int32 currentState)

jblackburn21 avatar Jul 08 '22 17:07 jblackburn21

Guess we don't really do that handling on Akka.Hosting today then, where the role types differ. So would it make sense to create an ClusterSingleton extension that can handle this business logic?

Aaronontheweb avatar Jul 08 '22 17:07 Aaronontheweb