@reventlessdev/reventless-seed-aws 1.0.0-alpha.3 → 1.0.0-alpha.30

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +207 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/.sourcedirs.json +1 -0
  4. package/lib/bs/compiler-info.json +2 -2
  5. package/lib/bs/src/ReventlessSeedAws.ast +0 -0
  6. package/lib/bs/src/ReventlessSeedAws.cmi +0 -0
  7. package/lib/bs/src/ReventlessSeedAws.cmj +0 -0
  8. package/lib/bs/src/ReventlessSeedAws.cmt +0 -0
  9. package/lib/bs/src/ReventlessSeedAws.res +198 -134
  10. package/lib/bs/src/ReventlessSeedAws.res.mjs +57 -31
  11. package/lib/bs/src/ReventlessSeedAws_Quiesce.ast +0 -0
  12. package/lib/bs/src/ReventlessSeedAws_Quiesce.cmi +0 -0
  13. package/lib/bs/src/ReventlessSeedAws_Quiesce.cmj +0 -0
  14. package/lib/bs/src/ReventlessSeedAws_Quiesce.cmt +0 -0
  15. package/lib/bs/src/ReventlessSeedAws_Quiesce.res +307 -0
  16. package/lib/bs/src/ReventlessSeedAws_Quiesce.res.mjs +217 -0
  17. package/lib/bs/src/ReventlessSeedAws_Reconcile.ast +0 -0
  18. package/lib/bs/src/ReventlessSeedAws_Reconcile.cmi +0 -0
  19. package/lib/bs/src/ReventlessSeedAws_Reconcile.cmj +0 -0
  20. package/lib/bs/src/ReventlessSeedAws_Reconcile.cmt +0 -0
  21. package/lib/bs/src/ReventlessSeedAws_Reconcile.res +391 -0
  22. package/lib/bs/src/ReventlessSeedAws_Reconcile.res.mjs +318 -0
  23. package/lib/bs/src/ReventlessSeedAws_Reset.ast +0 -0
  24. package/lib/bs/src/ReventlessSeedAws_Reset.cmi +0 -0
  25. package/lib/bs/src/ReventlessSeedAws_Reset.cmj +0 -0
  26. package/lib/bs/src/ReventlessSeedAws_Reset.cmt +0 -0
  27. package/lib/bs/src/ReventlessSeedAws_Reset.res +1103 -0
  28. package/lib/bs/src/ReventlessSeedAws_Reset.res.mjs +991 -0
  29. package/lib/bs/tests/EndpointResolutionTest.ast +0 -0
  30. package/lib/bs/tests/EndpointResolutionTest.cmi +0 -0
  31. package/lib/bs/tests/EndpointResolutionTest.cmj +0 -0
  32. package/lib/bs/tests/EndpointResolutionTest.cmt +0 -0
  33. package/lib/bs/tests/EndpointResolutionTest.res +83 -0
  34. package/lib/bs/tests/EndpointResolutionTest.res.mjs +115 -0
  35. package/lib/bs/tests/ObjectStoreReconcileTest.ast +0 -0
  36. package/lib/bs/tests/ObjectStoreReconcileTest.cmi +0 -0
  37. package/lib/bs/tests/ObjectStoreReconcileTest.cmj +0 -0
  38. package/lib/bs/tests/ObjectStoreReconcileTest.cmt +0 -0
  39. package/lib/bs/tests/ObjectStoreReconcileTest.res +83 -0
  40. package/lib/bs/tests/ObjectStoreReconcileTest.res.mjs +102 -0
  41. package/lib/bs/tests/ObjectStoreResetTest.ast +0 -0
  42. package/lib/bs/tests/ObjectStoreResetTest.cmi +0 -0
  43. package/lib/bs/tests/ObjectStoreResetTest.cmj +0 -0
  44. package/lib/bs/tests/ObjectStoreResetTest.cmt +0 -0
  45. package/lib/bs/tests/ObjectStoreResetTest.res +191 -0
  46. package/lib/bs/tests/ObjectStoreResetTest.res.mjs +211 -0
  47. package/lib/bs/tests/QuiesceResetTest.ast +0 -0
  48. package/lib/bs/tests/QuiesceResetTest.cmi +0 -0
  49. package/lib/bs/tests/QuiesceResetTest.cmj +0 -0
  50. package/lib/bs/tests/QuiesceResetTest.cmt +0 -0
  51. package/lib/bs/tests/QuiesceResetTest.res +169 -0
  52. package/lib/bs/tests/QuiesceResetTest.res.mjs +157 -0
  53. package/lib/ocaml/.compiler.log +2 -2
  54. package/lib/ocaml/EndpointResolutionTest.ast +0 -0
  55. package/lib/ocaml/EndpointResolutionTest.cmi +0 -0
  56. package/lib/ocaml/EndpointResolutionTest.cmj +0 -0
  57. package/lib/ocaml/EndpointResolutionTest.cmt +0 -0
  58. package/lib/ocaml/EndpointResolutionTest.res +83 -0
  59. package/lib/ocaml/ObjectStoreReconcileTest.ast +0 -0
  60. package/lib/ocaml/ObjectStoreReconcileTest.cmi +0 -0
  61. package/lib/ocaml/ObjectStoreReconcileTest.cmj +0 -0
  62. package/lib/ocaml/ObjectStoreReconcileTest.cmt +0 -0
  63. package/lib/ocaml/ObjectStoreReconcileTest.res +83 -0
  64. package/lib/ocaml/ObjectStoreResetTest.ast +0 -0
  65. package/lib/ocaml/ObjectStoreResetTest.cmi +0 -0
  66. package/lib/ocaml/ObjectStoreResetTest.cmj +0 -0
  67. package/lib/ocaml/ObjectStoreResetTest.cmt +0 -0
  68. package/lib/ocaml/ObjectStoreResetTest.res +191 -0
  69. package/lib/ocaml/QuiesceResetTest.ast +0 -0
  70. package/lib/ocaml/QuiesceResetTest.cmi +0 -0
  71. package/lib/ocaml/QuiesceResetTest.cmj +0 -0
  72. package/lib/ocaml/QuiesceResetTest.cmt +0 -0
  73. package/lib/ocaml/QuiesceResetTest.res +169 -0
  74. package/lib/ocaml/ReventlessSeedAws.ast +0 -0
  75. package/lib/ocaml/ReventlessSeedAws.cmi +0 -0
  76. package/lib/ocaml/ReventlessSeedAws.cmj +0 -0
  77. package/lib/ocaml/ReventlessSeedAws.cmt +0 -0
  78. package/lib/ocaml/ReventlessSeedAws.res +198 -134
  79. package/lib/ocaml/ReventlessSeedAws_Quiesce.ast +0 -0
  80. package/lib/ocaml/ReventlessSeedAws_Quiesce.cmi +0 -0
  81. package/lib/ocaml/ReventlessSeedAws_Quiesce.cmj +0 -0
  82. package/lib/ocaml/ReventlessSeedAws_Quiesce.cmt +0 -0
  83. package/lib/ocaml/ReventlessSeedAws_Quiesce.res +307 -0
  84. package/lib/ocaml/ReventlessSeedAws_Reconcile.ast +0 -0
  85. package/lib/ocaml/ReventlessSeedAws_Reconcile.cmi +0 -0
  86. package/lib/ocaml/ReventlessSeedAws_Reconcile.cmj +0 -0
  87. package/lib/ocaml/ReventlessSeedAws_Reconcile.cmt +0 -0
  88. package/lib/ocaml/ReventlessSeedAws_Reconcile.res +391 -0
  89. package/lib/ocaml/ReventlessSeedAws_Reset.ast +0 -0
  90. package/lib/ocaml/ReventlessSeedAws_Reset.cmi +0 -0
  91. package/lib/ocaml/ReventlessSeedAws_Reset.cmj +0 -0
  92. package/lib/ocaml/ReventlessSeedAws_Reset.cmt +0 -0
  93. package/lib/ocaml/ReventlessSeedAws_Reset.res +1103 -0
  94. package/package.json +7 -4
  95. package/rescript.json +9 -1
  96. package/src/ReventlessSeedAws.res +198 -134
  97. package/src/ReventlessSeedAws.res.mjs +57 -31
  98. package/src/ReventlessSeedAws_Quiesce.res +307 -0
  99. package/src/ReventlessSeedAws_Quiesce.res.mjs +217 -0
  100. package/src/ReventlessSeedAws_Reconcile.res +391 -0
  101. package/src/ReventlessSeedAws_Reconcile.res.mjs +318 -0
  102. package/src/ReventlessSeedAws_Reset.res +1103 -0
  103. package/src/ReventlessSeedAws_Reset.res.mjs +991 -0
  104. package/tests/EndpointResolutionTest.res +83 -0
  105. package/tests/EndpointResolutionTest.res.mjs +115 -0
  106. package/tests/ObjectStoreReconcileTest.res +83 -0
  107. package/tests/ObjectStoreReconcileTest.res.mjs +102 -0
  108. package/tests/ObjectStoreResetTest.res +191 -0
  109. package/tests/ObjectStoreResetTest.res.mjs +211 -0
  110. package/tests/QuiesceResetTest.res +169 -0
  111. package/tests/QuiesceResetTest.res.mjs +157 -0
@@ -0,0 +1,307 @@
1
+ // Taking a deployed stack's runtimes out of service for the length of a wipe,
2
+ // and putting them back exactly as they were.
3
+ //
4
+ // Why a wipe needs this at all: a truncate is not durable while the runtimes
5
+ // that own the data are running. A slice runtime keeps its TODO list in a
6
+ // module-level dict for the lifetime of its execution environment and re-saves
7
+ // every row it holds at the end of each invocation — including the scheduled
8
+ // sweep, which carries no events. Emptying the table under a warm container
9
+ // therefore succeeds and is then undone, byte for byte, by the next sweep: the
10
+ // rows are not recomputed from anything, so their ids and timestamps come back
11
+ // identical. Emptying the upstream stores first does not help, because nothing
12
+ // upstream is read.
13
+ //
14
+ // The answer is to remove the contention rather than to outrun it, which takes
15
+ // two distinct steps, in this order:
16
+ //
17
+ // 1. HOLD — reserve 0 concurrency on every function in scope, so no new
18
+ // invocation can start while the wipe and its verification run. This alone
19
+ // is not enough: a reservation stops invocations, it does not discard the
20
+ // execution environment that holds the state.
21
+ // 2. RECYCLE — change each function's configuration once. A configuration
22
+ // change is the documented way to make Lambda discard existing execution
23
+ // environments, so whatever a warm container was holding is gone before it
24
+ // can be invoked again. Done while still held, so the container that
25
+ // eventually replaces it starts from the emptied store.
26
+ //
27
+ // The recycle is a round trip — a marker variable added, then the original map
28
+ // put back — so the function ends the run byte-identical to how it started and
29
+ // `pulumi preview` reports no drift. The second update recycles again, which is
30
+ // harmless: it happens while the hold is still on and the store is already
31
+ // empty.
32
+ //
33
+ // Everything acquired here is released on the failure path as well as the
34
+ // success path, and the release never throws: a reset that aborted mid-wipe
35
+ // must still hand the stack back. Release failures come back as warnings for
36
+ // the caller to print, because a function left at zero concurrency is a stack
37
+ // left switched off, and that has to be said out loud rather than raised as one
38
+ // more exception on top of whatever already went wrong.
39
+
40
+ module Lambda = AwsSdk.Lambda
41
+
42
+ /** The variable a recycle adds and then removes. Named for what it is, so an
43
+ operator who finds one left behind by a killed run knows it is inert and can
44
+ be deleted (or left for the next `pulumi up` to remove). */
45
+ let markerKey = "REVENTLESS_RESET_RECYCLE"
46
+
47
+ /** What one function looked like before the reset touched it. */
48
+ type held = {
49
+ functionName: string,
50
+ /** `None` = the function had no reservation at all. That is a different state
51
+ from a reservation of 0, and restoring it needs
52
+ `DeleteFunctionConcurrency` rather than a put. */
53
+ priorReserved: option<int>,
54
+ /** The variable map as it was, flattened: a function with no `Environment`
55
+ block at all reads as an empty map and is restored to an empty map. */
56
+ priorVariables: dict<string>,
57
+ }
58
+
59
+ /** The Lambda control plane is rate-limited well below the data plane, and a
60
+ reset touches every function in a project at once. Six at a time keeps a
61
+ forty-function stack under a minute per pass without risking a throttle. */
62
+ let concurrency = 6
63
+
64
+ let mapBounded = async (items: array<'a>, ~limit: int, f: 'a => promise<'b>): array<'b> => {
65
+ let results = Array.make(~length=items->Array.length, None)
66
+ let next = ref(0)
67
+ let worker = async () => {
68
+ let running = ref(true)
69
+ while running.contents {
70
+ let i = next.contents
71
+ if i >= items->Array.length {
72
+ running := false
73
+ } else {
74
+ next := i + 1
75
+ let r = await f(items->Array.getUnsafe(i))
76
+ results->Array.set(i, Some(r))
77
+ }
78
+ }
79
+ }
80
+ let _ = await Array.fromInitializer(~length=Math.Int.min(limit, items->Array.length), _ =>
81
+ worker()
82
+ )->Promise.all
83
+ results->Array.filterMap(x => x)
84
+ }
85
+
86
+ let errorText = (exn: exn): string =>
87
+ exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown error")
88
+
89
+ /** A throttle and a missing permission both surface as a failure to hold, and
90
+ they call for opposite responses — wait and re-run, versus go and fix the
91
+ policy. The control plane says "Rate exceeded"; the SDK says
92
+ "TooManyRequestsException". Match on either. */
93
+ let isThrottle = (reason: string): bool => {
94
+ let r = reason->String.toLowerCase
95
+ r->String.includes("rate exceeded") ||
96
+ r->String.includes("toomanyrequests") ||
97
+ r->String.includes("throttl")
98
+ }
99
+
100
+ // An update is rejected while another is still settling, and a stack that was
101
+ // deployed moments ago can still be settling. Poll rather than assume.
102
+ let awaitSettled = async (~client, ~functionName: string): unit => {
103
+ let deadline = 60
104
+ let rec poll = async (attempt: int): unit =>
105
+ if attempt < deadline {
106
+ let out = await Lambda.GetFunctionConfigurationCommand.send(
107
+ client,
108
+ Lambda.GetFunctionConfigurationCommand.make({functionName: functionName}),
109
+ )
110
+ switch out.lastUpdateStatus {
111
+ | Some("InProgress") =>
112
+ await ReventlessSeed.Seed.Client.sleep(1000)
113
+ await poll(attempt + 1)
114
+ | _ => ()
115
+ }
116
+ }
117
+ await poll(0)
118
+ }
119
+
120
+ let updateVariables = async (~client, ~functionName: string, ~variables: dict<string>): unit => {
121
+ await awaitSettled(~client, ~functionName)
122
+ let _ = await Lambda.UpdateFunctionConfigurationCommand.send(
123
+ client,
124
+ Lambda.UpdateFunctionConfigurationCommand.make({
125
+ functionName,
126
+ environment: {variables: variables},
127
+ }),
128
+ )
129
+ await awaitSettled(~client, ~functionName)
130
+ }
131
+
132
+ /** Put one function's reservation back exactly as it was — a value it had, or no
133
+ reservation at all. Never throws: a function left reserved at 0 is a function
134
+ left switched off, which has to be said rather than raised. */
135
+ let restoreConcurrency = async (~client, ~h: held): option<string> =>
136
+ switch await (
137
+ switch h.priorReserved {
138
+ | Some(n) =>
139
+ Lambda.PutFunctionConcurrencyCommand.send(
140
+ client,
141
+ Lambda.PutFunctionConcurrencyCommand.make({
142
+ functionName: h.functionName,
143
+ reservedConcurrentExecutions: n,
144
+ }),
145
+ )->Promise.thenResolve(_ => ())
146
+ | None =>
147
+ Lambda.DeleteFunctionConcurrencyCommand.send(
148
+ client,
149
+ Lambda.DeleteFunctionConcurrencyCommand.make({functionName: h.functionName}),
150
+ )->Promise.thenResolve(_ => ())
151
+ }
152
+ ) {
153
+ | () => None
154
+ | exception exn =>
155
+ Some(
156
+ `LEFT SWITCHED OFF: could not restore concurrency on ${h.functionName} (${errorText(
157
+ exn,
158
+ )}). ` ++ `It is still reserved at 0 and will not run until that is undone.`,
159
+ )
160
+ }
161
+
162
+ /**
163
+ Hand the stack back: original environment first (which also drops the marker),
164
+ then the original concurrency.
165
+
166
+ That order matters. The environment restore is itself a configuration change, so
167
+ it recycles a second time; doing it while the function is still held means the
168
+ container that finally serves traffic is created after the restore, not during
169
+ it.
170
+
171
+ Never throws, for either step and for any function — this runs on the abort path
172
+ too, where an exception would replace the reason the reset failed with a
173
+ secondary one.
174
+ */
175
+ let release = async (~client, ~held: array<held>): array<string> => {
176
+ let warnings = await held->mapBounded(~limit=concurrency, async h => {
177
+ let envWarning = switch await updateVariables(
178
+ ~client,
179
+ ~functionName=h.functionName,
180
+ ~variables=h.priorVariables,
181
+ ) {
182
+ | () => None
183
+ | exception exn =>
184
+ Some(
185
+ `could not restore the environment of ${h.functionName} (${errorText(
186
+ exn,
187
+ )}) — it may still ` ++
188
+ `carry ${markerKey}, which is inert and is removed by the next deploy.`,
189
+ )
190
+ }
191
+ let concurrencyWarning = await restoreConcurrency(~client, ~h)
192
+ [envWarning, concurrencyWarning]->Array.filterMap(w => w)
193
+ })
194
+ warnings->Array.flat
195
+ }
196
+
197
+ /**
198
+ Discard every held function's execution environments, so nothing that loaded
199
+ state before the wipe can write it back afterwards.
200
+
201
+ Never throws. A function that cannot be recycled is the one case where the reset
202
+ genuinely cannot promise the store stays empty, so it comes back as a warning
203
+ naming the function — the caller reports it rather than swallowing it.
204
+ */
205
+ let recycle = async (~client, ~held: array<held>): array<string> => {
206
+ let warnings = await held->mapBounded(~limit=concurrency, async h => {
207
+ let marked = h.priorVariables->Dict.copy
208
+ marked->Dict.set(markerKey, "1")
209
+ switch await updateVariables(~client, ~functionName=h.functionName, ~variables=marked) {
210
+ | () => None
211
+ | exception exn =>
212
+ Some(
213
+ `could not recycle ${h.functionName} (${errorText(
214
+ exn,
215
+ )}) — a warm container may still hold ` ++ `pre-wipe state and write it back. Redeploy or update that function to clear it.`,
216
+ )
217
+ }
218
+ })
219
+ warnings->Array.filterMap(w => w)
220
+ }
221
+
222
+ /**
223
+ Take every named function out of service, recording what to put back.
224
+
225
+ Throws if any function cannot be held — before a single delete has been issued,
226
+ so a reset that cannot hold the platform still has the option of not starting.
227
+ The message names the IAM actions involved, because a missing permission is much
228
+ the likeliest cause and "AccessDenied" on its own does not say which one.
229
+
230
+ Crucially, it hands back whatever it did manage to take before throwing. The
231
+ functions are held concurrently, so a failure part-way through the batch leaves
232
+ earlier ones already at zero — and a reset that aborted while silently switching
233
+ off half a stack would be a worse failure than the one it was reporting. Only the
234
+ reservations are rolled back: this runs before `recycle`, so no environment has
235
+ been touched yet.
236
+ */
237
+ let hold = async (~client, ~functionNames: array<string>): array<held> => {
238
+ let attempts = await functionNames->mapBounded(~limit=concurrency, async functionName =>
239
+ switch await (
240
+ async () => {
241
+ let concurrencyOut = await Lambda.GetFunctionConcurrencyCommand.send(
242
+ client,
243
+ Lambda.GetFunctionConcurrencyCommand.make({functionName: functionName}),
244
+ )
245
+ let configOut = await Lambda.GetFunctionConfigurationCommand.send(
246
+ client,
247
+ Lambda.GetFunctionConfigurationCommand.make({functionName: functionName}),
248
+ )
249
+ let held = {
250
+ functionName,
251
+ priorReserved: concurrencyOut.reservedConcurrentExecutions,
252
+ priorVariables: configOut.environment
253
+ ->Option.flatMap(e => e.variables)
254
+ ->Option.getOr(Dict.make()),
255
+ }
256
+ let _ = await Lambda.PutFunctionConcurrencyCommand.send(
257
+ client,
258
+ Lambda.PutFunctionConcurrencyCommand.make({
259
+ functionName,
260
+ reservedConcurrentExecutions: 0,
261
+ }),
262
+ )
263
+ held
264
+ }
265
+ )() {
266
+ | held => Ok(held)
267
+ | exception exn => Error((functionName, errorText(exn)))
268
+ }
269
+ )
270
+
271
+ let taken = attempts->Array.filterMap(a =>
272
+ switch a {
273
+ | Ok(h) => Some(h)
274
+ | Error(_) => None
275
+ }
276
+ )
277
+ switch attempts->Array.findMap(a =>
278
+ switch a {
279
+ | Error(failure) => Some(failure)
280
+ | Ok(_) => None
281
+ }
282
+ ) {
283
+ | None => taken
284
+ | Some((functionName, reason)) =>
285
+ let rollback = await taken->mapBounded(~limit=concurrency, h => restoreConcurrency(~client, ~h))
286
+ let stranded = rollback->Array.filterMap(w => w)
287
+ let advice = if reason->isThrottle {
288
+ `The Lambda control plane throttled the hold. That budget is per account and region and is ` ++
289
+ `shared with anything else using it — a deploy, an inspector sync, another stack in the same ` ++
290
+ `region — so this is transient and nothing is wrong with the credentials. Wait for whatever ` ++ `else is running to finish and re-run.`
291
+ } else {
292
+ `The reset holds every runtime in scope for the length of the wipe, which needs ` ++
293
+ `lambda:GetFunctionConcurrency, lambda:GetFunctionConfiguration, lambda:PutFunctionConcurrency, ` ++ `lambda:DeleteFunctionConcurrency and lambda:UpdateFunctionConfiguration.`
294
+ }
295
+ throw(
296
+ ReventlessSeed.Seed.Failed(
297
+ `could not hold Lambda function ${functionName} at zero concurrency (${reason}) — nothing was ` ++
298
+ `deleted. ${advice} Set SEED_RESET_NO_QUIESCE=1 to wipe without the hold — but a running ` ++
299
+ `runtime can then write back what the wipe removes.` ++
300
+ switch stranded {
301
+ | [] => ""
302
+ | messages => `\n\n ` ++ messages->Array.join("\n ")
303
+ },
304
+ ),
305
+ )
306
+ }
307
+ }