-
Notifications
You must be signed in to change notification settings - Fork 88
Move Azure cache implementation to plugin #1057
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
Draft
Copilot
wants to merge
15
commits into
main
Choose a base branch
from
copilot/move-azure-cache-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae00ea9
Initial plan
Copilot 45d309f
Move Azure blob cache implementation to plugin package
Copilot c86e907
Update documentation for Azure blob cache plugin approach
Copilot 3897f78
Address code review: improve type safety for credential name validation
Copilot 2a02bfa
Apply suggestion from @ecraig12345
ecraig12345 db57a98
Add change file for Azure cache plugin migration
Copilot 8dda158
Address PR review comments: share code instead of duplicating, remove…
Copilot e81201a
Address follow-up review comments: restore CredentialCache comments, …
Copilot 66830c8
Remove disallowedChangeTypes from beachball.config.js to allow major …
Copilot b996507
Bring back custom plugin provider test in getCacheStorageProvider.tes…
Copilot 0228f07
Merge branch 'master' into copilot/move-azure-cache-implementation
ecraig12345 db9c2d8
lint
ecraig12345 291bc93
Merge branch 'master' into copilot/move-azure-cache-implementation
ecraig12345 8771b4b
format
ecraig12345 4e6fc21
Merge branch 'master' into copilot/move-azure-cache-implementation
ecraig12345 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
Some comments aren't visible on the classic Files Changed page.
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,5 @@ | ||
| **/* | ||
|
ecraig12345 marked this conversation as resolved.
Outdated
|
||
| !lib/**/* | ||
| lib/**/__tests__/* | ||
| lib/**/*.d.ts.map | ||
| !bin/**/* | ||
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,41 @@ | ||
| { | ||
| "name": "@lage-run/azure-blob-cache-storage", | ||
| "version": "0.1.0", | ||
| "description": "Azure Blob Storage cache plugin for backfill/lage", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/microsoft/lage" | ||
| }, | ||
| "homepage": "https://microsoft.github.io/lage/", | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
| "scripts": { | ||
| "build": "yarn types && yarn transpile", | ||
| "transpile": "monorepo-scripts transpile", | ||
| "types": "yarn run -T tsc", | ||
| "lint": "monorepo-scripts lint" | ||
| }, | ||
| "dependencies": { | ||
| "@azure/core-auth": "1.9.0", | ||
| "@azure/identity": "4.9.1", | ||
| "@azure/storage-blob": "12.27.0", | ||
| "@lage-run/globby": "workspace:^", | ||
| "backfill-config": "workspace:^", | ||
| "backfill-logger": "workspace:^", | ||
| "fs-extra": "8.1.0", | ||
| "tar-fs": "2.1.4" | ||
| }, | ||
| "devDependencies": { | ||
| "@lage-run/monorepo-scripts": "workspace:^", | ||
| "@types/fs-extra": "^8.0.0", | ||
| "@types/tar-fs": "^2.0.1" | ||
| }, | ||
| "engines": { | ||
| "node": ">=14" | ||
| }, | ||
| "files": [ | ||
| "lib/!(__*)", | ||
| "lib/!(__*)/**" | ||
| ] | ||
| } |
File renamed without changes.
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,79 @@ | ||
| import { globAsync } from "@lage-run/globby"; | ||
|
|
||
| import type { Logger } from "backfill-logger"; | ||
| import type { ICacheStorage } from "backfill-config"; | ||
| import { getFileHash } from "./hashFile.js"; | ||
|
|
||
| // First key is the hash, second key is the file relative path | ||
| const savedHashes: Map<string, Map<string, string>> = new Map(); | ||
|
|
||
| // contract: cwd should be absolute | ||
| // The return keys are relative path with posix file separators | ||
| async function getHashesFor(cwd: string): Promise<Map<string, string>> { | ||
| const result = new Map<string, string>(); | ||
|
|
||
| const allFiles = await globAsync(["**/*", "!node_modules"], { cwd }); | ||
| //globby returns relative path with posix file separator | ||
| await Promise.all( | ||
| allFiles.map(async (f) => { | ||
| const hash = await getFileHash(cwd, f); | ||
| result.set(f, hash); | ||
| }) | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export type { ICacheStorage }; | ||
|
|
||
| export abstract class CacheStorage implements ICacheStorage { | ||
|
ecraig12345 marked this conversation as resolved.
Outdated
|
||
| public constructor( | ||
| protected logger: Logger, | ||
| protected cwd: string, | ||
| private incrementalCaching = false | ||
| ) {} | ||
| public async fetch(hash: string): Promise<boolean> { | ||
| const tracer = this.logger.setTime("fetchTime"); | ||
|
|
||
| const result = await this._fetch(hash); | ||
|
|
||
| tracer.stop(); | ||
|
|
||
| this.logger.setHit(result); | ||
|
|
||
| if (!result && this.incrementalCaching) { | ||
| savedHashes.set(hash, await getHashesFor(this.cwd)); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public async put(hash: string, outputGlob: string[]): Promise<void> { | ||
| const tracer = this.logger.setTime("putTime"); | ||
|
|
||
| const filesMatchingOutputGlob = await globAsync(outputGlob, { | ||
| cwd: this.cwd, | ||
| }); | ||
|
|
||
| let filesToCache = filesMatchingOutputGlob; | ||
| if (this.incrementalCaching) { | ||
| // Get the list of files that have not changed so we don't need to cache them. | ||
| const hashesNow = await getHashesFor(this.cwd); | ||
| const hashesThen = | ||
| (await savedHashes.get(hash)) || new Map<string, string>(); | ||
| const unchangedFiles = [...hashesThen.keys()].filter( | ||
| (s) => hashesThen.get(s) === hashesNow.get(s) | ||
| ); | ||
| filesToCache = filesMatchingOutputGlob.filter( | ||
| (f) => !unchangedFiles.includes(f) | ||
| ); | ||
| } | ||
|
|
||
| await this._put(hash, filesToCache); | ||
| tracer.stop(); | ||
| } | ||
|
|
||
| protected abstract _fetch(hash: string): Promise<boolean>; | ||
|
|
||
| protected abstract _put(hash: string, filesToCache: string[]): Promise<void>; | ||
| } | ||
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,56 @@ | ||
| import * as path from "path"; | ||
| import { promises as fs } from "fs"; | ||
| import * as crypto from "crypto"; | ||
| import pLimit from "p-limit"; | ||
|
|
||
| let MAX_FILE_OPERATIONS = 5000; | ||
|
ecraig12345 marked this conversation as resolved.
Outdated
|
||
|
|
||
| try { | ||
| const maxFileOpEnv = process.env["BACKFILL_MAX_FILE_OP"]; | ||
| if (maxFileOpEnv) { | ||
| MAX_FILE_OPERATIONS = parseInt(maxFileOpEnv); | ||
| } | ||
| } catch (_) { | ||
| /* The env variable is not an integer, this is fine.*/ | ||
| } | ||
|
|
||
| const diskLimit = pLimit(MAX_FILE_OPERATIONS); | ||
|
|
||
| // The first key is the file path, the second key is mtime | ||
| const memo = new Map<string, Map<number, string>>(); | ||
|
|
||
| async function computeHash(filePath: string): Promise<string> { | ||
| const fileBuffer = await diskLimit(() => { | ||
| return fs.readFile(filePath); | ||
| }); | ||
| // We use sha1 for perf reason and because the hashing is not used for security reason. | ||
| const hashSum = crypto.createHash("sha1"); | ||
| hashSum.update(fileBuffer); | ||
| const hash = hashSum.digest("hex"); | ||
| return hash; | ||
| } | ||
|
|
||
| /* | ||
| * Get the hash of a file. | ||
| * This function memoizes the hash for files and mtimes. | ||
| */ | ||
| export async function getFileHash( | ||
| cwd: string, | ||
| filePath: string | ||
| ): Promise<string> { | ||
| const fileAbsPath = path.join(cwd, filePath); | ||
| const stat = await fs.stat(fileAbsPath); | ||
|
|
||
| let memoForFile = memo.get(fileAbsPath); | ||
| if (!memoForFile) { | ||
| memoForFile = new Map<number, string>(); | ||
| memo.set(fileAbsPath, memoForFile); | ||
| } | ||
|
|
||
| let hash = memoForFile.get(stat.mtimeMs); | ||
| if (!hash) { | ||
| hash = await computeHash(fileAbsPath); | ||
| memoForFile.set(stat.mtimeMs, hash); | ||
| } | ||
| return hash; | ||
| } | ||
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,51 @@ | ||
| import type { Logger } from "backfill-logger"; | ||
| import type { | ||
| ICacheStorage, | ||
| CustomCacheStoragePlugin, | ||
| AzureBlobCacheStorageOptions, | ||
| AzureBlobCacheStorageConnectionStringOptions, | ||
| } from "backfill-config"; | ||
|
|
||
| import { AzureBlobCacheStorage } from "./AzureBlobCacheStorage.js"; | ||
| import { CredentialCache, type AzureCredentialName } from "./CredentialCache.js"; | ||
|
|
||
| export type { AzureCredentialName } from "./CredentialCache.js"; | ||
| export { CredentialCache } from "./CredentialCache.js"; | ||
|
|
||
| export type AzureBlobPluginOptions = AzureBlobCacheStorageOptions & { | ||
| /** Optional credential name for Azure Identity authentication. */ | ||
| credentialName?: AzureCredentialName; | ||
| }; | ||
|
|
||
| function isTokenConnectionString(connectionString: string) { | ||
| return connectionString.includes("SharedAccessSignature") || connectionString.includes("AccountKey"); | ||
| } | ||
|
|
||
| const plugin: CustomCacheStoragePlugin<AzureBlobPluginOptions> = { | ||
|
ecraig12345 marked this conversation as resolved.
Outdated
|
||
| name: "azure-blob", | ||
| getProvider(logger: Logger, cwd: string, options: AzureBlobPluginOptions): ICacheStorage { | ||
| // Handle credential injection for connection-string-based options | ||
| if ("connectionString" in options && !isTokenConnectionString(options.connectionString)) { | ||
| const connStringOptions = options as AzureBlobCacheStorageConnectionStringOptions & { credentialName?: AzureCredentialName }; | ||
| if (!connStringOptions.credential) { | ||
| const credName = connStringOptions.credentialName | ||
| ?? (process.env.AZURE_IDENTITY_CREDENTIAL_NAME || undefined); | ||
|
|
||
| if (credName != null) { | ||
| if (!CredentialCache.credentialNames.includes(credName as AzureCredentialName)) { | ||
| throw new Error( | ||
| `Invalid credentialName: "${credName}". Allowed values: ${CredentialCache.credentialNames.join(", ")}` | ||
| ); | ||
| } | ||
| connStringOptions.credential = CredentialCache.getInstance(credName as AzureCredentialName); | ||
| } else { | ||
| connStringOptions.credential = CredentialCache.getInstance(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new AzureBlobCacheStorage(options, logger, cwd); | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; | ||
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,7 @@ | ||
| { | ||
| "extends": "@lage-run/monorepo-scripts/config/tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "lib" | ||
| }, | ||
| "include": ["src"] | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.