@@ -8,7 +8,11 @@ import { |
8 | 8 | getWorkflowRunAttempt, |
9 | 9 | getWorkflowRunID, |
10 | 10 | } from "../actions-util"; |
11 | | -import { getAutomationID, listActionsCaches } from "../api-client"; |
| 11 | +import { |
| 12 | + type ActionsCacheItem, |
| 13 | + getAutomationID, |
| 14 | + listActionsCaches, |
| 15 | +} from "../api-client"; |
12 | 16 | import { createCacheKeyHash } from "../caching-utils"; |
13 | 17 | import { type CodeQL } from "../codeql"; |
14 | 18 | import { type Config } from "../config-utils"; |
@@ -48,6 +52,12 @@ const OVERLAY_BASE_DATABASE_MAX_UPLOAD_SIZE_BYTES = |
48 | 52 | const CACHE_VERSION = 1; |
49 | 53 | const CACHE_PREFIX = "codeql-overlay-base-database"; |
50 | 54 |
|
| 55 | +// The Actions cache evicts entries that have not been accessed in the past 7 |
| 56 | +// days. We conservatively set a limit of 6 days to avoid using a cached base DB |
| 57 | +// that may be evicted before we can download it. |
| 58 | +const CACHE_ENTRY_MAX_AGE_DAYS = 6; |
| 59 | +const CACHE_ENTRY_MAX_AGE_MS = CACHE_ENTRY_MAX_AGE_DAYS * 24 * 60 * 60 * 1000; |
| 60 | + |
51 | 61 | // The purpose of this ten-minute limit is to guard against the possibility |
52 | 62 | // that the cache service is unresponsive, which would otherwise cause the |
53 | 63 | // entire action to hang. Normally we expect cache operations to complete |
@@ -435,6 +445,39 @@ async function getCacheKeyPrefixBase( |
435 | 445 | return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languagesComponent}-`; |
436 | 446 | } |
437 | 447 |
|
| 448 | +/** |
| 449 | + * Lists overlay-base database cache entries with the given key prefix, ignoring entries that are |
| 450 | + * old enough that they may be evicted by the Actions cache before we attempt to download them. |
| 451 | + */ |
| 452 | +async function listRecentOverlayBaseDatabaseCaches( |
| 453 | + cacheKeyPrefix: string, |
| 454 | + logger: Logger, |
| 455 | +): Promise<ActionsCacheItem[]> { |
| 456 | + const allCaches = await listActionsCaches(cacheKeyPrefix); |
| 457 | + |
| 458 | + if (allCaches.length === 0) { |
| 459 | + logger.info("No overlay-base databases found in Actions cache."); |
| 460 | + return []; |
| 461 | + } |
| 462 | + |
| 463 | + const cutoffMs = Date.now() - CACHE_ENTRY_MAX_AGE_MS; |
| 464 | + const recentCaches = allCaches.filter((cache) => { |
| 465 | + if (!cache.last_accessed_at) return true; |
| 466 | + const lastAccessedMs = Date.parse(cache.last_accessed_at); |
| 467 | + return Number.isNaN(lastAccessedMs) || lastAccessedMs >= cutoffMs; |
| 468 | + }); |
| 469 | + const numTooOldDatabases = allCaches.length - recentCaches.length; |
| 470 | + const tooOldSuffix = |
| 471 | + numTooOldDatabases > 0 |
| 472 | + ? ` (ignoring ${numTooOldDatabases} that may be evicted soon)` |
| 473 | + : ""; |
| 474 | + logger.info( |
| 475 | + `Found ${allCaches.length} overlay-base ${allCaches.length === 1 ? "database" : "databases"} in the Actions cache${tooOldSuffix}.`, |
| 476 | + ); |
| 477 | + |
| 478 | + return recentCaches; |
| 479 | +} |
| 480 | + |
438 | 481 | /** |
439 | 482 | * Searches the GitHub Actions cache for overlay-base databases matching the given languages, and |
440 | 483 | * returns all stable CodeQL versions found across matching cache entries. |
@@ -448,7 +491,7 @@ export async function getCodeQlVersionsForOverlayBaseDatabases( |
448 | 491 | ): Promise<string[] | undefined> { |
449 | 492 | const languages = rawLanguages.map(parseBuiltInLanguage); |
450 | 493 | if (languages.includes(undefined)) { |
451 | | - logger.warning( |
| 494 | + logger.info( |
452 | 495 | "One or more provided languages are not recognized as built-in languages. " + |
453 | 496 | "Skipping searching for overlay-base databases in cache.", |
454 | 497 | ); |
@@ -463,22 +506,19 @@ export async function getCodeQlVersionsForOverlayBaseDatabases( |
463 | 506 | `prefix ${cacheKeyPrefix}`, |
464 | 507 | ); |
465 | 508 |
|
466 | | - const caches = await listActionsCaches(cacheKeyPrefix); |
| 509 | + const caches = await listRecentOverlayBaseDatabaseCaches( |
| 510 | + cacheKeyPrefix, |
| 511 | + logger, |
| 512 | + ); |
467 | 513 |
|
468 | 514 | if (caches.length === 0) { |
469 | | - logger.info("No overlay-base databases found in Actions cache."); |
470 | 515 | return []; |
471 | 516 | } |
472 | 517 |
|
473 | | - logger.info( |
474 | | - `Found ${caches.length} overlay-base ` + |
475 | | - `${caches.length === 1 ? "database" : "databases"} in the Actions cache.`, |
476 | | - ); |
477 | | - |
478 | 518 | // Parse CodeQL versions from cache keys, matching only stable releases. |
479 | 519 | // |
480 | | - // After the prefix, the remaining key format starts with `${codeQlVersion}-`. Nightlies will have |
481 | | - // a suffix like `+202604201548` that will break the match. |
| 520 | + // After the prefix, the remaining key format starts with `${codeQlVersion}-`. Nightlies have a |
| 521 | + // suffix like `+202604201548` that will prevent a match. |
482 | 522 | // |
483 | 523 | // Caveat: this relies on the fact that we haven't released any CodeQL bundles with the |
484 | 524 | // `x.y.z-<pre-release>` semver format which does not interact well with the current overlay base |
@@ -506,7 +546,7 @@ export async function getCodeQlVersionsForOverlayBaseDatabases( |
506 | 546 | const versions = [...versionSet].sort(semver.rcompare); |
507 | 547 |
|
508 | 548 | logger.info( |
509 | | - `Found overlay databases for the following CodeQL versions in the Actions cache: ${versions.join(", ")}`, |
| 549 | + `Found overlay-base databases for the following CodeQL versions in the Actions cache: ${versions.join(", ")}`, |
510 | 550 | ); |
511 | 551 |
|
512 | 552 | return versions; |
|