Skip to content

Commit e37b293

Browse files
committed
Overlay: report overlay-base database stats
1 parent 19075c4 commit e37b293

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/init-action.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { Language } from "./languages";
4444
import { getActionsLogger, Logger } from "./logging";
4545
import {
4646
downloadOverlayBaseDatabaseFromCache,
47+
OverlayBaseDatabaseDownloadStats,
4748
OverlayDatabaseMode,
4849
} from "./overlay-database-utils";
4950
import { getRepositoryNwo } from "./repository";
@@ -107,6 +108,10 @@ interface InitWithConfigStatusReport extends InitStatusReport {
107108
trap_cache_download_size_bytes: number;
108109
/** Time taken to download TRAP caches, in milliseconds. */
109110
trap_cache_download_duration_ms: number;
111+
/** Size of the overlay-base database that we downloaded, in bytes. */
112+
overlay_base_database_download_size_bytes?: number;
113+
/** Time taken to download the overlay-base database, in milliseconds. */
114+
overlay_base_database_download_duration_ms?: number;
110115
/** Stringified JSON array of registry configuration objects, from the 'registries' config field
111116
or workflow input. **/
112117
registries: string;
@@ -134,6 +139,7 @@ async function sendCompletedStatusReport(
134139
toolsFeatureFlagsValid: boolean | undefined,
135140
toolsSource: ToolsSource,
136141
toolsVersion: string,
142+
overlayBaseDatabaseStats: OverlayBaseDatabaseDownloadStats | undefined,
137143
logger: Logger,
138144
error?: Error,
139145
) {
@@ -237,6 +243,10 @@ async function sendCompletedStatusReport(
237243
await getTotalCacheSize(Object.values(config.trapCaches), logger),
238244
),
239245
trap_cache_download_duration_ms: Math.round(config.trapCacheDownloadTime),
246+
overlay_base_database_download_size_bytes:
247+
overlayBaseDatabaseStats?.databaseSizeBytes,
248+
overlay_base_database_download_duration_ms:
249+
overlayBaseDatabaseStats?.databaseDownloadDurationMs,
240250
query_filters: JSON.stringify(
241251
config.originalUserInput["query-filters"] ?? [],
242252
),
@@ -400,6 +410,7 @@ async function run() {
400410
return;
401411
}
402412

413+
let overlayBaseDatabaseStats: OverlayBaseDatabaseDownloadStats | undefined;
403414
try {
404415
if (
405416
config.augmentationProperties.overlayDatabaseMode ===
@@ -417,9 +428,12 @@ async function run() {
417428
// necessary preparations. So, in that mode, we would assume that
418429
// everything is in order and let the analysis fail if that turns out not
419430
// to be the case.
420-
const overlayDatabaseDownloaded =
421-
await downloadOverlayBaseDatabaseFromCache(codeql, config, logger);
422-
if (!overlayDatabaseDownloaded) {
431+
overlayBaseDatabaseStats = await downloadOverlayBaseDatabaseFromCache(
432+
codeql,
433+
config,
434+
logger,
435+
);
436+
if (!overlayBaseDatabaseStats) {
423437
config.augmentationProperties.overlayDatabaseMode =
424438
OverlayDatabaseMode.None;
425439
logger.info(
@@ -729,6 +743,7 @@ async function run() {
729743
toolsFeatureFlagsValid,
730744
toolsSource,
731745
toolsVersion,
746+
overlayBaseDatabaseStats,
732747
logger,
733748
error,
734749
);
@@ -744,6 +759,7 @@ async function run() {
744759
toolsFeatureFlagsValid,
745760
toolsSource,
746761
toolsVersion,
762+
overlayBaseDatabaseStats,
747763
logger,
748764
);
749765
}

src/overlay-database-utils.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { type CodeQL } from "./codeql";
88
import { type Config } from "./config-utils";
99
import { getCommitOid, getFileOidsUnderPath } from "./git-utils";
1010
import { Logger } from "./logging";
11-
import { isInTestMode, withTimeout } from "./util";
11+
import { isInTestMode, tryGetFolderBytes, withTimeout } from "./util";
1212

1313
export 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
*/
248254
export 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

322348
async function generateCacheKey(

0 commit comments

Comments
 (0)