cpp kafka源码阅读:buffered_producer之AckMonitor

        ssize_t get_current_thread_pending_acks_impl() const {
            auto it  = last_ack_.find(std::this_thread::get_id());
            if (it != last_ack_.end()) {
                return (it->second > received_acks_) ? it->second - received_acks_ : 0;
            }
            return 0;
        }

AckMonitor的该方法判断某线程的消息是否都有ACK,只是简单比较了已发送消息计数sent_acks_的最大值。

而这个计数的增加也有意思,仅仅让每个线程记录sent_acks_的最大值。

        // Increments the number of sent acks
        void increment_pending_acks() {
            while (!flag_.test_and_set()) {
                //save the last ack number for this thread so we only
                //wait up to this number.
                last_ack_[std::this_thread::get_id()] = ++sent_acks_;
                flag_.clear();
                break;
            }
        }

这样的实现是很精巧。但个人认为,如果各个线程的返回ACK不是按顺序,则有可能误判。

比如按时序:

1:某个线程的last_acks_[tid1] = 100, 但是received_acks_=99

2:之后某个时刻sent_acks=101, last_acks_[tid2]=101, 此时last_acks_[tid1] = 100不变。

3:tid2收到了应答,received_acks_=100。此时检查tid1的get_current_thread_pending_acks_impl会返回true。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值