Example #1
0
        private Tuple <BrokerGroup, NewQmqClient> SelectWritableClient()
        {
            var cluster = _isDelayMessage ? _brokerGroupService.DelayProducerGetSubjectCluster(_subject) : _brokerGroupService.ProducerGetSubjectCluster(_subject);

            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }

            var brokerGroups = DictValueFilter.Filter(cluster.BrokerGroups, BrokerGroup.IsWritable);

            if (brokerGroups.Count == 0)
            {
                return(null);
            }

            // First of all, random select a client.
            // If random selected client is not writable, then select first writable client.
            var randBrokerGroup = brokerGroups.ElementAt(StaticRandom.NextRand(brokerGroups.Count));
            var randClient      = ClientManager.GetOrCreate(randBrokerGroup);

            if (randClient.Writtable)
            {
                return(new Tuple <BrokerGroup, NewQmqClient>(randBrokerGroup, randClient));
            }
            var client = brokerGroups.Select(brokerGroup => new Tuple <BrokerGroup, NewQmqClient>(brokerGroup, ClientManager.GetOrCreate(brokerGroup)))
                         .FirstOrDefault(tuple => tuple.Item2.Writtable);

            return(client);
        }
        private NewQmqClient SelectWritableClient(string subject, bool forceRefreshBrokerGroup, bool isDelayMessage)
        {
            var cluster = isDelayMessage ? _brokerGroupService.DelayProducerGetSubjectCluster(subject, forceRefreshBrokerGroup) : _brokerGroupService.ProducerGetSubjectCluster(subject, forceRefreshBrokerGroup);

            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }

            var brokerGroups = cluster.BrokerGroups.Values.Where(BrokerGroup.IsWritable).ToList();

            if (brokerGroups.Count == 0)
            {
                return(null);
            }

            // First of all, random select a client.
            // If random selected client is not writable, then select first writable client.
            var randBrokerGroup = brokerGroups.ElementAt(StaticRandom.NextRand(brokerGroups.Count));
            var randClient      = _clientManager.GetOrCreate(randBrokerGroup);

            if (randClient.Writtable)
            {
                return(randClient);
            }
            return(brokerGroups.Select(brokerGroup => _clientManager.GetOrCreate(brokerGroup))
                   .FirstOrDefault(client => client.Writtable));
        }
Example #3
0
        public BrokerGroup Select(NewQmqClusterInfo cluster)
        {
            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }
            var groups = DictValueFilter.Filter(cluster.BrokerGroups, BrokerGroup.IsReadable);

            if (groups == null || groups.Count == 0)
            {
                return(null);
            }

            var totalWeight = 0;
            var sameWeight  = true;
            var lastWeight  = -1;

            foreach (var group in groups)
            {
                if (!weights.TryGetValue(group.Name, out int weight))
                {
                    weights.TryAdd(group.Name, DEFAULT_WEIGHT);
                    weight = DEFAULT_WEIGHT;
                }

                if (lastWeight != -1 && lastWeight != weight)
                {
                    sameWeight = false;
                }
                lastWeight   = weight;
                totalWeight += weight;
            }

            if (totalWeight == 0)
            {
                return(null);
            }

            if (totalWeight > 0 && !sameWeight)
            {
                int offset = StaticRandom.NextRand(totalWeight);
                foreach (var group in groups)
                {
                    weights.TryGetValue(group.Name, out int weight);
                    offset -= weight;
                    if (offset <= 0)
                    {
                        return(group);
                    }
                }
            }

            var index = StaticRandom.NextRand(groups.Count);

            return(groups[index]);
        }
Example #4
0
 private int RandomWeightThreshold()
 {
     return(StaticRandom.NextRand(0, MaxWeight));
 }