package ingest
import (
"context"
"log"
"time"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/obs"
"craftdesign.group/quarry/internal/zitcli"
)
// lineCountBatch is how many check-ins' counts one backfill
// transaction writes.
const lineCountBatch = 500
// SetMaintenance hands the pipeline's line-count backfill to the
// maintenance lane: schedule runs the task when the machine can spare
// it, and reports false when that task is already queued. Without it
// the backfill runs on a rebuild slot, as it always did.
func (p *Pipeline) SetMaintenance(schedule func(task string, run func(context.Context) error) bool) {
p.maint = schedule
}
// backfillLineCounts fills the line counts an index folded before they
// existed (#349) is missing: one pass of zit's crosslink, whose counts
// a rebuild would have taken, written only into rows that have none.
// Once per process, in the background — on the maintenance lane when
// one is set, otherwise on a rebuild slot so a fleet of stores does not
// all count at once. The first crosslink with counts pays one diff per
// changed file. It never holds the store's round lock: live folds count
// their own check-ins meanwhile, and the fill skips any row that
// already has counts. It is not part of any rebuild's correctness, which
// is what lets it wait for a quiet machine.
func (p *Pipeline) backfillLineCounts() {
if p.ix.LineCountsDone() || !p.countsStarted.CompareAndSwap(false, true) {
return
}
if p.maint != nil {
p.maint("line counts", func(ctx context.Context) error {
// The pipeline's own stop ends it too.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
select {
case <-p.stop:
cancel()
case <-ctx.Done():
}
}()
return p.countLines(ctx)
})
return
}
go func() {
release, err := acquireRebuild(p.code, p.stop)
if err != nil {
return // stopping
}
defer release()
_ = p.countLines(context.Background())
}()
}
// countLines is the backfill itself. A failure is left unmarked, so the
// next process tries again.
func (p *Pipeline) countLines(ctx context.Context) error {
start := time.Now()
n, err := fillLineCounts(ctx, p.cli, p.ix)
if err != nil {
log.Printf("ingest: %s: line counts: %v", p.code, err)
return err
}
if err := p.ix.MarkLineCountsDone(); err != nil {
log.Printf("ingest: %s: mark line counts: %v", p.code, err)
return err
}
log.Printf("ingest: %s: line counts for %d check-ins in %.1fs", p.code, n, time.Since(start).Seconds())
return nil
}
// fillLineCounts streams crosslink and writes its counts into ix.
func fillLineCounts(ctx context.Context, cli *zitcli.Client, ix *index.Index) (int, error) {
if obs.Reason(ctx) == "" {
ctx = obs.WithReason(ctx, obs.ReasonIngest)
}
n := 0
batch := map[string]map[string]index.LineCount{}
flush := func() error {
if len(batch) == 0 {
return nil
}
err := ix.FillLineCounts(batch)
clear(batch)
return err
}
err := cli.Crosslink(ctx, func(r zitcli.CrosslinkRecord) error {
lines := rootless(r.Parents, indexLines(r.Lines))
if len(lines) == 0 {
return nil
}
batch[r.Hash] = lines
n++
if len(batch) >= lineCountBatch {
return flush()
}
return nil
})
if err != nil {
return n, err
}
return n, flush()
}
// indexLines converts crosslink's counts to the index's.
func indexLines(lines map[string]zitcli.LineCount) map[string]index.LineCount {
if len(lines) == 0 {
return nil
}
out := make(map[string]index.LineCount, len(lines))
for path, lc := range lines {
out[path] = index.LineCount{Added: lc.Added, Removed: lc.Removed}
}
return out
}
// rootless drops the counts of a check-in with no parent. Crosslink
// counts one as all lines added, but zit diff --checkin refuses it, so
// a live fold cannot count it: dropping crosslink's count too keeps a
// rebuilt index and a live one agreeing. A repository's first check-in
// shows its files alone, which is all a reader needs of it.
func rootless(parents []string, lines map[string]index.LineCount) map[string]index.LineCount {
if len(parents) == 0 {
return nil
}
return lines
}