Exemplo n.º 1
0
        private async Task RebalanceClusteringAsync(KeyValuePair <string, HashSet <string> > pair)
        {
            var topic = pair.Key;

            try
            {
                var consumerIdList = await GetConsumerIdsForTopic(topic);

                if (consumerIdList == null || consumerIdList.Count == 0)
                {
                    _logger.WarnFormat("No available consumers found.");
                    UpdatePullRequestDict(pair, new List <MessageQueue>());
                    return;
                }
                var messageQueueList = await _clientService.GetTopicMessageQueuesAsync(topic);

                if (messageQueueList == null || messageQueueList.Count == 0)
                {
                    _logger.WarnFormat("No available message queues found, topic: {0}", topic);
                    UpdatePullRequestDict(pair, new List <MessageQueue>());
                    return;
                }
                var allocatedMessageQueueList = _allocateMessageQueueStragegy.Allocate(_clientId, messageQueueList, consumerIdList).ToList();
                UpdatePullRequestDict(pair, allocatedMessageQueueList);
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("RebalanceClustering has exception, consumerGroup: {0}, consumerId: {1}, topic: {2}", _consumer.GroupName, _clientId, topic), ex);
                UpdatePullRequestDict(pair, new List <MessageQueue>());
            }
        }
Exemplo n.º 2
0
        private void RebalanceClustering(string subscriptionTopic)
        {
            List <string> consumerIdList;

            try
            {
                consumerIdList = QueryGroupConsumers(subscriptionTopic).ToList();
                if (_consumerIds.Count != consumerIdList.Count)
                {
                    _logger.DebugFormat("ConsumerIds changed, current consumerId:{0}, group:{1}, old:{2}, new:{3}", Id, GroupName, string.Join(",", _consumerIds), string.Join(",", consumerIdList));
                    _consumerIds = consumerIdList;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("RebalanceClustering failed as QueryGroupConsumers has exception, consumerId:{0}, group:{1}, topic:{2}", Id, GroupName, subscriptionTopic), ex);
                return;
            }

            consumerIdList.Sort();

            IList <MessageQueue> messageQueues;

            if (_topicQueuesDict.TryGetValue(subscriptionTopic, out messageQueues))
            {
                var messageQueueList = messageQueues.ToList();
                messageQueueList.Sort((x, y) =>
                {
                    if (x.QueueId > y.QueueId)
                    {
                        return(1);
                    }
                    else if (x.QueueId < y.QueueId)
                    {
                        return(-1);
                    }
                    return(0);
                });

                IEnumerable <MessageQueue> allocatedMessageQueues = new List <MessageQueue>();
                try
                {
                    allocatedMessageQueues = _allocateMessageQueueStragegy.Allocate(Id, messageQueueList, consumerIdList);
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Allocate message queue has exception, consumerId:{0}, group:{1}, topic:{2}", Id, GroupName, subscriptionTopic), ex);
                    return;
                }

                UpdatePullRequestDict(subscriptionTopic, allocatedMessageQueues.ToList());
            }
        }