Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __test__/mirror-cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('cleanup commit decision', () => {
process.env.BLACKSMITH_STICKY_DISK_GRPC_PORT = '1'
mirrorPath = fs.mkdtempSync(path.join(os.tmpdir(), 'mirror-cleanup-'))
fs.mkdirSync(path.join(mirrorPath, 'objects', 'pack'), {recursive: true})
// Enough small packs to reach the incremental roll-up limit.
for (let i = 0; i < 50; i++) {
fs.writeFileSync(
path.join(mirrorPath, 'objects', 'pack', `pack-${i}.pack`),
''
)
}
repackExitCode = 0
packRefsExitCode = 0
mockExec.mockResolvedValue(0)
Expand Down
16 changes: 5 additions & 11 deletions __test__/mirror-gc-git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,13 @@ describe('mirror maintenance telemetry', () => {
it('reports a repack that consolidated packs as a real run', async () => {
const mirror = twoPackMirror(root)

const result = await blacksmithCache.cleanup({
exposeId: 'expose-1',
stickyDiskKey: 'owner-repo',
mirrorPath: mirror,
shouldCommit: true,
vmHydratedGitMirror: false
})
const result = await blacksmithCache.runMirrorMaintenance(mirror)

expect(packCount(mirror)).toBe(1)
expect(result.maintenanceResult.success).toBe(true)
expect(result.maintenanceResult.skipped).toBeUndefined()
expect(result.maintenanceResult.durationMs).toBeGreaterThanOrEqual(0)
expect(result.maintenanceResult.mirrorSizeBytes).toBeGreaterThan(0)
expect(result.success).toBe(true)
expect(result.skipped).toBeUndefined()
expect(result.durationMs).toBeGreaterThanOrEqual(0)
expect(result.mirrorSizeBytes).toBeGreaterThan(0)
})

it('reports no maintenance when the disk is not committed', async () => {
Expand Down
24 changes: 24 additions & 0 deletions __test__/mirror-maintenance-git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@ describe('runMirrorMaintenance (real git)', () => {
fsck(mirror)
})

it('defers the roll-up until the small packs reach the limit', async () => {
const {mirror} = buildMirror(root)
const before = packs(mirror)
const smallPacks = before.length - 1

const deferred = await blacksmithCache.runMirrorMaintenance(mirror, {
timeoutSecs: 60,
keepBytes: KEEP_BYTES,
rollupPackLimit: smallPacks + 1
})
expect(deferred).toMatchObject({success: true, skipped: true})
expect(packs(mirror)).toEqual(before)

const rolled = await blacksmithCache.runMirrorMaintenance(mirror, {
timeoutSecs: 60,
keepBytes: KEEP_BYTES,
rollupPackLimit: smallPacks
})
expect(rolled.skipped).toBeUndefined()
expect(packs(mirror).length).toBeLessThan(before.length)
fsck(mirror)
refsResolve(mirror)
})

it('excludes kept packs from the roll-up regardless of size', async () => {
const {mirror, basePack} = buildMirror(root)
// Kept by an earlier run; a larger pack arriving later must not pull
Expand Down
52 changes: 36 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ const MAINTENANCE_TIMEOUT_SECS = 120; // 2 minutes
// Packs at or above this size are kept permanently; the packs below it that
// one run rewrites are chosen so their combined size stays under it too.
const MAINTENANCE_KEEP_PACK_BYTES = 256 * 1024 * 1024;
// A geometric repack that folds any packs enumerates objects across the whole
// mirror, however small the packs (about 18s on a 12 GiB mirror), and every
// concurrent job repeats it on its own copy. Each sync adds one small pack, so
// defer roll-ups until there are as many as git's gc.autoPackLimit default.
const MAINTENANCE_ROLLUP_PACK_LIMIT = 50;
// Kept packs hold on to objects that later become unreachable (deleted or
// rewritten branches). A full rewrite with the keeps lifted reclaims them;
// it is unbounded in size, so it runs rarely and under its own deadline.
Expand Down Expand Up @@ -1730,6 +1735,7 @@ function runMirrorMaintenance(mirrorPath_1) {
const deadline = start + budgetSecs * 1000;
const remainingSecs = () => Math.max(1, Math.ceil((deadline - Date.now()) / 1000));
let deferred = [];
let keptCount = 0;
let label;
let repackArgs;
if (reclaim) {
Expand All @@ -1742,6 +1748,7 @@ function runMirrorMaintenance(mirrorPath_1) {
else {
const selection = yield markKeepPacks(mirrorPath, keepBytes);
deferred = selection.deferred;
keptCount = selection.kept.length;
label = 'Incremental';
repackArgs = ['-d', '-l', '-n', '--geometric=2'];
core.info(`[git-mirror] Running incremental maintenance (timeout: ${budgetSecs}s, ${selection.kept.length} kept pack(s), ${deferred.length} deferred)`);
Expand All @@ -1750,6 +1757,15 @@ function runMirrorMaintenance(mirrorPath_1) {
// pack consolidation; loose objects are never walked on the sticky disk.
const sizeBefore = yield (0, mirror_telemetry_1.packSizeBytesOrNull)(mirrorPath);
const packsBefore = yield (0, mirror_telemetry_1.packNamesOrNull)(mirrorPath);
const smallPacks = packsBefore === null
? null
: packsBefore.length - keptCount - deferred.length;
const rollUp = reclaim ||
smallPacks === null ||
smallPacks >= (options.rollupPackLimit ?? 0);
if (!rollUp) {
core.info(`[git-mirror] ${smallPacks} small pack(s), below the roll-up limit of ${options.rollupPackLimit}; skipping repack`);
}
const fail = (timedOut, error) => __awaiter(this, void 0, void 0, function* () {
core.warning(`[git-mirror] ${timedOut ? `Maintenance timed out after ${budgetSecs}s` : error}; committing the synced mirror without it`);
yield removeMaintenanceLeftovers(mirrorPath);
Expand All @@ -1766,21 +1782,23 @@ function runMirrorMaintenance(mirrorPath_1) {
if (!(yield removeMultiPackIndex(mirrorPath))) {
return yield fail(false, 'could not remove the multi-pack-index');
}
const result = yield exec.getExecOutput('timeout', [
String(remainingSecs()),
'git',
'-c',
'repack.writeBitmaps=false',
'-C',
mirrorPath,
'repack',
...repackArgs
], { ignoreReturnCode: true });
if (result.exitCode === TIMEOUT_EXIT_CODE) {
return yield fail(true, `git repack timed out after ${budgetSecs}s`);
}
if (result.exitCode !== 0) {
return yield fail(false, `git repack failed with exit code ${result.exitCode}`);
if (rollUp) {
const result = yield exec.getExecOutput('timeout', [
String(remainingSecs()),
'git',
'-c',
'repack.writeBitmaps=false',
'-C',
mirrorPath,
'repack',
...repackArgs
], { ignoreReturnCode: true });
if (result.exitCode === TIMEOUT_EXIT_CODE) {
return yield fail(true, `git repack timed out after ${budgetSecs}s`);
}
if (result.exitCode !== 0) {
return yield fail(false, `git repack failed with exit code ${result.exitCode}`);
}
}
if (reclaim) {
const prune = yield exec.getExecOutput('timeout', [
Expand Down Expand Up @@ -1963,7 +1981,9 @@ function cleanup(options) {
}
// Maintenance only pays off if the result is persisted.
if (mirrorPath && shouldCommit) {
result.maintenanceResult = yield runMirrorMaintenance(mirrorPath);
result.maintenanceResult = yield runMirrorMaintenance(mirrorPath, {
rollupPackLimit: MAINTENANCE_ROLLUP_PACK_LIMIT
});
// Catch-up for mirrors that predate commit-graph writing: build the
// initial graph here in the post step, off the checkout critical path.
// Once it exists, the sync fetch keeps it current incrementally.
Expand Down
71 changes: 49 additions & 22 deletions src/blacksmith-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const MAINTENANCE_TIMEOUT_SECS = 120 // 2 minutes
// Packs at or above this size are kept permanently; the packs below it that
// one run rewrites are chosen so their combined size stays under it too.
const MAINTENANCE_KEEP_PACK_BYTES = 256 * 1024 * 1024
// A geometric repack that folds any packs enumerates objects across the whole
// mirror, however small the packs (about 18s on a 12 GiB mirror), and every
// concurrent job repeats it on its own copy. Each sync adds one small pack, so
// defer roll-ups until there are as many as git's gc.autoPackLimit default.
const MAINTENANCE_ROLLUP_PACK_LIMIT = 50
// Kept packs hold on to objects that later become unreachable (deleted or
// rewritten branches). A full rewrite with the keeps lifted reclaims them;
// it is unbounded in size, so it runs rarely and under its own deadline.
Expand Down Expand Up @@ -1976,6 +1981,9 @@ async function reclaimDue(
export interface MaintenanceOptions {
timeoutSecs?: number
keepBytes?: number
// Minimum number of small (not kept or deferred) packs before an
// incremental run rolls them up; 0 always rolls up.
rollupPackLimit?: number
reclaimIntervalMs?: number
reclaimTimeoutSecs?: number
now?: number
Expand Down Expand Up @@ -2046,6 +2054,7 @@ export async function runMirrorMaintenance(
Math.max(1, Math.ceil((deadline - Date.now()) / 1000))

let deferred: string[] = []
let keptCount = 0
let label: string
let repackArgs: string[]
if (reclaim) {
Expand All @@ -2059,6 +2068,7 @@ export async function runMirrorMaintenance(
} else {
const selection = await markKeepPacks(mirrorPath, keepBytes)
deferred = selection.deferred
keptCount = selection.kept.length
label = 'Incremental'
repackArgs = ['-d', '-l', '-n', '--geometric=2']
core.info(
Expand All @@ -2070,6 +2080,19 @@ export async function runMirrorMaintenance(
// pack consolidation; loose objects are never walked on the sticky disk.
const sizeBefore = await packSizeBytesOrNull(mirrorPath)
const packsBefore = await packNamesOrNull(mirrorPath)
const smallPacks =
packsBefore === null
? null
: packsBefore.length - keptCount - deferred.length
const rollUp =
reclaim ||
smallPacks === null ||
smallPacks >= (options.rollupPackLimit ?? 0)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if (!rollUp) {
core.info(
`[git-mirror] ${smallPacks} small pack(s), below the roll-up limit of ${options.rollupPackLimit}; skipping repack`
)
}

const fail = async (
timedOut: boolean,
Expand All @@ -2095,28 +2118,30 @@ export async function runMirrorMaintenance(
if (!(await removeMultiPackIndex(mirrorPath))) {
return await fail(false, 'could not remove the multi-pack-index')
}
const result = await exec.getExecOutput(
'timeout',
[
String(remainingSecs()),
'git',
'-c',
'repack.writeBitmaps=false',
'-C',
mirrorPath,
'repack',
...repackArgs
],
{ignoreReturnCode: true}
)
if (result.exitCode === TIMEOUT_EXIT_CODE) {
return await fail(true, `git repack timed out after ${budgetSecs}s`)
}
if (result.exitCode !== 0) {
return await fail(
false,
`git repack failed with exit code ${result.exitCode}`
if (rollUp) {
const result = await exec.getExecOutput(
'timeout',
[
String(remainingSecs()),
'git',
'-c',
'repack.writeBitmaps=false',
'-C',
mirrorPath,
'repack',
...repackArgs
],
{ignoreReturnCode: true}
)
if (result.exitCode === TIMEOUT_EXIT_CODE) {
return await fail(true, `git repack timed out after ${budgetSecs}s`)
}
if (result.exitCode !== 0) {
return await fail(
false,
`git repack failed with exit code ${result.exitCode}`
)
}
}

if (reclaim) {
Expand Down Expand Up @@ -2376,7 +2401,9 @@ export async function cleanup(options: CleanupOptions): Promise<CleanupResult> {

// Maintenance only pays off if the result is persisted.
if (mirrorPath && shouldCommit) {
result.maintenanceResult = await runMirrorMaintenance(mirrorPath)
result.maintenanceResult = await runMirrorMaintenance(mirrorPath, {
rollupPackLimit: MAINTENANCE_ROLLUP_PACK_LIMIT
})
// Catch-up for mirrors that predate commit-graph writing: build the
// initial graph here in the post step, off the checkout critical path.
// Once it exists, the sync fetch keeps it current incrementally.
Expand Down
Loading