Skip to content

Commit d17642a

Browse files
authored
fix(cli): asset existence check is slow for many assets (#25866)
When an application has many assets (think ~100) the existence check which seeks to scrub already existing assets from the work graph is starting to take a significant amount of time. Do those checks in parallel. Also in this PR: - Improve the printing of the work graph a little to make it slightly less unreadable if the graph gets large. - Print the graphviz representation of the work graph to the trace log if the graph gets stuck, for debugging purposes. - In the work graph builder, only add dependencies on stacks. Previously, it used to add dependencies on assets as if they were stacks (those were later on removed, but we didn't need to add them in the first place). ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2b6538d commit d17642a

File tree

4 files changed

+105
-16
lines changed

4 files changed

+105
-16
lines changed

packages/aws-cdk/lib/util/parallel.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Run a number of promise generators with max parallelism
3+
*
4+
* Order is not maintained between the input and output.
5+
*/
6+
export async function parallelPromises<A>(n: number, promises: Array<() => Promise<A>>): Promise<Array<A>> {
7+
const ret = new Array<A>();
8+
let count = 0;
9+
let error: Error | undefined;
10+
const queue = [...promises];
11+
12+
return new Promise((ok, ko) => {
13+
tick();
14+
15+
function tick() {
16+
if (count === 0 && error) {
17+
ko(error);
18+
return;
19+
}
20+
if (count === 0 && queue.length === 0) {
21+
ok(ret);
22+
return;
23+
}
24+
25+
while (count < n && queue.length > 0 && !error) {
26+
const next = queue.shift();
27+
if (next !== undefined) {
28+
start(next);
29+
}
30+
}
31+
}
32+
33+
function start(fn: () => Promise<A>) {
34+
count += 1;
35+
fn()
36+
.then((result) => { ret.push(result); })
37+
.catch((e) => { error = e; })
38+
.finally(() => {
39+
count -= 1;
40+
tick();
41+
});
42+
}
43+
});
44+
}

packages/aws-cdk/lib/util/work-graph-builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class WorkGraphBuilder {
2727
this.graph.addNodes({
2828
type: 'stack',
2929
id: `${this.idPrefix}${artifact.id}`,
30-
dependencies: new Set(this.getDepIds(artifact.dependencies)),
30+
dependencies: new Set(this.getDepIds(onlyStacks(artifact.dependencies))),
3131
stack: artifact,
3232
deploymentState: DeploymentState.PENDING,
3333
priority: WorkGraphBuilder.PRIORITIES.stack,

packages/aws-cdk/lib/util/work-graph.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { parallelPromises } from './parallel';
12
import { WorkNode, DeploymentState, StackNode, AssetBuildNode, AssetPublishNode } from './work-graph-types';
3+
import { debug, trace } from '../logging';
24

35
export type Concurrency = number | Record<WorkNode['type'], number>;
46

@@ -215,15 +217,19 @@ export class WorkGraph {
215217
function renderNode(id: string, node: WorkNode): string[] {
216218
const ret = [];
217219
if (node.deploymentState === DeploymentState.COMPLETED) {
218-
ret.push(` "${id}" [style=filled,fillcolor=yellow];`);
220+
ret.push(` "${simplifyId(id)}" [style=filled,fillcolor=yellow];`);
219221
} else {
220-
ret.push(` "${id}";`);
222+
ret.push(` "${simplifyId(id)}";`);
221223
}
222224
for (const dep of node.dependencies) {
223-
ret.push(` "${id}" -> "${dep}";`);
225+
ret.push(` "${simplifyId(id)}" -> "${simplifyId(dep)}";`);
224226
}
225227
return ret;
226228
}
229+
230+
function simplifyId(id: string) {
231+
return id.replace(/([0-9a-f]{6})[0-9a-f]{6,}/g, '$1');
232+
}
227233
}
228234

229235
/**
@@ -234,12 +240,8 @@ export class WorkGraph {
234240
*/
235241
public removeUnavailableDependencies() {
236242
for (const node of Object.values(this.nodes)) {
237-
const removeDeps = [];
238-
for (const dep of node.dependencies) {
239-
if (this.nodes[dep] === undefined) {
240-
removeDeps.push(dep);
241-
}
242-
}
243+
const removeDeps = Array.from(node.dependencies).filter((dep) => this.nodes[dep] === undefined);
244+
243245
removeDeps.forEach((d) => {
244246
node.dependencies.delete(d);
245247
});
@@ -249,16 +251,25 @@ export class WorkGraph {
249251
/**
250252
* Remove all asset publishing steps for assets that are already published, and then build
251253
* that aren't used anymore.
254+
*
255+
* Do this in parallel, because there may be a lot of assets in an application (seen in practice: >100 assets)
252256
*/
253257
public async removeUnnecessaryAssets(isUnnecessary: (x: AssetPublishNode) => Promise<boolean>) {
258+
debug('Checking for previously published assets');
259+
254260
const publishes = this.nodesOfType('asset-publish');
255-
for (const assetNode of publishes) {
256-
const unnecessary = await isUnnecessary(assetNode);
257-
if (unnecessary) {
258-
this.removeNode(assetNode);
259-
}
261+
262+
const classifiedNodes = await parallelPromises(
263+
8,
264+
publishes.map((assetNode) => async() => [assetNode, await isUnnecessary(assetNode)] as const));
265+
266+
const alreadyPublished = classifiedNodes.filter(([_, unnecessary]) => unnecessary).map(([assetNode, _]) => assetNode);
267+
for (const assetNode of alreadyPublished) {
268+
this.removeNode(assetNode);
260269
}
261270

271+
debug(`${publishes.length} total assets, ${publishes.length - alreadyPublished.length} still need to be published`);
272+
262273
// Now also remove any asset build steps that don't have any dependencies on them anymore
263274
const unusedBuilds = this.nodesOfType('asset-build').filter(build => this.dependees(build).length === 0);
264275
for (const unusedBuild of unusedBuilds) {
@@ -288,7 +299,8 @@ export class WorkGraph {
288299

289300
if (this.readyPool.length === 0 && activeCount === 0 && pendingCount > 0) {
290301
const cycle = this.findCycle() ?? ['No cycle found!'];
291-
throw new Error(`Unable to make progress anymore, dependency cycle between remaining artifacts: ${cycle.join(' -> ')}`);
302+
trace(`Cycle ${cycle.join(' -> ')} in graph ${this}`);
303+
throw new Error(`Unable to make progress anymore, dependency cycle between remaining artifacts: ${cycle.join(' -> ')} (run with -vv for full graph)`);
292304
}
293305
}
294306

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { parallelPromises } from '../../lib/util/parallel';
2+
import { sleep } from '../util';
3+
4+
test('parallelPromises', async () => {
5+
const N = 4;
6+
const J = 100;
7+
8+
let jobsDone = 0;
9+
let concurrent = 0;
10+
let maxConcurrent = 0;
11+
12+
const jobs = range(J).map(() => async () => {
13+
concurrent += 1;
14+
maxConcurrent = Math.max(concurrent, maxConcurrent);
15+
await sleep(Math.round(Math.random() * 100));
16+
concurrent -= 1;
17+
jobsDone += 1;
18+
});
19+
20+
await parallelPromises(N, jobs);
21+
22+
expect(maxConcurrent).toBeLessThanOrEqual(N);
23+
expect(maxConcurrent).toBeGreaterThan(1);
24+
expect(jobsDone).toEqual(J);
25+
});
26+
27+
function range(n: number) {
28+
const ret = new Array<number>();
29+
for (let i = 0; i < n; i++) {
30+
ret.push(i);
31+
}
32+
return ret;
33+
}

0 commit comments

Comments
 (0)