Skip to content

Commit 0a3d60d

Browse files
committed
Overlay: clarify save vs restore keys
1 parent fc58478 commit 0a3d60d

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

src/overlay-database-utils.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,19 @@ export async function uploadOverlayBaseDatabaseToCache(
253253

254254
const codeQlVersion = (await codeql.getVersion()).version;
255255
const checkoutPath = getRequiredInput("checkout_path");
256-
const cacheKey = await generateCacheKey(config, codeQlVersion, checkoutPath);
256+
const cacheSaveKey = await getCacheSaveKey(
257+
config,
258+
codeQlVersion,
259+
checkoutPath,
260+
);
257261
logger.info(
258-
`Uploading overlay-base database to Actions cache with key ${cacheKey}`,
262+
`Uploading overlay-base database to Actions cache with key ${cacheSaveKey}`,
259263
);
260264

261265
try {
262266
const cacheId = await withTimeout(
263267
MAX_CACHE_OPERATION_MS,
264-
actionsCache.saveCache([dbLocation], cacheKey),
268+
actionsCache.saveCache([dbLocation], cacheSaveKey),
265269
() => {},
266270
);
267271
if (cacheId === undefined) {
@@ -324,18 +328,22 @@ export async function downloadOverlayBaseDatabaseFromCache(
324328

325329
const dbLocation = config.dbLocation;
326330
const codeQlVersion = (await codeql.getVersion()).version;
327-
const restoreKey = await getCacheRestoreKey(config, codeQlVersion);
331+
const cacheRestoreKeyPrefix = await getCacheRestoreKeyPrefix(
332+
config,
333+
codeQlVersion,
334+
);
328335

329336
logger.info(
330-
`Looking in Actions cache for overlay-base database with restore key ${restoreKey}`,
337+
"Looking in Actions cache for overlay-base database with " +
338+
`restore key ${cacheRestoreKeyPrefix}`,
331339
);
332340

333341
let databaseDownloadDurationMs = 0;
334342
try {
335343
const databaseDownloadStart = performance.now();
336344
const foundKey = await withTimeout(
337345
MAX_CACHE_OPERATION_MS,
338-
actionsCache.restoreCache([dbLocation], restoreKey),
346+
actionsCache.restoreCache([dbLocation], cacheRestoreKeyPrefix),
339347
() => {
340348
logger.info("Timed out downloading overlay-base database from cache");
341349
},
@@ -389,29 +397,44 @@ export async function downloadOverlayBaseDatabaseFromCache(
389397
};
390398
}
391399

392-
async function generateCacheKey(
400+
/**
401+
* Computes the cache key for saving the overlay-base database to the GitHub
402+
* Actions cache.
403+
*
404+
* The key consists of the restore key prefix (which does not include the
405+
* commit SHA) and the commit SHA of the current checkout.
406+
*/
407+
async function getCacheSaveKey(
393408
config: Config,
394409
codeQlVersion: string,
395410
checkoutPath: string,
396411
): Promise<string> {
397412
const sha = await getCommitOid(checkoutPath);
398-
const restoreKey = await getCacheRestoreKey(config, codeQlVersion);
399-
return `${restoreKey}${sha}`;
413+
const restoreKeyPrefix = await getCacheRestoreKeyPrefix(
414+
config,
415+
codeQlVersion,
416+
);
417+
return `${restoreKeyPrefix}${sha}`;
400418
}
401419

402-
async function getCacheRestoreKey(
420+
/**
421+
* Computes the cache key prefix for restoring the overlay-base database from
422+
* the GitHub Actions cache.
423+
*
424+
* Actions cache supports using multiple restore keys to indicate preference,
425+
* and this function could in principle take advantage of that feature by
426+
* returning a list of restore key prefixes. However, since overlay-base
427+
* databases are built from the default branch and used in PR analysis, it is
428+
* exceedingly unlikely that the commit SHA will ever be the same.
429+
*
430+
* Therefore, this function returns only a single restore key prefix, which does
431+
* not include the commit SHA. This allows us to restore the most recent
432+
* compatible overlay-base database.
433+
*/
434+
async function getCacheRestoreKeyPrefix(
403435
config: Config,
404436
codeQlVersion: string,
405437
): Promise<string> {
406-
// The restore key (prefix) specifies which cached overlay-base databases are
407-
// compatible with the current analysis: the cached database must have the
408-
// same cache version and the same CodeQL bundle version.
409-
//
410-
// Actions cache supports using multiple restore keys to indicate preference.
411-
// Technically we prefer a cached overlay-base database with the same SHA as
412-
// we are analyzing. However, since overlay-base databases are built from the
413-
// default branch and used in PR analysis, it is exceedingly unlikely that
414-
// the commit SHA will ever be the same, so we can just leave it out.
415438
const languages = [...config.languages].sort().join("_");
416439

417440
const cacheKeyComponents = {

0 commit comments

Comments
 (0)