Skip to content

Commit 4c2dcd5

Browse files
authored
feat(kernel): experimental runtime package cache (#3724)
Adds an experimental (hence opt-in) feature that caches the contents of loaded libraries in a directory that persists between executions, in order to spare the time it takes to extract the tarballs. When this feature is enabled, packages present in the cache will be used as-is (i.e: they are not checked for tampering) instead of being extracted from the tarball. The cache is keyed on: - The hash of the tarball - The name of the library - The version of the library Objects in the cache will expire if they are not used for 30 days, and are subsequently removed from disk (this avoids a cache growing extremely large over time). In order to enable the feature, the following environment variables are used: - `JSII_RUNTIME_PACKAGE_CACHE` must be set to `enabled` in order for the package cache to be active at all; - `JSII_RUNTIME_PACKAGE_CACHE_ROOT` can be used to change which directory is used as a cache root. It defaults to: * On MacOS: `$HOME/Library/Caches/com.amazonaws.jsii` * On Linux: `$HOME/.cache/aws/jsii/package-cache` * On Windows: `%LOCALAPPDATA%\AWS\jsii\package-cache` * On other platforms: `$TMP/aws-jsii-package-cache` - `JSII_RUNTIME_PACKAGE_CACHE_TTL` can be used to change the default time entries will remain in cache before expiring if they are not used. This defaults to 30 days, and the value is expressed in days. Set to `0` to immediately expire all the cache's content. When troubleshooting load performance, it is possible to obtain timing data for some critical parts of the library load process within the jsii kernel by setting `JSII_DEBUG_TIMING` environment variable. Related to #3389 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 4a52d4c commit 4c2dcd5

File tree

13 files changed

+574
-24
lines changed

13 files changed

+574
-24
lines changed

packages/@jsii/kernel/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
"dependencies": {
3434
"@jsii/spec": "^0.0.0",
3535
"fs-extra": "^10.1.0",
36+
"lockfile": "^1.0.4",
3637
"tar": "^6.1.11"
3738
},
3839
"devDependencies": {
3940
"@scope/jsii-calc-base": "^0.0.0",
4041
"@scope/jsii-calc-lib": "^0.0.0",
4142
"@types/fs-extra": "^9.0.13",
43+
"@types/lockfile": "^1.0.2",
4244
"@types/tar": "^6.1.2",
4345
"jest-expect-message": "^1.0.2",
4446
"jsii-build-tools": "^0.0.0",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createHash } from 'crypto';
2+
import { openSync, readSync, closeSync } from 'fs';
3+
4+
const ALGORITHM = 'sha256';
5+
6+
export function digestFile(
7+
path: string,
8+
...comments: readonly string[]
9+
): Buffer {
10+
const hash = createHash(ALGORITHM);
11+
12+
const buffer = Buffer.alloc(16_384);
13+
const fd = openSync(path, 'r');
14+
try {
15+
let bytesRead = 0;
16+
while ((bytesRead = readSync(fd, buffer)) > 0) {
17+
hash.update(buffer.slice(0, bytesRead));
18+
}
19+
for (const comment of comments) {
20+
hash.update('\0');
21+
hash.update(comment);
22+
}
23+
return hash.digest();
24+
} finally {
25+
closeSync(fd);
26+
}
27+
}
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import {
2+
existsSync,
3+
mkdirSync,
4+
readdirSync,
5+
readFileSync,
6+
realpathSync,
7+
rmdirSync,
8+
rmSync,
9+
statSync,
10+
utimesSync,
11+
writeFileSync,
12+
} from 'fs';
13+
import { lockSync, unlockSync } from 'lockfile';
14+
import { dirname, join } from 'path';
15+
16+
import { digestFile } from './digest-file';
17+
18+
const MARKER_FILE_NAME = '.jsii-runtime-package-cache';
19+
20+
const ONE_DAY_IN_MS = 86_400_000;
21+
const PRUNE_AFTER_MILLISECONDS = process.env.JSII_RUNTIME_PACKAGE_CACHE_TTL
22+
? parseInt(process.env.JSII_RUNTIME_PACKAGE_CACHE_TTL, 10) * ONE_DAY_IN_MS
23+
: 30 * ONE_DAY_IN_MS;
24+
25+
export class DiskCache {
26+
private static readonly CACHE = new Map<string, DiskCache>();
27+
28+
public static inDirectory(path: string): DiskCache {
29+
const didCreate = mkdirSync(path, { recursive: true }) != null;
30+
if (didCreate && process.platform === 'darwin') {
31+
// Mark the directories for no iCloud sync, no Spotlight indexing, no TimeMachine backup
32+
// @see https://michaelbach.de/2019/03/19/MacOS-nosync-noindex-nobackup.html
33+
writeFileSync(join(path, '.nobackup'), '');
34+
writeFileSync(join(path, '.noindex'), '');
35+
writeFileSync(join(path, '.nosync'), '');
36+
}
37+
38+
path = realpathSync(path);
39+
if (!this.CACHE.has(path)) {
40+
this.CACHE.set(path, new DiskCache(path));
41+
}
42+
return this.CACHE.get(path)!;
43+
}
44+
45+
readonly #root: string;
46+
47+
private constructor(root: string) {
48+
this.#root = root;
49+
process.once('beforeExit', () => this.pruneExpiredEntries());
50+
}
51+
52+
public entry(...key: readonly string[]): Entry {
53+
if (key.length === 0) {
54+
throw new Error(`Cache entry key must contain at least 1 element!`);
55+
}
56+
57+
return new Entry(
58+
join(
59+
this.#root,
60+
...key.flatMap((s) =>
61+
s
62+
.replace(/[^@a-z0-9_.\\/-]+/g, '_')
63+
.split(/[\\/]+/)
64+
.map((ss) => {
65+
if (ss === '..') {
66+
throw new Error(
67+
`A cache entry key cannot contain a '..' path segment! (${s})`,
68+
);
69+
}
70+
return ss;
71+
}),
72+
),
73+
),
74+
);
75+
}
76+
77+
public entryFor(path: string, ...comments: readonly string[]): Entry {
78+
const rawDigest = digestFile(path, ...comments);
79+
return this.entry(...comments, rawDigest.toString('hex'));
80+
}
81+
82+
public pruneExpiredEntries() {
83+
const cutOff = new Date(Date.now() - PRUNE_AFTER_MILLISECONDS);
84+
for (const entry of this.entries()) {
85+
if (entry.atime < cutOff) {
86+
entry.lock((lockedEntry) => {
87+
// Check again in case it's been accessed which we waited for the lock...
88+
if (entry.atime > cutOff) {
89+
return;
90+
}
91+
lockedEntry.delete();
92+
});
93+
}
94+
}
95+
96+
for (const dir of directoriesUnder(this.#root, true)) {
97+
if (process.platform === 'darwin') {
98+
try {
99+
rmSync(join(dir, '.DS_Store'), { force: true });
100+
} catch {
101+
// Ignore errors...
102+
}
103+
}
104+
if (readdirSync(dir).length === 0) {
105+
try {
106+
rmdirSync(dir);
107+
} catch {
108+
// Ignore errors, directory may no longer be empty...
109+
}
110+
}
111+
}
112+
}
113+
114+
private *entries(): Generator<Entry, void, void> {
115+
yield* inDirectory(this.#root);
116+
117+
function* inDirectory(dir: string): Generator<Entry, void, void> {
118+
if (existsSync(join(dir, MARKER_FILE_NAME))) {
119+
return yield new Entry(dir);
120+
}
121+
for (const file of directoriesUnder(dir)) {
122+
yield* inDirectory(file);
123+
}
124+
}
125+
}
126+
}
127+
128+
export class Entry {
129+
public constructor(public readonly path: string) {}
130+
131+
public get atime(): Date {
132+
try {
133+
const stat = statSync(this.markerFile);
134+
return stat.atime;
135+
} catch (err: any) {
136+
if (err.code !== 'ENOENT') {
137+
throw err;
138+
}
139+
return new Date(0);
140+
}
141+
}
142+
143+
public get pathExists() {
144+
return existsSync(this.path);
145+
}
146+
147+
private get lockFile(): string {
148+
return `${this.path}.lock`;
149+
}
150+
151+
private get markerFile(): string {
152+
return join(this.path, MARKER_FILE_NAME);
153+
}
154+
155+
public lock<T>(cb: (entry: LockedEntry) => T): T {
156+
mkdirSync(dirname(this.path), { recursive: true });
157+
lockSync(this.lockFile, { retries: 12, stale: 5_000 });
158+
let disposed = false;
159+
try {
160+
return cb({
161+
delete: () => {
162+
if (disposed) {
163+
throw new Error(
164+
`Cannot delete ${this.path} once the lock block was returned!`,
165+
);
166+
}
167+
rmSync(this.path, { force: true, recursive: true });
168+
},
169+
write: (name, content) => {
170+
if (disposed) {
171+
throw new Error(
172+
`Cannot write ${join(
173+
this.path,
174+
name,
175+
)} once the lock block was returned!`,
176+
);
177+
}
178+
179+
mkdirSync(dirname(join(this.path, name)), { recursive: true });
180+
writeFileSync(join(this.path, name), content);
181+
},
182+
touch: () => {
183+
if (disposed) {
184+
throw new Error(
185+
`Cannot touch ${this.path} once the lock block was returned!`,
186+
);
187+
}
188+
if (this.pathExists) {
189+
if (existsSync(this.markerFile)) {
190+
const now = new Date();
191+
utimesSync(this.markerFile, now, now);
192+
} else {
193+
writeFileSync(this.markerFile, '');
194+
}
195+
}
196+
},
197+
});
198+
} finally {
199+
disposed = true;
200+
unlockSync(this.lockFile);
201+
}
202+
}
203+
204+
public read(file: string): Buffer | undefined {
205+
try {
206+
return readFileSync(join(this.path, file));
207+
} catch (error: any) {
208+
if (error.code === 'ENOENT') {
209+
return undefined;
210+
}
211+
throw error;
212+
}
213+
}
214+
}
215+
216+
export interface LockedEntry {
217+
delete(): void;
218+
write(name: string, data: Buffer): void;
219+
220+
touch(): void;
221+
}
222+
223+
function* directoriesUnder(
224+
root: string,
225+
recursive = false,
226+
ignoreErrors = true,
227+
): Generator<string, void, void> {
228+
for (const file of readdirSync(root)) {
229+
const path = join(root, file);
230+
try {
231+
const stat = statSync(path);
232+
if (stat.isDirectory()) {
233+
if (recursive) {
234+
yield* directoriesUnder(path, recursive, ignoreErrors);
235+
}
236+
yield path;
237+
}
238+
} catch (error) {
239+
if (!ignoreErrors) {
240+
throw error;
241+
}
242+
}
243+
}
244+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './disk-cache';

packages/@jsii/kernel/src/kernel.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import {
1414
WireStruct,
1515
TOKEN_STRUCT,
1616
} from './api';
17+
import { DiskCache } from './disk-cache';
1718
import { Kernel } from './kernel';
1819
import { closeRecording, recordInteraction } from './recording';
20+
import * as tar from './tar-cache';
21+
import { defaultCacheRoot } from './tar-cache/default-cache-root';
1922

2023
/* eslint-disable require-atomic-updates */
2124

@@ -49,6 +52,11 @@ if (recordingOutput) {
4952
console.error(`JSII_RECORD=${recordingOutput}`);
5053
}
5154

55+
afterAll(() => {
56+
// Jest prevents execution of "beforeExit" events.
57+
DiskCache.inDirectory(defaultCacheRoot()).pruneExpiredEntries();
58+
});
59+
5260
function defineTest(
5361
name: string,
5462
method: (sandbox: Kernel) => Promise<any> | any,
@@ -2147,6 +2155,9 @@ defineTest('invokeBinScript() return output', (sandbox) => {
21472155
const testNames: { [name: string]: boolean } = {};
21482156

21492157
async function createCalculatorSandbox(name: string) {
2158+
// Run half the tests with cache, half without cache... so we test both.
2159+
tar.setPackageCacheEnabled(!tar.getPackageCacheEnabled());
2160+
21502161
if (name in testNames) {
21512162
throw new Error(`Duplicate test name: ${name}`);
21522163
}

0 commit comments

Comments
 (0)