-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(caching) Release registry caching #15339
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
Open
sergical
wants to merge
16
commits into
master
Choose a base branch
from
sergical/cache-issues
base: master
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.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fd05490
feat(caching) try with 2 workers on enhanced build
sergical 821177e
testing with more workers
sergical 9c46c0b
75% workers
sergical 3191193
test 1 worker
sergical e73a0e6
caching release registry
sergical a6d806e
bring back 75% cpu
sergical e5d53b0
back to half
sergical 66699f2
registry cache logs
sergical 33de122
lets test file changes
sergical 0c7b636
revert content changes
sergical fccc572
cleanup old cache files
sergical 04b82db
lint
sergical ff65004
delete if not used
sergical f1924b5
[getsentry/action-github-commit] Auto commit
getsantry[bot] 3559d45
worker cleanup
sergical 7e13fec
Merge branch 'sergical/cache-issues' of github.com:getsentry/sentry-d…
sergical 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import imgLinks from '@pondorasti/remark-img-links'; | |
| import {selectAll} from 'hast-util-select'; | ||
| import {createHash} from 'node:crypto'; | ||
| import {createReadStream, createWriteStream, existsSync} from 'node:fs'; | ||
| import {mkdir, opendir, readFile, rm, writeFile} from 'node:fs/promises'; | ||
| import {mkdir, opendir, readdir, readFile, rm, writeFile} from 'node:fs/promises'; | ||
| import {cpus} from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import {compose, Readable} from 'node:stream'; | ||
|
|
@@ -58,7 +58,15 @@ async function uploadToCFR2(s3Client, relativePath, data) { | |
| return; | ||
| } | ||
|
|
||
| function taskFinishHandler({id, success, failedTasks}) { | ||
| function taskFinishHandler( | ||
| {id, success, failedTasks, usedCacheFiles}, | ||
| allUsedCacheFiles | ||
| ) { | ||
| // Collect cache files used by this worker | ||
| if (usedCacheFiles) { | ||
| usedCacheFiles.forEach(file => allUsedCacheFiles.add(file)); | ||
| } | ||
|
|
||
| if (failedTasks.length === 0) { | ||
| console.log(`✅ Worker[${id}]: converted ${success} files successfully.`); | ||
| return false; | ||
|
|
@@ -95,6 +103,9 @@ async function createWork() { | |
| await mkdir(CACHE_DIR, {recursive: true}); | ||
| } | ||
|
|
||
| // Track which cache files are used during this build | ||
| const usedCacheFiles = new Set(); | ||
|
|
||
| // On a 16-core machine, 8 workers were optimal (and slightly faster than 16) | ||
| const numWorkers = Math.max(Math.floor(cpus().length / 2), 2); | ||
| const workerTasks = new Array(numWorkers).fill(null).map(() => []); | ||
|
|
@@ -163,7 +174,7 @@ async function createWork() { | |
| }, | ||
| }); | ||
| let hasErrors = false; | ||
| worker.on('message', data => (hasErrors = taskFinishHandler(data))); | ||
| worker.on('message', data => (hasErrors = taskFinishHandler(data, usedCacheFiles))); | ||
| worker.on('error', reject); | ||
| worker.on('exit', code => { | ||
| if (code !== 0) { | ||
|
|
@@ -175,28 +186,51 @@ async function createWork() { | |
| }); | ||
| }); | ||
| // The main thread can also process tasks -- That's 65% more bullet per bullet! -Cave Johnson | ||
| const mainThreadUsedFiles = new Set(); | ||
| workerPromises.push( | ||
| processTaskList({ | ||
| id: workerTasks.length - 1, | ||
| tasks: workerTasks[workerTasks.length - 1], | ||
| cacheDir: CACHE_DIR, | ||
| noCache, | ||
| usedCacheFiles: mainThreadUsedFiles, | ||
| }).then(data => { | ||
| if (taskFinishHandler(data)) { | ||
| if (taskFinishHandler(data, usedCacheFiles)) { | ||
| throw new Error(`Worker[${data.id}] had some errors.`); | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| await Promise.all(workerPromises); | ||
|
|
||
| // Clean up unused cache files to prevent unbounded growth | ||
| if (!noCache) { | ||
| try { | ||
| const allFiles = await readdir(CACHE_DIR); | ||
| let cleanedCount = 0; | ||
|
|
||
| for (const file of allFiles) { | ||
| if (!usedCacheFiles.has(file)) { | ||
| await rm(path.join(CACHE_DIR, file), {force: true}); | ||
| cleanedCount++; | ||
| } | ||
| } | ||
|
|
||
| if (cleanedCount > 0) { | ||
| console.log(`🧹 Cleaned up ${cleanedCount} unused cache files`); | ||
| } | ||
| } catch (err) { | ||
| console.warn('Failed to clean unused cache files:', err); | ||
| } | ||
| } | ||
|
|
||
| console.log(`📄 Generated ${numFiles} markdown files from HTML.`); | ||
| console.log('✅ Markdown export generation complete!'); | ||
| } | ||
|
|
||
| const md5 = data => createHash('md5').update(data).digest('hex'); | ||
|
|
||
| async function genMDFromHTML(source, target, {cacheDir, noCache}) { | ||
| async function genMDFromHTML(source, target, {cacheDir, noCache, usedCacheFiles}) { | ||
| const leanHTML = (await readFile(source, {encoding: 'utf8'})) | ||
| // Remove all script tags, as they are not needed in markdown | ||
| // and they are not stable across builds, causing cache misses | ||
|
|
@@ -210,6 +244,11 @@ async function genMDFromHTML(source, target, {cacheDir, noCache}) { | |
| ); | ||
| await writeFile(target, data, {encoding: 'utf8'}); | ||
|
|
||
| // Track that we used this cache file | ||
| if (usedCacheFiles) { | ||
| usedCacheFiles.add(cacheKey); | ||
| } | ||
|
|
||
| return {cacheHit: true, data}; | ||
| } catch (err) { | ||
| if (err.code !== 'ENOENT') { | ||
|
|
@@ -304,10 +343,20 @@ async function genMDFromHTML(source, target, {cacheDir, noCache}) { | |
| ).catch(err => console.warn('Error writing cache file:', err)), | ||
| ]); | ||
|
|
||
| // Track that we created this cache file | ||
| if (usedCacheFiles) { | ||
| usedCacheFiles.add(cacheKey); | ||
| } | ||
|
|
||
| return {cacheHit: false, data}; | ||
| } | ||
|
|
||
| async function processTaskList({id, tasks, cacheDir, noCache}) { | ||
| async function processTaskList({id, tasks, cacheDir, noCache, usedCacheFiles}) { | ||
| // Workers don't receive usedCacheFiles in workerData, so create a new Set | ||
| if (!usedCacheFiles) { | ||
| usedCacheFiles = new Set(); | ||
| } | ||
|
|
||
| const s3Client = getS3Client(); | ||
| const failedTasks = []; | ||
| let cacheMisses = []; | ||
|
|
@@ -318,6 +367,7 @@ async function processTaskList({id, tasks, cacheDir, noCache}) { | |
| const {data, cacheHit} = await genMDFromHTML(sourcePath, targetPath, { | ||
| cacheDir, | ||
| noCache, | ||
| usedCacheFiles, | ||
|
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. Bug: Worker Initialization Missing Cache SetWorker threads are initialized without a Additional Locations (1) |
||
| }); | ||
| if (!cacheHit) { | ||
| cacheMisses.push(relativePath); | ||
|
|
@@ -357,6 +407,7 @@ async function processTaskList({id, tasks, cacheDir, noCache}) { | |
| id, | ||
| success, | ||
| failedTasks, | ||
| usedCacheFiles: Array.from(usedCacheFiles), | ||
| }; | ||
| } | ||
|
|
||
|
|
||
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.
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.
Bug: Cache Miss Handling Fails
The cache cleanup logic immediately deletes newly created cache files. When
genMDFromHTMLgenerates a cache file due to a miss, it doesn't add the file's key to theusedCacheFilesset. This prevents the cache from being effective for those files in subsequent builds.Additional Locations (1)
scripts/generate-md-exports.mjs#L211-L216