-
Notifications
You must be signed in to change notification settings - Fork 798
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
Extract record batch structure #314
Merged
achille-roussel
merged 5 commits into
segmentio:master
from
VictorDenisov:transactions/extract_record_batch
Sep 10, 2019
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e68bec
Replace argument of writeProduceRequestV3
VictorDenisov 27ffeb1
Calculate recordBatch size in init
VictorDenisov db7f161
Extract writeTo method for record batch
VictorDenisov eaba0a1
Extract recordBatch processing from ProduceRequestV7
VictorDenisov 57ef4ba
Add constructor for record batch
VictorDenisov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package kafka | ||
|
||
import ( | ||
"bytes" | ||
"time" | ||
) | ||
|
||
const recordBatchHeaderSize int32 = 0 + | ||
8 + // base offset | ||
4 + // batch length | ||
4 + // partition leader epoch | ||
1 + // magic | ||
4 + // crc | ||
2 + // attributes | ||
4 + // last offset delta | ||
8 + // first timestamp | ||
8 + // max timestamp | ||
8 + // producer id | ||
2 + // producer epoch | ||
4 + // base sequence | ||
4 // msg count | ||
|
||
func recordBatchSize(msgs ...Message) (size int32) { | ||
size = recordBatchHeaderSize | ||
baseTime := msgs[0].Time | ||
|
||
for i := range msgs { | ||
msg := &msgs[i] | ||
msz := recordSize(msg, msg.Time.Sub(baseTime), int64(i)) | ||
size += int32(msz + varIntLen(int64(msz))) | ||
} | ||
|
||
return | ||
} | ||
|
||
func compressRecordBatch(codec CompressionCodec, msgs ...Message) (compressed *bytes.Buffer, attributes int16, size int32, err error) { | ||
compressed = acquireBuffer() | ||
compressor := codec.NewWriter(compressed) | ||
wb := &writeBuffer{w: compressor} | ||
|
||
for i, msg := range msgs { | ||
wb.writeRecord(0, msgs[0].Time, int64(i), msg) | ||
} | ||
|
||
if err = compressor.Close(); err != nil { | ||
releaseBuffer(compressed) | ||
return | ||
} | ||
|
||
attributes = int16(codec.Code()) | ||
size = recordBatchHeaderSize + int32(compressed.Len()) | ||
return | ||
} | ||
|
||
type recordBatch struct { | ||
// required input parameters | ||
codec CompressionCodec | ||
attributes int16 | ||
msgs []Message | ||
|
||
// parameters calculated during init | ||
compressed *bytes.Buffer | ||
size int32 | ||
} | ||
|
||
func (r *recordBatch) init() (err error) { | ||
if r.codec == nil { | ||
r.size = recordBatchSize(r.msgs...) | ||
} else { | ||
r.compressed, r.attributes, r.size, err = compressRecordBatch(r.codec, r.msgs...) | ||
} | ||
return | ||
} | ||
|
||
func (r *recordBatch) writeTo(wb *writeBuffer) { | ||
wb.writeInt32(r.size) | ||
|
||
baseTime := r.msgs[0].Time | ||
lastTime := r.msgs[len(r.msgs)-1].Time | ||
if r.compressed != nil { | ||
wb.writeRecordBatch(r.attributes, r.size, len(r.msgs), baseTime, lastTime, func(wb *writeBuffer) { | ||
wb.Write(r.compressed.Bytes()) | ||
}) | ||
releaseBuffer(r.compressed) | ||
} else { | ||
wb.writeRecordBatch(r.attributes, r.size, len(r.msgs), baseTime, lastTime, func(wb *writeBuffer) { | ||
for i, msg := range r.msgs { | ||
wb.writeRecord(0, r.msgs[0].Time, int64(i), msg) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func recordSize(msg *Message, timestampDelta time.Duration, offsetDelta int64) int { | ||
return 1 + // attributes | ||
varIntLen(int64(milliseconds(timestampDelta))) + | ||
varIntLen(offsetDelta) + | ||
varBytesLen(msg.Key) + | ||
varBytesLen(msg.Value) + | ||
varArrayLen(len(msg.Headers), func(i int) int { | ||
h := &msg.Headers[i] | ||
return varStringLen(h.Key) + varBytesLen(h.Value) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it's never valid to use a
recordBatch
without callinginit
, how about making it a constructor instead?One challenge is managing the error returned by
compressRecordBatch
, maybe therecordBatch
type can hold the error value like we do forwriteBuffer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was choosing between this option and the option that I implemented. My motivation was to not duplicate the fields in the record batch and in the declaration of the method. I don't have any strong attachment to either solution. Would you prefer your variant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since a
recordBatch
value is never fully initialized without callinginit
I think it would make the code harder to use wrong to have the construction contained in a single function 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done