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

Fix the list topics API #508

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions lib/kafka/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ def create_topic(name, num_partitions: 1, replication_factor: 1, timeout: 30)
#
# @return [Array<String>] the list of topic names.
def topics
@cluster.clear_target_topics
@cluster.topics
@cluster.list_topics
end

def has_topic?(topic)
Expand Down
6 changes: 6 additions & 0 deletions lib/kafka/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ def topics
cluster_info.topics.map(&:topic_name)
end

# Lists all topics in the cluster.
def list_topics
response = random_broker.fetch_metadata(topics: nil)
response.topics.map(&:topic_name)
end

def disconnect
@broker_pool.close
end
Expand Down
9 changes: 7 additions & 2 deletions lib/kafka/protocol/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ def write_int64(int)
# @param array [Array]
# @return [nil]
def write_array(array, &block)
write_int32(array.size)
array.each(&block)
if array.nil?
# An array can be null, which is different from it being empty.
write_int32(-1)
else
write_int32(array.size)
array.each(&block)
end
end

# Writes a string to the IO object.
Expand Down
8 changes: 5 additions & 3 deletions spec/functional/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
let!(:topic) { create_random_topic(num_partitions: 3) }

example "listing all topics in the cluster" do
expect(kafka.has_topic?(topic)).to eq true
# Use a clean Kafka instance to avoid hitting caches.
kafka = Kafka.new(seed_brokers: KAFKA_BROKERS, logger: LOGGER)

topic2 = create_random_topic
topics = kafka.topics

expect(kafka.has_topic?(topic2)).to eq true
expect(topics).to include topic
expect(kafka.has_topic?(topic)).to eq true
end

example "fetching the partition count for a topic" do
Expand Down