Skip to content

Commit 5f8df27

Browse files
authored
[Refactor] refactor duplicated code lines and fix typo errors (#1039)
* [Refactor] refactor duplicated code lines and fix typo errors * [typo] revert * [typo] revert * [typo] revert * [refactor] delete redunpdant code lines * [typo] revert some words * [fix] set useTxn when use transaction --------- Co-authored-by: gunli <[email protected]>
1 parent 6f01a7c commit 5f8df27

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

pulsar/producer_partition.go

100644100755
Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,19 @@ func addRequestToBatch(smm *pb.SingleMessageMetadata, p *partitionProducer,
710710
uncompressedPayload []byte,
711711
request *sendRequest, msg *ProducerMessage, deliverAt time.Time,
712712
schemaVersion []byte, multiSchemaEnabled bool) bool {
713-
var ok bool
713+
var useTxn bool
714+
var mostSigBits uint64
715+
var leastSigBits uint64
714716
if request.transaction != nil {
715717
txnID := request.transaction.GetTxnID()
716-
ok = p.batchBuilder.Add(smm, p.sequenceIDGenerator, uncompressedPayload, request,
717-
msg.ReplicationClusters, deliverAt, schemaVersion, multiSchemaEnabled, true, txnID.MostSigBits,
718-
txnID.LeastSigBits)
719-
} else {
720-
ok = p.batchBuilder.Add(smm, p.sequenceIDGenerator, uncompressedPayload, request,
721-
msg.ReplicationClusters, deliverAt, schemaVersion, multiSchemaEnabled, false, 0, 0)
718+
useTxn = true
719+
mostSigBits = txnID.MostSigBits
720+
leastSigBits = txnID.LeastSigBits
722721
}
723-
return ok
722+
723+
return p.batchBuilder.Add(smm, p.sequenceIDGenerator, uncompressedPayload, request,
724+
msg.ReplicationClusters, deliverAt, schemaVersion, multiSchemaEnabled, useTxn, mostSigBits,
725+
leastSigBits)
724726
}
725727

726728
func (p *partitionProducer) genMetadata(msg *ProducerMessage,
@@ -764,6 +766,14 @@ func (p *partitionProducer) updateMetadataSeqID(mm *pb.MessageMetadata, msg *Pro
764766
}
765767
}
766768

769+
func (p *partitionProducer) updateSingleMessageMetadataSeqID(smm *pb.SingleMessageMetadata, msg *ProducerMessage) {
770+
if msg.SequenceID != nil {
771+
smm.SequenceId = proto.Uint64(uint64(*msg.SequenceID))
772+
} else {
773+
smm.SequenceId = proto.Uint64(internal.GetAndAdd(p.sequenceIDGenerator, 1))
774+
}
775+
}
776+
767777
func (p *partitionProducer) genSingleMessageMetadataInBatch(msg *ProducerMessage,
768778
uncompressedSize int) (smm *pb.SingleMessageMetadata) {
769779
smm = &pb.SingleMessageMetadata{
@@ -786,14 +796,7 @@ func (p *partitionProducer) genSingleMessageMetadataInBatch(msg *ProducerMessage
786796
smm.Properties = internal.ConvertFromStringMap(msg.Properties)
787797
}
788798

789-
var sequenceID uint64
790-
if msg.SequenceID != nil {
791-
sequenceID = uint64(*msg.SequenceID)
792-
} else {
793-
sequenceID = internal.GetAndAdd(p.sequenceIDGenerator, 1)
794-
}
795-
796-
smm.SequenceId = proto.Uint64(sequenceID)
799+
p.updateSingleMessageMetadataSeqID(smm, msg)
797800

798801
return
799802
}
@@ -813,35 +816,30 @@ func (p *partitionProducer) internalSingleSend(mm *pb.MessageMetadata,
813816
}
814817

815818
sid := *mm.SequenceId
816-
var err error
819+
var useTxn bool
820+
var mostSigBits uint64
821+
var leastSigBits uint64
822+
817823
if request.transaction != nil {
818824
txnID := request.transaction.GetTxnID()
819-
err = internal.SingleSend(
820-
buffer,
821-
p.producerID,
822-
sid,
823-
mm,
824-
payloadBuf,
825-
p.encryptor,
826-
maxMessageSize,
827-
true,
828-
txnID.MostSigBits,
829-
txnID.LeastSigBits,
830-
)
831-
} else {
832-
err = internal.SingleSend(
833-
buffer,
834-
p.producerID,
835-
sid,
836-
mm,
837-
payloadBuf,
838-
p.encryptor,
839-
maxMessageSize,
840-
false,
841-
0,
842-
0,
843-
)
844-
}
825+
useTxn = true
826+
mostSigBits = txnID.MostSigBits
827+
leastSigBits = txnID.LeastSigBits
828+
}
829+
830+
err := internal.SingleSend(
831+
buffer,
832+
p.producerID,
833+
sid,
834+
mm,
835+
payloadBuf,
836+
p.encryptor,
837+
maxMessageSize,
838+
useTxn,
839+
mostSigBits,
840+
leastSigBits,
841+
)
842+
845843
if err != nil {
846844
runCallback(request.callback, nil, request.msg, err)
847845
p.releaseSemaphoreAndMem(int64(len(msg.Payload)))
@@ -1001,7 +999,7 @@ func (p *partitionProducer) failTimeoutMessages() {
1001999
}
10021000
}
10031001

1004-
// flag the send has completed with error, flush make no effect
1002+
// flag the sending has completed with error, flush make no effect
10051003
pi.Complete()
10061004
pi.Unlock()
10071005

@@ -1116,8 +1114,10 @@ func (p *partitionProducer) internalSendAsync(ctx context.Context, msg *Producer
11161114
callback func(MessageID, *ProducerMessage, error), flushImmediately bool) {
11171115
// Register transaction operation to transaction and the transaction coordinator.
11181116
var newCallback func(MessageID, *ProducerMessage, error)
1117+
var txn *transaction
11191118
if msg.Transaction != nil {
11201119
transactionImpl := (msg.Transaction).(*transaction)
1120+
txn = transactionImpl
11211121
if transactionImpl.state != TxnOpen {
11221122
p.log.WithField("state", transactionImpl.state).Error("Failed to send message" +
11231123
" by a non-open transaction.")
@@ -1150,10 +1150,6 @@ func (p *partitionProducer) internalSendAsync(ctx context.Context, msg *Producer
11501150

11511151
// callbackOnce make sure the callback is only invoked once in chunking
11521152
callbackOnce := &sync.Once{}
1153-
var txn *transaction
1154-
if msg.Transaction != nil {
1155-
txn = (msg.Transaction).(*transaction)
1156-
}
11571153
sr := &sendRequest{
11581154
ctx: ctx,
11591155
msg: msg,
@@ -1398,7 +1394,7 @@ func (p *partitionProducer) _setConn(conn internal.Connection) {
13981394
// _getConn returns internal connection field of this partition producer atomically.
13991395
// Note: should only be called by this partition producer before attempting to use the connection
14001396
func (p *partitionProducer) _getConn() internal.Connection {
1401-
// Invariant: The conn must be non-nill for the lifetime of the partitionProducer.
1397+
// Invariant: The conn must be non-nil for the lifetime of the partitionProducer.
14021398
// For this reason we leave this cast unchecked and panic() if the
14031399
// invariant is broken
14041400
return p.conn.Load().(internal.Connection)

0 commit comments

Comments
 (0)