@@ -8,7 +8,7 @@ import { type CodeQL } from "./codeql";
88import { type Config } from "./config-utils" ;
99import { getCommitOid , getFileOidsUnderPath } from "./git-utils" ;
1010import { Logger } from "./logging" ;
11- import { isInTestMode , withTimeout } from "./util" ;
11+ import { isInTestMode , tryGetFolderBytes , withTimeout } from "./util" ;
1212
1313export enum OverlayDatabaseMode {
1414 Overlay = "overlay" ,
@@ -235,41 +235,47 @@ export async function uploadOverlayBaseDatabaseToCache(
235235 return true ;
236236}
237237
238+ export interface OverlayBaseDatabaseDownloadStats {
239+ databaseSizeBytes : number ;
240+ databaseDownloadDurationMs : number ;
241+ }
242+
238243/**
239244 * Downloads the overlay-base database from the GitHub Actions cache. If conditions
240245 * for downloading are not met, the function does nothing and returns false.
241246 *
242247 * @param codeql The CodeQL instance
243248 * @param config The configuration object
244249 * @param logger The logger instance
245- * @returns A promise that resolves to true if the download was performed and
246- * successfully completed, or false otherwise
250+ * @returns A promise that resolves to download statistics if an overlay-base
251+ * database was successfully downloaded, or undefined if the download was
252+ * either not performed or failed.
247253 */
248254export async function downloadOverlayBaseDatabaseFromCache (
249255 codeql : CodeQL ,
250256 config : Config ,
251257 logger : Logger ,
252- ) : Promise < boolean > {
258+ ) : Promise < OverlayBaseDatabaseDownloadStats | undefined > {
253259 const overlayDatabaseMode = config . augmentationProperties . overlayDatabaseMode ;
254260 if ( overlayDatabaseMode !== OverlayDatabaseMode . Overlay ) {
255261 logger . debug (
256262 `Overlay database mode is ${ overlayDatabaseMode } . ` +
257263 "Skip downloading overlay-base database from cache." ,
258264 ) ;
259- return false ;
265+ return undefined ;
260266 }
261267 if ( ! config . augmentationProperties . useOverlayDatabaseCaching ) {
262268 logger . debug (
263269 "Overlay database caching is disabled. " +
264270 "Skip downloading overlay-base database from cache." ,
265271 ) ;
266- return false ;
272+ return undefined ;
267273 }
268274 if ( isInTestMode ( ) ) {
269275 logger . debug (
270276 "In test mode. Skip downloading overlay-base database from cache." ,
271277 ) ;
272- return false ;
278+ return undefined ;
273279 }
274280
275281 const dbLocation = config . dbLocation ;
@@ -280,18 +286,23 @@ export async function downloadOverlayBaseDatabaseFromCache(
280286 `Looking in Actions cache for overlay-base database with restore key ${ restoreKey } ` ,
281287 ) ;
282288
289+ let databaseDownloadDurationMs = 0 ;
283290 try {
291+ const databaseDownloadStart = performance . now ( ) ;
284292 const foundKey = await withTimeout (
285293 MAX_CACHE_OPERATION_MS ,
286294 actionsCache . restoreCache ( [ dbLocation ] , restoreKey ) ,
287295 ( ) => {
288296 logger . info ( "Timed out downloading overlay-base database from cache" ) ;
289297 } ,
290298 ) ;
299+ databaseDownloadDurationMs = Math . round (
300+ performance . now ( ) - databaseDownloadStart ,
301+ ) ;
291302
292303 if ( foundKey === undefined ) {
293304 logger . info ( "No overlay-base database found in Actions cache" ) ;
294- return false ;
305+ return undefined ;
295306 }
296307
297308 logger . info (
@@ -302,7 +313,7 @@ export async function downloadOverlayBaseDatabaseFromCache(
302313 "Failed to download overlay-base database from cache: " +
303314 `${ error instanceof Error ? error . message : String ( error ) } ` ,
304315 ) ;
305- return false ;
316+ return undefined ;
306317 }
307318
308319 const databaseIsValid = checkOverlayBaseDatabase (
@@ -312,11 +323,26 @@ export async function downloadOverlayBaseDatabaseFromCache(
312323 ) ;
313324 if ( ! databaseIsValid ) {
314325 logger . warning ( "Downloaded overlay-base database failed validation" ) ;
315- return false ;
326+ return undefined ;
327+ }
328+
329+ const databaseSizeBytes = await tryGetFolderBytes ( dbLocation , logger ) ;
330+ if ( databaseSizeBytes === undefined ) {
331+ logger . info (
332+ "Filesystem error while accessing downloaded overlay-base database" ,
333+ ) ;
334+ // The problem that warrants reporting download failure is not that we are
335+ // unable to determine the size of the database. Rather, it is that we
336+ // encountered a filesystem error while accessing the database, which
337+ // indicates that an overlay analysis will likely fail.
338+ return undefined ;
316339 }
317340
318341 logger . info ( `Successfully downloaded overlay-base database to ${ dbLocation } ` ) ;
319- return true ;
342+ return {
343+ databaseSizeBytes : Math . round ( databaseSizeBytes ) ,
344+ databaseDownloadDurationMs,
345+ } ;
320346}
321347
322348async function generateCacheKey (
0 commit comments