Probe bootstrap tip optimistically · Entire

Probe bootstrap tip optimistically

3f2d1cb→main·

Soph·3mo ago·2 files·+86 added/-4 removed

Sessions

021d8d15741bView transcript

Changes

2

371 unmodified lines

prevIdx := -1
prevHash := plumbing.ZeroHash
prevSpan := initialSpan
prevMeasuredBytes := 0
probeCache := make(map[string]probeResult)
prefetched := make(map[plumbing.Hash][]byte)
for prevIdx < len(chain)-1 {
    bestIdx, err := planner.SampledCheckpointUnderLimit(chain, prevIdx, prevSpan, func(idx int) (bool, error) {
        probe := func(idx int) (bool, error) {
            cacheKey := prevHash.String() + ":" + strconv.Itoa(idx)
            if result, ok := probeCache[cacheKey]; ok {
                return result.tooLarge, nil
            }
            probeCache[cacheKey] = probeResult{tooLarge: tooLarge, data: data}
            return tooLarge, nil
        })
    ...

return next
}

func shouldProbeTipFirst(limit int64, measuredBytes int, measuredSpan int, remaining int) bool {
    if limit <= 0 || measuredBytes <= 0 || measuredSpan <= 0 || remaining <= measuredSpan {
        return false
    }
    estimated := (int64(measuredBytes) * int64(remaining)) / int64(measuredSpan)
    return estimated <= (limit*9)/10
}

func fetchPackForProbe(ctx context.Context, p Params, ref planner.DesiredRef, want, have plumbing.Hash, limit int64) ([]byte, bool, error) {
    desired := singleGP(ref.SourceRef, ref.TargetRef, want)
    haves := planner.SingleHaveMap(have)

Minternal/strategy/bootstrap/bootstrap.go+32/-4

184 unmodified lines

func TestShouldProbeTipFirst(t *testing.T) {
    tests := []struct {
        name         string
        limit        int64
        measured     int
        measuredSpan int
        remaining    int
        want         bool
    }{
        {
            name:         "probes tip when estimate is comfortably under limit",
            limit:        1000,
            measured:     200,
            measuredSpan: 4,
            remaining:    10,
            want:         true,
        },
        {
            name:         "does not probe tip when estimate is too close to limit",
            limit:        1000,
            measured:     400,
            measuredSpan: 4,
            remaining:    10,
            want:         false,
        },
        {
            name:         "does not probe tip without measurements",
            limit:        1000,
            measured:     0,
            measuredSpan: 4,
            remaining:    10,
            want:         false,
        },
        {
            name:         "does not probe tip when remaining span is not larger",
            limit:        1000,
            measured:     200,
            measuredSpan: 4,
            remaining:    4,
            want:         false,
        },
    }

for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := shouldProbeTipFirst(tt.limit, tt.measured, tt.measuredSpan, tt.remaining)
            if got != tt.want {
                t.Fatalf("shouldProbeTipFirst(%d, %d, %d, %d) = %v, want %v",
                    tt.limit, tt.measured, tt.measuredSpan, tt.remaining, got, tt.want)
            }
        })
    }
}

type fakeBootstrapSource struct {
    fetchPack func(context.Context, *gitproto.Conn, map[plumbing.ReferenceName]gitproto.DesiredRef, map[plumbing.ReferenceName]plumbing.Hash) (io.ReadCloser, error)
}