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

Correct offsets in nested message sets #458

Merged
merged 1 commit into from
Oct 31, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes and additions to the library will be listed here.

## Unreleased

- Fix bug when using compression (#458).

## v0.5.0

- Drops support for Kafka 0.9 in favor of Kafka 0.10 (#381)!
Expand Down
14 changes: 13 additions & 1 deletion lib/kafka/protocol/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,20 @@ def decompress
# For some weird reason we need to cut out the first 20 bytes.
data = codec.decompress(value)
message_set_decoder = Decoder.from_string(data)
message_set = MessageSet.decode(message_set_decoder)

# The contained messages need to have their offset corrected.
messages = message_set.messages.each_with_index.map do |message, i|
Message.new(
offset: offset + i,
value: message.value,
key: message.key,
create_time: message.create_time,
codec_id: message.codec_id
)
end

MessageSet.decode(message_set_decoder)
MessageSet.new(messages: messages)
end

def self.decode(decoder)
Expand Down
8 changes: 7 additions & 1 deletion spec/compressor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
decoder = Kafka::Protocol::Decoder.from_string(data)
decoded_message = Kafka::Protocol::Message.decode(decoder)
decoded_message_set = decoded_message.decompress
messages = decoded_message_set.messages

expect(decoded_message_set.messages.map(&:value)).to eq ["hello1", "hello2"]
expect(messages.map(&:value)).to eq ["hello1", "hello2"]

# When decoding a compressed message, the offsets are calculated relative to that
# of the container message. The broker will set the offset in normal operation,
# but at the client-side we set it to -1.
expect(messages.map(&:offset)).to eq [-1, 0]
end

it "only compresses the messages if there are at least the configured threshold" do
Expand Down