1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2016-2022, Magnus Edenhill
* 2025, Confluent Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <ctime>
#include "testcpp.h"
/**
* binary search by timestamp: excercices KafkaConsumer's seek() API.
*/
static std::string topic;
static const int partition = 0;
static int64_t golden_timestamp = -1;
static int64_t golden_offset = -1;
/**
* @brief Seek to offset and consume that message.
*
* Asserts on failure.
*/
static RdKafka::Message *get_msg(RdKafka::KafkaConsumer *c,
int64_t offset,
bool use_seek) {
RdKafka::TopicPartition *next =
RdKafka::TopicPartition::create(topic, partition, offset);
RdKafka::ErrorCode err;
/* Since seek() can only be used to change the currently consumed
* offset we need to start consuming the first time we run this
* loop by calling assign() */
test_timing_t t_seek;
TIMING_START(&t_seek, "seek");
if (!use_seek) {
std::vector<RdKafka::TopicPartition *> parts;
parts.push_back(next);
err = c->assign(parts);
if (err)
Test::Fail("assign() failed: " + RdKafka::err2str(err));
} else {
err = c->seek(*next, tmout_multip(5000));
if (err)
Test::Fail("seek() failed: " + RdKafka::err2str(err));
}
TIMING_STOP(&t_seek);
delete next;
test_timing_t t_consume;
TIMING_START(&t_consume, "consume");
RdKafka::Message *msg = c->consume(tmout_multip(5000));
if (!msg)
Test::Fail("consume() returned NULL");
TIMING_STOP(&t_consume);
if (msg->err())
Test::Fail("consume() returned error: " + msg->errstr());
if (msg->offset() != offset)
Test::Fail(tostr() << "seek()ed to offset " << offset
<< " but consume() returned offset " << msg->offset());
return msg;
}
class MyDeliveryReportCb : public RdKafka::DeliveryReportCb {
public:
void dr_cb(RdKafka::Message &msg) {
if (msg.err())
Test::Fail("Delivery failed: " + msg.errstr());
if (!msg.msg_opaque())
return;
RdKafka::MessageTimestamp ts = msg.timestamp();
if (ts.type != RdKafka::MessageTimestamp::MSG_TIMESTAMP_CREATE_TIME)
Test::Fail(tostr() << "Dr msg timestamp type wrong: " << ts.type);
golden_timestamp = ts.timestamp;
golden_offset = msg.offset();
}
};
static void do_test_bsearch(void) {
RdKafka::Conf *conf, *tconf;
int msgcnt = 1000;
int64_t timestamp_ms;
std::string errstr;
RdKafka::ErrorCode err;
MyDeliveryReportCb my_dr;
topic = Test::mk_topic_name("0059-bsearch", 1);
Test::conf_init(&conf, &tconf, 0);
Test::conf_set(tconf, "acks", "all");
Test::conf_set(conf, "api.version.request", "true");
conf->set("dr_cb", &my_dr, errstr);
conf->set("default_topic_conf", tconf, errstr);
RdKafka::Producer *p = RdKafka::Producer::create(conf, errstr);
if (!p)
Test::Fail("Failed to create Producer: " + errstr);
delete conf;
delete tconf;
/* Start with now() - 1h */
timestamp_ms = std::time(0) * 1000LL - 3600LL * 1000LL;
for (int i = 0; i < msgcnt; i++) {
err = p->produce(topic, partition, RdKafka::Producer::RK_MSG_COPY,
(void *)topic.c_str(), topic.size(), NULL, 0, timestamp_ms,
i == 357 ? (void *)1 /*golden*/ : NULL);
if (err != RdKafka::ERR_NO_ERROR)
Test::Fail("Produce failed: " + RdKafka::err2str(err));
timestamp_ms += 100 + (i % 10);
}
if (p->flush(tmout_multip(5000)) != 0)
Test::Fail("Not all messages flushed");
Test::Say(tostr() << "Produced " << msgcnt << " messages, "
<< "golden message with timestamp " << golden_timestamp
<< " at offset " << golden_offset << "\n");
delete p;
/*
* Now find the golden message using bsearch
*/
/* Create consumer */
Test::conf_init(&conf, NULL, 10);
Test::conf_set(conf, "group.id", topic);
Test::conf_set(conf, "api.version.request", "true");
Test::conf_set(conf, "fetch.wait.max.ms", "1");
Test::conf_set(conf, "fetch.error.backoff.ms", "1");
Test::conf_set(conf, "queued.min.messages", "1");
Test::conf_set(conf, "enable.auto.commit", "false");
RdKafka::KafkaConsumer *c = RdKafka::KafkaConsumer::create(conf, errstr);
if (!c)
Test::Fail("Failed to create KafkaConsumer: " + errstr);
delete conf;
Test::Say("Find initial middle offset\n");
int64_t low, high;
test_timing_t t_qr;
TIMING_START(&t_qr, "query_watermark_offsets");
err = c->query_watermark_offsets(topic, partition, &low, &high,
tmout_multip(5000));
TIMING_STOP(&t_qr);
if (err)
Test::Fail("query_watermark_offsets failed: " + RdKafka::err2str(err));
/* Divide and conquer */
test_timing_t t_bsearch;
TIMING_START(&t_bsearch, "actual bsearch");
int itcnt = 0;
do {
int64_t mid;
mid = low + ((high - low) / 2);
Test::Say(1, tostr() << "Get message at mid point of " << low << ".."
<< high << " -> " << mid << "\n");
RdKafka::Message *msg = get_msg(c, mid,
/* use assign() on first iteration,
* then seek() */
itcnt > 0);
RdKafka::MessageTimestamp ts = msg->timestamp();
if (ts.type != RdKafka::MessageTimestamp::MSG_TIMESTAMP_CREATE_TIME)
Test::Fail(tostr() << "Expected CreateTime timestamp, not " << ts.type
<< " at offset " << msg->offset());
Test::Say(1, tostr() << "Message at offset " << msg->offset()
<< " with timestamp " << ts.timestamp << "\n");
if (ts.timestamp == golden_timestamp) {
Test::Say(1, tostr() << "Found golden timestamp " << ts.timestamp
<< " at offset " << msg->offset() << " in "
<< itcnt + 1 << " iterations\n");
delete msg;
break;
}
if (low == high) {
Test::Fail(tostr() << "Search exhausted at offset " << msg->offset()
<< " with timestamp " << ts.timestamp
<< " without finding golden timestamp "
<< golden_timestamp << " at offset " << golden_offset);
} else if (ts.timestamp < golden_timestamp)
low = msg->offset() + 1;
else if (ts.timestamp > golden_timestamp)
high = msg->offset() - 1;
delete msg;
itcnt++;
} while (true);
TIMING_STOP(&t_bsearch);
c->close();
delete c;
}
extern "C" {
int main_0059_bsearch(int argc, char **argv) {
do_test_bsearch();
return 0;
}
}
|