This is an automated email from the ASF dual-hosted git repository. aaronai pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
commit d957e524325ac8164ed3b25e100ee099a526d33f Author: colprog <[email protected]> AuthorDate: Fri Dec 23 02:02:28 2022 +0800 Fix SimpleConsumer with multiple topics loadbalancing code in SimpleConsumer is faulty. with two topics and the same number of queues. because topic index and queue index is incremented simutaneusly, some queue will never be polled. Also implemented a simple delay if NextQueue returns null to avoid potential starvation --- csharp/rocketmq-client-csharp/SimpleConsumer.cs | 31 +++++-------- .../SubscriptionLoadBalancer.cs | 52 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/csharp/rocketmq-client-csharp/SimpleConsumer.cs b/csharp/rocketmq-client-csharp/SimpleConsumer.cs index efe38211..5dd3a03b 100644 --- a/csharp/rocketmq-client-csharp/SimpleConsumer.cs +++ b/csharp/rocketmq-client-csharp/SimpleConsumer.cs @@ -33,8 +33,8 @@ namespace Org.Apache.Rocketmq public SimpleConsumer(string accessUrl, string group) : base(accessUrl) { - _subscriptions = new ConcurrentDictionary<string, rmq.SubscriptionEntry>(); - _topicAssignments = new ConcurrentDictionary<string, List<rmq.Assignment>>(); + _subscriptions = new(); + _topicAssignments = new(); _group = group; } @@ -133,16 +133,16 @@ namespace Org.Apache.Rocketmq var i = 0; foreach (var assignments in list) { - string topic = topics[i]; + string topic = topics[i++]; if (null == assignments || 0 == assignments.Count) { Logger.Warn($"Faild to acquire assignments. Topic={topic}, Group={_group}"); - ++i; continue; } + Logger.Debug($"Assignments received. Topic={topic}, Group={_group}"); - _topicAssignments.AddOrUpdate(topic, assignments, (t, prev) => assignments); - ++i; + var newSubscriptionLB = new SubscriptionLoadBalancer(assignments); + _topicAssignments.AddOrUpdate(topic, newSubscriptionLB, (t, prev) => prev.Update(assignments)); } } @@ -201,8 +201,7 @@ namespace Org.Apache.Rocketmq var messageQueue = NextQueue(); if (null == messageQueue) { - Logger.Debug("NextQueue returned null"); - return new List<Message>(); + throw new TopicRouteException("No topic to receive message from"); } var request = new rmq.ReceiveMessageRequest @@ -299,31 +298,23 @@ namespace Org.Apache.Rocketmq var total = _topicAssignments.Count; var topicIndex = topicSeq % total; var topic = _topicAssignments.Keys.Skip((int)topicIndex).First(); - - UInt32 queueSeq = _currentQueueSequence.Value; - _currentQueueSequence.Value = queueSeq + 1; - if (!_topicAssignments.TryGetValue(topic, out var assignments)) + + if (!_topicAssignments.TryGetValue(topic, out var subscriptionLB)) { return null; } - var idx = queueSeq % assignments?.Count; - return assignments?[(int)idx].MessageQueue; + return subscriptionLB.TakeMessageQueue(); } private readonly ThreadLocal<UInt32> _currentTopicSequence = new ThreadLocal<UInt32>(true) { Value = 0 }; - - private readonly ThreadLocal<UInt32> _currentQueueSequence = new ThreadLocal<UInt32>(true) - { - Value = 0 - }; private readonly string _group; private readonly ConcurrentDictionary<string, rmq::SubscriptionEntry> _subscriptions; - private readonly ConcurrentDictionary<string, List<rmq.Assignment>> _topicAssignments; + private readonly ConcurrentDictionary<string, SubscriptionLoadBalancer> _topicAssignments; private readonly CancellationTokenSource _scanAssignmentCts = new CancellationTokenSource(); } } \ No newline at end of file diff --git a/csharp/rocketmq-client-csharp/SubscriptionLoadBalancer.cs b/csharp/rocketmq-client-csharp/SubscriptionLoadBalancer.cs new file mode 100644 index 00000000..cf803377 --- /dev/null +++ b/csharp/rocketmq-client-csharp/SubscriptionLoadBalancer.cs @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Threading; +using rmq = Apache.Rocketmq.V2; + +namespace Org.Apache.Rocketmq +{ + internal sealed class SubscriptionLoadBalancer + { + public List<rmq.Assignment> Assignments { get; private set; } + private uint index = 0; + + public SubscriptionLoadBalancer(List<rmq.Assignment> assignments) + { + Assignments = assignments; + } + + private SubscriptionLoadBalancer(uint oldIndex, List<rmq.Assignment> assignments) + { + index = oldIndex; + Assignments = assignments; + } + + public SubscriptionLoadBalancer Update(List<rmq.Assignment> newAssignments) + { + return new SubscriptionLoadBalancer(index, newAssignments); + } + + public rmq.MessageQueue TakeMessageQueue() + { + var i = Interlocked.Increment(ref index); + return Assignments[(int)(i % Assignments.Count)].MessageQueue; + } + } +}
