Skip to content

Commit 78ab5c2

Browse files
committed
Compute cache key for overlay language status
1 parent c8e1e56 commit 78ab5c2

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/overlay/status.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import test from "ava";
2+
3+
import { mockCodeQLVersion, setupTests } from "../testing-utils";
4+
import { DiskUsage } from "../util";
5+
6+
import { getCacheKey } from "./status";
7+
8+
setupTests(test);
9+
10+
function makeDiskUsage(totalGiB: number): DiskUsage {
11+
return {
12+
numTotalBytes: totalGiB * 1024 * 1024 * 1024,
13+
numAvailableBytes: 0,
14+
};
15+
}
16+
17+
test("getCacheKey incorporates language, CodeQL version, and disk space", async (t) => {
18+
const codeql = mockCodeQLVersion("2.20.0");
19+
t.is(
20+
await getCacheKey(codeql, "javascript", makeDiskUsage(50)),
21+
"codeql-overlay-status-javascript-2.20.0-runner-50GB",
22+
);
23+
t.is(
24+
await getCacheKey(codeql, "python", makeDiskUsage(50)),
25+
"codeql-overlay-status-python-2.20.0-runner-50GB",
26+
);
27+
t.is(
28+
await getCacheKey(
29+
mockCodeQLVersion("2.21.0"),
30+
"javascript",
31+
makeDiskUsage(50),
32+
),
33+
"codeql-overlay-status-javascript-2.21.0-runner-50GB",
34+
);
35+
t.is(
36+
await getCacheKey(codeql, "javascript", makeDiskUsage(100)),
37+
"codeql-overlay-status-javascript-2.20.0-runner-100GB",
38+
);
39+
});
40+
41+
test("getCacheKey rounds disk space down to nearest 10 GiB", async (t) => {
42+
const codeql = mockCodeQLVersion("2.20.0");
43+
t.is(
44+
await getCacheKey(codeql, "javascript", makeDiskUsage(14)),
45+
"codeql-overlay-status-javascript-2.20.0-runner-10GB",
46+
);
47+
t.is(
48+
await getCacheKey(codeql, "javascript", makeDiskUsage(19)),
49+
"codeql-overlay-status-javascript-2.20.0-runner-10GB",
50+
);
51+
});

src/overlay/status.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* We perform enablement checks for overlay analysis to avoid using it on runners that are too small
3+
* to support it. However these checks cannot avoid every potential issue without being overly
4+
* conservative. Therefore, if our enablement checks enable overlay analysis for a runner that is
5+
* too small, we want to remember that, so that we will not try to use overlay analysis until
6+
* something changes (e.g. a larger runner is provisioned, or a new CodeQL version is released).
7+
*
8+
* We use the Actions cache as a lightweight way of providing this functionality.
9+
*/
10+
11+
import { type CodeQL } from "../codeql";
12+
import { DiskUsage } from "../util";
13+
14+
export async function getCacheKey(
15+
codeql: CodeQL,
16+
language: string,
17+
diskUsage: DiskUsage,
18+
): Promise<string> {
19+
// Total disk space, rounded to the nearest 10 GB. This is included in the cache key so that if a
20+
// customer upgrades their runner, we will try again to use overlay analysis, even if the CodeQL
21+
// version has not changed. We round to the nearest 10 GB to work around small differences in disk
22+
// space.
23+
//
24+
// Limitation: this can still flip from "too small" to "large enough" and back again if the disk
25+
// space fluctuates above and below a multiple of 10 GB.
26+
const diskSpaceToNearest10Gb = `${10 * Math.floor(diskUsage.numTotalBytes / (10 * 1024 * 1024 * 1024))}GB`;
27+
28+
// Include the CodeQL version in the cache key so we will try again to use overlay analysis when
29+
// new queries and libraries that may be more efficient are released.
30+
return `codeql-overlay-status-${language}-${(await codeql.getVersion()).version}-runner-${diskSpaceToNearest10Gb}`;
31+
}

0 commit comments

Comments
 (0)