Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kafka: fix timequery failing for empty topics #19937

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/v/kafka/server/handlers/list_offsets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "kafka/server/replicated_partition.h"
#include "kafka/server/request_context.h"
#include "kafka/server/response.h"
#include "model/fundamental.h"
#include "model/namespace.h"
#include "resource_mgmt/io_priority.h"

Expand Down Expand Up @@ -129,10 +130,22 @@ static ss::future<list_offset_partition_response> list_offsets_partition(
offset,
kafka_partition->leader_epoch());
}
auto min_offset = kafka_partition->start_offset();
auto max_offset = model::prev_offset(offset);

// Empty partition.
if (max_offset < min_offset) {
co_return list_offsets_response::make_partition(
ktp.get_partition(),
model::timestamp(-1),
model::offset(-1),
kafka_partition->leader_epoch());
Comment on lines +138 to +142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to push this check into cluster::partition too / instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to refactor this to use the bounded interval. Need to make it support Kafka offsets too first iirc.

Will merge it like this for now to make progress on the backports and will try to clean it up in a separate PR.

thanks for pointing it out!

}

auto res = co_await kafka_partition->timequery(storage::timequery_config{
kafka_partition->start_offset(),
min_offset,
timestamp,
model::prev_offset(offset),
max_offset,
kafka_read_priority(),
{model::record_batch_type::raft_data},
octx.rctx.abort_source().local()});
Expand Down
19 changes: 18 additions & 1 deletion tests/rptest/tests/timequery_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def _test_timequery(self, cluster, cloud_storage: bool, batch_cache: bool):
base_ts = 1664453149000
msg_count = (self.log_segment_size * total_segments) // record_size
local_retention = self.log_segment_size * 4
kcat = KafkaCat(cluster)

# Test the base case with an empty topic.
empty_topic = TopicSpec(name="tq_empty_topic",
partition_count=1,
replication_factor=3)
self.client().create_topic(empty_topic)
offset = kcat.query_offset(empty_topic.name, 0, base_ts)
self.logger.info(f"Time query returned offset {offset}")
assert offset == -1, f"Expected -1, got {offset}"

# Create a topic and produce a run of messages we will query.
topic, timestamps = self._create_and_produce(cluster, cloud_storage,
local_retention, base_ts,
record_size, msg_count)
Expand Down Expand Up @@ -163,7 +175,6 @@ def __init__(self, offset, ts=None, expect_read=True):
# offset should cause cloud downloads.
hit_offsets = set()

kcat = KafkaCat(cluster)
cloud_metrics = None
local_metrics = None

Expand Down Expand Up @@ -515,6 +526,12 @@ def test_timequery_with_trim_prefix(self, cloud_storage: bool,
offset = kcat.query_offset(topic.name, 0, timestamps[0] - 1000)
assert offset == msg_count - 1, f"Expected {msg_count - 1}, got {offset}"

# Trim everything, leaving an empty log.
rpk.trim_prefix(topic.name, offset=p.high_watermark, partitions=[0])
kcat = KafkaCat(self.redpanda)
offset = kcat.query_offset(topic.name, 0, timestamps[0] - 1000)
assert offset == -1, f"Expected -1, got {offset}"

@cluster(
num_nodes=4,
log_allow_list=["Failed to upload spillover manifest {timed_out}"])
Expand Down