Skip to content

Commit

Permalink
Merge pull request #451 from zendesk/dasch/fix-nil-timestamp
Browse files Browse the repository at this point in the history
Don't assume there's a timestamp on messages
  • Loading branch information
dasch authored Oct 26, 2017
2 parents 07614b0 + ba8fa96 commit e3c8add
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/kafka/protocol/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def self.decode(decoder)
# attributes.
codec_id = attributes & 0b111

new(key: key, value: value, codec_id: codec_id, offset: offset, create_time: Time.at(timestamp / 1000.0))
# The timestamp will be nil if the message was written in the Kafka 0.9 log format.
create_time = timestamp && Time.at(timestamp / 1000.0)

new(key: key, value: value, codec_id: codec_id, offset: offset, create_time: create_time)
end

private
Expand Down
Binary file added spec/fixtures/message-0.9-format
Binary file not shown.
29 changes: 29 additions & 0 deletions spec/protocol/message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe Kafka::Protocol::Message do
it "encodes and decodes messages" do
message = Kafka::Protocol::Message.new(
value: "yolo",
key: "xx",
)

io = StringIO.new
encoder = Kafka::Protocol::Encoder.new(io)
message.encode(encoder)
data = StringIO.new(io.string)
decoder = Kafka::Protocol::Decoder.new(data)

expect(Kafka::Protocol::Message.decode(decoder)).to eq message
end

it "decodes messages written in the 0.9 format" do
data = File.open("spec/fixtures/message-0.9-format")

decoder = Kafka::Protocol::Decoder.new(data)
message = Kafka::Protocol::Message.decode(decoder)

expect(message.key).to eq "xx"
expect(message.value).to eq "yolo"

# Messages didn't have timestamps back in the 0.9 days.
expect(message.create_time).to eq nil
end
end

0 comments on commit e3c8add

Please sign in to comment.