-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): lock graph creation when running in another process (#29408)
## Current Behavior Running Nx in multiple processes at the same time with the daemon disabled can cripple a system due to excess memory usage when creating the graph. This is due to plugin workers being started per-parent process when there is no daemon. This change enables a file lock to prevent the simultaneous processing, and read from the cache when the first run completes. Currently, running `nx show projects` 30 times in parallel looks something like this: 30 processes exited within 37535ms ## Expected Behavior 30 processes exited within 6435ms ## Test Script ```js //@ts-check const { spawn } = require('child_process'); let alive = new Set(); let start = Date.now(); let iterations = 30; for (let i = 0; i < iterations; i++) { const cp = spawn('npx nx show projects', [], { shell: true, env: { ...process.env, NX_DAEMON: 'false', NX_VERBOSE_LOGGING: 'true', }, }); alive.add(i); // cp.stdout.on('data', (data) => { // console.log(`stdout [${i}]: ${data}`); // }); cp.stderr.on('data', (data) => { console.error(`stderr [${i}]: ${data}`); }); cp.on('exit', (code) => { console.log(`child process ${i} exited with code ${code}`); alive.delete(i); }); } const i = setInterval(() => { if (alive.size > 0) { } else { clearInterval(i); console.log( `${iterations} processes exited within ${Date.now() - start}ms` ); } }, 1); ```
- Loading branch information
1 parent
cbbe14b
commit 5721ea3
Showing
23 changed files
with
629 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,144 @@ | ||
# Class: StaleProjectGraphCacheError | ||
|
||
## Hierarchy | ||
|
||
- `Error` | ||
|
||
↳ **`StaleProjectGraphCacheError`** | ||
|
||
## Table of contents | ||
|
||
### Constructors | ||
|
||
- [constructor](../../devkit/documents/StaleProjectGraphCacheError#constructor) | ||
|
||
### Properties | ||
|
||
- [cause](../../devkit/documents/StaleProjectGraphCacheError#cause): unknown | ||
- [message](../../devkit/documents/StaleProjectGraphCacheError#message): string | ||
- [name](../../devkit/documents/StaleProjectGraphCacheError#name): string | ||
- [stack](../../devkit/documents/StaleProjectGraphCacheError#stack): string | ||
- [prepareStackTrace](../../devkit/documents/StaleProjectGraphCacheError#preparestacktrace): Function | ||
- [stackTraceLimit](../../devkit/documents/StaleProjectGraphCacheError#stacktracelimit): number | ||
|
||
### Methods | ||
|
||
- [captureStackTrace](../../devkit/documents/StaleProjectGraphCacheError#capturestacktrace) | ||
|
||
## Constructors | ||
|
||
### constructor | ||
|
||
• **new StaleProjectGraphCacheError**(): [`StaleProjectGraphCacheError`](../../devkit/documents/StaleProjectGraphCacheError) | ||
|
||
#### Returns | ||
|
||
[`StaleProjectGraphCacheError`](../../devkit/documents/StaleProjectGraphCacheError) | ||
|
||
#### Overrides | ||
|
||
Error.constructor | ||
|
||
## Properties | ||
|
||
### cause | ||
|
||
• `Optional` **cause**: `unknown` | ||
|
||
#### Inherited from | ||
|
||
Error.cause | ||
|
||
--- | ||
|
||
### message | ||
|
||
• **message**: `string` | ||
|
||
#### Inherited from | ||
|
||
Error.message | ||
|
||
--- | ||
|
||
### name | ||
|
||
• **name**: `string` | ||
|
||
#### Inherited from | ||
|
||
Error.name | ||
|
||
--- | ||
|
||
### stack | ||
|
||
• `Optional` **stack**: `string` | ||
|
||
#### Inherited from | ||
|
||
Error.stack | ||
|
||
--- | ||
|
||
### prepareStackTrace | ||
|
||
▪ `Static` `Optional` **prepareStackTrace**: (`err`: `Error`, `stackTraces`: `CallSite`[]) => `any` | ||
|
||
Optional override for formatting stack traces | ||
|
||
**`See`** | ||
|
||
https://v8.dev/docs/stack-trace-api#customizing-stack-traces | ||
|
||
#### Type declaration | ||
|
||
▸ (`err`, `stackTraces`): `any` | ||
|
||
##### Parameters | ||
|
||
| Name | Type | | ||
| :------------ | :----------- | | ||
| `err` | `Error` | | ||
| `stackTraces` | `CallSite`[] | | ||
|
||
##### Returns | ||
|
||
`any` | ||
|
||
#### Inherited from | ||
|
||
Error.prepareStackTrace | ||
|
||
--- | ||
|
||
### stackTraceLimit | ||
|
||
▪ `Static` **stackTraceLimit**: `number` | ||
|
||
#### Inherited from | ||
|
||
Error.stackTraceLimit | ||
|
||
## Methods | ||
|
||
### captureStackTrace | ||
|
||
▸ **captureStackTrace**(`targetObject`, `constructorOpt?`): `void` | ||
|
||
Create .stack property on a target object | ||
|
||
#### Parameters | ||
|
||
| Name | Type | | ||
| :---------------- | :--------- | | ||
| `targetObject` | `object` | | ||
| `constructorOpt?` | `Function` | | ||
|
||
#### Returns | ||
|
||
`void` | ||
|
||
#### Inherited from | ||
|
||
Error.captureStackTrace |
This file contains 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 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 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 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 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 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 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 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 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 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 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
20 changes: 20 additions & 0 deletions
20
packages/nx/src/native/tests/__fixtures__/file-lock.fixture.js
This file contains 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,20 @@ | ||
const { FileLock } = require('../../native-bindings.js'); | ||
const ora = require('ora'); | ||
const tmp = require('os').tmpdir(); | ||
|
||
(async () => { | ||
const lock = new FileLock( | ||
require('path').join(tmp, 'nx-unit-tests', 'file-lock-fixture') | ||
); | ||
if (lock.locked) { | ||
const s = ora('Waiting for lock').start(); | ||
await lock.wait(); | ||
s.stop(); | ||
console.log('waited for lock'); | ||
} else { | ||
await lock.lock(); | ||
await new Promise((resolve) => setTimeout(resolve, 5000)); | ||
console.log('ran with lock'); | ||
await lock.unlock(); | ||
} | ||
})(); |
Oops, something went wrong.