@@ -542,66 +542,112 @@ func closeDataReader(pieceData storiface.Data) {
542
542
}
543
543
544
544
func maybeApplyBackpressure (tx * harmonydb.Tx , cfg config.CurioIngestConfig , ssize abi.SectorSize ) (wait bool , err error ) {
545
- var bufferedSDR , bufferedTrees , bufferedPoRep , waitDealSectors int
546
- err = tx .QueryRow (`
547
- WITH BufferedSDR AS (
548
- SELECT COUNT(p.task_id_sdr) - COUNT(t.owner_id) AS buffered_sdr_count
549
- FROM sectors_sdr_pipeline p
550
- LEFT JOIN harmony_task t ON p.task_id_sdr = t.id
551
- WHERE p.after_sdr = false
552
- ),
553
- BufferedTrees AS (
554
- SELECT COUNT(p.task_id_tree_r) - COUNT(t.owner_id) AS buffered_trees_count
555
- FROM sectors_sdr_pipeline p
556
- LEFT JOIN harmony_task t ON p.task_id_tree_r = t.id
557
- WHERE p.after_sdr = true AND p.after_tree_r = false
558
- ),
559
- BufferedPoRep AS (
560
- SELECT COUNT(p.task_id_porep) - COUNT(t.owner_id) AS buffered_porep_count
561
- FROM sectors_sdr_pipeline p
562
- LEFT JOIN harmony_task t ON p.task_id_porep = t.id
563
- WHERE p.after_tree_r = true AND p.after_porep = false
564
- ),
565
- WaitDealSectors AS (
566
- SELECT COUNT(DISTINCT sip.sector_number) AS wait_deal_sectors_count
567
- FROM sectors_sdr_initial_pieces sip
568
- LEFT JOIN sectors_sdr_pipeline sp ON sip.sp_id = sp.sp_id AND sip.sector_number = sp.sector_number
569
- WHERE sp.sector_number IS NULL
570
- )
571
- SELECT
572
- (SELECT buffered_sdr_count FROM BufferedSDR) AS total_buffered_sdr,
573
- (SELECT buffered_trees_count FROM BufferedTrees) AS buffered_trees_count,
574
- (SELECT buffered_porep_count FROM BufferedPoRep) AS buffered_porep_count,
575
- (SELECT wait_deal_sectors_count FROM WaitDealSectors) AS wait_deal_sectors_count
576
- ` ).Scan (& bufferedSDR , & bufferedTrees , & bufferedPoRep , & waitDealSectors )
577
- if err != nil {
578
- return false , xerrors .Errorf ("counting buffered sectors: %w" , err )
579
- }
580
-
581
545
var pieceSizes []abi.PaddedPieceSize
582
546
583
547
err = tx .Select (& pieceSizes , `SELECT piece_padded_size FROM parked_pieces WHERE complete = false;` )
584
548
if err != nil {
585
549
return false , xerrors .Errorf ("getting in-process pieces: %w" , err )
586
550
}
587
-
588
551
sectors := sectorCount (pieceSizes , abi .PaddedPieceSize (ssize ))
589
- if cfg .MaxQueueDealSector != 0 && waitDealSectors + sectors > cfg .MaxQueueDealSector {
590
- log .Debugw ("backpressure" , "reason" , "too many wait deal sectors" , "wait_deal_sectors" , waitDealSectors , "max" , cfg .MaxQueueDealSector )
591
- return true , nil
592
- }
593
552
594
- if bufferedSDR > cfg .MaxQueueSDR {
595
- log .Debugw ("backpressure" , "reason" , "too many SDR tasks" , "buffered" , bufferedSDR , "max" , cfg .MaxQueueSDR )
596
- return true , nil
597
- }
598
- if cfg .MaxQueueTrees != 0 && bufferedTrees > cfg .MaxQueueTrees {
599
- log .Debugw ("backpressure" , "reason" , "too many tree tasks" , "buffered" , bufferedTrees , "max" , cfg .MaxQueueTrees )
600
- return true , nil
601
- }
602
- if cfg .MaxQueuePoRep != 0 && bufferedPoRep > cfg .MaxQueuePoRep {
603
- log .Debugw ("backpressure" , "reason" , "too many PoRep tasks" , "buffered" , bufferedPoRep , "max" , cfg .MaxQueuePoRep )
604
- return true , nil
553
+ if cfg .DoSnap {
554
+ var bufferedEncode , bufferedProve , waitDealSectors int
555
+ err = tx .QueryRow (`
556
+ WITH BufferedEncode AS (
557
+ SELECT COUNT(p.task_id_encode) - COUNT(t.owner_id) AS buffered_encode
558
+ FROM sectors_snap_pipeline p
559
+ LEFT JOIN harmony_task t ON p.task_id_encode = t.id
560
+ WHERE p.after_encode = false
561
+ ),
562
+ BufferedProve AS (
563
+ SELECT COUNT(p.task_id_prove) - COUNT(t.owner_id) AS buffered_prove
564
+ FROM sectors_snap_pipeline p
565
+ LEFT JOIN harmony_task t ON p.task_id_prove = t.id
566
+ WHERE p.after_prove = true AND p.after_move_storage = false
567
+ ),
568
+ WaitDealSectors AS (
569
+ SELECT COUNT(DISTINCT sip.sector_number) AS wait_deal_sectors_count
570
+ FROM sectors_snap_initial_pieces sip
571
+ LEFT JOIN curio.sectors_snap_pipeline sp ON sip.sp_id = sp.sp_id AND sip.sector_number = sp.sector_number
572
+ WHERE sp.sector_number IS NULL
573
+ )
574
+ SELECT
575
+ (SELECT buffered_encode FROM BufferedEncode) AS total_encode,
576
+ (SELECT buffered_prove FROM BufferedProve) AS buffered_prove,
577
+ (SELECT wait_deal_sectors_count FROM WaitDealSectors) AS wait_deal_sectors_count
578
+ ` ).Scan (& bufferedEncode , & bufferedProve , & waitDealSectors )
579
+ if err != nil {
580
+ return false , xerrors .Errorf ("counting buffered sectors: %w" , err )
581
+ }
582
+
583
+ if cfg .MaxQueueDealSector != 0 && waitDealSectors + sectors > cfg .MaxQueueDealSector {
584
+ log .Infow ("backpressure" , "reason" , "too many wait deal sectors" , "wait_deal_sectors" , waitDealSectors , "max" , cfg .MaxQueueDealSector )
585
+ return true , nil
586
+ }
587
+
588
+ if cfg .MaxQueueSnapEncode != 0 && bufferedEncode > cfg .MaxQueueSnapEncode {
589
+ log .Infow ("backpressure" , "reason" , "too many encode tasks" , "buffered" , bufferedEncode , "max" , cfg .MaxQueueSnapEncode )
590
+ return true , nil
591
+ }
592
+
593
+ if cfg .MaxQueueSnapProve != 0 && bufferedProve > cfg .MaxQueueSnapProve {
594
+ log .Infow ("backpressure" , "reason" , "too many prove tasks" , "buffered" , bufferedProve , "max" , cfg .MaxQueueSnapProve )
595
+ return
596
+ }
597
+ } else {
598
+ var bufferedSDR , bufferedTrees , bufferedPoRep , waitDealSectors int
599
+ err = tx .QueryRow (`
600
+ WITH BufferedSDR AS (
601
+ SELECT COUNT(p.task_id_sdr) - COUNT(t.owner_id) AS buffered_sdr_count
602
+ FROM sectors_sdr_pipeline p
603
+ LEFT JOIN harmony_task t ON p.task_id_sdr = t.id
604
+ WHERE p.after_sdr = false
605
+ ),
606
+ BufferedTrees AS (
607
+ SELECT COUNT(p.task_id_tree_r) - COUNT(t.owner_id) AS buffered_trees_count
608
+ FROM sectors_sdr_pipeline p
609
+ LEFT JOIN harmony_task t ON p.task_id_tree_r = t.id
610
+ WHERE p.after_sdr = true AND p.after_tree_r = false
611
+ ),
612
+ BufferedPoRep AS (
613
+ SELECT COUNT(p.task_id_porep) - COUNT(t.owner_id) AS buffered_porep_count
614
+ FROM sectors_sdr_pipeline p
615
+ LEFT JOIN harmony_task t ON p.task_id_porep = t.id
616
+ WHERE p.after_tree_r = true AND p.after_porep = false
617
+ ),
618
+ WaitDealSectors AS (
619
+ SELECT COUNT(DISTINCT sip.sector_number) AS wait_deal_sectors_count
620
+ FROM sectors_sdr_initial_pieces sip
621
+ LEFT JOIN sectors_sdr_pipeline sp ON sip.sp_id = sp.sp_id AND sip.sector_number = sp.sector_number
622
+ WHERE sp.sector_number IS NULL
623
+ )
624
+ SELECT
625
+ (SELECT buffered_sdr_count FROM BufferedSDR) AS total_buffered_sdr,
626
+ (SELECT buffered_trees_count FROM BufferedTrees) AS buffered_trees_count,
627
+ (SELECT buffered_porep_count FROM BufferedPoRep) AS buffered_porep_count,
628
+ (SELECT wait_deal_sectors_count FROM WaitDealSectors) AS wait_deal_sectors_count
629
+ ` ).Scan (& bufferedSDR , & bufferedTrees , & bufferedPoRep , & waitDealSectors )
630
+ if err != nil {
631
+ return false , xerrors .Errorf ("counting buffered sectors: %w" , err )
632
+ }
633
+
634
+ if cfg .MaxQueueDealSector != 0 && waitDealSectors + sectors > cfg .MaxQueueDealSector {
635
+ log .Infow ("backpressure" , "reason" , "too many wait deal sectors" , "wait_deal_sectors" , waitDealSectors , "max" , cfg .MaxQueueDealSector )
636
+ return true , nil
637
+ }
638
+
639
+ if bufferedSDR > cfg .MaxQueueSDR {
640
+ log .Infow ("backpressure" , "reason" , "too many SDR tasks" , "buffered" , bufferedSDR , "max" , cfg .MaxQueueSDR )
641
+ return true , nil
642
+ }
643
+ if cfg .MaxQueueTrees != 0 && bufferedTrees > cfg .MaxQueueTrees {
644
+ log .Infow ("backpressure" , "reason" , "too many tree tasks" , "buffered" , bufferedTrees , "max" , cfg .MaxQueueTrees )
645
+ return true , nil
646
+ }
647
+ if cfg .MaxQueuePoRep != 0 && bufferedPoRep > cfg .MaxQueuePoRep {
648
+ log .Infow ("backpressure" , "reason" , "too many PoRep tasks" , "buffered" , bufferedPoRep , "max" , cfg .MaxQueuePoRep )
649
+ return true , nil
650
+ }
605
651
}
606
652
607
653
return false , nil
0 commit comments