|
| 1 | +#include "kafka/addons/UnorderedOffsetCommitQueue.h" |
| 2 | + |
| 3 | +#include "gtest/gtest.h" |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | +#include <chrono> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +namespace Kafka = KAFKA_API; |
| 10 | + |
| 11 | +TEST(UnorderedOffsetCommitQueue, Functionality) |
| 12 | +{ |
| 13 | + Kafka::UnorderedOffsetCommitQueue queue; |
| 14 | + |
| 15 | + // Suppose consumer received some records with a sigle `poll`, and forwarded them to several handlers |
| 16 | + queue.waitOffset(1); |
| 17 | + queue.waitOffset(2); |
| 18 | + queue.waitOffset(3); |
| 19 | + queue.waitOffset(4); |
| 20 | + queue.waitOffset(5); |
| 21 | + queue.waitOffset(6); |
| 22 | + queue.waitOffset(7); |
| 23 | + queue.waitOffset(8); |
| 24 | + queue.waitOffset(9); |
| 25 | + |
| 26 | + // Suppose these handlers would ack these offsets occasionaly |
| 27 | + // And we'll check whether we could get the right offset to commit |
| 28 | + queue.ackOffset(3); |
| 29 | + EXPECT_FALSE(queue.popOffsetToCommit()); |
| 30 | + EXPECT_FALSE(queue.lastPoppedOffset()); |
| 31 | + |
| 32 | + queue.ackOffset(2); |
| 33 | + EXPECT_FALSE(queue.popOffsetToCommit()); |
| 34 | + EXPECT_FALSE(queue.lastPoppedOffset()); |
| 35 | + |
| 36 | + queue.ackOffset(5); |
| 37 | + EXPECT_FALSE(queue.popOffsetToCommit()); |
| 38 | + EXPECT_FALSE(queue.lastPoppedOffset()); |
| 39 | + |
| 40 | + queue.ackOffset(1); |
| 41 | + auto offset = queue.popOffsetToCommit(); |
| 42 | + EXPECT_EQ(*offset, queue.lastPoppedOffset()); |
| 43 | + EXPECT_EQ(3 + 1, *offset); |
| 44 | + |
| 45 | + // No new offset to commit |
| 46 | + offset = queue.popOffsetToCommit(); |
| 47 | + EXPECT_FALSE(offset); |
| 48 | + |
| 49 | + queue.ackOffset(4); |
| 50 | + offset = queue.popOffsetToCommit(); |
| 51 | + EXPECT_EQ(5 + 1, *offset); |
| 52 | + |
| 53 | + queue.ackOffset(7); |
| 54 | + offset = queue.popOffsetToCommit(); |
| 55 | + EXPECT_FALSE(offset); |
| 56 | + |
| 57 | + queue.ackOffset(6); |
| 58 | + offset = queue.popOffsetToCommit(); |
| 59 | + EXPECT_EQ(*offset, *queue.lastPoppedOffset()); |
| 60 | + EXPECT_EQ(7 + 1, *offset); |
| 61 | + |
| 62 | + queue.ackOffset(8); |
| 63 | + offset = queue.popOffsetToCommit(); |
| 64 | + EXPECT_EQ(8 + 1, *offset); |
| 65 | + |
| 66 | + queue.ackOffset(9); |
| 67 | + offset = queue.popOffsetToCommit(); |
| 68 | + EXPECT_EQ(9 + 1, *offset); |
| 69 | + |
| 70 | + // No more records to commit |
| 71 | + offset = queue.popOffsetToCommit(); |
| 72 | + EXPECT_FALSE(offset); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(UnorderedOffsetCommitQueue, AbnormalCases) |
| 76 | +{ |
| 77 | + Kafka::UnorderedOffsetCommitQueue queue("some-topic", 2); |
| 78 | + |
| 79 | + queue.waitOffset(1); |
| 80 | + queue.waitOffset(2); |
| 81 | + // duplicated offset |
| 82 | + queue.waitOffset(2); |
| 83 | + // invalid offset |
| 84 | + queue.waitOffset(-1); |
| 85 | + queue.waitOffset(3); |
| 86 | + queue.waitOffset(4); |
| 87 | + queue.waitOffset(5); |
| 88 | + |
| 89 | + queue.ackOffset(3); |
| 90 | + auto offset = queue.popOffsetToCommit(); |
| 91 | + EXPECT_FALSE(offset); |
| 92 | + |
| 93 | + queue.ackOffset(2); |
| 94 | + offset = queue.popOffsetToCommit(); |
| 95 | + EXPECT_FALSE(offset); |
| 96 | + |
| 97 | + queue.ackOffset(1); |
| 98 | + offset = queue.popOffsetToCommit(); |
| 99 | + EXPECT_EQ(3 + 1, *offset); |
| 100 | + |
| 101 | + // ack an offset even smaller than expected |
| 102 | + queue.ackOffset(2); |
| 103 | + offset = queue.popOffsetToCommit(); |
| 104 | + EXPECT_FALSE(offset); |
| 105 | + |
| 106 | + // ack an offset even smaller than expected |
| 107 | + queue.ackOffset(6); |
| 108 | + offset = queue.popOffsetToCommit(); |
| 109 | + EXPECT_FALSE(offset); |
| 110 | + |
| 111 | + queue.ackOffset(4); |
| 112 | + offset = queue.popOffsetToCommit(); |
| 113 | + EXPECT_EQ(4 + 1, *offset); |
| 114 | + |
| 115 | + // Now only 1 offset left un-popped |
| 116 | + EXPECT_EQ(1, queue.size()); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +namespace { |
| 121 | + |
| 122 | +auto checkTimeMsConsumedToSortOffsets(std::size_t testNum, std::size_t step) |
| 123 | +{ |
| 124 | + Kafka::UnorderedOffsetCommitQueue queue; |
| 125 | + |
| 126 | + std::vector<Kafka::Offset> waitSequence(testNum); |
| 127 | + for (std::size_t i = 0 ; i < testNum; ++i) |
| 128 | + { |
| 129 | + waitSequence[i] = i; |
| 130 | + } |
| 131 | + |
| 132 | + std::vector<Kafka::Offset> ackSequence = waitSequence; |
| 133 | + std::random_device rd; |
| 134 | + std::mt19937 g(rd()); |
| 135 | + for (std::size_t iBegin = 0; iBegin < ackSequence.size(); iBegin += step) |
| 136 | + { |
| 137 | + std::size_t iEnd = std::min(iBegin + step, ackSequence.size()); |
| 138 | + std::shuffle(ackSequence.begin() + iBegin, ackSequence.begin() + iEnd, g); |
| 139 | + } |
| 140 | + |
| 141 | + using namespace std::chrono; |
| 142 | + auto timestampBegin = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 143 | + |
| 144 | + std::size_t indexWait = 0; |
| 145 | + std::size_t indexAck = 0; |
| 146 | + while (indexAck < testNum) |
| 147 | + { |
| 148 | + for (std::size_t i = 0; i < step && indexWait < testNum; ++i) |
| 149 | + { |
| 150 | + queue.waitOffset(waitSequence[indexWait++]); |
| 151 | + } |
| 152 | + |
| 153 | + for (std::size_t i = 0; i < step && indexAck < testNum; ++i) |
| 154 | + { |
| 155 | + queue.ackOffset(ackSequence[indexAck++]); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + auto timestampEnd = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 160 | + |
| 161 | + // All offsets have been acked |
| 162 | + EXPECT_EQ(testNum, *queue.popOffsetToCommit()); |
| 163 | + |
| 164 | + return (timestampEnd - timestampBegin); |
| 165 | +} |
| 166 | + |
| 167 | +} // end of namespace |
| 168 | + |
| 169 | +TEST(UnorderedOffsetCommitQueue, CheckPerf) |
| 170 | +{ |
| 171 | + std::size_t testNum = 1000000; |
| 172 | + std::size_t step = 100; |
| 173 | + std::cout << "Took " << checkTimeMsConsumedToSortOffsets(testNum, step) << " ms to sort " << testNum << " offsets (with step:" << step << ")." << std::endl; |
| 174 | + |
| 175 | + testNum = 1000000; |
| 176 | + step = 1000; |
| 177 | + std::cout << "Took " << checkTimeMsConsumedToSortOffsets(testNum, step) << " ms to sort " << testNum << " offsets (with step:" << step << ")." << std::endl; |
| 178 | + |
| 179 | + testNum = 1000000; |
| 180 | + step = 10000; |
| 181 | + std::cout << "Took " << checkTimeMsConsumedToSortOffsets(testNum, step) << " ms to sort " << testNum << " offsets (with step:" << step << ")." << std::endl; |
| 182 | + |
| 183 | + testNum = 1000000; |
| 184 | + step = 100000; |
| 185 | + std::cout << "Took " << checkTimeMsConsumedToSortOffsets(testNum, step) << " ms to sort " << testNum << " offsets (with step:" << step << ")." << std::endl; |
| 186 | +} |
| 187 | + |
0 commit comments