(ctx context.Context, po ProcessingOptions, countOnly bool, done <-chan struct{})
| 47 | const defaultBatchSize = 10 |
| 48 | |
| 49 | func (p *processor) beginProcessing(ctx context.Context, po ProcessingOptions, countOnly bool, done <-chan struct{}) (*sync.WaitGroup, chan<- *Graph) { |
| 50 | wg := new(sync.WaitGroup) |
| 51 | ch := make(chan *Graph) |
| 52 | |
| 53 | if po.BatchSize <= 0 { |
| 54 | po.BatchSize = defaultBatchSize |
| 55 | } |
| 56 | |
| 57 | wg.Add(1) |
| 58 | go func() { |
| 59 | defer wg.Done() |
| 60 | |
| 61 | // addToBatch adds g to the batch, and if the batch is full, it |
| 62 | // sends it for processing and resets the batch. If g is nil, |
| 63 | // the batch is processed regardless of its size. |
| 64 | addToBatch := func(g *Graph) { |
| 65 | var batch []*Graph |
| 66 | |
| 67 | // add the new graph to the batch, keeping track of its actual |
| 68 | // nested size; and if the batch is now large enough to process, |
| 69 | // copy it (just the slice header) then reset the batch |
| 70 | if g != nil { |
| 71 | p.batch = append(p.batch, g) |
| 72 | p.batchSize += g.Size() |
| 73 | } |
| 74 | if p.batchSize >= po.BatchSize || (g == nil && len(p.batch) > 0) { |
| 75 | batch = p.batch |
| 76 | p.batch = make([]*Graph, 0, po.BatchSize) |
| 77 | p.batchSize = 0 |
| 78 | } |
| 79 | |
| 80 | if len(batch) > 0 { |
| 81 | err := p.pipeline(ctx, batch) |
| 82 | if err != nil { |
| 83 | p.log.Error("batch pipeline", zap.Error(err)) |
| 84 | } |
| 85 | |
| 86 | lastChkptIdx := len(batch) - 1 |
| 87 | for i := len(batch) - 1; i >= 0; i-- { |
| 88 | if batch[i].Checkpoint != nil { |
| 89 | lastChkptIdx = i |
| 90 | break |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | var batchSizeToCheckpoint int |
| 95 | for i := 0; i <= lastChkptIdx; i++ { |
| 96 | batchSizeToCheckpoint += batch[i].Size() |
| 97 | } |
| 98 | |
| 99 | p.ij.job.Progress(batchSizeToCheckpoint) |
| 100 | |
| 101 | // persist the last (most recent) checkpoint in the batch |
| 102 | if batch[lastChkptIdx].Checkpoint != nil { |
| 103 | if err := p.ij.checkpoint(p.estimatedCount, p.outerLoopIdx, p.innerLoopIdx, batch[lastChkptIdx].Checkpoint); err != nil { |
| 104 | p.log.Error("checkpointing", zap.Error(err)) |
| 105 | } |
| 106 | } |
no test coverage detected