Exemple #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);
        }
        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]);
        }
Exemple #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 count = groups.Count;

            return(groups[Math.Abs(Interlocked.Increment(ref index) % count)]);
        }