@hot-updater/plugin-insights 1.2.0-rc.58 → 1.2.0-rc.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,12 +3,26 @@ let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
3
3
  //#region src/server/backfill.ts
4
4
  /**
5
5
  * Stored events read a page, and rewritten up to `BATCH` a transaction. A
6
- * store that takes fewer physical writes a transaction, as DynamoDB's 100
7
- * items hold fewer than 25 rewrites with their index copies and counts,
8
- * refuses the batch before writing anything; it is then rewritten in halves.
6
+ * run keeps one batch size, which only shrinks, halving whenever a store
7
+ * refuses a batch:
8
+ * - as too large, before writing anything: DynamoDB's 100 items hold fewer
9
+ * than 25 rewrites with their index copies and counts;
10
+ * - as busy through the transaction's retries: a write-capacity cap, such as
11
+ * the 100 write units a second `hot-updater init` gives a DynamoDB table,
12
+ * throttles a transaction that costs more than the cap admits, about 13
13
+ * rewrites of 2 KB events there, and refuses every write for seconds once
14
+ * the run has spent it.
15
+ * After a busy refusal the run also waits before it writes again, `PAUSE_MS`
16
+ * and twice as long after each further one, so the cap refills; a commit
17
+ * starts the waits over. A batch the store still refuses after the
18
+ * `MAX_PAUSE_MS` wait, or a one-event batch too large to write, fails the
19
+ * run, which a rerun resumes.
9
20
  */
10
21
  const PAGE = 500;
11
22
  const BATCH = 25;
23
+ const PAUSE_MS = 1e3;
24
+ const MAX_PAUSE_MS = 32e3;
25
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
12
26
  const sameRefs = (stored, refs) => {
13
27
  const values = Array.isArray(stored) ? stored : [];
14
28
  return values.length === refs.length && values.every((value) => refs.includes(value));
@@ -40,19 +54,30 @@ const namesArtifact = (row) => {
40
54
  const backfillReleaseRefs = async (db, now, { rawDays }, { dryRun = false } = {}) => {
41
55
  const today = now() - now() % require_schema.DAY_MS;
42
56
  let events = 0;
43
- const move = async (candidates) => {
57
+ let size = BATCH;
58
+ let paused = 0;
59
+ const move = async (stale) => {
44
60
  if (dryRun) {
45
- events += candidates.length;
61
+ events += stale.length;
46
62
  return;
47
63
  }
48
- const moved = await rewrite(candidates).catch(async (error) => {
49
- if (!(error instanceof _hot_updater_plugin_core.DatabaseConstraintError) || error.reason !== "too_large" || candidates.length === 1) throw error;
50
- const half = Math.ceil(candidates.length / 2);
51
- await move(candidates.slice(0, half));
52
- await move(candidates.slice(half));
53
- return 0;
54
- });
55
- events += moved;
64
+ for (let start = 0; start < stale.length;) {
65
+ const candidates = stale.slice(start, start + size);
66
+ try {
67
+ events += await rewrite(candidates);
68
+ start += candidates.length;
69
+ paused = 0;
70
+ } catch (error) {
71
+ if (error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "too_large" && candidates.length > 1) {
72
+ size = Math.ceil(candidates.length / 2);
73
+ continue;
74
+ }
75
+ if (!(0, _hot_updater_plugin_core.isDatabaseBusyError)(error) || paused >= MAX_PAUSE_MS) throw error;
76
+ size = Math.ceil(candidates.length / 2);
77
+ paused = paused === 0 ? PAUSE_MS : paused * 2;
78
+ await sleep(paused);
79
+ }
80
+ }
56
81
  };
57
82
  const rewrite = (candidates) => db.transaction(async (tx) => {
58
83
  const rows = await Promise.all(candidates.map(({ id }) => tx.findOne("bundle_events", { id })));
@@ -82,8 +107,7 @@ const backfillReleaseRefs = async (db, now, { rawDays }, { dryRun = false } = {}
82
107
  limit: PAGE,
83
108
  ...cursor === void 0 ? {} : { cursor }
84
109
  });
85
- const stale = page.rows.filter(namesArtifact);
86
- for (let start = 0; start < stale.length; start += BATCH) await move(stale.slice(start, start + BATCH));
110
+ await move(page.rows.filter(namesArtifact));
87
111
  cursor = page.next;
88
112
  } while (cursor !== void 0);
89
113
  }
@@ -1,14 +1,28 @@
1
1
  import { DAY_MS, HOUR_MS, bundleRefsOf, outcomeRefsOf } from "./schema.mjs";
2
- import { DatabaseConstraintError } from "@hot-updater/plugin-core";
2
+ import { DatabaseConstraintError, isDatabaseBusyError } from "@hot-updater/plugin-core";
3
3
  //#region src/server/backfill.ts
4
4
  /**
5
5
  * Stored events read a page, and rewritten up to `BATCH` a transaction. A
6
- * store that takes fewer physical writes a transaction, as DynamoDB's 100
7
- * items hold fewer than 25 rewrites with their index copies and counts,
8
- * refuses the batch before writing anything; it is then rewritten in halves.
6
+ * run keeps one batch size, which only shrinks, halving whenever a store
7
+ * refuses a batch:
8
+ * - as too large, before writing anything: DynamoDB's 100 items hold fewer
9
+ * than 25 rewrites with their index copies and counts;
10
+ * - as busy through the transaction's retries: a write-capacity cap, such as
11
+ * the 100 write units a second `hot-updater init` gives a DynamoDB table,
12
+ * throttles a transaction that costs more than the cap admits, about 13
13
+ * rewrites of 2 KB events there, and refuses every write for seconds once
14
+ * the run has spent it.
15
+ * After a busy refusal the run also waits before it writes again, `PAUSE_MS`
16
+ * and twice as long after each further one, so the cap refills; a commit
17
+ * starts the waits over. A batch the store still refuses after the
18
+ * `MAX_PAUSE_MS` wait, or a one-event batch too large to write, fails the
19
+ * run, which a rerun resumes.
9
20
  */
10
21
  const PAGE = 500;
11
22
  const BATCH = 25;
23
+ const PAUSE_MS = 1e3;
24
+ const MAX_PAUSE_MS = 32e3;
25
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
12
26
  const sameRefs = (stored, refs) => {
13
27
  const values = Array.isArray(stored) ? stored : [];
14
28
  return values.length === refs.length && values.every((value) => refs.includes(value));
@@ -40,19 +54,30 @@ const namesArtifact = (row) => {
40
54
  const backfillReleaseRefs = async (db, now, { rawDays }, { dryRun = false } = {}) => {
41
55
  const today = now() - now() % DAY_MS;
42
56
  let events = 0;
43
- const move = async (candidates) => {
57
+ let size = BATCH;
58
+ let paused = 0;
59
+ const move = async (stale) => {
44
60
  if (dryRun) {
45
- events += candidates.length;
61
+ events += stale.length;
46
62
  return;
47
63
  }
48
- const moved = await rewrite(candidates).catch(async (error) => {
49
- if (!(error instanceof DatabaseConstraintError) || error.reason !== "too_large" || candidates.length === 1) throw error;
50
- const half = Math.ceil(candidates.length / 2);
51
- await move(candidates.slice(0, half));
52
- await move(candidates.slice(half));
53
- return 0;
54
- });
55
- events += moved;
64
+ for (let start = 0; start < stale.length;) {
65
+ const candidates = stale.slice(start, start + size);
66
+ try {
67
+ events += await rewrite(candidates);
68
+ start += candidates.length;
69
+ paused = 0;
70
+ } catch (error) {
71
+ if (error instanceof DatabaseConstraintError && error.reason === "too_large" && candidates.length > 1) {
72
+ size = Math.ceil(candidates.length / 2);
73
+ continue;
74
+ }
75
+ if (!isDatabaseBusyError(error) || paused >= MAX_PAUSE_MS) throw error;
76
+ size = Math.ceil(candidates.length / 2);
77
+ paused = paused === 0 ? PAUSE_MS : paused * 2;
78
+ await sleep(paused);
79
+ }
80
+ }
56
81
  };
57
82
  const rewrite = (candidates) => db.transaction(async (tx) => {
58
83
  const rows = await Promise.all(candidates.map(({ id }) => tx.findOne("bundle_events", { id })));
@@ -82,8 +107,7 @@ const backfillReleaseRefs = async (db, now, { rawDays }, { dryRun = false } = {}
82
107
  limit: PAGE,
83
108
  ...cursor === void 0 ? {} : { cursor }
84
109
  });
85
- const stale = page.rows.filter(namesArtifact);
86
- for (let start = 0; start < stale.length; start += BATCH) await move(stale.slice(start, start + BATCH));
110
+ await move(page.rows.filter(namesArtifact));
87
111
  cursor = page.next;
88
112
  } while (cursor !== void 0);
89
113
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hot-updater/plugin-insights",
3
3
  "type": "module",
4
- "version": "1.2.0-rc.58",
4
+ "version": "1.2.0-rc.59",
5
5
  "description": "Hot Updater's Insights plugin: launch, download, and update failure events, for the server and the React Native app",
6
6
  "exports": {
7
7
  "./client": {
@@ -33,10 +33,10 @@
33
33
  "package.json"
34
34
  ],
35
35
  "dependencies": {
36
- "@hot-updater/protocol": "1.2.0-rc.58"
36
+ "@hot-updater/protocol": "1.2.0-rc.59"
37
37
  },
38
38
  "peerDependencies": {
39
- "@hot-updater/plugin-core": "^1.2.0-rc.58"
39
+ "@hot-updater/plugin-core": "^1.2.0-rc.59"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@hot-updater/plugin-core": {
@@ -46,7 +46,7 @@
46
46
  "devDependencies": {
47
47
  "@types/node": "^20",
48
48
  "vitest": "4.1.4",
49
- "@hot-updater/plugin-core": "1.2.0-rc.58"
49
+ "@hot-updater/plugin-core": "1.2.0-rc.59"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsdown",