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

check for stale cluster info before retrying #2

Open
wants to merge 1 commit into
base: v0.6-stable
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/kafka/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def refresh_metadata_if_necessary!
# @param partition [Integer]
# @return [Broker] the broker that's currently leader.
def get_leader(topic, partition)
refresh_metadata_if_necessary!
connect_to_broker(get_leader_id(topic, partition))
end

Expand Down
68 changes: 68 additions & 0 deletions spec/cluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@
allow(broker).to receive(:disconnect)
end

it 'raises NotLeaderForPartition if the current broker is not the leader' do
metadata = Kafka::Protocol::MetadataResponse.new(
brokers: [
Kafka::Protocol::MetadataResponse::BrokerInfo.new(
node_id: 42,
host: "test1",
port: 9092,
)
],
controller_id: 42,
topics: [
Kafka::Protocol::MetadataResponse::TopicMetadata.new(
topic_name: "greetings",
partitions: [
Kafka::Protocol::MetadataResponse::PartitionMetadata.new(
partition_id: 42,
leader: 2,

partition_error_code: 6, # <-- this is the important bit. NotLeaderForPartition
)
]
)
]
)

allow(broker).to receive(:fetch_metadata) { metadata }

expect {
cluster.get_leader("greetings", 42)
}.to raise_error Kafka::NotLeaderForPartition
end

it "raises LeaderNotAvailable if there's no leader for the partition" do
metadata = Kafka::Protocol::MetadataResponse.new(
brokers: [
Expand Down Expand Up @@ -88,5 +120,41 @@
cluster.get_leader("greetings", 42)
}.to raise_exception(Kafka::ConnectionError)
end

context 'meta data is stale' do
before do
allow(cluster).to receive(:stale) { true }
end

it 'should re fetch metadata' do
metadata = Kafka::Protocol::MetadataResponse.new(
brokers: [
Kafka::Protocol::MetadataResponse::BrokerInfo.new(
node_id: 42,
host: "test1",
port: 9092,
)
],
controller_id: 42,
topics: [
Kafka::Protocol::MetadataResponse::TopicMetadata.new(
topic_name: "greetings",
partitions: [
Kafka::Protocol::MetadataResponse::PartitionMetadata.new(
partition_id: 42,
leader: 42,
partition_error_code: 0
)
]
)
]
)

allow(broker).to receive(:fetch_metadata) { metadata }
expect(cluster).to receive(:refresh_metadata!)

cluster.get_leader("greetings", 42)
end
end
end
end