Skip to content

Commit

Permalink
Merge pull request #1002 from emfree/assign_relative_offsets
Browse files Browse the repository at this point in the history
Assign relative offsets in compressed message sets
  • Loading branch information
eapache committed Dec 20, 2017
2 parents d5db635 + f0d0b0f commit f144d11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions produce_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func (ps *produceSet) buildRequest() *ProduceRequest {
// and sent as the payload of a single fake "message" with the appropriate codec
// set and no key. When the server sees a message with a compression codec, it
// decompresses the payload and treats the result as its message set.

if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
// If our version is 0.10 or later, assign relative offsets
// to the inner messages. This lets the broker avoid
// recompressing the message set.
// (See https://cwiki.apache.org/confluence/display/KAFKA/KIP-31+-+Move+to+relative+offsets+in+compressed+message+sets
// for details on relative offsets.)
for i, msg := range set.recordsToSend.msgSet.Messages {
msg.Offset = int64(i)
}
}
payload, err := encode(set.recordsToSend.msgSet, ps.parent.conf.MetricRegistry)
if err != nil {
Logger.Println(err) // if this happens, it's basically our fault.
Expand Down
5 changes: 4 additions & 1 deletion produce_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ func TestProduceSetCompressedRequestBuilding(t *testing.T) {
if err != nil {
t.Error("Failed to decode set from payload")
}
for _, compMsgBlock := range msg.Set.Messages {
for i, compMsgBlock := range msg.Set.Messages {
compMsg := compMsgBlock.Msg
if compMsg.Version != 1 {
t.Error("Wrong compressed message version")
}
if compMsgBlock.Offset != int64(i) {
t.Error("Wrong relative inner offset")
}
}
if msg.Version != 1 {
t.Error("Wrong compressed parent message version")
Expand Down

0 comments on commit f144d11

Please sign in to comment.