Use two-stage bootstrap batch planning · Entire
Use two-stage bootstrap batch planning
a6721ca→main·
Soph·3mo ago·2 files·+156 added/-17 removed
Sessions
0670ea271363View transcript
Changes
2
- internal/syncer
- Msyncer.go+120/-17
- Msyncer_test.go+36
1175 unmodified lines
1176
1177
1178
1179
1180
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1192
1194
1195
1196
1197
12 unmodified lines
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1255
1256
1257
1258
1226
1259
1260
1261
1229
1230
1262
1263
1264
1265
1266
1233
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1235
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1175 unmodified lines
checkpoints := make([]plumbing.Hash, 0, len(chain))
prevIdx := -1
prevHash := plumbing.ZeroHash
prevSpan := 0
for prevIdx < len(chain)-1 {
bestIdx, err := largestCheckpointUnderLimit(ctx, cfg, sourceConn, sourceService, ref, chain, prevIdx, prevHash)
bestIdx, err := largestCheckpointUnderLimit(ctx, cfg, sourceConn, sourceService, ref, chain, prevIdx, prevHash, prevSpan)
if err != nil {
return nil, err
}
if bestIdx <= prevIdx {
return nil, fmt.Errorf("could not find bootstrap checkpoint for %s under batch-max-pack-bytes=%d", ref.TargetRef, cfg.BatchMaxPackBytes)
}
prevSpan = bestIdx - prevIdx
prevIdx = bestIdx
prevHash = chain[bestIdx]
checkpoints = append(checkpoints, prevHash)
progressf(
cfg.Verbose,
"bootstrap-batch: branch=%s planned-checkpoint=%s (%d/%d)",
"bootstrap-batch: branch=%s planned-checkpoint=%s selected=%d chain-len=%d",
ref.TargetRef,
shortHash(prevHash),
len(checkpoints),
12 unmodified lines
chain []plumbing.Hash,
prevIdx int,
prevHash plumbing.Hash,
prevSpan int,
) (int, error) {
return largestCheckpointUnderLimitByProbe(
chain,
prevIdx,
prevSpan,
func(idx int) (bool, error) {
progressf(
cfg.Verbose,
"bootstrap-batch: branch=%s probe checkpoint=%s base=%s",
ref.TargetRef,
shortHash(chain[idx]),
shortHash(prevHash),
)
tooLarge, err := sourcePackExceedsLimit(ctx, sourceConn, sourceService, ref, chain[idx], prevHash, cfg.BatchMaxPackBytes)
if err != nil {
return false, fmt.Errorf("measure bootstrap batch for %s at %s: %w", ref.TargetRef, shortHash(chain[idx]), err)
}
if tooLarge {
progressf(cfg.Verbose, "bootstrap-batch: checkpoint=%s exceeds limit=%d", shortHash(chain[idx]), cfg.BatchMaxPackBytes)
} else {
progressf(cfg.Verbose, "bootstrap-batch: checkpoint=%s fits limit=%d", shortHash(chain[idx]), cfg.BatchMaxPackBytes)
}
return tooLarge, nil
},
)
}
func largestCheckpointUnderLimitByProbe(
chain []plumbing.Hash,
prevIdx int,
prevSpan int,
probe func(idx int) (bool, error),
) (int, error) {
lo := prevIdx + 1
hi := len(chain) - 1
if lo > hi {
return -1, nil
}
coarse := coarseCheckpointCandidates(lo, hi, prevSpan)
best := -1
for lo <= hi {
mid := lo + (hi-lo)/2
progressf(
cfg.Verbose,
"bootstrap-batch: branch=%s probe checkpoint=%s base=%s",
ref.TargetRef,
shortHash(chain[mid]),
shortHash(prevHash),
)
tooLarge, err := sourcePackExceedsLimit(ctx, sourceConn, sourceService, ref, chain[mid], prevHash, cfg.BatchMaxPackBytes)
firstTooLarge := -1
for _, idx := range coarse {
tooLarge, err := probe(idx)
if err != nil {
return -1, fmt.Errorf("measure bootstrap batch for %s at %s: %w", ref.TargetRef, shortHash(chain[mid]), err)
}
if tooLarge {
progressf(cfg.Verbose, "bootstrap-batch: checkpoint=%s exceeds limit=%d", shortHash(chain[mid]), cfg.BatchMaxPackBytes)
hi = mid - 1
if firstTooLarge == -1 || idx < firstTooLarge {
firstTooLarge = idx
}
continue
}
progressf(cfg.Verbose, "bootstrap-batch: checkpoint=%s fits limit=%d", shortHash(chain[mid]), cfg.BatchMaxPackBytes)
best = idx
break
}
if best == -1 {
return -1, nil
}
if best == hi {
return best, nil
}
searchLo := best + 1
searchHi := hi
if firstTooLarge != -1 {
searchHi = firstTooLarge - 1
}
for searchLo <= searchHi {
mid := searchLo + (searchHi-searchLo)/2
tooLarge, err := probe(mid)
if err != nil {
return -1, err
}
if tooLarge {
searchHi = mid - 1
continue
}
best = mid
lo = mid + 1
searchLo = mid + 1
}
return best, nil
}
func coarseCheckpointCandidates(lo, hi int, prevSpan int) []int {
if lo > hi {
return nil
}
set := map[int]struct{}{
hi: {},
lo: {},
}
if prevSpan > 0 {
projected := lo + prevSpan - 1
if projected < lo {
projected = lo
}
if projected > hi {
projected = hi
}
set[projected] = struct{}{}
}
const coarseBuckets = 6
width := hi - lo
if width > 0 {
for i := 1; i < coarseBuckets; i++ {
idx := lo + (width*i)/coarseBuckets
if idx < lo {
idx = lo
}
if idx > hi {
idx = hi
}
set[idx] = struct{}{}
}
}
candidates := make([]int, 0, len(set))
for idx := range set {
candidates = append(candidates, idx)
}
sort.Sort(sort.Reverse(sort.IntSlice(candidates)))
return candidates
}
func firstParentChain(repo *git.Repository, tip plumbing.Hash) ([]plumbing.Hash, error) {
commit, err := repo.CommitObject(tip)
if err != nil {