Exemple #1
0
        public async Task <IEnumerable <MatchingActor> > GetMatchingActorsAsync(M message, IMessagePatternFactory <P> messagePatternFactory)
        {
            var replicator        = DistributedData.Get(ActorSystem).Replicator;
            GetKeysIdsResult keys = await replicator.Ask <GetKeysIdsResult>(Dsl.GetKeyIds);

            var matchingActors = new List <MatchingActor>();

            foreach (var node in keys.Keys)
            {
                var setKey      = new LWWRegisterKey <ActorProps[]>(node);
                var getResponse = await replicator.Ask <IGetResponse>(Dsl.Get(setKey, ReadLocal.Instance));

                if (!getResponse.IsSuccessful)
                {
                    throw new Exception($"cannot get message patterns for node {node}");
                }
                var actorPropsList = getResponse.Get(setKey).Value;

                foreach (var actorProps in actorPropsList)
                {
                    var path                  = actorProps.Path;
                    var matchingPatterns      = actorProps.Patterns.Where(pattern => pattern.Match(message));
                    int matchingPatternsCount = matchingPatterns.Count();
                    if (matchingPatternsCount > 0)
                    {
                        var matchingPattern = matchingPatterns.First();
                        if (matchingPatternsCount > 1)
                        {
                            Logger.Warning("For actor {0}, found {1} patterns matching message {2}. Taking first one = {3}", path, matchingPatternsCount, message, matchingPattern);
                        }
                        matchingActors.Add(
                            new MatchingActor
                        {
                            Path           = path,
                            IsSecondary    = matchingPattern.IsSecondary,
                            MatchingScore  = matchingPattern.Conjuncts.Length,
                            MistrustFactor = actorProps.MistrustFactor
                        });
                    }
                }
            }
            return(matchingActors);
        }
Exemple #2
0
        public DDataShardCoordinator(string typeName, ClusterShardingSettings settings, IShardAllocationStrategy allocationStrategy, IActorRef replicator, int majorityMinCap, bool rememberEntities)
        {
            _replicator        = replicator;
            _rememberEntities  = rememberEntities;
            Settings           = settings;
            AllocationStrategy = allocationStrategy;
            Log           = Context.GetLogger();
            Cluster       = Cluster.Get(Context.System);
            CurrentState  = PersistentShardCoordinator.State.Empty.WithRememberEntities(settings.RememberEntities);
            RemovalMargin = Cluster.DowningProvider.DownRemovalMargin;
            MinMembers    = string.IsNullOrEmpty(settings.Role)
                ? Cluster.Settings.MinNrOfMembers
                : (Cluster.Settings.MinNrOfMembersOfRole.TryGetValue(settings.Role, out var min) ? min : Cluster.Settings.MinNrOfMembers);
            RebalanceTask = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(Settings.TunningParameters.RebalanceInterval, Settings.TunningParameters.RebalanceInterval, Self, RebalanceTick.Instance, Self);

            _readConsistency     = new ReadMajority(settings.TunningParameters.WaitingForStateTimeout, majorityMinCap);
            _writeConsistency    = new WriteMajority(settings.TunningParameters.UpdatingStateTimeout, majorityMinCap);
            _coordinatorStateKey = new LWWRegisterKey <PersistentShardCoordinator.State>(typeName + "CoordinatorState");
            _allShardsKey        = new GSetKey <string>($"shard-{typeName}-all");
            _allKeys             = rememberEntities
                ? ImmutableHashSet.CreateRange(new IKey <IReplicatedData>[] { _coordinatorStateKey, _allShardsKey })
                : ImmutableHashSet.Create <IKey <IReplicatedData> >(_coordinatorStateKey);

            if (rememberEntities)
            {
                replicator.Tell(Dsl.Subscribe(_allShardsKey, Self));
            }

            Cluster.Subscribe(Self, ClusterEvent.SubscriptionInitialStateMode.InitialStateAsEvents, typeof(ClusterEvent.ClusterShuttingDown));

            // get state from ddata replicator, repeat until GetSuccess
            GetCoordinatorState();
            GetAllShards();

            Context.Become(WaitingForState(_allKeys));
        }