-
Notifications
You must be signed in to change notification settings - Fork 226
Clean up variant analyses from query history #2118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface QueryHistoryDirs { | ||
| localQueriesDirPath: string; | ||
| variantAnalysesDirPath: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import { pathExists, readdir, stat, remove, readFile } from "fs-extra"; | ||
| import { pathExists, stat, remove, readFile } from "fs-extra"; | ||
| import { EOL } from "os"; | ||
| import { join } from "path"; | ||
| import { Disposable, ExtensionContext } from "vscode"; | ||
| import { extLogger } from "../common"; | ||
| import { readDirFullPaths } from "../pure/files"; | ||
| import { QueryHistoryDirs } from "./query-history-dirs"; | ||
| import { QueryHistoryManager } from "./query-history-manager"; | ||
|
|
||
| const LAST_SCRUB_TIME_KEY = "lastScrubTime"; | ||
|
|
@@ -23,14 +25,14 @@ type Counter = { | |
| * @param wakeInterval How often to check to see if the job should run. | ||
| * @param throttleTime How often to actually run the job. | ||
| * @param maxQueryTime The maximum age of a query before is ready for deletion. | ||
| * @param queryDirectory The directory containing all queries. | ||
| * @param queryHistoryDirs The directories containing all query history information. | ||
| * @param ctx The extension context. | ||
| */ | ||
| export function registerQueryHistoryScrubber( | ||
| wakeInterval: number, | ||
| throttleTime: number, | ||
| maxQueryTime: number, | ||
| queryDirectory: string, | ||
| queryHistoryDirs: QueryHistoryDirs, | ||
| qhm: QueryHistoryManager, | ||
| ctx: ExtensionContext, | ||
|
|
||
|
|
@@ -42,7 +44,7 @@ export function registerQueryHistoryScrubber( | |
| wakeInterval, | ||
| throttleTime, | ||
| maxQueryTime, | ||
| queryDirectory, | ||
| queryHistoryDirs, | ||
| qhm, | ||
| ctx, | ||
| counter, | ||
|
|
@@ -58,7 +60,7 @@ export function registerQueryHistoryScrubber( | |
| async function scrubQueries( | ||
| throttleTime: number, | ||
| maxQueryTime: number, | ||
| queryDirectory: string, | ||
| queryHistoryDirs: QueryHistoryDirs, | ||
| qhm: QueryHistoryManager, | ||
| ctx: ExtensionContext, | ||
| counter?: Counter, | ||
|
|
@@ -74,18 +76,33 @@ async function scrubQueries( | |
| let scrubCount = 0; // total number of directories deleted | ||
| try { | ||
| counter?.increment(); | ||
| void extLogger.log("Scrubbing query directory. Removing old queries."); | ||
| if (!(await pathExists(queryDirectory))) { | ||
| void extLogger.log( | ||
| "Cleaning up query history directories. Removing old entries.", | ||
| ); | ||
|
|
||
| if (!(await pathExists(queryHistoryDirs.localQueriesDirPath))) { | ||
| void extLogger.log( | ||
| `Cannot clean up query history directories. Local queries directory does not exist: ${queryHistoryDirs.localQueriesDirPath}`, | ||
| ); | ||
| return; | ||
| } | ||
| if (!(await pathExists(queryHistoryDirs.variantAnalysesDirPath))) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is ok to do since https://github.com/github/vscode-codeql/blob/main/extensions/ql-vscode/src/extension.ts#L628 is not checking for canary flags. |
||
| void extLogger.log( | ||
| `Cannot scrub. Query directory does not exist: ${queryDirectory}`, | ||
| `Cannot clean up query history directories. Variant analyses directory does not exist: ${queryHistoryDirs.variantAnalysesDirPath}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const baseNames = await readdir(queryDirectory); | ||
| const localQueryDirPaths = await readDirFullPaths( | ||
| queryHistoryDirs.localQueriesDirPath, | ||
| ); | ||
| const variantAnalysisDirPaths = await readDirFullPaths( | ||
| queryHistoryDirs.variantAnalysesDirPath, | ||
| ); | ||
| const allDirPaths = [...localQueryDirPaths, ...variantAnalysisDirPaths]; | ||
|
|
||
| const errors: string[] = []; | ||
| for (const baseName of baseNames) { | ||
| const dir = join(queryDirectory, baseName); | ||
| for (const dir of allDirPaths) { | ||
| const scrubResult = await scrubDirectory(dir, now, maxQueryTime); | ||
| if (scrubResult.errorMsg) { | ||
| errors.push(scrubResult.errorMsg); | ||
|
|
||
14 changes: 14 additions & 0 deletions
14
extensions/ql-vscode/test/factories/query-history/query-history-dirs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { QueryHistoryDirs } from "../../../src/query-history/query-history-dirs"; | ||
|
|
||
| export function createMockQueryHistoryDirs({ | ||
| localQueriesDirPath = "mock-local-queries-dir-path", | ||
| variantAnalysesDirPath = "mock-variant-analyses-dir-path", | ||
| }: { | ||
| localQueriesDirPath?: string; | ||
| variantAnalysesDirPath?: string; | ||
| } = {}): QueryHistoryDirs { | ||
| return { | ||
| localQueriesDirPath, | ||
| variantAnalysesDirPath, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to return here? If the local query history directory doesn't exist we can still clean up the variant analysis directory. I suppose we do expect both dirs to exist, so this is still an unexpected error, but do we want to abort fully or try to continue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that since this is an exceptional case then it's okay to abort. I don't have a strong opinion though - happy to change it to be more resilient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right it should be an exceptional case that doesn't happen in practice. Also it's not like one of local or remote queries is a more central part of the extension, so it's not like a user would want to only use one and therefore want to run without one of the directories existing. And we recreate the directories on extension startup. So if this does happen it hopefully won't be a long term problem and will be fixed by restarting.
That's a lot of words, but in short I think it's fine as it is. Feel free to abort if any of the directories don't exist and keep the code simple.