1- // Copyright © 2023 Kaleido, Inc.
1+ // Copyright © 2024 Kaleido, Inc.
22//
33// SPDX-License-Identifier: Apache-2.0
44//
@@ -40,6 +40,7 @@ type transactionOperation struct {
4040 sentConflict bool
4141 done chan error
4242
43+ opID string
4344 isShutdown bool
4445 txInsert * apitypes.ManagedTX
4546 noncePreAssigned bool
@@ -122,6 +123,7 @@ func newTransactionWriter(bgCtx context.Context, p *sqlPersistence, conf config.
122123
123124func newTransactionOperation (txID string ) * transactionOperation {
124125 return & transactionOperation {
126+ opID : fftypes .ShortID (),
125127 txID : txID ,
126128 done : make (chan error , 1 ), // 1 slot to ensure we don't block the writer
127129 }
@@ -130,6 +132,7 @@ func newTransactionOperation(txID string) *transactionOperation {
130132func (op * transactionOperation ) flush (ctx context.Context ) error {
131133 select {
132134 case err := <- op .done :
135+ log .L (ctx ).Debugf ("Flushed write operation %s (err=%v)" , op .opID , err )
133136 return err
134137 case <- ctx .Done ():
135138 return i18n .NewError (ctx , i18n .MsgContextCanceled )
@@ -165,6 +168,7 @@ func (tw *transactionWriter) queue(ctx context.Context, op *transactionOperation
165168 h := fnv .New32a () // simple non-cryptographic hash algo
166169 _ , _ = h .Write ([]byte (hashKey ))
167170 routine := h .Sum32 () % tw .workerCount
171+ log .L (ctx ).Debugf ("Queuing write operation %s to worker tx_writer_%.4d" , op .opID , routine )
168172 select {
169173 case tw .workQueues [routine ] <- op : // it's queued
170174 case <- ctx .Done (): // timeout of caller context
@@ -180,6 +184,7 @@ func (tw *transactionWriter) worker(i int) {
180184 defer close (tw .workersDone [i ])
181185 workerID := fmt .Sprintf ("tx_writer_%.4d" , i )
182186 ctx := log .WithLogField (tw .bgCtx , "job" , workerID )
187+ l := log .L (ctx )
183188 var batch * transactionWriterBatch
184189 batchCount := 0
185190 workQueue := tw .workQueues [i ]
@@ -208,18 +213,20 @@ func (tw *transactionWriter) worker(i int) {
208213 batchCount ++
209214 }
210215 batch .ops = append (batch .ops , op )
216+ l .Debugf ("Added write operation %s to batch %s (len=%d)" , op .opID , batch .id , len (batch .ops ))
211217 case <- timeoutContext .Done ():
212218 timedOut = true
213219 select {
214220 case <- ctx .Done ():
215- log . L ( ctx ) .Debugf ("Transaction writer ending" )
221+ l .Debugf ("Transaction writer ending" )
216222 return
217223 default :
218224 }
219225 }
220226
221227 if batch != nil && (timedOut || (len (batch .ops ) >= tw .batchMaxSize )) {
222228 batch .timeoutCancel ()
229+ l .Debugf ("Running batch %s (len=%d)" , batch .id , len (batch .ops ))
223230 tw .runBatch (ctx , batch )
224231 batch = nil
225232 }
@@ -383,6 +390,7 @@ func (tw *transactionWriter) preInsertIdempotencyCheck(ctx context.Context, b *t
383390 txOp .sentConflict = true
384391 txOp .done <- i18n .NewError (ctx , tmmsgs .MsgDuplicateID , txOp .txID )
385392 } else {
393+ log .L (ctx ).Debugf ("Adding TX %s from write operation %s to insert idx=%d" , txOp .txID , txOp .opID , len (validInserts ))
386394 validInserts = append (validInserts , txOp .txInsert )
387395 }
388396 }
@@ -413,9 +421,19 @@ func (tw *transactionWriter) executeBatchOps(ctx context.Context, b *transaction
413421 }
414422 }
415423 // Do all the transaction updates
424+ mergedUpdates := make (map [string ]* apitypes.TXUpdates )
416425 for _ , op := range b .txUpdates {
417- if err := tw .p .updateTransaction (ctx , op .txID , op .txUpdate ); err != nil {
418- log .L (ctx ).Errorf ("Update transaction %s failed: %s" , op .txID , err )
426+ update , merge := mergedUpdates [op .txID ]
427+ if merge {
428+ update .Merge (op .txUpdate )
429+ } else {
430+ mergedUpdates [op .txID ] = op .txUpdate
431+ }
432+ log .L (ctx ).Debugf ("Updating transaction %s in write operation %s (merged=%t)" , op .txID , op .opID , merge )
433+ }
434+ for txID , update := range mergedUpdates {
435+ if err := tw .p .updateTransaction (ctx , txID , update ); err != nil {
436+ log .L (ctx ).Errorf ("Update transaction %s failed: %s" , txID , err )
419437 return err
420438 }
421439 }
0 commit comments