MCPcopy Create free account
hub / github.com/Tencent/phxqueue / MakeHandleBuckets

Method MakeHandleBuckets

phxqueue/consumer/consumer.cpp:171–232  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169}
170
171comm::RetCode Consumer::MakeHandleBuckets() {
172 comm::ConsumerConsumeBP::GetThreadInstance()->OnMakeHandleBucket(impl_->cc);
173
174 for (int i{0}; i < impl_->opt.nhandler; ++i) {
175 while(!impl_->handle_buckets[i].empty())impl_->handle_buckets[i].pop();
176 }
177
178 comm::RetCode ret;
179
180 shared_ptr<const config::TopicConfig> topic_config;
181 if (comm::RetCode::RET_OK != (ret = config::GlobalConfig::GetThreadInstance()->GetTopicConfigByTopicID(impl_->topic_id, topic_config))) {
182 QLErr("GetTopicConfigByTopicID ret %d topic_id %d", comm::as_integer(ret), impl_->topic_id);
183 return ret;
184 }
185
186 shared_ptr<const config::proto::QueueInfo> queue_info;
187 if (comm::RetCode::RET_OK != (ret = topic_config->GetQueueInfoByQueue(impl_->cc.queue_id(), queue_info))) {
188 QLErr("GetQueueInfoByQueue ret %d queue_id %d", comm::as_integer(ret), impl_->cc.queue_id());
189 return ret;
190 }
191
192 int nbucket_used{0};
193 map<uint64_t, int> uin2bucket_idx;
194 int max_sz = -1;
195 int max_handle_id = -1;
196 uint64_t max_key = -1;
197 for (int i{0}; i < impl_->items.size(); ++i) {
198 auto key = impl_->items[i]->meta().uin();
199 auto handle_id = impl_->items[i]->meta().handle_id();
200
201 int bucket_idx = -1;
202 bool update_uin2bucket_idx = false;
203 if (key && !queue_info->handle_by_random_uin()) {
204 auto &&it = uin2bucket_idx.find(key);
205 if (uin2bucket_idx.end() != it) {
206 bucket_idx = it->second;
207 } else {
208 update_uin2bucket_idx = true;
209 }
210 }
211 if (-1 == bucket_idx) {
212 if (nbucket_used >= impl_->opt.nhandler) {
213 bucket_idx = comm::utils::OtherUtils::FastRand() % impl_->opt.nhandler;
214 } else {
215 bucket_idx = nbucket_used++;
216 }
217 }
218 if (update_uin2bucket_idx) uin2bucket_idx[key] = bucket_idx;
219
220 impl_->handle_buckets[bucket_idx].push(i);
221 auto sz = impl_->handle_buckets[bucket_idx].size();
222 if (-1 == max_sz || sz > max_sz) {
223 max_sz = sz;
224 max_handle_id = handle_id;
225 max_key = key;
226 }
227 }
228

Callers

nothing calls this directly

Calls 4

as_integerFunction · 0.85
OnMakeHandleBucketMethod · 0.80
GetQueueInfoByQueueMethod · 0.80

Tested by

no test coverage detected