Repository navigation
fix(runtime): read script caches whole and publish them atomically #2167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3910d37
fix(runtime): read script caches whole and publish them atomically
edusperoni dd18955
test(runtime): terminate a failed concurrent-startup batch's workers …
edusperoni b627846
test(runtime): reject a failed concurrent-startup batch only after it…
edusperoni 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
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
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modA.js
Large diffs are not rendered by default.
Oops, something went wrong.
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modB.js
Large diffs are not rendered by default.
Oops, something went wrong.
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modC.js
Large diffs are not rendered by default.
Oops, something went wrong.
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modD.js
Large diffs are not rendered by default.
Oops, something went wrong.
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modE.js
Large diffs are not rendered by default.
Oops, something went wrong.
608 changes: 608 additions & 0 deletions
608
test-app/app/src/main/assets/app/tests/concurrentStartup/modF.js
Large diffs are not rendered by default.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker0.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modA = require("./modA").value; | ||
| values.modB = require("./modB").value; | ||
| values.modC = require("./modC").value; | ||
| values.modD = require("./modD").value; | ||
| values.modE = require("./modE").value; | ||
| values.modF = require("./modF").value; | ||
|
|
||
| postMessage({ values: values }); |
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker1.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modB = require("./modB").value; | ||
| values.modC = require("./modC").value; | ||
| values.modD = require("./modD").value; | ||
| values.modE = require("./modE").value; | ||
| values.modF = require("./modF").value; | ||
| values.modA = require("./modA").value; | ||
|
|
||
| postMessage({ values: values }); |
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker2.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modC = require("./modC").value; | ||
| values.modD = require("./modD").value; | ||
| values.modE = require("./modE").value; | ||
| values.modF = require("./modF").value; | ||
| values.modA = require("./modA").value; | ||
| values.modB = require("./modB").value; | ||
|
|
||
| postMessage({ values: values }); |
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker3.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modD = require("./modD").value; | ||
| values.modE = require("./modE").value; | ||
| values.modF = require("./modF").value; | ||
| values.modA = require("./modA").value; | ||
| values.modB = require("./modB").value; | ||
| values.modC = require("./modC").value; | ||
|
|
||
| postMessage({ values: values }); |
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker4.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modE = require("./modE").value; | ||
| values.modF = require("./modF").value; | ||
| values.modA = require("./modA").value; | ||
| values.modB = require("./modB").value; | ||
| values.modC = require("./modC").value; | ||
| values.modD = require("./modD").value; | ||
|
|
||
| postMessage({ values: values }); |
12 changes: 12 additions & 0 deletions
12
test-app/app/src/main/assets/app/tests/concurrentStartup/worker5.js
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Entry for testWorkerConcurrentStartup: reports the fixture values it | ||
| // observed, keyed by module name. Each entry loads the modules in a | ||
| // different order, so workers starting together hit different files at once. | ||
| var values = {}; | ||
| values.modF = require("./modF").value; | ||
| values.modA = require("./modA").value; | ||
| values.modB = require("./modB").value; | ||
| values.modC = require("./modC").value; | ||
| values.modD = require("./modD").value; | ||
| values.modE = require("./modE").value; | ||
|
|
||
| postMessage({ values: values }); |
112 changes: 112 additions & 0 deletions
112
test-app/app/src/main/assets/app/tests/testWorkerConcurrentStartup.js
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Needs "codeCache": true in the app's package.json; without it no runtime | ||
| // reads or writes the caches these specs race on. | ||
| describe("Script code caches with workers starting together", function () { | ||
| var moduleNames = ["modA", "modB", "modC", "modD", "modE", "modF"]; | ||
| var entryCount = 6; | ||
| var batchSize = entryCount * 2; | ||
| var fixtureDir = __dirname + "/concurrentStartup"; | ||
|
|
||
| var expected = {}; | ||
| moduleNames.forEach(function (name) { | ||
| expected[name] = require("./concurrentStartup/" + name).value; | ||
| }); | ||
|
|
||
| var cacheFile = function (name) { | ||
| return new java.io.File(fixtureDir, name + ".js.cache"); | ||
| }; | ||
|
|
||
| var dropCaches = function () { | ||
| moduleNames.forEach(function (name) { | ||
| cacheFile(name).delete(); | ||
| }); | ||
| }; | ||
|
|
||
| // Resolves with every worker's reported values once all of them have | ||
| // reported. A worker error terminates the whole batch and rejects only | ||
| // once every worker's thread has ended (`nsworkerended`), so no worker from | ||
| // a failed batch is still writing caches when the next spec drops them. | ||
| var runBatch = function () { | ||
| return new Promise(function (resolve, reject) { | ||
| var reports = []; | ||
| var workers = []; | ||
| var ended = 0; | ||
| var failure = null; | ||
| var rejectOnceAllEnded = function () { | ||
| if (failure !== null && ended === workers.length) { | ||
| reject(failure); | ||
| } | ||
| }; | ||
| for (var i = 0; i < batchSize; i++) { | ||
| (function (entry) { | ||
| var worker = new Worker("./concurrentStartup/worker" + entry + ".js"); | ||
| workers.push(worker); | ||
| worker.addEventListener("nsworkerended", function () { | ||
| ended++; | ||
| rejectOnceAllEnded(); | ||
| }); | ||
| worker.onmessage = function (msg) { | ||
| worker.terminate(); | ||
| if (failure !== null) { | ||
| return; | ||
| } | ||
| reports.push({ entry: entry, values: msg.data.values }); | ||
| if (reports.length === batchSize) { | ||
| resolve(reports); | ||
| } | ||
| }; | ||
| worker.onerror = function (e) { | ||
| if (failure !== null) { | ||
| return; | ||
| } | ||
| failure = new Error("worker" + entry + ": " + (e && e.message ? e.message : e)); | ||
| workers.forEach(function (w) { | ||
| w.terminate(); | ||
| }); | ||
| rejectOnceAllEnded(); | ||
| }; | ||
| })(i % entryCount); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| var expectValues = function (reports) { | ||
| expect(reports.length).toBe(batchSize); | ||
| reports.forEach(function (report) { | ||
| expect({ entry: report.entry, values: report.values }) | ||
| .toEqual({ entry: report.entry, values: expected }); | ||
| }); | ||
| }; | ||
|
|
||
| // A cold batch has some workers compiling and publishing a module's cache | ||
| // while others, further along their rotated load order, already consume | ||
| // it. Several rounds widen the chance of a reader overlapping a writer. | ||
| it("loads correct modules while caches are written and read concurrently", async function () { | ||
| for (var round = 0; round < 4; round++) { | ||
| dropCaches(); | ||
| expectValues(await runBatch()); | ||
| } | ||
| expectValues(await runBatch()); | ||
| }, 120000); | ||
|
|
||
| it("publishes caches stamped with their source's mtime and leaves no temporaries", async function () { | ||
| dropCaches(); | ||
| expectValues(await runBatch()); | ||
|
|
||
| moduleNames.forEach(function (name) { | ||
| var cache = cacheFile(name); | ||
| var source = new java.io.File(fixtureDir, name + ".js"); | ||
| expect(name + ": " + cache.exists()).toBe(name + ": true"); | ||
| expect(Math.floor(cache.lastModified() / 1000)) | ||
| .toBe(Math.floor(source.lastModified() / 1000)); | ||
| }); | ||
|
|
||
| var names = new java.io.File(fixtureDir).list(); | ||
| var leftovers = []; | ||
| for (var i = 0; i < names.length; i++) { | ||
| if (String(names[i]).indexOf(".cache.") !== -1) { | ||
| leftovers.push(String(names[i])); | ||
| } | ||
| } | ||
| expect(leftovers).toEqual([]); | ||
| }, 60000); | ||
| }); | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.