public static Unit Startup(Option <ICluster> cluster) { if (started) { return(unit); } ActorContext.cluster = cluster; var name = GetRootProcessName(); if (name.Value == "root" && cluster.IsSome) { throw new ArgumentException("Cluster node name cannot be 'root', it's reserved for local use only."); } if (name.Value == "disp" && cluster.IsSome) { throw new ArgumentException("Cluster node name cannot be 'disp', it's reserved for internal use."); } if (name.Value == "js") { throw new ArgumentException("Node name cannot be 'js', it's reserved for ProcessJS."); } lock (sync) { if (started) { return(unit); } startupTimestamp = DateTime.UtcNow.Ticks; startupSubscription?.Dispose(); startupSubscription = NotifyCluster(cluster, startupTimestamp); Dispatch.init(); Role.init(cluster.Map(r => r.Role).IfNone("local")); Reg.init(); watchers = Map.empty <ProcessId, Set <ProcessId> >(); watchings = Map.empty <ProcessId, Set <ProcessId> >(); var root = ProcessId.Top.Child(name); var rootInbox = new ActorInboxLocal <ActorSystemState, Unit>(); var parent = new ActorItem(new NullProcess(), new NullInbox(), ProcessFlags.Default); var go = new AutoResetEvent(false); var state = new ActorSystemState( cluster, root, null, rootInbox, cluster.Map(x => x.NodeName).IfNone(ActorSystemConfig.Default.RootProcessName), ActorSystemConfig.Default ); var rootProcess = state.RootProcess; state.Startup(); userContext = new ActorRequestContext( rootProcess.Children["user"], ProcessId.NoSender, rootItem, null, null, ProcessFlags.Default, ProcessOpTransaction.Start(rootProcess.Children["user"].Actor.Id) ); rootInbox.Startup(rootProcess, parent, cluster, ProcessConfig.Settings.GetProcessMailboxSize(rootProcess.Id)); rootItem = new ActorItem(rootProcess, rootInbox, ProcessFlags.Default); started = true; SessionManager.Init(cluster); ClusterWatch(cluster); } return(unit); }
/// <summary> /// Static ctor /// Sets up the default roles /// </summary> static Role() { ProcessName first = "role-first"; ProcessName second = "role-second"; ProcessName third = "role-third"; ProcessName last = "role-last"; ProcessName next = "role-next"; ProcessName prev = "role-prev"; ProcessName broadcast = "role-broadcast"; ProcessName leastBusy = "role-least-busy"; ProcessName random = "role-random"; ProcessName roundRobin = "role-round-robin"; var nextNode = fun((bool fwd) => fun((ProcessId leaf) => { var self = leaf.Take(1).GetName(); var isNext = false; var nodeMap = Nodes(leaf); var nodes = fwd ? nodeMap.Values.Append(nodeMap.Values) : nodeMap.Values.Append(nodeMap.Values).Reverse(); //< TODO: Inefficient foreach (var node in nodes) { if (isNext) { return(new[] { ProcessId.Top[node.NodeName].Append(leaf.Skip(1)) }.AsEnumerable()); } if (node.NodeName == self) { isNext = true; } } return(new ProcessId[0].AsEnumerable()); })); // Next nextRoot = Dispatch.register(next, nextNode(true)); // Prev prevRoot = Dispatch.register(prev, nextNode(false)); // First First = Dispatch.register(first, leaf => NodeIds(leaf).Take(1)); // Second Second = Dispatch.register(second, leaf => NodeIds(leaf).Skip(1).Take(1)); // Third Third = Dispatch.register(third, leaf => NodeIds(leaf).Skip(2).Take(1)); // Last Last = Dispatch.register(last, leaf => NodeIds(leaf).Reverse().Take(1)); // Broadcast Broadcast = Dispatch.register(broadcast, NodeIds); // Least busy LeastBusy = Dispatch.register(leastBusy, leaf => NodeIds(leaf) .Map(pid => Tuple(inboxCount(pid), pid)) .OrderBy(tup => tup.Item1) .Map(tup => tup.Item2) .Take(1)); // Random Random = Dispatch.register(random, leaf => { var workers = NodeIds(leaf).ToArray(); return(new ProcessId[1] { workers[Prelude.random(workers.Length)] }); }); // Round-robin object sync = new object(); Map <string, int> roundRobinState = Map.empty <string, int>(); RoundRobin = Dispatch.register(roundRobin, leaf => { var key = leaf.ToString(); var workers = NodeIds(leaf).ToArray(); int index = 0; lock (sync) { roundRobinState = roundRobinState.AddOrUpdate(key, x => { index = x % workers.Length; return(x + 1); }, 0); } return(new ProcessId[1] { workers[index] }); }); }
public ActorSystem(SystemName systemName, Option <ICluster> cluster, AppProfile appProfile, ProcessSystemConfig settings) { var name = GetRootProcessName(cluster); if (name.Value == "root" && cluster.IsSome) { throw new ArgumentException("Cluster node name cannot be 'root', it's reserved for local use only."); } if (name.Value == "disp" && cluster.IsSome) { throw new ArgumentException("Cluster node name cannot be 'disp', it's reserved for internal use."); } if (name.Value == "js") { throw new ArgumentException("Node name cannot be 'js', it's reserved for ProcessJS."); } SystemName = systemName; this.appProfile = appProfile; this.settings = settings; this.cluster = cluster; startupTimestamp = DateTime.UtcNow.Ticks; sessionManager = new SessionManager(cluster, SystemName, appProfile.NodeName, VectorConflictStrategy.Branch); watchers = Map.empty <ProcessId, Set <ProcessId> >(); watchings = Map.empty <ProcessId, Set <ProcessId> >(); startupSubscription = NotifyCluster(cluster, startupTimestamp); Dispatch.init(); Role.init(cluster.Map(r => r.Role).IfNone("local")); Reg.init(); var root = ProcessId.Top.Child(GetRootProcessName(cluster)); var rootInbox = new ActorInboxLocal <ActorSystemBootstrap, Unit>(); var parent = new ActorItem(new NullProcess(SystemName), new NullInbox(), ProcessFlags.Default); var state = new ActorSystemBootstrap( this, cluster, root, null, rootInbox, cluster.Map(x => x.NodeName).IfNone(ActorSystemConfig.Default.RootProcessName), ActorSystemConfig.Default, Settings, sessionManager.Sync ); var rootProcess = state.RootProcess; state.Startup(); rootItem = new ActorItem(rootProcess, rootInbox, ProcessFlags.Default); userContext = new ActorRequestContext( this, rootProcess.Children["user"], ProcessId.NoSender, rootItem, null, null, ProcessFlags.Default, null, null); rootInbox.Startup(rootProcess, parent, cluster, settings.GetProcessMailboxSize(rootProcess.Id)); }