private void Handle(DistributedActorTableMessage <TKey> .Internal.Remove m)
        {
            IActorRef actor;

            if (_actorMap.TryGetValue(m.Id, out actor) == false)
            {
                return;
            }

            if (actor == null)
            {
                _log.Error($"Cannot remove an actor waiting for creating. (Id={m.Id})");
                return;
            }

            Container container;

            if (_containerMap.TryGetValue(Sender, out container) == false)
            {
                _log.Error($"Cannot find a container trying to remove an actor. (Id={m.Id} Container={Sender})");
                return;
            }

            if (container.ActorMap.ContainsKey(m.Id) == false)
            {
                _log.Error($"Cannot remove an actor owned by another container. (Id={m.Id} Container={Sender})");
                return;
            }

            _actorMap.Remove(m.Id);
            container.ActorMap.Remove(m.Id);
        }
Example #2
0
        private void Handle(DistributedActorTableMessage <TKey> .Internal.AddReply m)
        {
            IActorRef requester;

            if (_addingMap.TryGetValue(m.Id, out requester) == false)
            {
                // already removed locally
                return;
            }

            _addingMap.Remove(m.Id);

            if (m.Added)
            {
                requester.Tell(new DistributedActorTableMessage <TKey> .AddReply(m.Id, m.Actor, true));
            }
            else
            {
                _actorMap.Remove(m.Id);
                _actorInverseMap.Remove(m.Actor);
                Context.Unwatch(m.Actor);
                _watchingActorCount -= 1;

                requester.Tell(new DistributedActorTableMessage <TKey> .AddReply(m.Id, m.Actor, false));
            }
        }
        private void Handle(DistributedActorTableMessage <TKey> .Internal.Add m)
        {
            if (m.Actor == null)
            {
                _log.Error($"Invalid null actor for adding. (Id={m.Id})");
                Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.AddReply(m.Id, m.Actor, false));
                return;
            }

            Container container;

            if (_containerMap.TryGetValue(Sender, out container) == false)
            {
                _log.Error($"Cannot find a container trying to add an actor. (Id={m.Id} Container={Sender})");
                return;
            }

            try
            {
                _actorMap.Add(m.Id, m.Actor);
                container.ActorMap.Add(m.Id, m.Actor);
            }
            catch (Exception)
            {
                Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.AddReply(m.Id, m.Actor, false));
            }

            Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.AddReply(m.Id, m.Actor, true));
        }
        private void Handle(DistributedActorTableMessage <TKey> .GetOrCreate m)
        {
            if (_stopping)
            {
                return;
            }

            var id = m.Id;

            // try to get actor

            IActorRef actor;

            if (_actorMap.TryGetValue(id, out actor))
            {
                if (actor != null)
                {
                    Sender.Tell(new DistributedActorTableMessage <TKey> .GetOrCreateReply(m.Id, actor, false));
                    return;
                }
                else
                {
                    PutOnCreateWaitingList(RequestType.GetOrCreate, id, Sender);
                    return;
                }
            }

            CreateActor(RequestType.GetOrCreate, id, m.Args);
        }
Example #5
0
        private void Handle(DistributedActorTableMessage <TKey> .Internal.Create m)
        {
            if (_table == null || _stopping)
            {
                return;
            }

            if (_actorFactory == null)
            {
                _log.Error("I don't have ActorFactory.");
                Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.CreateReply(m.Id, null));
                return;
            }

            IActorRef actor;

            try
            {
                actor = _actorFactory.CreateActor(Context, m.Id, m.Args);
            }
            catch (Exception e)
            {
                _log.Error(e, $"Exception in creating actor (Id={m.Id})");
                Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.CreateReply(m.Id, null));
                return;
            }

            _actorMap.Add(m.Id, actor);
            _actorInverseMap.Add(actor, m.Id);
            Context.Watch(actor);
            _watchingActorCount += 1;

            Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.CreateReply(m.Id, actor));
        }
Example #6
0
        private void Handle(DistributedActorTableMessage <TKey> .Add m)
        {
            if (_table == null || _stopping)
            {
                Sender.Tell(new DistributedActorTableMessage <TKey> .AddReply(m.Id, m.Actor, false));
                return;
            }

            if (m.Actor == null)
            {
                _log.Error($"Invalid null actor. (ID={m.Id})");
                Sender.Tell(new DistributedActorTableMessage <TKey> .AddReply(m.Id, m.Actor, false));
                return;
            }

            if (_actorMap.ContainsKey(m.Id))
            {
                _log.Error($"Duplicate ID in local container. (ID={m.Id})");
                Sender.Tell(new DistributedActorTableMessage <TKey> .AddReply(m.Id, m.Actor, false));
                return;
            }

            _actorMap.Add(m.Id, m.Actor);
            _actorInverseMap.Add(m.Actor, m.Id);
            _addingMap.Add(m.Id, Sender);
            Context.Watch(m.Actor);
            _watchingActorCount += 1;

            _table.Tell(new DistributedActorTableMessage <TKey> .Internal.Add(m.Id, m.Actor));
        }
Example #7
0
        private void Handle(DistributedActorTableMessage <TKey> .Remove m)
        {
            IActorRef actor;

            if (_actorMap.TryGetValue(m.Id, out actor) == false)
            {
                _log.Error($"Cannot remove an actor that doesn't exist. (Id={m.Id} Sender={Sender})");
                return;
            }

            _actorMap.Remove(m.Id);
            _actorInverseMap.Remove(actor);
            Context.Unwatch(actor);
            _watchingActorCount -= 1;

            _table?.Tell(new DistributedActorTableMessage <TKey> .Internal.Remove(m.Id));
        }
        private void Handle(DistributedActorTableMessage <TKey> .Internal.CreateReply m)
        {
            var id = m.Id;

            IActorRef actor;

            if (_actorMap.TryGetValue(id, out actor) == false)
            {
                // request was already expired so ask to remove it
                _log.Info($"I got CreateReply but might be timeouted. " + $"(Id={id})");
                Sender.Tell(new DistributedActorTableMessage <TKey> .Internal.Remove(id));
                return;
            }

            if (actor != null)
            {
                _log.Error($"I got CreateReply but already have an actor. " + $"(Id={id} Actor={actor} ArrivedActor={m.Actor})");
                return;
            }

            Creating creating;

            if (_creatingMap.TryGetValue(id, out creating) == false)
            {
                _log.Error($"I got CreateReply but I don't have a creating. " + $"(Id={id} Actor={actor})");
                return;
            }

            // update created actor in map

            _creatingMap.Remove(id);
            _actorMap[id] = m.Actor;
            _containerMap[creating.WorkingContainer].ActorMap[id] = m.Actor;

            // send reply to requesters

            for (var i = 0; i < creating.Requesters.Count; i++)
            {
                var requester = creating.Requesters[i];
                requester.Item1.Tell(CreateReplyMessage(requester.Item2, id, m.Actor, i == 0));
            }
        }
        private void Handle(DistributedActorTableMessage <TKey> .GracefulStop m)
        {
            if (_stopping)
            {
                return;
            }

            _stopping = true;

            if (_containerMap.Count > 0)
            {
                foreach (var i in _containerMap)
                {
                    i.Key.Tell(new DistributedActorTableMessage <TKey> .Internal.GracefulStop(m.StopMessage));
                }
            }
            else
            {
                Context.Stop(Self);
            }
        }
        private void Handle(DistributedActorTableMessage <TKey> .Internal.GracefulStop m)
        {
            if (_stopping)
            {
                return;
            }

            _stopping = true;

            if (_actorMap.Count > 0)
            {
                foreach (var i in _actorMap)
                {
                    i.Value.Tell(m.StopMessage ?? PoisonPill.Instance);
                }
            }
            else
            {
                Context.Stop(Self);
            }
        }
        private void Handle(DistributedActorTableMessage <TKey> .Create m)
        {
            if (_stopping)
            {
                return;
            }

            // decide ID (if provided, use it, otherwise generate new one)

            TKey id;

            if (m.Id.Equals(default(TKey)) == false)
            {
                if (_actorMap.ContainsKey(m.Id))
                {
                    Sender.Tell(new DistributedActorTableMessage <TKey> .CreateReply(m.Id, null));
                    return;
                }
                id = m.Id;
            }
            else
            {
                if (_idGenerator == null)
                {
                    _log.Error("I don't have ID Generator.");
                    Sender.Tell(new DistributedActorTableMessage <TKey> .CreateReply(m.Id, null));
                    return;
                }

                id = _idGenerator.GenerateId();
                if (_actorMap.ContainsKey(id))
                {
                    _log.Error($"ID generated by generator is duplicated. ID={id}, Actor={_actorMap[id]}");
                    Sender.Tell(new DistributedActorTableMessage <TKey> .CreateReply(m.Id, null));
                    return;
                }
            }

            CreateActor(RequestType.Create, id, m.Args);
        }
 private void Handle(DistributedActorTableMessage <TKey> .GetIds m)
 {
     Sender.Tell(new DistributedActorTableMessage <TKey> .GetIdsReply(_actorMap.Keys.ToArray()));
 }