@@ -81,6 +81,8 @@ type tsoDispatcher struct {
81
81
tsDeadlineCh chan * deadline
82
82
lastTSOInfo * tsoInfo
83
83
84
+ batchBufferPool sync.Pool
85
+
84
86
updateConnectionCtxsCh chan struct {}
85
87
}
86
88
@@ -186,7 +188,7 @@ func (td *tsoDispatcher) handleDispatcher(wg *sync.WaitGroup) {
186
188
streamCtx context.Context
187
189
cancel context.CancelFunc
188
190
streamURL string
189
- stream * tsoStream
191
+ stream * tsoStream [ notifier ]
190
192
)
191
193
// Loop through each batch of TSO requests and send them for processing.
192
194
streamLoopTimer := time .NewTimer (option .timeout )
@@ -422,26 +424,44 @@ func (td *tsoDispatcher) processRequests(
422
424
keyspaceID = svcDiscovery .GetKeyspaceID ()
423
425
reqKeyspaceGroupID = svcDiscovery .GetKeyspaceGroupID ()
424
426
)
425
- respKeyspaceGroupID , physical , logical , suffixBits , err := stream .processRequests (
427
+
428
+ cb := func (result tsoRequestResult , reqKeyspaceGroupID uint32 , streamURL string , err error ) {
429
+ curTSOInfo := & tsoInfo {
430
+ tsoServer : stream .getServerURL (),
431
+ reqKeyspaceGroupID : reqKeyspaceGroupID ,
432
+ respKeyspaceGroupID : result .respKeyspaceGroupID ,
433
+ respReceivedAt : time .Now (),
434
+ physical : result .physical ,
435
+ logical : result .logical ,
436
+ }
437
+ // `logical` is the largest ts's logical part here, we need to do the subtracting before we finish each TSO request.
438
+ firstLogical := tsoutil .AddLogical (result .logical , - int64 (result .count )+ 1 , result .suffixBits )
439
+ td .compareAndSwapTS (curTSOInfo , firstLogical )
440
+ finishCollectedRequests (tbc .getCollectedRequests (), result .physical , firstLogical , result .suffixBits , err )
441
+ tbc .collectedRequestCount = 0
442
+ td .batchBufferPool .Put (tbc )
443
+ }
444
+
445
+ err := stream .processRequests (
426
446
clusterID , keyspaceID , reqKeyspaceGroupID ,
427
- dcLocation , count , tbc .batchStartTime )
447
+ dcLocation , count , tbc .batchStartTime , cb )
428
448
if err != nil {
429
449
tbc .finishCollectedRequests (0 , 0 , 0 , err )
430
450
return err
431
451
}
432
- curTSOInfo := & tsoInfo {
433
- tsoServer : stream .getServerURL (),
434
- reqKeyspaceGroupID : reqKeyspaceGroupID ,
435
- respKeyspaceGroupID : respKeyspaceGroupID ,
436
- respReceivedAt : time .Now (),
437
- physical : physical ,
438
- logical : logical ,
439
- }
440
- // `logical` is the largest ts's logical part here, we need to do the subtracting before we finish each TSO request.
441
- firstLogical := tsoutil .AddLogical (logical , - count + 1 , suffixBits )
442
- td .compareAndSwapTS (curTSOInfo , firstLogical )
443
- tbc .finishCollectedRequests (physical , firstLogical , suffixBits , nil )
444
- return nil
452
+ // curTSOInfo := &tsoInfo{
453
+ // tsoServer: stream.getServerURL(),
454
+ // reqKeyspaceGroupID: reqKeyspaceGroupID,
455
+ // respKeyspaceGroupID: respKeyspaceGroupID,
456
+ // respReceivedAt: time.Now(),
457
+ // physical: physical,
458
+ // logical: logical,
459
+ // }
460
+ //// `logical` is the largest ts's logical part here, we need to do the subtracting before we finish each TSO request.
461
+ // firstLogical := tsoutil.AddLogical(logical, -count+1, suffixBits)
462
+ // td.compareAndSwapTS(curTSOInfo, firstLogical)
463
+ // tbc.finishCollectedRequests(physical, firstLogical, suffixBits, nil)
464
+ // return nil
445
465
}
446
466
447
467
func (td * tsoDispatcher ) compareAndSwapTS (
@@ -482,3 +502,24 @@ func (td *tsoDispatcher) compareAndSwapTS(
482
502
}
483
503
td .lastTSOInfo = curTSOInfo
484
504
}
505
+
506
+ type notifier struct {
507
+ tbc * tsoBatchController
508
+ td * tsoDispatcher
509
+ }
510
+
511
+ func (n notifier ) finish (result tsoRequestResult , err error ) {
512
+ curTSOInfo := & tsoInfo {
513
+ tsoServer : stream .getServerURL (),
514
+ reqKeyspaceGroupID : reqKeyspaceGroupID ,
515
+ respKeyspaceGroupID : result .respKeyspaceGroupID ,
516
+ respReceivedAt : time .Now (),
517
+ physical : result .physical ,
518
+ logical : result .logical ,
519
+ }
520
+ // `logical` is the largest ts's logical part here, we need to do the subtracting before we finish each TSO request.
521
+ firstLogical := tsoutil .AddLogical (result .logical , - int64 (result .count )+ 1 , result .suffixBits )
522
+ n .td .compareAndSwapTS (curTSOInfo , firstLogical )
523
+ finishCollectedRequests (n .tbc .getCollectedRequests (), result .physical , firstLogical , result .suffixBits , err )
524
+ n .tbc .collectedRequestCount = 0
525
+ }
0 commit comments