Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions pulsar/internal/batch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type BatchBuilder interface {
// IsFull check if the size in the current batch exceeds the maximum size allowed by the batch
IsFull() bool

// compress the payload
Compress(uncompressedPayload []byte) []byte

// Add will add single message to batch.
Add(
metadata *pb.SingleMessageMetadata, sequenceIDGenerator *uint64,
Expand Down Expand Up @@ -152,6 +155,11 @@ func NewBatchBuilder(
return &bc, nil
}

// compress the payload
func (bc *batchContainer) Compress(uncompressedPayload []byte) []byte {
return bc.compressionProvider.Compress(nil, uncompressedPayload)
}

// IsFull checks if the size in the current batch meets or exceeds the maximum size allowed by the batch
func (bc *batchContainer) IsFull() bool {
return bc.numMessages >= bc.maxMessages || bc.buffer.ReadableBytes() >= uint32(bc.maxBatchSize)
Expand Down
3 changes: 2 additions & 1 deletion pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ func (p *partitionProducer) internalSend(request *sendRequest) {
}

// if msg is too large
if len(payload) > int(p._getConn().GetMaxMessageSize()) {
if len(payload) > int(p._getConn().GetMaxMessageSize()) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that in this PR we do the compression twice when sending the message. The batch builder will do the compression here: https://github.com/apache/pulsar-client-go/blob/master/pulsar/internal/commands.go#L251
Could we move the compression from the batch builder to here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this modification just an translation the implement of https://github.com/apache/pulsar/blob/782132561ac9fc8430ae3ef12913999e5871d3d2/pulsar-client-cpp/lib/ProducerImpl.cc#L431.
It indeed do compress twice.

// Wire format
// [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [PAYLOAD]
// |
// compressed or uncompressed {[METADATA_SIZE_0] [METADATA_0] [PAYLOAD_0]} ...
Acording to the wire format above, SDK cannot compress messages one by one but should compress the whole batchs together.

Another way to avoid 'twice-compress', I think is flushing batchs immediately before and after add big message to batchBuilder.

len(p.batchBuilder.Compress(payload)) > int(p._getConn().GetMaxMessageSize()) {
p.publishSemaphore.Release()
request.callback(nil, request.msg, errMessageTooLarge)
p.log.WithError(errMessageTooLarge).
Expand Down