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
fix(ngcc): do not spawn unlocker processes on cluster workers #36569
Closed
Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/compiler-cli/ngcc/src/execution/cluster/lock_file_with_child_process.ts
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,42 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/// <reference types="node" /> | ||
|
||
import {ChildProcess} from 'child_process'; | ||
import * as cluster from 'cluster'; | ||
|
||
import {AbsoluteFsPath} from '../../../../src/ngtsc/file_system'; | ||
import {LockFileWithChildProcess} from '../../locking/lock_file_with_child_process'; | ||
|
||
|
||
/** | ||
* A `LockFileWithChildProcess` that is `cluster`-aware and does not spawn unlocker processes from | ||
* worker processes (only from the master process, which does the locking). | ||
*/ | ||
export class ClusterLockFileWithChildProcess extends LockFileWithChildProcess { | ||
write(): void { | ||
if (!cluster.isMaster) { | ||
// This is a worker process: | ||
// This method should only be on the master process. | ||
throw new Error('Tried to create a lock-file from a worker process.'); | ||
} | ||
|
||
return super.write(); | ||
} | ||
|
||
protected createUnlocker(path: AbsoluteFsPath): ChildProcess|null { | ||
if (cluster.isMaster) { | ||
// This is the master process: | ||
// Create the unlocker. | ||
return super.createUnlocker(path); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
packages/compiler-cli/ngcc/test/execution/cluster/lock_file_with_child_process_spec.ts
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,87 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/// <reference types="node" /> | ||
|
||
import {ChildProcess} from 'child_process'; | ||
import * as cluster from 'cluster'; | ||
|
||
import {getFileSystem} from '../../../../src/ngtsc/file_system'; | ||
import {runInEachFileSystem} from '../../../../src/ngtsc/file_system/testing'; | ||
import {ClusterLockFileWithChildProcess} from '../../../src/execution/cluster/lock_file_with_child_process'; | ||
import {LockFileWithChildProcess} from '../../../src/locking/lock_file_with_child_process'; | ||
import {MockLogger} from '../../helpers/mock_logger'; | ||
import {mockProperty} from '../../helpers/spy_utils'; | ||
|
||
|
||
runInEachFileSystem(() => { | ||
describe('ClusterLockFileWithChildProcess', () => { | ||
const runAsClusterMaster = mockProperty(cluster, 'isMaster'); | ||
const mockUnlockerProcess = {} as ChildProcess; | ||
let lockFileWithChildProcessSpies: | ||
Record<'createUnlocker'|'read'|'remove'|'write', jasmine.Spy>; | ||
|
||
beforeEach(() => { | ||
lockFileWithChildProcessSpies = { | ||
createUnlocker: spyOn(LockFileWithChildProcess.prototype as any, 'createUnlocker') | ||
.and.returnValue(mockUnlockerProcess), | ||
read: spyOn(LockFileWithChildProcess.prototype, 'read').and.returnValue('{unknown}'), | ||
remove: spyOn(LockFileWithChildProcess.prototype, 'remove'), | ||
write: spyOn(LockFileWithChildProcess.prototype, 'write'), | ||
}; | ||
}); | ||
|
||
it('should be an instance of `LockFileWithChildProcess`', () => { | ||
const lockFile = new ClusterLockFileWithChildProcess(getFileSystem(), new MockLogger()); | ||
|
||
expect(lockFile).toEqual(jasmine.any(ClusterLockFileWithChildProcess)); | ||
expect(lockFile).toEqual(jasmine.any(LockFileWithChildProcess)); | ||
}); | ||
|
||
describe('write()', () => { | ||
it('should create the lock-file when called on the cluster master', () => { | ||
runAsClusterMaster(true); | ||
const lockFile = new ClusterLockFileWithChildProcess(getFileSystem(), new MockLogger()); | ||
|
||
expect(lockFileWithChildProcessSpies.write).not.toHaveBeenCalled(); | ||
|
||
lockFile.write(); | ||
expect(lockFileWithChildProcessSpies.write).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should throw an error when called on a cluster worker', () => { | ||
runAsClusterMaster(false); | ||
const lockFile = new ClusterLockFileWithChildProcess(getFileSystem(), new MockLogger()); | ||
|
||
expect(() => lockFile.write()) | ||
.toThrowError('Tried to create a lock-file from a worker process.'); | ||
expect(lockFileWithChildProcessSpies.write).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('createUnlocker()', () => { | ||
it('should create the unlocker when called on the cluster master', () => { | ||
runAsClusterMaster(true); | ||
const lockFile = new ClusterLockFileWithChildProcess(getFileSystem(), new MockLogger()); | ||
|
||
lockFileWithChildProcessSpies.createUnlocker.calls.reset(); | ||
|
||
expect((lockFile as any).createUnlocker(lockFile.path)).toBe(mockUnlockerProcess); | ||
expect(lockFileWithChildProcessSpies.createUnlocker).toHaveBeenCalledWith(lockFile.path); | ||
}); | ||
|
||
it('should not create the unlocker when called on a cluster worker', () => { | ||
runAsClusterMaster(false); | ||
const lockFile = new ClusterLockFileWithChildProcess(getFileSystem(), new MockLogger()); | ||
|
||
expect((lockFile as any).createUnlocker(lockFile.path)).toBeNull(); | ||
expect(lockFileWithChildProcessSpies.createUnlocker).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
Rather than extending
LockFileWithChildProcess
, how about using composition instead...Obviously we would need to make
createUnlocker()
public, but this would make testing a lot cleaner, avoiding all those spies, since you could just pass a mockdelegate
in for testing.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.
It might need a bit of tweaking.... to work properly but you get the idea...
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.
Or perhaps we could move this logic into the
Locker
classes themselves? Saving the middleman?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.
That would be my preference too, but the
delegate
would have spawned the process in its constructor. There are ways around that but they either make things more complicated than they already are or end up delaying the creation of the unlocker (which we are trying to avoid here).Not sure what you mean by that 😅