Skip to content

Commit 9ee088e

Browse files
authored
Merge pull request #4080 from github/henrymercer/studious-giggle
Determine the overlay minimum disk space requirement from feature flags
2 parents d97b342 + 1aef003 commit 9ee088e

4 files changed

Lines changed: 275 additions & 20 deletions

File tree

lib/entry-points.js

Lines changed: 66 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config-utils.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,101 @@ checkOverlayEnablementMacro.serial(
13331333
},
13341334
);
13351335

1336+
// Check that each feature flag lowers the limit to the threshold that its name
1337+
// declares. Both sides of the boundary are needed to pin the threshold down: a
1338+
// mapping to a lower value would still pass the case at the limit, and one to a
1339+
// higher value would still fail the case below it.
1340+
for (const [feature, thresholdGb] of [
1341+
[Feature.OverlayAnalysisMinDisk8Gb, 8],
1342+
[Feature.OverlayAnalysisMinDisk9Gb, 9],
1343+
[Feature.OverlayAnalysisMinDisk10Gb, 10],
1344+
[Feature.OverlayAnalysisMinDisk11Gb, 11],
1345+
[Feature.OverlayAnalysisMinDisk12Gb, 12],
1346+
[Feature.OverlayAnalysisMinDisk13Gb, 13],
1347+
] as Array<[Feature, number]>) {
1348+
const features = [
1349+
Feature.OverlayAnalysis,
1350+
Feature.OverlayAnalysisCodeScanningJavascript,
1351+
feature,
1352+
];
1353+
1354+
checkOverlayEnablementMacro.serial(
1355+
`Overlay-base database on default branch if ${feature} is enabled and runner disk space is at its limit`,
1356+
{
1357+
languages: [BuiltInLanguage.javascript],
1358+
features,
1359+
isDefaultBranch: true,
1360+
diskUsage: {
1361+
numAvailableBytes: thresholdGb * 1_000_000_000,
1362+
numTotalBytes: 100_000_000_000,
1363+
},
1364+
},
1365+
{
1366+
overlayDatabaseMode: OverlayDatabaseMode.OverlayBase,
1367+
useOverlayDatabaseCaching: true,
1368+
},
1369+
);
1370+
1371+
checkOverlayEnablementMacro.serial(
1372+
`No overlay-base database on default branch if ${feature} is enabled and runner disk space is below its limit`,
1373+
{
1374+
languages: [BuiltInLanguage.javascript],
1375+
features,
1376+
isDefaultBranch: true,
1377+
diskUsage: {
1378+
numAvailableBytes: thresholdGb * 1_000_000_000 - 1_000_000,
1379+
numTotalBytes: 100_000_000_000,
1380+
},
1381+
},
1382+
{
1383+
disabledReason: OverlayDisabledReason.InsufficientDiskSpace,
1384+
},
1385+
);
1386+
}
1387+
1388+
checkOverlayEnablementMacro.serial(
1389+
"Overlay-base database on default branch if runner disk space is exactly at the lowest limit enabled by a feature flag",
1390+
{
1391+
languages: [BuiltInLanguage.javascript],
1392+
features: [
1393+
Feature.OverlayAnalysis,
1394+
Feature.OverlayAnalysisCodeScanningJavascript,
1395+
Feature.OverlayAnalysisMinDisk9Gb,
1396+
Feature.OverlayAnalysisMinDisk12Gb,
1397+
],
1398+
isDefaultBranch: true,
1399+
diskUsage: {
1400+
numAvailableBytes: 9_000_000_000,
1401+
numTotalBytes: 100_000_000_000,
1402+
},
1403+
},
1404+
{
1405+
overlayDatabaseMode: OverlayDatabaseMode.OverlayBase,
1406+
useOverlayDatabaseCaching: true,
1407+
},
1408+
);
1409+
1410+
checkOverlayEnablementMacro.serial(
1411+
"No overlay-base database on default branch if runner disk space is below the lowest limit enabled by a feature flag",
1412+
{
1413+
languages: [BuiltInLanguage.javascript],
1414+
features: [
1415+
Feature.OverlayAnalysis,
1416+
Feature.OverlayAnalysisCodeScanningJavascript,
1417+
Feature.OverlayAnalysisMinDisk9Gb,
1418+
Feature.OverlayAnalysisMinDisk12Gb,
1419+
],
1420+
isDefaultBranch: true,
1421+
diskUsage: {
1422+
numAvailableBytes: 8_500_000_000,
1423+
numTotalBytes: 100_000_000_000,
1424+
},
1425+
},
1426+
{
1427+
disabledReason: OverlayDisabledReason.InsufficientDiskSpace,
1428+
},
1429+
);
1430+
13361431
checkOverlayEnablementMacro.serial(
13371432
"No overlay-base database on default branch if memory flag is too low",
13381433
{

src/config-utils.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
import { prepareDiffInformedAnalysis } from "./diff-informed-analysis-utils";
4949
import { EnvVar } from "./environment";
5050
import * as errorMessages from "./error-messages";
51-
import { Feature, FeatureEnablement } from "./feature-flags";
51+
import { Feature, FeatureEnablement, FeatureWithoutCLI } from "./feature-flags";
5252
import {
5353
RepositoryProperties,
5454
RepositoryPropertyName,
@@ -101,10 +101,23 @@ export { type Config } from "./config/action-config";
101101
* whether to perform overlay analysis, then the action will not perform overlay
102102
* analysis unless overlay analysis has been explicitly enabled via environment
103103
* variable.
104+
*
105+
* This threshold can be lowered by the feature flags in
106+
* `OVERLAY_MINIMUM_DISK_SPACE_MB_BY_FEATURE`.
104107
*/
105108
const OVERLAY_MINIMUM_AVAILABLE_DISK_SPACE_MB = 14000;
106-
const OVERLAY_MINIMUM_AVAILABLE_DISK_SPACE_BYTES =
107-
OVERLAY_MINIMUM_AVAILABLE_DISK_SPACE_MB * 1_000_000;
109+
110+
/**
111+
* Minimum available disk space (in MB) enabled by each overlay feature flag.
112+
*/
113+
const OVERLAY_MINIMUM_DISK_SPACE_MB_BY_FEATURE = {
114+
[Feature.OverlayAnalysisMinDisk8Gb]: 8000,
115+
[Feature.OverlayAnalysisMinDisk9Gb]: 9000,
116+
[Feature.OverlayAnalysisMinDisk10Gb]: 10000,
117+
[Feature.OverlayAnalysisMinDisk11Gb]: 11000,
118+
[Feature.OverlayAnalysisMinDisk12Gb]: 12000,
119+
[Feature.OverlayAnalysisMinDisk13Gb]: 13000,
120+
} satisfies Partial<Record<FeatureWithoutCLI, number>>;
108121

109122
/**
110123
* The minimum memory (in MB) that must be available for CodeQL to perform overlay analysis. If
@@ -579,21 +592,44 @@ async function checkOverlayAnalysisFeatureEnabled(
579592
return new Success(undefined);
580593
}
581594

595+
/**
596+
* Returns the minimum available disk space (in MB) required to perform overlay
597+
* analysis, which is the lowest threshold enabled by a feature flag, or the
598+
* default threshold if no such feature flag is enabled.
599+
*/
600+
async function getMinimumDiskSpaceMb(
601+
features: FeatureEnablement,
602+
): Promise<number> {
603+
let minimumMb = OVERLAY_MINIMUM_AVAILABLE_DISK_SPACE_MB;
604+
for (const [feature, thresholdMb] of Object.entries(
605+
OVERLAY_MINIMUM_DISK_SPACE_MB_BY_FEATURE,
606+
)) {
607+
if (await features.getValue(feature as FeatureWithoutCLI)) {
608+
minimumMb = Math.min(minimumMb, thresholdMb);
609+
}
610+
}
611+
return minimumMb;
612+
}
613+
582614
/** Checks if the runner has enough disk space for overlay analysis. */
583615
function runnerHasSufficientDiskSpace(
584616
diskUsage: DiskUsage,
585617
logger: Logger,
618+
minimumDiskSpaceMb: number,
586619
): boolean {
587-
const minimumDiskSpaceBytes = OVERLAY_MINIMUM_AVAILABLE_DISK_SPACE_BYTES;
588-
if (diskUsage.numAvailableBytes < minimumDiskSpaceBytes) {
589-
const diskSpaceMb = Math.round(diskUsage.numAvailableBytes / 1_000_000);
590-
const minimumDiskSpaceMb = Math.round(minimumDiskSpaceBytes / 1_000_000);
620+
const diskSpaceMb = Math.round(diskUsage.numAvailableBytes / 1_000_000);
621+
if (diskUsage.numAvailableBytes < minimumDiskSpaceMb * 1_000_000) {
591622
logger.info(
592623
`Setting overlay database mode to ${OverlayDatabaseMode.None} ` +
593624
`due to insufficient disk space (${diskSpaceMb} MB, needed ${minimumDiskSpaceMb} MB).`,
594625
);
595626
return false;
596627
}
628+
629+
logger.debug(
630+
`Disk space available for CodeQL analysis is ${diskSpaceMb} MB, which is at or above the ` +
631+
`minimum of ${minimumDiskSpaceMb} MB.`,
632+
);
597633
return true;
598634
}
599635

@@ -625,7 +661,7 @@ async function runnerHasSufficientMemory(
625661
}
626662

627663
logger.debug(
628-
`Memory available for CodeQL analysis is ${memoryFlagValue} MB, which is above the minimum of ${OVERLAY_MINIMUM_MEMORY_MB} MB.`,
664+
`Memory available for CodeQL analysis is ${memoryFlagValue} MB, which is at or above the minimum of ${OVERLAY_MINIMUM_MEMORY_MB} MB.`,
629665
);
630666
return true;
631667
}
@@ -636,11 +672,13 @@ async function runnerHasSufficientMemory(
636672
*/
637673
async function checkRunnerResources(
638674
codeql: CodeQL,
675+
features: FeatureEnablement,
639676
diskUsage: DiskUsage,
640677
ramInput: string | undefined,
641678
logger: Logger,
642679
): Promise<Result<void, OverlayDisabledReason>> {
643-
if (!runnerHasSufficientDiskSpace(diskUsage, logger)) {
680+
const minimumDiskSpaceMb = await getMinimumDiskSpaceMb(features);
681+
if (!runnerHasSufficientDiskSpace(diskUsage, logger, minimumDiskSpaceMb)) {
644682
return new Failure(OverlayDisabledReason.InsufficientDiskSpace);
645683
}
646684
if (!(await runnerHasSufficientMemory(codeql, ramInput, logger))) {
@@ -752,7 +790,13 @@ export async function checkOverlayEnablement(
752790
}
753791
const resourceResult =
754792
performResourceChecks && diskUsage !== undefined
755-
? await checkRunnerResources(codeql, diskUsage, ramInput, logger)
793+
? await checkRunnerResources(
794+
codeql,
795+
features,
796+
diskUsage,
797+
ramInput,
798+
logger,
799+
)
756800
: new Success<void>(undefined);
757801
if (resourceResult.isFailure()) {
758802
return resourceResult;

0 commit comments

Comments
 (0)