-
Notifications
You must be signed in to change notification settings - Fork 356
[WIP] [Issue 456] feat: support chunked msg #717
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package pulsar | |
|
||
import ( | ||
"context" | ||
uuidGen "github.com/google/uuid" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
|
@@ -411,8 +412,8 @@ func (p *partitionProducer) internalSend(request *sendRequest) { | |
payload = schemaPayload | ||
} | ||
|
||
// if msg is too large | ||
if len(payload) > int(p._getConn().GetMaxMessageSize()) { | ||
// if msg is too large and chunked msg not enabled | ||
if len(payload) > int(p._getConn().GetMaxMessageSize()) && !p.options.ChunkingEnabled { | ||
p.publishSemaphore.Release() | ||
request.callback(nil, request.msg, errMessageTooLarge) | ||
p.log.WithError(errMessageTooLarge). | ||
|
@@ -423,6 +424,16 @@ func (p *partitionProducer) internalSend(request *sendRequest) { | |
return | ||
} | ||
|
||
// if msg is too large and chunked msg enabled, send chunked msg | ||
if len(payload) > int(p._getConn().GetMaxMessageSize()) && p.options.ChunkingEnabled { | ||
p.log. | ||
WithField("size", len(payload)). | ||
WithField("MaxMessageSize %d", int(p._getConn().GetMaxMessageSize())). | ||
Info("Size exceed limit, send with chunks") | ||
p.internalSendWithTrunks(request, payload) | ||
return | ||
} | ||
|
||
deliverAt := msg.DeliverAt | ||
if msg.DeliverAfter.Nanoseconds() > 0 { | ||
deliverAt = time.Now().Add(msg.DeliverAfter) | ||
|
@@ -496,6 +507,77 @@ func (p *partitionProducer) internalSend(request *sendRequest) { | |
} | ||
} | ||
|
||
func (p *partitionProducer) internalSendWithTrunks(request *sendRequest, payload []byte) { | ||
chunkSize := int(p._getConn().GetMaxMessageSize()) | ||
totalChunks := (len(payload)+1)/chunkSize + 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last
|
||
uuid := uuidGen.New().String() | ||
|
||
for chunkId := 0; chunkId < chunkSize; chunkId++ { | ||
left := chunkId * chunkSize | ||
right := left + chunkSize | ||
if right > len(payload)-1 { | ||
right = len(payload) - 1 | ||
} | ||
// [left, right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this as a separate function to return a slice of [ {trunk 0 left,right}, {trunk 1 left,right} ... ]. So that we can write a unit test to verify any number is missing from the splitting. WDYT? |
||
p.internalSendSingleChunk(request, payload[left:right], uuid, totalChunks, len(payload), chunkId) | ||
} | ||
} | ||
|
||
func (p *partitionProducer) internalSendSingleChunk(request *sendRequest, payload []byte, | ||
uuid string, totalChunks int, totalSize int, chunkId int) { | ||
|
||
msg := request.msg | ||
mm := &pb.MessageMetadata{} | ||
|
||
deliverAt := msg.DeliverAt | ||
if msg.DeliverAfter.Nanoseconds() > 0 { | ||
deliverAt = time.Now().Add(msg.DeliverAfter) | ||
mm.DeliverAtTime = proto.Int64(int64(internal.TimestampMillis(deliverAt))) | ||
} | ||
|
||
if msg.EventTime.UnixNano() != 0 { | ||
mm.EventTime = proto.Uint64(internal.TimestampMillis(msg.EventTime)) | ||
} | ||
|
||
if msg.Key != "" { | ||
mm.PartitionKey = proto.String(msg.Key) | ||
} | ||
|
||
if len(msg.OrderingKey) != 0 { | ||
mm.OrderingKey = []byte(msg.OrderingKey) | ||
} | ||
|
||
if msg.Properties != nil { | ||
mm.Properties = internal.ConvertFromStringMap(msg.Properties) | ||
} | ||
|
||
if msg.SequenceID != nil { | ||
sequenceID := uint64(*msg.SequenceID) | ||
mm.SequenceId = proto.Uint64(sequenceID) | ||
} | ||
|
||
// Fields required for chunked data | ||
mm.Uuid = proto.String(uuid) | ||
mm.NumChunksFromMsg = proto.Int(totalChunks) | ||
mm.TotalChunkMsgSize = proto.Int(totalSize) | ||
mm.ChunkId = proto.Int(chunkId) | ||
|
||
// Directly construct a buffer and put it to the pending queue | ||
newBuffer := p.GetBuffer() | ||
internal.ConstructBufferFromMessage(newBuffer, mm, payload) | ||
|
||
callbacks := make([]interface{}, 1) | ||
callbacks[0] = request.callback | ||
|
||
p.pendingQueue.Put(&pendingItem{ | ||
sentAt: time.Now(), | ||
batchData: newBuffer, | ||
sequenceID: uint64(*msg.SequenceID), | ||
sendRequests: callbacks, | ||
}) | ||
p._getConn().WriteData(newBuffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a batching is enabled, how does this flush work with batching?
|
||
} | ||
|
||
type pendingItem struct { | ||
sync.Mutex | ||
batchData internal.Buffer | ||
|
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.
I could not find any code calls this function. Who's supposed to call this function?