Exemplo n.º 1
0
        private void MemberJoined(MemberJoinedEvent msg, IContext context)
        {
            _logger.LogInformation($"Kind {_kind} Member Joined {msg.Address}");
            //TODO: right now we transfer ownership on a per actor basis.
            //this could be done in a batch
            //ownership is also racy, new nodes should maybe forward requests to neighbours (?)
            foreach (var(actorId, _) in _partition.ToArray())
            {
                var address = MemberList.GetPartition(actorId, _kind);

                if (!string.IsNullOrEmpty(address) && address != ProcessRegistry.Instance.Address)
                {
                    TransferOwnership(actorId, address, context);
                }
            }

            foreach (var(actorId, sp) in _spawningProcs)
            {
                var address = MemberList.GetPartition(actorId, _kind);

                if (!string.IsNullOrEmpty(address) && address != ProcessRegistry.Instance.Address)
                {
                    sp.TrySetResult(ActorPidResponse.Unavailable);
                }
            }
        }
Exemplo n.º 2
0
        private void MemberLeft(MemberLeftEvent msg, IContext context)
        {
            _logger.LogInformation($"Kind {_kind} Member Left {msg.Address}");
            foreach (var(actorId, pid) in _partition.ToArray())
            {
                if (pid.Address == msg.Address)
                {
                    _partition.Remove(actorId);
                    _reversePartition.Remove(pid);
                }
            }

            //If the left member is self, transfer remaining pids to others
            if (msg.Address == ProcessRegistry.Instance.Address)
            {
                //TODO: right now we transfer ownership on a per actor basis.
                //this could be done in a batch
                //ownership is also racy, new nodes should maybe forward requests to neighbours (?)
                foreach (var(actorId, _) in _partition.ToArray())
                {
                    var address = MemberList.GetPartition(actorId, _kind);

                    if (!string.IsNullOrEmpty(address))
                    {
                        TransferOwnership(actorId, address, context);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static async Task <(PID, ResponseStatusCode)> GetAsync(string name, string kind, CancellationToken ct)
        {
            //Check Cache
            if (PidCache.TryGetCache(name, out var pid))
            {
                return(pid, ResponseStatusCode.OK);
            }

            //Get Pid
            var address = MemberList.GetPartition(name, kind);

            if (string.IsNullOrEmpty(address))
            {
                return(null, ResponseStatusCode.Unavailable);
            }

            var remotePid = Partition.PartitionForKind(address, kind);
            var req       = new ActorPidRequest
            {
                Kind = kind,
                Name = name
            };

            try
            {
                var resp = ct == CancellationToken.None
                           ? await RootContext.Empty.RequestAsync <ActorPidResponse>(remotePid, req, Config.TimeoutTimespan)
                           : await RootContext.Empty.RequestAsync <ActorPidResponse>(remotePid, req, ct);

                var status = (ResponseStatusCode)resp.StatusCode;
                switch (status)
                {
                case ResponseStatusCode.OK:
                    PidCache.TryAddCache(name, resp.Pid);
                    return(resp.Pid, status);

                default:
                    return(resp.Pid, status);
                }
            }
            catch (TimeoutException)
            {
                return(null, ResponseStatusCode.Timeout);
            }
            catch
            {
                return(null, ResponseStatusCode.Error);
            }
        }
Exemplo n.º 4
0
        private void TakeOwnership(TakeOwnership msg, IContext context)
        {
            //Check again if I'm the owner
            var address = MemberList.GetPartition(msg.Name, _kind);

            if (!string.IsNullOrEmpty(address) && address != ProcessRegistry.Instance.Address)
            {
                //if not, forward to the correct owner
                var owner = Partition.PartitionForKind(address, _kind);
                owner.Tell(msg);
            }
            else
            {
                _logger.LogDebug($"Kind {_kind} Take Ownership name: {msg.Name}, pid: {msg.Pid}");
                _partition[msg.Name]       = msg.Pid;
                _reversePartition[msg.Pid] = msg.Name;
                context.Watch(msg.Pid);
            }
        }