Gleiphir2769 commented on code in PR #805:
URL: https://github.com/apache/pulsar-client-go/pull/805#discussion_r983291109


##########
pulsar/consumer_partition.go:
##########
@@ -1475,3 +1609,210 @@ func convertToMessageID(id *pb.MessageIdData) 
trackingMessageID {
 
        return msgID
 }
+
+type chunkedMsgCtx struct {
+       totalChunks      int32
+       chunkedMsgBuffer internal.Buffer
+       lastChunkedMsgID int32
+       chunkedMsgIDs    []messageID
+       receivedTime     int64
+
+       mu sync.Mutex
+}
+
+func newChunkedMsgCtx(numChunksFromMsg int32, totalChunkMsgSize int) 
*chunkedMsgCtx {
+       return &chunkedMsgCtx{
+               totalChunks:      numChunksFromMsg,
+               chunkedMsgBuffer: internal.NewBuffer(totalChunkMsgSize),
+               lastChunkedMsgID: -1,
+               chunkedMsgIDs:    make([]messageID, numChunksFromMsg),
+               receivedTime:     time.Now().Unix(),
+       }
+}
+
+func (c *chunkedMsgCtx) refresh(chunkID int32, msgID messageID, partPayload 
internal.Buffer) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       c.chunkedMsgIDs[chunkID] = msgID
+       c.chunkedMsgBuffer.Write(partPayload.ReadableSlice())
+       c.lastChunkedMsgID = chunkID
+}
+
+func (c *chunkedMsgCtx) firstChunkID() messageID {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if len(c.chunkedMsgIDs) == 0 {
+               return messageID{}
+       }
+       return c.chunkedMsgIDs[0]
+}
+
+func (c *chunkedMsgCtx) lastChunkID() messageID {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if len(c.chunkedMsgIDs) == 0 {
+               return messageID{}
+       }
+       return c.chunkedMsgIDs[len(c.chunkedMsgIDs)-1]
+}
+
+func (c *chunkedMsgCtx) discard(pc *partitionConsumer) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+
+       for _, mid := range c.chunkedMsgIDs {
+               pc.log.Info("Removing chunk message-id", mid.String())
+               tmid, _ := toTrackingMessageID(mid)
+               pc.AckID(tmid)
+       }
+}
+
+type chunkedMsgCtxMap struct {
+       chunkedMsgCtxs map[string]*chunkedMsgCtx
+       pendingQueue   *list.List
+       maxPending     int
+       pc             *partitionConsumer
+       mu             sync.Mutex
+       closed         bool
+}
+
+func newChunkedMsgCtxMap(maxPending int, pc *partitionConsumer) 
*chunkedMsgCtxMap {
+       return &chunkedMsgCtxMap{
+               chunkedMsgCtxs: make(map[string]*chunkedMsgCtx, maxPending),
+               pendingQueue:   list.New(),
+               maxPending:     maxPending,
+               pc:             pc,
+               mu:             sync.Mutex{},
+       }
+}
+
+func (c *chunkedMsgCtxMap) addIfAbsent(uuid string, totalChunks int32, 
totalChunkMsgSize int) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if c.closed {
+               return
+       }
+       if _, ok := c.chunkedMsgCtxs[uuid]; !ok {
+               c.chunkedMsgCtxs[uuid] = newChunkedMsgCtx(totalChunks, 
totalChunkMsgSize)
+               c.pendingQueue.PushBack(uuid)
+               go c.removeChunkIfExpire(uuid, true, 
c.pc.options.expireTimeOfIncompleteChunk)
+       }
+       if c.maxPending > 0 && c.pendingQueue.Len() > c.maxPending {
+               go c.removeChunkMessage(uuid, 
c.pc.options.autoAckIncompleteChunk)
+       }
+}
+
+func (c *chunkedMsgCtxMap) get(uuid string) *chunkedMsgCtx {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if c.closed {
+               return nil
+       }
+       return c.chunkedMsgCtxs[uuid]
+}
+
+func (c *chunkedMsgCtxMap) remove(uuid string) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if c.closed {
+               return
+       }
+       delete(c.chunkedMsgCtxs, uuid)
+       e := c.pendingQueue.Front()
+       for ; e != nil; e = e.Next() {
+               if e.Value.(string) == uuid {
+                       c.pendingQueue.Remove(e)
+                       break
+               }
+       }
+}
+
+func (c *chunkedMsgCtxMap) removeChunkMessage(uuid string, autoAck bool) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if c.closed {
+               return
+       }
+       ctx, ok := c.chunkedMsgCtxs[uuid]
+       if !ok {
+               return
+       }
+       if autoAck {
+               ctx.discard(c.pc)
+       }
+       delete(c.chunkedMsgCtxs, uuid)
+       e := c.pendingQueue.Front()
+       for ; e != nil; e = e.Next() {
+               if e.Value.(string) == uuid {
+                       c.pendingQueue.Remove(e)
+                       break
+               }
+       }
+       c.pc.log.Infof("Chunked message [%s] has been removed from 
chunkedMsgCtxMap", uuid)
+}
+
+func (c *chunkedMsgCtxMap) removeChunkIfExpire(uuid string, autoAck bool, 
expire time.Duration) {
+       timer := time.NewTimer(expire)
+       <-timer.C
+       c.removeChunkMessage(uuid, autoAck)
+}
+
+func (c *chunkedMsgCtxMap) Close() {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       c.closed = true
+}
+
+type unAckChunksTracker struct {
+       chunkIDs map[chunkMessageID][]messageID
+       pc       *partitionConsumer
+       mu       sync.Mutex
+}
+
+func newUnAckChunksTracker(pc *partitionConsumer) *unAckChunksTracker {
+       return &unAckChunksTracker{
+               chunkIDs: make(map[chunkMessageID][]messageID),
+               pc:       pc,
+       }
+}
+
+func (u *unAckChunksTracker) add(cmid chunkMessageID, ids []messageID) {
+       u.mu.Lock()
+       defer u.mu.Unlock()
+
+       u.chunkIDs[cmid] = ids
+}
+
+func (u *unAckChunksTracker) get(cmid chunkMessageID) []messageID {
+       u.mu.Lock()
+       defer u.mu.Unlock()
+
+       return u.chunkIDs[cmid]
+}
+
+func (u *unAckChunksTracker) remove(cmid chunkMessageID) {
+       u.mu.Lock()
+       defer u.mu.Unlock()
+
+       delete(u.chunkIDs, cmid)
+}
+
+func (u *unAckChunksTracker) ack(cmid chunkMessageID) error {
+       ids := u.get(cmid)
+       for _, id := range ids {
+               if err := u.pc.AckID(id); err != nil {
+                       return err
+               }
+       }
+       u.remove(cmid)
+       return nil
+}
+
+func (u *unAckChunksTracker) nack(cmid chunkMessageID) {
+       ids := u.get(cmid)
+       for _, id := range ids {
+               u.pc.nackTracker.Add(id)
+               u.pc.metrics.NacksCounter.Inc()

Review Comment:
   > I think it's better to call u.pc.NackID here. 
   
   My thought at the time was to avoid unnecessary type judgments twice in 
`u.pc.NackID`.
   
   > we need to maintain in two places.
   
   But I agree with you that saving two type judgments is less beneficial than 
keeping the code concise.
   
   I will fix it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to