@rocicorp/zero 1.12.0-canary.10 → 1.12.0-canary.11

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 (218) hide show
  1. package/out/analyze-query/src/zql-chain.js +9 -1
  2. package/out/analyze-query/src/zql-chain.js.map +1 -1
  3. package/out/ast-to-zql/src/ast-to-zql.d.ts.map +1 -1
  4. package/out/ast-to-zql/src/ast-to-zql.js +61 -20
  5. package/out/ast-to-zql/src/ast-to-zql.js.map +1 -1
  6. package/out/z2s/src/compiler.d.ts +1 -1
  7. package/out/z2s/src/compiler.d.ts.map +1 -1
  8. package/out/z2s/src/compiler.js +339 -2
  9. package/out/z2s/src/compiler.js.map +1 -1
  10. package/out/z2s/src/sql.d.ts +8 -0
  11. package/out/z2s/src/sql.d.ts.map +1 -1
  12. package/out/z2s/src/sql.js +58 -11
  13. package/out/z2s/src/sql.js.map +1 -1
  14. package/out/zero/package.js +1 -1
  15. package/out/zero/package.js.map +1 -1
  16. package/out/zero/src/bindings.js +4 -4
  17. package/out/zero-cache/src/auth/write-authorizer.js +1 -1
  18. package/out/zero-cache/src/config/zero-config.d.ts +4 -0
  19. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  20. package/out/zero-cache/src/config/zero-config.js +13 -0
  21. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  22. package/out/zero-cache/src/db/create.js +1 -1
  23. package/out/zero-cache/src/scripts/decommission.js +1 -1
  24. package/out/zero-cache/src/server/runner/run-worker.d.ts.map +1 -1
  25. package/out/zero-cache/src/server/runner/run-worker.js +8 -5
  26. package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
  27. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  28. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
  29. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +1 -1
  30. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +1 -1
  31. package/out/zero-cache/src/services/change-source/pg/schema/init.js +1 -1
  32. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +1 -1
  33. package/out/zero-cache/src/services/http-service.d.ts +6 -0
  34. package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
  35. package/out/zero-cache/src/services/http-service.js +13 -1
  36. package/out/zero-cache/src/services/http-service.js.map +1 -1
  37. package/out/zero-cache/src/services/life-cycle.d.ts +19 -0
  38. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  39. package/out/zero-cache/src/services/life-cycle.js +53 -1
  40. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  41. package/out/zero-cache/src/services/replicator/change-processor.js +1 -1
  42. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +100 -12
  43. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  44. package/out/zero-cache/src/services/view-syncer/query-covering.js +2 -2
  45. package/out/zero-cache/src/services/view-syncer/query-covering.js.map +1 -1
  46. package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
  47. package/out/zero-cache/src/types/processes.js +4 -4
  48. package/out/zero-cache/src/types/processes.js.map +1 -1
  49. package/out/zero-cache/src/types/row-key.js +8 -1
  50. package/out/zero-cache/src/types/row-key.js.map +1 -1
  51. package/out/zero-cache/src/workers/connection.js +2 -2
  52. package/out/zero-client/src/client/bindings.d.ts +1 -1
  53. package/out/zero-client/src/client/bindings.d.ts.map +1 -1
  54. package/out/zero-client/src/client/bindings.js +4 -4
  55. package/out/zero-client/src/client/context.d.ts +7 -1
  56. package/out/zero-client/src/client/context.d.ts.map +1 -1
  57. package/out/zero-client/src/client/context.js +7 -0
  58. package/out/zero-client/src/client/context.js.map +1 -1
  59. package/out/zero-client/src/client/crud-impl.d.ts.map +1 -1
  60. package/out/zero-client/src/client/crud-impl.js +171 -2
  61. package/out/zero-client/src/client/crud-impl.js.map +1 -1
  62. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  63. package/out/zero-client/src/client/custom.js +42 -3
  64. package/out/zero-client/src/client/custom.js.map +1 -1
  65. package/out/zero-client/src/client/ivm-branch.d.ts +62 -2
  66. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
  67. package/out/zero-client/src/client/ivm-branch.js +152 -31
  68. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  69. package/out/zero-client/src/client/keys.d.ts +33 -0
  70. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  71. package/out/zero-client/src/client/keys.js +43 -4
  72. package/out/zero-client/src/client/keys.js.map +1 -1
  73. package/out/zero-client/src/client/version.js +1 -1
  74. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  75. package/out/zero-client/src/client/zero-poke-handler.js +29 -1
  76. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  77. package/out/zero-client/src/client/zero-rep.d.ts +1 -1
  78. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  79. package/out/zero-client/src/client/zero-rep.js +2 -15
  80. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  81. package/out/zero-client/src/client/zero.js +1 -1
  82. package/out/zero-client/src/mod.d.ts +3 -3
  83. package/out/zero-client/src/mod.d.ts.map +1 -1
  84. package/out/zero-protocol/src/aggregate-table.d.ts +15 -0
  85. package/out/zero-protocol/src/aggregate-table.d.ts.map +1 -0
  86. package/out/zero-protocol/src/aggregate-table.js +5 -0
  87. package/out/zero-protocol/src/aggregate-table.js.map +1 -0
  88. package/out/zero-protocol/src/ast.d.ts +141 -5
  89. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  90. package/out/zero-protocol/src/ast.js +244 -18
  91. package/out/zero-protocol/src/ast.js.map +1 -1
  92. package/out/zero-protocol/src/down.d.ts +29 -29
  93. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  94. package/out/zero-protocol/src/protocol-version.d.ts +1 -1
  95. package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
  96. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  97. package/out/zero-protocol/src/query-hash-visitor.d.ts.map +1 -1
  98. package/out/zero-protocol/src/query-hash-visitor.js +38 -0
  99. package/out/zero-protocol/src/query-hash-visitor.js.map +1 -1
  100. package/out/zero-react/src/bindings.js +4 -4
  101. package/out/zero-react/src/use-query.d.ts +3 -1
  102. package/out/zero-react/src/use-query.d.ts.map +1 -1
  103. package/out/zero-react/src/use-query.js +4 -2
  104. package/out/zero-react/src/use-query.js.map +1 -1
  105. package/out/zero-schema/src/builder/schema-builder.d.ts.map +1 -1
  106. package/out/zero-schema/src/builder/schema-builder.js +2 -0
  107. package/out/zero-schema/src/builder/schema-builder.js.map +1 -1
  108. package/out/zero-schema/src/name-mapper.d.ts.map +1 -1
  109. package/out/zero-schema/src/name-mapper.js +7 -4
  110. package/out/zero-schema/src/name-mapper.js.map +1 -1
  111. package/out/zero-server/src/custom.js +1 -1
  112. package/out/zero-server/src/schema.d.ts.map +1 -1
  113. package/out/zero-server/src/schema.js +13 -2
  114. package/out/zero-server/src/schema.js.map +1 -1
  115. package/out/zero-solid/src/bindings.js +4 -4
  116. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  117. package/out/zero-solid/src/solid-view.js +3 -3
  118. package/out/zero-solid/src/solid-view.js.map +1 -1
  119. package/out/zero-types/src/format.d.ts +13 -0
  120. package/out/zero-types/src/format.d.ts.map +1 -1
  121. package/out/zero-types/src/format.js.map +1 -1
  122. package/out/zero-types/src/name-mapper.d.ts +14 -0
  123. package/out/zero-types/src/name-mapper.d.ts.map +1 -1
  124. package/out/zero-types/src/name-mapper.js +12 -0
  125. package/out/zero-types/src/name-mapper.js.map +1 -1
  126. package/out/zero-types/src/schema-value.d.ts +2 -0
  127. package/out/zero-types/src/schema-value.d.ts.map +1 -1
  128. package/out/zero-types/src/schema-value.js +15 -0
  129. package/out/zero-types/src/schema-value.js.map +1 -0
  130. package/out/zero-types/src/server-schema.d.ts +18 -0
  131. package/out/zero-types/src/server-schema.d.ts.map +1 -1
  132. package/out/zql/src/builder/builder.d.ts +126 -1
  133. package/out/zql/src/builder/builder.d.ts.map +1 -1
  134. package/out/zql/src/builder/builder.js +281 -11
  135. package/out/zql/src/builder/builder.js.map +1 -1
  136. package/out/zql/src/builder/filter.d.ts +1 -1
  137. package/out/zql/src/builder/filter.d.ts.map +1 -1
  138. package/out/zql/src/builder/filter.js +41 -3
  139. package/out/zql/src/builder/filter.js.map +1 -1
  140. package/out/zql/src/ivm/aggregate.d.ts +188 -0
  141. package/out/zql/src/ivm/aggregate.d.ts.map +1 -0
  142. package/out/zql/src/ivm/aggregate.js +470 -0
  143. package/out/zql/src/ivm/aggregate.js.map +1 -0
  144. package/out/zql/src/ivm/array-view.d.ts +1 -1
  145. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  146. package/out/zql/src/ivm/array-view.js +3 -3
  147. package/out/zql/src/ivm/array-view.js.map +1 -1
  148. package/out/zql/src/ivm/cap.js +2 -2
  149. package/out/zql/src/ivm/constraint.d.ts.map +1 -1
  150. package/out/zql/src/ivm/constraint.js +2 -1
  151. package/out/zql/src/ivm/constraint.js.map +1 -1
  152. package/out/zql/src/ivm/deferred-input.js +1 -1
  153. package/out/zql/src/ivm/fan-in.js +1 -1
  154. package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
  155. package/out/zql/src/ivm/flipped-join.js +3 -2
  156. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  157. package/out/zql/src/ivm/group-by.d.ts +98 -0
  158. package/out/zql/src/ivm/group-by.d.ts.map +1 -0
  159. package/out/zql/src/ivm/group-by.js +384 -0
  160. package/out/zql/src/ivm/group-by.js.map +1 -0
  161. package/out/zql/src/ivm/join-index.d.ts.map +1 -1
  162. package/out/zql/src/ivm/join-index.js +2 -1
  163. package/out/zql/src/ivm/join-index.js.map +1 -1
  164. package/out/zql/src/ivm/join-utils.d.ts.map +1 -1
  165. package/out/zql/src/ivm/join-utils.js +2 -1
  166. package/out/zql/src/ivm/join-utils.js.map +1 -1
  167. package/out/zql/src/ivm/join.js +1 -1
  168. package/out/zql/src/ivm/lift-field.d.ts +54 -0
  169. package/out/zql/src/ivm/lift-field.d.ts.map +1 -0
  170. package/out/zql/src/ivm/lift-field.js +168 -0
  171. package/out/zql/src/ivm/lift-field.js.map +1 -0
  172. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +1 -1
  173. package/out/zql/src/ivm/memory-source.d.ts +2 -0
  174. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  175. package/out/zql/src/ivm/memory-source.js +9 -4
  176. package/out/zql/src/ivm/memory-source.js.map +1 -1
  177. package/out/zql/src/ivm/push-accumulated.js +1 -1
  178. package/out/zql/src/ivm/schema.d.ts +1 -0
  179. package/out/zql/src/ivm/schema.d.ts.map +1 -1
  180. package/out/zql/src/ivm/source.d.ts +21 -0
  181. package/out/zql/src/ivm/source.d.ts.map +1 -1
  182. package/out/zql/src/ivm/source.js.map +1 -1
  183. package/out/zql/src/ivm/take.js +1 -1
  184. package/out/zql/src/ivm/union-fan-in.js +1 -1
  185. package/out/zql/src/ivm/view-apply-change.d.ts +5 -0
  186. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  187. package/out/zql/src/ivm/view-apply-change.js +23 -3
  188. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  189. package/out/zql/src/planner/planner-debug.d.ts +1 -1
  190. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  191. package/out/zql/src/planner/planner-debug.js +4 -0
  192. package/out/zql/src/planner/planner-debug.js.map +1 -1
  193. package/out/zql/src/query/expression.d.ts +50 -3
  194. package/out/zql/src/query/expression.d.ts.map +1 -1
  195. package/out/zql/src/query/expression.js +62 -6
  196. package/out/zql/src/query/expression.js.map +1 -1
  197. package/out/zql/src/query/measure-push-operator.js +1 -1
  198. package/out/zql/src/query/query-delegate-base.d.ts +8 -0
  199. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  200. package/out/zql/src/query/query-delegate-base.js +13 -5
  201. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  202. package/out/zql/src/query/query-impl.d.ts +17 -3
  203. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  204. package/out/zql/src/query/query-impl.js +239 -7
  205. package/out/zql/src/query/query-impl.js.map +1 -1
  206. package/out/zql/src/query/query.d.ts +319 -7
  207. package/out/zql/src/query/query.d.ts.map +1 -1
  208. package/out/zqlite/src/pending-delta.d.ts.map +1 -1
  209. package/out/zqlite/src/pending-delta.js +4 -3
  210. package/out/zqlite/src/pending-delta.js.map +1 -1
  211. package/out/zqlite/src/query-builder.d.ts +12 -1
  212. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  213. package/out/zqlite/src/query-builder.js +114 -13
  214. package/out/zqlite/src/query-builder.js.map +1 -1
  215. package/out/zqlite/src/table-source.d.ts.map +1 -1
  216. package/out/zqlite/src/table-source.js +37 -3
  217. package/out/zqlite/src/table-source.js.map +1 -1
  218. package/package.json +1 -1
@@ -3,11 +3,11 @@ import { parse } from "../../../../../shared/src/valita.js";
3
3
  import { must } from "../../../../../shared/src/must.js";
4
4
  import { randomCharacters } from "../../../../../shared/src/random-values.js";
5
5
  import { equals } from "../../../../../shared/src/set-utils.js";
6
+ import { id } from "../../../types/sql.js";
6
7
  import { connectPgClient, pgClient } from "../../../types/pg.js";
7
8
  import { toStateVersionString } from "./lsn.js";
8
9
  import { READONLY } from "../../../db/mode-enum.js";
9
10
  import { getPublicationInfo } from "./schema/published.js";
10
- import { id } from "../../../types/sql.js";
11
11
  import { Queue } from "../../../../../shared/src/queue.js";
12
12
  import { resumePoint } from "../protocol/backfill-progress.js";
13
13
  import { SchemaIncompatibilityError } from "../common/backfill-manager.js";
@@ -2,6 +2,7 @@ import { assert } from "../../../../../shared/src/asserts.js";
2
2
  import { must } from "../../../../../shared/src/must.js";
3
3
  import { equals } from "../../../../../shared/src/set-utils.js";
4
4
  import { ALLOWED_APP_ID_CHARACTERS } from "../../../types/shards.js";
5
+ import { id } from "../../../types/sql.js";
5
6
  import { Database } from "../../../../../zqlite/src/db.js";
6
7
  import { connectPgClient } from "../../../types/pg.js";
7
8
  import { getOrCreateCounter, getOrCreateHistogram } from "../../../observability/metrics.js";
@@ -16,7 +17,6 @@ import { toStateVersionString } from "./lsn.js";
16
17
  import { READONLY } from "../../../db/mode-enum.js";
17
18
  import { runTx } from "../../../db/run-transaction.js";
18
19
  import { getPublicationInfo, warnForSkippedIndexes } from "./schema/published.js";
19
- import { id } from "../../../types/sql.js";
20
20
  import "./schema/replica-stage-enum.js";
21
21
  import { dropShard, getInternalShardConfig, getReplicaState, initInitialSyncReplica, validatePublications } from "./schema/shard.js";
22
22
  import { createLiteIndexStatement, createLiteTableStatement } from "../../../db/create.js";
@@ -1,10 +1,10 @@
1
1
  import { sleep } from "../../../../../../shared/src/sleep.js";
2
2
  import { childWorker } from "../../../../types/processes.js";
3
+ import { id, lit } from "../../../../types/sql.js";
3
4
  import "../../../../types/pg.js";
4
5
  import { getOrCreateHistogram } from "../../../../observability/metrics.js";
5
6
  import { SLOT_KEEPER_URL } from "../../../../server/worker-urls.js";
6
7
  import { fromBigInt, toBigInt } from "../lsn.js";
7
- import { id, lit } from "../../../../types/sql.js";
8
8
  import { Subscription } from "../../../../types/subscription.js";
9
9
  import { pipe } from "../../../../types/streams.js";
10
10
  import { AutoResetSignal } from "../../../change-streamer/schema/tables.js";
@@ -1,8 +1,8 @@
1
1
  import { assert } from "../../../../../../shared/src/asserts.js";
2
2
  import { literalUnion, valita_exports } from "../../../../../../shared/src/valita.js";
3
3
  import { upstreamSchema } from "../../../../types/shards.js";
4
- import { publishedSchema, publishedSchemaQuery } from "./published.js";
5
4
  import { id } from "../../../../types/sql.js";
5
+ import { publishedSchema, publishedSchemaQuery } from "./published.js";
6
6
  import { literal } from "pg-format";
7
7
  //#region ../zero-cache/src/services/change-source/pg/schema/ddl.ts
8
8
  var PROTOCOL_VERSION = 1;
@@ -1,8 +1,8 @@
1
1
  import { assert } from "../../../../../../shared/src/asserts.js";
2
2
  import { parse, valita_exports } from "../../../../../../shared/src/valita.js";
3
3
  import { upstreamSchema } from "../../../../types/shards.js";
4
- import { publishedSchema } from "./published.js";
5
4
  import { id } from "../../../../types/sql.js";
5
+ import { publishedSchema } from "./published.js";
6
6
  import "./replica-stage-enum.js";
7
7
  import { getMutationsTableDefinition, legacyReplicationSlot, metadataPublicationName, setupTablesAndReplication, setupTriggers } from "./shard.js";
8
8
  import { AutoResetSignal } from "../../../change-streamer/schema/tables.js";
@@ -2,8 +2,8 @@ import { assert } from "../../../../../../shared/src/asserts.js";
2
2
  import { parse, valita_exports } from "../../../../../../shared/src/valita.js";
3
3
  import { jsonObjectSchema, stringify } from "../../../../../../shared/src/bigint-json.js";
4
4
  import { appSchema, check, upstreamSchema } from "../../../../types/shards.js";
5
- import { getPublicationInfo, publishedSchema } from "./published.js";
6
5
  import { id } from "../../../../types/sql.js";
6
+ import { getPublicationInfo, publishedSchema } from "./published.js";
7
7
  import { createEventFunctionStatements, createEventTriggerStatements } from "./ddl.js";
8
8
  import { replicaStageSchema } from "./replica-stage-enum.js";
9
9
  import { validate } from "./validation.js";
@@ -28,6 +28,12 @@ export declare class HttpService implements Service {
28
28
  * is draining (see {@link SelfDrain}).
29
29
  */
30
30
  failHealthChecks(): void;
31
+ /**
32
+ * Responds to subsequent readiness checks ("/") with a 503, while liveness
33
+ * checks ("/keepalive") continue to succeed, e.g. to signal the load
34
+ * balancer to stop routing to a server that is about to drain.
35
+ */
36
+ failReadiness(): void;
31
37
  /**
32
38
  * Stops accepting new connections (e.g. when draining), while existing
33
39
  * connections, including websockets, continue to be served until
@@ -1 +1 @@
1
- {"version":3,"file":"http-service.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/services/http-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAgB,EAAC,KAAK,eAAe,EAAC,MAAM,SAAS,CAAC;AAGtD,OAAO,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;A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AM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IAGvC,aAAa,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC;CAC9C,CAAC;AAEF;;;;GAIG;AACH,qBAAa,WAAY,YAAW,OAAO;;IACzC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;IAGnC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAQxC,YACE,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAczD;IAGD,SAAS,CAAC,QAAQ,SAAK;IACvB,SAAS,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAEjC;IAGK,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAwB7B;IAEK,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAGzB;IAED;;;;OAIG;IACH,gBAAgB,SAKf;IAED;;;;OAIG;IACH,aAAa,SAKZ;IAED;;;;OAIG;IACH,aAAa,SAKZ;IAEK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAM1B;CACF"}
@@ -17,6 +17,7 @@ var HttpService = class {
17
17
  #heartbeatMonitor;
18
18
  #init;
19
19
  #ready = false;
20
+ #failReadiness = false;
20
21
  #failHealthChecks = false;
21
22
  constructor(id, lc, opts, init) {
22
23
  const { port, keepaliveTimeoutMs, readinessGate = promiseVoid } = opts;
@@ -35,7 +36,7 @@ var HttpService = class {
35
36
  }
36
37
  async start() {
37
38
  this.#fastify.get("/", (_req, res) => {
38
- if (this.#failHealthChecks) res.code(503).send("Draining");
39
+ if (this.#failReadiness || this.#failHealthChecks) res.code(503).send("Draining");
39
40
  else if (this.#ready) res.send("OK");
40
41
  });
41
42
  this.#fastify.get("/keepalive", ({ headers }, res) => {
@@ -68,6 +69,17 @@ var HttpService = class {
68
69
  }
69
70
  }
70
71
  /**
72
+ * Responds to subsequent readiness checks ("/") with a 503, while liveness
73
+ * checks ("/keepalive") continue to succeed, e.g. to signal the load
74
+ * balancer to stop routing to a server that is about to drain.
75
+ */
76
+ failReadiness() {
77
+ if (!this.#failReadiness) {
78
+ this._lc.info?.(`${this.id}: failing readiness checks before draining`);
79
+ this.#failReadiness = true;
80
+ }
81
+ }
82
+ /**
71
83
  * Stops accepting new connections (e.g. when draining), while existing
72
84
  * connections, including websockets, continue to be served until
73
85
  * {@link stop} is called.
@@ -1 +1 @@
1
- {"version":3,"file":"http-service.js","names":[],"sources":["../../../../../zero-cache/src/services/http-service.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport Fastify, {type FastifyInstance} from 'fastify';\nimport {promiseVoid} from '../../../shared/src/resolved-promises.ts';\nimport {HeartbeatMonitor} from './life-cycle.ts';\nimport {RunningState} from './running-state.ts';\nimport type {Service} from './service.ts';\n\nexport type Options = {\n port: number;\n keepaliveTimeoutMs: number | undefined;\n\n // Wait for the readinessGate to resolve before responding to health checks.\n readinessGate?: Promise<unknown> | undefined;\n};\n\n/**\n * Common functionality for all HttpServices. These include:\n * * Responding to health checks at \"/\"\n * * Tracking optional heartbeats at \"/keepalive\" and draining when they stop.\n */\nexport class HttpService implements Service {\n readonly id: string;\n protected readonly _lc: LogContext;\n readonly #fastify: FastifyInstance;\n readonly #port: number;\n protected readonly _state: RunningState;\n readonly #heartbeatMonitor: HeartbeatMonitor | undefined;\n readonly #init: (fastify: FastifyInstance) => void | Promise<void>;\n\n #ready = false;\n #failHealthChecks = false;\n\n constructor(\n id: string,\n lc: LogContext,\n opts: Options,\n init: (fastify: FastifyInstance) => void | Promise<void>,\n ) {\n const {port, keepaliveTimeoutMs, readinessGate = promiseVoid} = opts;\n this.id = id;\n this._lc = lc.withContext('component', this.id);\n this.#fastify = Fastify();\n this.#port = port;\n this.#init = init;\n this._state = new RunningState(id);\n this.#heartbeatMonitor = keepaliveTimeoutMs\n ? new HeartbeatMonitor(this._lc, keepaliveTimeoutMs)\n : undefined;\n\n void readinessGate.then(() => (this.#ready = true));\n }\n\n // Life-cycle hooks for subclass implementations\n protected _onStart() {}\n protected _onStop(): Promise<void> {\n return promiseVoid;\n }\n // start() is used in unit tests.\n // run() is the lifecycle method called by the ServiceRunner.\n async start(): Promise<string> {\n this.#fastify.get('/', (_req, res) => {\n if (this.#failHealthChecks) {\n res.code(503).send('Draining');\n } else if (this.#ready) {\n res.send('OK');\n }\n });\n this.#fastify.get('/keepalive', ({headers}, res) => {\n this.#heartbeatMonitor?.onHeartbeat(headers);\n if (this.#failHealthChecks) {\n res.code(503).send('Draining');\n } else if (this.#ready) {\n res.send('OK');\n }\n });\n await this.#init(this.#fastify);\n const address = await this.#fastify.listen({\n host: '::',\n port: this.#port,\n });\n this._lc.info?.(`${this.id} listening at ${address}`);\n this._onStart();\n return address;\n }\n\n async run(): Promise<void> {\n await this.start();\n await this._state.stopped();\n }\n\n /**\n * Responds to subsequent health checks (both \"/\" and \"/keepalive\") with a\n * 503, e.g. to signal the load balancer to stop routing to a server that\n * is draining (see {@link SelfDrain}).\n */\n failHealthChecks() {\n if (!this.#failHealthChecks) {\n this._lc.info?.(`${this.id}: failing health checks while draining`);\n this.#failHealthChecks = true;\n }\n }\n\n /**\n * Stops accepting new connections (e.g. when draining), while existing\n * connections, including websockets, continue to be served until\n * {@link stop} is called.\n */\n stopAccepting() {\n if (this.#fastify.server.listening) {\n this._lc.info?.(`${this.id}: no longer accepting new connections`);\n this.#fastify.server.close();\n }\n }\n\n async stop(): Promise<void> {\n this._lc.info?.(`${this.id}: no longer accepting connections`);\n this.#heartbeatMonitor?.stop();\n this._state.stop(this._lc);\n await this.#fastify.close();\n await this._onStop();\n }\n}\n"],"mappings":";;;;;;;;;;AAoBA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K,IAAI,OAAO,GAAG,KAAK,GAAG,uCAAuC;GAClE,KAAK,oBAAoB;EAC3B;CACF;;;;;;CAOA,gBAAgB;EACd,IAAI,KAAK,SAAS,OAAO,WAAW;GAClC,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,sCAAsC;GACjE,KAAK,SAAS,OAAO,MAAM;EAC7B;CACF;CAEA,MAAM,OAAsB;EAC1B,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,kCAAkC;EAC7D,KAAK,mBAAmB,KAAK;EAC7B,KAAK,OAAO,KAAK,KAAK,GAAG;EACzB,MAAM,KAAK,SAAS,MAAM;EAC1B,MAAM,KAAK,QAAQ;CACrB;AACF"}
1
+ {"version":3,"file":"http-service.js","names":[],"sources":["../../../../../zero-cache/src/services/http-service.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport Fastify, {type FastifyInstance} from 'fastify';\nimport {promiseVoid} from '../../../shared/src/resolved-promises.ts';\nimport {HeartbeatMonitor} from './life-cycle.ts';\nimport {RunningState} from './running-state.ts';\nimport type {Service} from './service.ts';\n\nexport type Options = {\n port: number;\n keepaliveTimeoutMs: number | undefined;\n\n // Wait for the readinessGate to resolve before responding to health checks.\n readinessGate?: Promise<unknown> | undefined;\n};\n\n/**\n * Common functionality for all HttpServices. These include:\n * * Responding to health checks at \"/\"\n * * Tracking optional heartbeats at \"/keepalive\" and draining when they stop.\n */\nexport class HttpService implements Service {\n readonly id: string;\n protected readonly _lc: LogContext;\n readonly #fastify: FastifyInstance;\n readonly #port: number;\n protected readonly _state: RunningState;\n readonly #heartbeatMonitor: HeartbeatMonitor | undefined;\n readonly #init: (fastify: FastifyInstance) => void | Promise<void>;\n\n #ready = false;\n #failReadiness = false;\n #failHealthChecks = false;\n\n constructor(\n id: string,\n lc: LogContext,\n opts: Options,\n init: (fastify: FastifyInstance) => void | Promise<void>,\n ) {\n const {port, keepaliveTimeoutMs, readinessGate = promiseVoid} = opts;\n this.id = id;\n this._lc = lc.withContext('component', this.id);\n this.#fastify = Fastify();\n this.#port = port;\n this.#init = init;\n this._state = new RunningState(id);\n this.#heartbeatMonitor = keepaliveTimeoutMs\n ? new HeartbeatMonitor(this._lc, keepaliveTimeoutMs)\n : undefined;\n\n void readinessGate.then(() => (this.#ready = true));\n }\n\n // Life-cycle hooks for subclass implementations\n protected _onStart() {}\n protected _onStop(): Promise<void> {\n return promiseVoid;\n }\n // start() is used in unit tests.\n // run() is the lifecycle method called by the ServiceRunner.\n async start(): Promise<string> {\n this.#fastify.get('/', (_req, res) => {\n if (this.#failReadiness || this.#failHealthChecks) {\n res.code(503).send('Draining');\n } else if (this.#ready) {\n res.send('OK');\n }\n });\n this.#fastify.get('/keepalive', ({headers}, res) => {\n this.#heartbeatMonitor?.onHeartbeat(headers);\n if (this.#failHealthChecks) {\n res.code(503).send('Draining');\n } else if (this.#ready) {\n res.send('OK');\n }\n });\n await this.#init(this.#fastify);\n const address = await this.#fastify.listen({\n host: '::',\n port: this.#port,\n });\n this._lc.info?.(`${this.id} listening at ${address}`);\n this._onStart();\n return address;\n }\n\n async run(): Promise<void> {\n await this.start();\n await this._state.stopped();\n }\n\n /**\n * Responds to subsequent health checks (both \"/\" and \"/keepalive\") with a\n * 503, e.g. to signal the load balancer to stop routing to a server that\n * is draining (see {@link SelfDrain}).\n */\n failHealthChecks() {\n if (!this.#failHealthChecks) {\n this._lc.info?.(`${this.id}: failing health checks while draining`);\n this.#failHealthChecks = true;\n }\n }\n\n /**\n * Responds to subsequent readiness checks (\"/\") with a 503, while liveness\n * checks (\"/keepalive\") continue to succeed, e.g. to signal the load\n * balancer to stop routing to a server that is about to drain.\n */\n failReadiness() {\n if (!this.#failReadiness) {\n this._lc.info?.(`${this.id}: failing readiness checks before draining`);\n this.#failReadiness = true;\n }\n }\n\n /**\n * Stops accepting new connections (e.g. when draining), while existing\n * connections, including websockets, continue to be served until\n * {@link stop} is called.\n */\n stopAccepting() {\n if (this.#fastify.server.listening) {\n this._lc.info?.(`${this.id}: no longer accepting new connections`);\n this.#fastify.server.close();\n }\n }\n\n async stop(): Promise<void> {\n this._lc.info?.(`${this.id}: no longer accepting connections`);\n this.#heartbeatMonitor?.stop();\n this._state.stop(this._lc);\n await this.#fastify.close();\n await this._onStop();\n }\n}\n"],"mappings":";;;;;;;;;;AAoBA,IAAa,cAAb,MAA4C;CAC1C;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,SAAS;CACT,iBAAiB;CACjB,oBAAoB;CAEpB,YACE,IACA,IACA,MACA,MACA;EACA,MAAM,EAAC,MAAM,oBAAoB,gBAAgB,gBAAe;EAChE,KAAK,KAAK;EACV,KAAK,MAAM,GAAG,YAAY,aAAa,KAAK,EAAE;EAC9C,KAAK,WAAW,QAAQ;EACxB,KAAK,QAAQ;EACb,KAAK,QAAQ;EACb,KAAK,SAAS,IAAI,aAAa,EAAE;EACjC,KAAK,oBAAoB,qBACrB,IAAI,iBAAiB,KAAK,KAAK,kBAAkB,IACjD,KAAA;EAEJ,cAAmB,WAAY,KAAK,SAAS,IAAK;CACpD;CAGA,WAAqB,CAAC;CACtB,UAAmC;EACjC,OAAO;CACT;CAGA,MAAM,QAAyB;EAC7B,KAAK,SAAS,IAAI,MAAM,MAAM,QAAQ;GACpC,IAAI,KAAK,kBAAkB,KAAK,mBAC9B,IAAI,KAAK,GAAG,CAAC,CAAC,KAAK,UAAU;QACxB,IAAI,KAAK,QACd,IAAI,KAAK,IAAI;EAEjB,CAAC;EACD,KAAK,SAAS,IAAI,eAAe,EAAC,WAAU,QAAQ;GAClD,KAAK,mBAAmB,YAAY,OAAO;GAC3C,IAAI,KAAK,mBACP,IAAI,KAAK,GAAG,CAAC,CAAC,KAAK,UAAU;QACxB,IAAI,KAAK,QACd,IAAI,KAAK,IAAI;EAEjB,CAAC;EACD,MAAM,KAAK,MAAM,KAAK,QAAQ;EAC9B,MAAM,UAAU,MAAM,KAAK,SAAS,OAAO;GACzC,MAAM;GACN,MAAM,KAAK;EACb,CAAC;EACD,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,gBAAgB,SAAS;EACpD,KAAK,SAAS;EACd,OAAO;CACT;CAEA,MAAM,MAAqB;EACzB,MAAM,KAAK,MAAM;EACjB,MAAM,KAAK,OAAO,QAAQ;CAC5B;;;;;;CAOA,mBAAmB;EACjB,IAAI,CAAC,KAAK,mBAAmB;GAC3B,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,uCAAuC;GAClE,KAAK,oBAAoB;EAC3B;CACF;;;;;;CAOA,gBAAgB;EACd,IAAI,CAAC,KAAK,gBAAgB;GACxB,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,2CAA2C;GACtE,KAAK,iBAAiB;EACxB;CACF;;;;;;CAOA,gBAAgB;EACd,IAAI,KAAK,SAAS,OAAO,WAAW;GAClC,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,sCAAsC;GACjE,KAAK,SAAS,OAAO,MAAM;EAC7B;CACF;CAEA,MAAM,OAAsB;EAC1B,KAAK,IAAI,OAAO,GAAG,KAAK,GAAG,kCAAkC;EAC7D,KAAK,mBAAmB,KAAK;EAC7B,KAAK,OAAO,KAAK,KAAK,GAAG;EACzB,MAAM,KAAK,SAAS,MAAM;EAC1B,MAAM,KAAK,QAAQ;CACrB;AACF"}
@@ -1,3 +1,4 @@
1
+ import { EventEmitter as NodeEventEmitter } from 'node:events';
1
2
  import type { IncomingHttpHeaders } from 'node:http';
2
3
  import type { EventEmitter } from 'stream';
3
4
  import type { LogContext } from '@rocicorp/logger';
@@ -94,6 +95,24 @@ export declare class SelfDrain {
94
95
  constructor(lc: LogContext, proc: EventEmitter, opts: SelfDrainOptions);
95
96
  request(): void;
96
97
  }
98
+ /**
99
+ * The event that a {@link PreDrain} emits immediately upon the first graceful
100
+ * shutdown signal, with that signal as its argument, before the delay.
101
+ */
102
+ export declare const PRE_DRAIN = "preDrain";
103
+ /**
104
+ * Delays graceful shutdown signals (`SIGTERM`, `SIGINT`), e.g. so that load
105
+ * balancers can observe a failed readiness check before the server begins
106
+ * to drain. The first graceful signal is announced immediately with a
107
+ * {@link PRE_DRAIN} event, and is re-emitted to listeners of the signal, such
108
+ * as those of the {@link ProcessManager} and {@link runUntilKilled}, once the
109
+ * delay has elapsed. Repeated graceful signals are dropped. All other events,
110
+ * including forceful signals, are forwarded immediately.
111
+ */
112
+ export declare class PreDrain extends NodeEventEmitter {
113
+ #private;
114
+ constructor(lc: LogContext, proc: EventEmitter, delayMs: number);
115
+ }
97
116
  /**
98
117
  * The maximum time that {@link runUntilKilled} waits for its services to stop
99
118
  * (i.e. for their `run()` to settle) after stopping them.
@@ -1 +1 @@
1
- {"version":3,"file":"life-cycle.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/services/life-cycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAEnD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AACzC,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AASjD,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,MAAM,EACZ,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAEnD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,UAAU,GAAG,aAAa,GAAG,YAAY,CAAC;AAEtD,eAAO,MAAM,iBAAiB,YAAI,SAAS,EAAE,QAAQ,CAAU,CAAC;AAChE,eAAO,MAAM,iBAAiB,YAAI,SAAS,EAAE,SAAS,CAAU,CAAC;AA8CjE,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,MAAM,QAEzD;AAaD,eAAO,MAAM,8BAA8B,KAAK,CAAC;AAKjD,eAAO,MAAM,+BAA+B,KAAK,CAAC;AAElD;;;GAGG;AACH,qBAAa,cAAc;;IAWzB,YAAY,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAyC7C;IAEK,QAAQ,kBAK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;;GAUG;AACH,wBAAsB,cAAc,CAClC,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,YAAY,EACpB,GAAG,QAAQ,EAAE,gBAAgB,EAAE,GAC9B,OAAO,CAAC,IAAI,CAAC,CA2Ef;AA8BD,wBAAsB,SAAS,CAC7B,UAAU,EAAE,UAAU,GAAG,CAAC,MAAM,UAAU,CAAC,EAC3C,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,iBAoCzB;AAED;;;;;;;;;;GAUG;AACH,qBAAa,gBAAgB;;IAQ3B,YAAY,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAG/C;IAED,WAAW,CAAC,UAAU,EAAE,mBAAmB,QAgB1C;IAsCD,IAAI,SAKH;CACF"}
@@ -7,6 +7,7 @@ import { LONG_DURATION_HISTOGRAM_BOUNDARIES_S, getOrCreateHistogram } from "../o
7
7
  import { orTimeout } from "../types/timeout.js";
8
8
  import { RunningState } from "./running-state.js";
9
9
  import { pid } from "node:process";
10
+ import { EventEmitter } from "node:events";
10
11
  //#region ../zero-cache/src/services/life-cycle.ts
11
12
  var GRACEFUL_SHUTDOWN = ["SIGTERM", "SIGINT"];
12
13
  var FORCEFUL_SHUTDOWN = ["SIGQUIT", "SIGABRT"];
@@ -261,6 +262,57 @@ var SelfDrain = class {
261
262
  }
262
263
  };
263
264
  /**
265
+ * The event that a {@link PreDrain} emits immediately upon the first graceful
266
+ * shutdown signal, with that signal as its argument, before the delay.
267
+ */
268
+ var PRE_DRAIN = "preDrain";
269
+ /**
270
+ * Delays graceful shutdown signals (`SIGTERM`, `SIGINT`), e.g. so that load
271
+ * balancers can observe a failed readiness check before the server begins
272
+ * to drain. The first graceful signal is announced immediately with a
273
+ * {@link PRE_DRAIN} event, and is re-emitted to listeners of the signal, such
274
+ * as those of the {@link ProcessManager} and {@link runUntilKilled}, once the
275
+ * delay has elapsed. Repeated graceful signals are dropped. All other events,
276
+ * including forceful signals, are forwarded immediately.
277
+ */
278
+ var PreDrain = class extends EventEmitter {
279
+ #lc;
280
+ #proc;
281
+ #delayMs;
282
+ #forwarded = /* @__PURE__ */ new Set(["newListener", PRE_DRAIN]);
283
+ #signaled = false;
284
+ #timer;
285
+ constructor(lc, proc, delayMs) {
286
+ super();
287
+ this.#lc = lc.withContext("component", "pre-drain");
288
+ this.#proc = proc;
289
+ this.#delayMs = delayMs;
290
+ for (const signal of GRACEFUL_SHUTDOWN) {
291
+ this.#forwarded.add(signal);
292
+ proc.on(signal, () => this.#onGracefulSignal(signal));
293
+ }
294
+ this.on("newListener", (event) => this.#forward(event));
295
+ }
296
+ #forward(event) {
297
+ if (this.#forwarded.has(event)) return;
298
+ this.#forwarded.add(event);
299
+ this.#proc.on(event, (...args) => {
300
+ if (event === "disconnect" || FORCEFUL_SHUTDOWN.includes(event)) clearTimeout(this.#timer);
301
+ this.emit(event, ...args);
302
+ });
303
+ }
304
+ #onGracefulSignal(signal) {
305
+ if (this.#signaled) {
306
+ this.#lc.info?.(`already shutting down. ignoring ${signal}`);
307
+ return;
308
+ }
309
+ this.#signaled = true;
310
+ this.#lc.info?.(`received ${signal}. draining in ${this.#delayMs} ms`);
311
+ this.emit(PRE_DRAIN, signal);
312
+ this.#timer = setTimeout(() => this.emit(signal, signal), this.#delayMs);
313
+ }
314
+ };
315
+ /**
264
316
  * The maximum time that {@link runUntilKilled} waits for its services to stop
265
317
  * (i.e. for their `run()` to settle) after stopping them.
266
318
  */
@@ -397,6 +449,6 @@ var HeartbeatMonitor = class {
397
449
  }
398
450
  };
399
451
  //#endregion
400
- export { FORCEFUL_SHUTDOWN, GRACEFUL_SHUTDOWN, HeartbeatMonitor, ProcessManager, STOP_TIMEOUT_MS, SelfDrain, SupportingProcesses, exitAfter, recordStartupDurationMs, runUntilKilled };
452
+ export { FORCEFUL_SHUTDOWN, GRACEFUL_SHUTDOWN, HeartbeatMonitor, PRE_DRAIN, PreDrain, ProcessManager, STOP_TIMEOUT_MS, SelfDrain, SupportingProcesses, exitAfter, recordStartupDurationMs, runUntilKilled };
401
453
 
402
454
  //# sourceMappingURL=life-cycle.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"life-cycle.js","names":[],"sources":["../../../../../zero-cache/src/services/life-cycle.ts"],"sourcesContent":["import type {IncomingHttpHeaders} from 'node:http';\nimport {pid} from 'node:process';\nimport type {EventEmitter} from 'stream';\nimport type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../shared/src/abort-error.ts';\nimport {resolver} from '../../../shared/src/resolver.ts';\nimport {logLastChanceSQLiteCorruptionDiagnostics} from '../db/sqlite-corruption.ts';\nimport {\n getOrCreateHistogram,\n LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n} from '../observability/metrics.ts';\nimport {ConfigurationError} from '../types/configuration-error.ts';\nimport {\n singleProcessMode,\n type Subprocess,\n type Worker,\n} from '../types/processes.ts';\nimport {orTimeout} from '../types/timeout.ts';\nimport {RunningState} from './running-state.ts';\nimport type {SingletonService} from './service.ts';\n\n/**\n * * `user-facing` workers serve external requests and are the first to\n * receive a `SIGTERM` or `SIGINT` signal for graceful shutdown.\n *\n * * `supporting` workers support `user-facing` workers and are sent\n * the `SIGTERM` signal only after all `user-facing` workers have\n * exited.\n *\n * For other kill signals, such as `SIGQUIT` and `SIGABRT`, all workers\n * are stopped without draining. `SIGQUIT` is used to represent an\n * intentional shutdown (for which draining is not beneficial), whereas\n * `SIGABRT` is used for unexpected process exits.\n *\n * A clean exit (i.e. code 0) of a worker of either type that is not the\n * result of a drain is treated as an intentional shutdown of the server.\n */\nexport type WorkerType = 'user-facing' | 'supporting';\n\nexport const GRACEFUL_SHUTDOWN = ['SIGTERM', 'SIGINT'] as const;\nexport const FORCEFUL_SHUTDOWN = ['SIGQUIT', 'SIGABRT'] as const;\n\ntype GracefulShutdownSignal = (typeof GRACEFUL_SHUTDOWN)[number];\n\nfunction workerStartupMetricName(name: string) {\n if (name === 'zero-cache') {\n return undefined;\n }\n if (name.startsWith('change-streamer')) {\n return 'change_streamer';\n }\n if (name.startsWith('replicator')) {\n if (name.includes('(backup)')) {\n return 'backup_replicator';\n }\n if (name.includes('(serving-copy)')) {\n return 'serving_copy_replicator';\n }\n if (name.includes('(serving)')) {\n return 'serving_replicator';\n }\n return 'other';\n }\n if (name.startsWith('syncer')) {\n return 'syncer';\n }\n if (name.startsWith('reaper')) {\n return 'reaper';\n }\n if (name.startsWith('shadow-syncer')) {\n return 'shadow_syncer';\n }\n if (name.startsWith('mutator')) {\n return 'mutator';\n }\n return 'other';\n}\n\nfunction startupDuration() {\n return getOrCreateHistogram('server', 'startup_duration', {\n description: 'Duration from starting zero-cache to its ready signal.',\n unit: 's',\n bucketBoundaries: LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n });\n}\n\nexport function recordStartupDurationMs(durationMs: number) {\n startupDuration().recordMs(durationMs, {component: 'dispatcher'});\n}\n\nfunction workerStartupDuration() {\n return getOrCreateHistogram('server', 'worker_startup_duration', {\n description: 'Duration from starting a worker to its ready signal.',\n unit: 's',\n bucketBoundaries: LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n });\n}\n\n// An internal error code used to indicate that a message has already been\n// logged at level ERRROR. When a process exits with this error code, the\n// parent process logs the exit at level WARN instead of ERROR.\nexport const UNHANDLED_EXCEPTION_ERROR_CODE = 13;\n\n// An internal error code used to indicate that the server should exit\n// without draining (e.g. due to a worker exiting cleanly outside of a drain,\n// such as for an auto reset), but the exit is otherwise intentional.\nexport const INTENTIONAL_SHUTDOWN_ERROR_CODE = 14;\n\n/**\n * Handles readiness, termination signals, and coordination of graceful\n * shutdown.\n */\nexport class ProcessManager {\n readonly #lc: LogContext;\n readonly #userFacing = new Set<Subprocess>();\n readonly #all = new Set<Subprocess>();\n readonly #exitImpl: (code: number) => never;\n readonly #start = Date.now();\n readonly #ready: Promise<void>[] = [];\n\n #runningState = new RunningState('process-manager');\n #drainStart = 0;\n\n constructor(lc: LogContext, proc: EventEmitter) {\n this.#lc = lc.withContext('component', 'process-manager');\n\n // Propagate `SIGTERM` and `SIGINT` to all user-facing workers,\n // initiating a graceful shutdown. The parent process will\n // exit once all user-facing workers have exited ...\n for (const signal of GRACEFUL_SHUTDOWN) {\n proc.on(signal, () => this.#startDrain(signal));\n }\n\n // ... which will result in sending `SIGTERM` to the remaining workers.\n proc.on('exit', code =>\n this.#kill(\n this.#all,\n code === 0\n ? 'SIGTERM' // graceful, drained shutdown\n : code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'SIGQUIT' // intentional abort without drain\n : 'SIGABRT', // unintentional shutdown, alertable error\n ),\n );\n\n // For other (catchable) kill signals, exit with a non-zero error code\n // to send a `SIGQUIT` (intentional shutdown) or `SIGABRT` (unexpected\n // shutdown) to all workers. For these signals, workers are stopped\n // immediately without draining, since there is no merit to slowly draining\n // when supporting workers have stopped.\n //\n // The logic for handling these signals is in `runUntilKilled()`.\n for (const signal of FORCEFUL_SHUTDOWN) {\n proc.on(signal, () =>\n this.#exit(signal === 'SIGQUIT' ? INTENTIONAL_SHUTDOWN_ERROR_CODE : -1),\n );\n }\n\n this.#exitImpl = (code: number) => {\n if (singleProcessMode()) {\n return proc.emit('exit', code) as never; // For unit / integration tests.\n }\n process.exit(code);\n };\n }\n\n async shutdown() {\n if (this.#drainStart === 0) {\n this.#startDrain(GRACEFUL_SHUTDOWN[0]);\n }\n await this.#runningState.stopped();\n }\n\n #exit(code: number) {\n this.#lc.info?.('exiting with code', code);\n this.#runningState.stop(this.#lc);\n void this.#lc.flush().finally(() => this.#exitImpl(code));\n }\n\n #startDrain(signal: GracefulShutdownSignal) {\n if (this.#all.size === 0) {\n // Shutdown if a signal is received before any subprocesses are added.\n this.#lc.info?.(`exiting on ${signal}`);\n this.#exit(0);\n }\n this.#lc.info?.(`initiating drain (${signal})`);\n this.#drainStart = Date.now();\n if (this.#userFacing.size) {\n this.#kill(this.#userFacing, signal);\n } else {\n this.#kill(this.#all, signal);\n }\n }\n\n addSubprocess(proc: Subprocess, type: WorkerType, name: string) {\n if (type === 'user-facing') {\n this.#userFacing.add(proc);\n }\n this.#all.add(proc);\n\n let isOpen = true;\n proc.on('close', (code, signal) => {\n isOpen = false;\n this.#onExit(code, signal, null, type, name, proc);\n });\n\n // As per https://nodejs.org/api/child_process.html#event-error\n // 'error' events can happen when sending a message to a child process\n // fails. This is not really an error when the server is shutting down,\n // so log any post-close errors at 'warn'.\n proc.on('error', err =>\n this.#lc[!isOpen || this.#drainStart > 0 ? 'warn' : 'error']?.(\n `error from ${name} ${proc.pid}`,\n err,\n ),\n );\n }\n\n readonly #initializing = new Map<number, string>();\n #nextID = 0;\n\n addWorker(worker: Worker, type: WorkerType, name: string): Worker {\n this.addSubprocess(worker, type, name);\n\n const start = performance.now();\n const id = ++this.#nextID;\n this.#initializing.set(id, name);\n const {promise, resolve} = resolver();\n this.#ready.push(promise);\n\n worker.onceMessageType('ready', () => {\n const elapsed = performance.now() - start;\n const workerMetricName = workerStartupMetricName(name);\n if (workerMetricName) {\n workerStartupDuration().recordMs(elapsed, {\n worker: workerMetricName,\n type,\n });\n }\n this.#lc.debug?.(`${name} ready (${Date.now() - this.#start} ms)`);\n this.#initializing.delete(id);\n resolve();\n });\n\n return worker;\n }\n\n initializing(): string[] {\n return [...this.#initializing.values()];\n }\n\n async allWorkersReady() {\n await Promise.all(this.#ready);\n }\n\n logErrorAndExit(err: unknown, name: string) {\n // only accessible by the main (i.e. user-facing) process.\n this.#onExit(-1, null, err, 'user-facing', name, undefined);\n }\n\n #onExit(\n code: number,\n sig: NodeJS.Signals | null,\n err: unknown,\n type: WorkerType,\n name: string,\n worker: Subprocess | undefined,\n ) {\n // Remove the worker from maps to avoid attempting to send more signals to it.\n if (worker) {\n this.#userFacing.delete(worker);\n this.#all.delete(worker);\n }\n\n const pid = worker?.pid ?? process.pid;\n\n // A clean exit outside of a drain is an intentional shutdown, e.g. the\n // replication-manager shutting down for protocol-specific instructions\n // (like auto reset), or a configuration error. In this case, a special\n // error code is used to signal that the server should be shut down\n // without draining, but it is otherwise not considered an\n // unexpected/alertable error. The same applies to a supporting worker\n // that exits while user-facing workers are still draining.\n if (\n code === 0 &&\n (this.#drainStart === 0 ||\n (type === 'supporting' && this.#userFacing.size > 0))\n ) {\n code = INTENTIONAL_SHUTDOWN_ERROR_CODE;\n }\n\n if (type === 'supporting') {\n const log =\n code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'info'\n : 'warn';\n this.#lc[log]?.(`${name} (${pid}) exited with code (${code})`, err ?? '');\n return this.#exit(code);\n }\n\n const log =\n code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'info'\n : this.#drainStart > 0 || code === UNHANDLED_EXCEPTION_ERROR_CODE\n ? 'warn'\n : 'error';\n this.#lc[log]?.(\n sig\n ? `${name} (${pid}) killed with (${sig})`\n : `${name} (${pid}) exited with code (${code})`,\n err ?? '',\n );\n\n // user-facing workers exited or finished draining.\n if (this.#userFacing.size === 0) {\n this.#lc.info?.(\n this.#drainStart\n ? `all user-facing workers drained (${\n Date.now() - this.#drainStart\n } ms)`\n : `all user-facing workers exited`,\n );\n return this.#exit(this.#drainStart ? 0 : code);\n }\n\n if (this.#drainStart === 0) {\n // If a user-facing worker exits without receiving a drain signal,\n // shutdown the server.\n return this.#exit(code);\n }\n\n return undefined;\n }\n\n #kill(workers: Iterable<Subprocess>, signal: NodeJS.Signals) {\n for (const worker of workers) {\n try {\n worker.kill(signal);\n } catch (e) {\n this.#lc.error?.(e);\n }\n }\n }\n}\n\n/**\n * Runs the supporting subprocesses of a worker (e.g. the litestream process\n * of the backup-replicator) as a {@link SingletonService}.\n *\n * Unlike a {@link ProcessManager}, it does not forward `SIGTERM` or `SIGINT`\n * to its subprocesses. Instead, {@link runUntilKilled} stops it along with the\n * worker's other services, i.e. after they have drained, so that the\n * subprocesses keep supporting them until then. Forceful signals (`SIGQUIT`\n * and `SIGABRT`), on the other hand, are forwarded immediately once the\n * service is running, and the subprocesses are killed if the worker exits\n * before they are stopped.\n *\n * `run()` settles when a subprocess exits before {@link stop()} is called,\n * which stops the worker: a clean exit resolves (and the parent in turn treats\n * the worker's clean exit outside of a drain as an intentional shutdown),\n * while any other exit rejects.\n */\nexport class SupportingProcesses implements SingletonService {\n readonly id = 'supporting-processes';\n readonly #lc: LogContext;\n readonly #parent: EventEmitter;\n readonly #procs = new Map<Subprocess, string>();\n readonly #done = resolver<void>();\n #signaled: NodeJS.Signals | undefined;\n\n constructor(lc: LogContext, parent: EventEmitter) {\n this.#lc = lc.withContext('component', 'supporting-processes');\n this.#parent = parent;\n // run() may never be called if the worker fails before running services.\n this.#done.promise.catch(() => {});\n\n parent.on('exit', code =>\n this.#kill(\n code === 0\n ? 'SIGTERM'\n : code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'SIGQUIT'\n : 'SIGABRT',\n ),\n );\n }\n\n add<P extends Subprocess>(proc: P, name: string): P {\n this.#procs.set(proc, name);\n proc.on('close', (code: number | null, sig: NodeJS.Signals | null) =>\n this.#onClose(proc, name, code, sig),\n );\n // As with the ProcessManager, 'error' events after a signal (e.g. failing\n // to send a message to an exiting process) are logged at 'warn'.\n proc.on('error', err =>\n this.#lc[this.#signaled ? 'warn' : 'error']?.(\n `error from ${name} ${proc.pid}`,\n err,\n ),\n );\n return proc;\n }\n\n run(): Promise<void> {\n // Signal listeners are only installed when running, i.e. alongside those\n // of runUntilKilled(). Installing them any earlier would consume signals\n // received during worker initialization, which would otherwise terminate\n // the worker (or be handled by the initialization logic).\n for (const signal of FORCEFUL_SHUTDOWN) {\n this.#parent.on(signal, () => this.#kill(signal));\n }\n return this.#done.promise;\n }\n\n async stop(): Promise<void> {\n if (!this.#signaled) {\n this.#kill('SIGTERM');\n }\n if (this.#procs.size === 0) {\n this.#done.resolve();\n }\n await this.#done.promise.catch(() => {});\n }\n\n #onClose(\n proc: Subprocess,\n name: string,\n code: number | null,\n sig: NodeJS.Signals | null,\n ) {\n this.#procs.delete(proc);\n const exit = sig\n ? `${name} (${proc.pid}) killed with (${sig})`\n : `${name} (${proc.pid}) exited with code (${code})`;\n\n if (this.#signaled) {\n this.#lc.info?.(exit);\n if (this.#procs.size === 0) {\n this.#done.resolve();\n }\n } else if (code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE) {\n this.#lc.info?.(exit);\n this.#done.resolve();\n } else {\n this.#done.reject(new Error(exit));\n }\n }\n\n #kill(signal: NodeJS.Signals) {\n this.#signaled = signal;\n for (const proc of this.#procs.keys()) {\n try {\n proc.kill(signal);\n } catch (e) {\n this.#lc.error?.(e);\n }\n }\n }\n}\n\nexport type SelfDrainOptions = {\n /**\n * The time after a self drain is requested at which the server sends itself\n * a `SIGTERM`, unless one was received sooner (e.g. from the container\n * runner).\n */\n delayMs: number;\n\n /**\n * Called when a self drain is requested. This should fail health checks\n * (both readiness and liveness) so that the container runner stops routing\n * to the server and initiates its normal shutdown process.\n */\n onRequested: () => void;\n};\n\n/**\n * Coordinates a drain that is initiated by the server itself (e.g. by a\n * view-syncer that has been granted permission to restore a new replica),\n * as opposed to one initiated by the container runner with a `SIGTERM`.\n *\n * Health checks start failing immediately, upon which an orchestrator such\n * as Kubernetes stops routing to the server and shuts it down as usual\n * (i.e. running any pre-stop hooks before sending `SIGTERM`). As a backstop,\n * and for environments without liveness checks, the server sends itself a\n * `SIGTERM` after the configured delay.\n */\nexport class SelfDrain {\n readonly #lc: LogContext;\n readonly #proc: EventEmitter;\n readonly #delayMs: number;\n readonly #onRequested: () => void;\n #requested = false;\n #signaled = false;\n #timer: ReturnType<typeof setTimeout> | undefined;\n\n constructor(lc: LogContext, proc: EventEmitter, opts: SelfDrainOptions) {\n this.#lc = lc.withContext('component', 'self-drain');\n this.#proc = proc;\n this.#delayMs = opts.delayMs;\n this.#onRequested = opts.onRequested;\n\n for (const signal of GRACEFUL_SHUTDOWN) {\n proc.once(signal, () => {\n this.#signaled = true;\n clearTimeout(this.#timer);\n });\n }\n }\n\n request() {\n if (this.#requested || this.#signaled) {\n return; // Already draining.\n }\n this.#requested = true;\n this.#lc.info?.(\n `self drain requested. sending SIGTERM in ${this.#delayMs} ms`,\n );\n this.#onRequested();\n this.#timer = setTimeout(() => {\n if (!this.#signaled) {\n this.#lc.info?.(`initiating self drain`);\n this.#proc.emit('SIGTERM', 'SIGTERM');\n }\n }, this.#delayMs);\n }\n}\n\n/**\n * The maximum time that {@link runUntilKilled} waits for its services to stop\n * (i.e. for their `run()` to settle) after stopping them.\n */\nexport const STOP_TIMEOUT_MS = 60_000;\n\n/**\n * Runs the specified services, stopping them on `SIGTERM` or `SIGINT` with\n * an optional {@link SingletonService.drain drain()}, or stopping them\n * without draining for `SIGQUIT`, or when the parent process disconnects.\n * Repeated graceful signals are ignored. If a service stops (or throws) on its\n * own, the remaining services are stopped without draining.\n *\n * @returns a Promise that settles once all services have stopped (or\n * {@link STOP_TIMEOUT_MS} has elapsed), resolving or rejecting\n * with the outcome of the first service to stop.\n */\nexport async function runUntilKilled(\n lc: LogContext,\n parent: EventEmitter,\n ...services: SingletonService[]\n): Promise<void> {\n if (services.length === 0) {\n return;\n }\n const stopped = new Set<SingletonService>();\n const runs = services.map(svc => svc.run().finally(() => stopped.add(svc)));\n\n const allStopped = resolver<void>();\n let stopping: Promise<void> | undefined;\n const stopAll = (reason: string) =>\n (stopping ??= stopServices(lc, services, runs, stopped, reason).then(\n allStopped.resolve,\n ));\n\n const GRACEFUL_SIGNALS = GRACEFUL_SHUTDOWN as readonly NodeJS.Signals[];\n let draining = false;\n const onSignal = async (signal: NodeJS.Signals) => {\n if (GRACEFUL_SIGNALS.includes(signal)) {\n if (draining || stopping) {\n // e.g. A SIGINT from both the terminal and a wrapping process.\n lc.info?.(`already shutting down. ignoring ${signal}`);\n return;\n }\n draining = true;\n // Drain all services before stopping any of them. Stopping a service\n // resolves its run() and thus ends runUntilKilled(), so a service\n // without a drain() must not be stopped while others are draining.\n const results = await Promise.allSettled(\n services.map(async svc => {\n if (svc.drain) {\n lc.info?.(`draining ${svc.constructor.name} ${svc.id} (${signal})`);\n await svc.drain();\n }\n }),\n );\n for (const result of results) {\n if (result.status === 'rejected') {\n lc.warn?.(`error while draining (${signal})`, result.reason);\n }\n }\n }\n // A forceful signal stops the services without waiting for (or after)\n // a drain.\n await stopAll(signal);\n };\n // The listeners remain installed for the lifetime of the process, as a\n // signal without a listener would terminate the process (e.g. a repeated\n // SIGINT during a drain).\n for (const signal of [...GRACEFUL_SHUTDOWN, ...FORCEFUL_SHUTDOWN]) {\n parent.on(signal, () => void onSignal(signal));\n }\n // A forked worker whose parent has exited (e.g. was killed by a signal,\n // in which case it cannot signal its workers) stops without draining, so\n // that it (and in turn its own subprocesses) is not orphaned.\n parent.once('disconnect', () => {\n lc.warn?.(`parent process disconnected. stopping without draining`);\n void stopAll('disconnect');\n });\n\n try {\n // Run all services and resolve when any of them stops, or when stopping\n // them (after a signal) has timed out.\n const svc = await Promise.race([\n ...runs.map((run, i) => run.then(() => services[i])),\n allStopped.promise,\n ]);\n if (svc) {\n lc.info?.(`${svc.constructor.name} (${svc.id}) stopped`);\n }\n } catch (e) {\n lc.error?.(`exiting on error`, e);\n throw e;\n } finally {\n await stopAll('stopped');\n }\n}\n\nasync function stopServices(\n lc: LogContext,\n services: SingletonService[],\n runs: Promise<void>[],\n stopped: Set<SingletonService>,\n reason: string,\n) {\n for (const svc of services) {\n if (!stopped.has(svc)) {\n lc.info?.(`stopping ${svc.constructor.name} ${svc.id} (${reason})`);\n void svc\n .stop()\n .catch(e =>\n lc.warn?.(`error stopping ${svc.constructor.name} ${svc.id}`, e),\n );\n }\n }\n if (\n (await orTimeout(Promise.allSettled(runs), STOP_TIMEOUT_MS)) === 'timed-out'\n ) {\n const running = services.filter(svc => !stopped.has(svc));\n lc.warn?.(\n `services did not stop within ${STOP_TIMEOUT_MS} ms: ` +\n running.map(svc => `${svc.constructor.name} ${svc.id}`).join(', '),\n );\n }\n}\n\nexport async function exitAfter(\n logContext: LogContext | (() => LogContext),\n run: () => Promise<void>,\n) {\n const getLogContext =\n typeof logContext === 'function' ? logContext : () => logContext;\n try {\n await run();\n const lc = getLogContext();\n lc.info?.(`pid ${pid} exiting normally`);\n process.exit(0);\n } catch (e) {\n const lc = getLogContext();\n if (e instanceof ConfigurationError) {\n lc.error?.(`exiting with configuration error: ${String(e)}`, e);\n process.exit(0);\n }\n if (e instanceof AbortError) {\n // AbortErrors signal an intentional shutdown or restart (e.g. SIGTERM\n // during a rollout, AutoResetSignal) and should not raise alerts. The\n // non-zero exit code is kept so that restart semantics are unchanged.\n lc.warn?.(`exiting after abort: ${String(e)}`, e);\n } else {\n lc.error?.(`exiting with error: ${String(e)}`, e);\n }\n try {\n logLastChanceSQLiteCorruptionDiagnostics(lc, e);\n } catch (diagnosticError) {\n lc.error?.('SQLite corruption last-chance diagnostic failed', {\n error: diagnosticError,\n });\n }\n try {\n await lc.flush();\n } finally {\n process.exit(-1);\n }\n }\n}\n\n/**\n * The HeartbeatMonitor monitors the cadence heartbeats (e.g. \"/keepalive\"\n * health checks made to HttpServices) that signal that the server\n * should continue processing requests. When a configurable `stopInterval`\n * elapses without receiving these heartbeats, the monitor initiates a\n * graceful shutdown of the server. This works with common load balancing\n * frameworks such as AWS Elastic Load Balancing.\n *\n * The HeartbeatMonitor is **opt-in** in that it only kicks in after it\n * starts receiving keepalives.\n */\nexport class HeartbeatMonitor {\n readonly #stopInterval: number;\n\n #lc: LogContext;\n #checkIntervalTimer: NodeJS.Timeout | undefined;\n #checkImmediateTimer: NodeJS.Immediate | undefined;\n #lastHeartbeat = 0;\n\n constructor(lc: LogContext, stopInterval: number) {\n this.#lc = lc;\n this.#stopInterval = stopInterval;\n }\n\n onHeartbeat(reqHeaders: IncomingHttpHeaders) {\n this.#lastHeartbeat = Date.now();\n if (this.#checkIntervalTimer === undefined) {\n this.#lc.info?.(\n `starting heartbeat monitor at ${\n this.#stopInterval / 1000\n } second interval`,\n reqHeaders,\n );\n // e.g. check every 5 seconds to see if it's been over 20 seconds\n // since the last heartbeat.\n this.#checkIntervalTimer = setInterval(\n this.#checkStopInterval,\n this.#stopInterval / 4,\n );\n }\n }\n\n #checkStopInterval = () => {\n // In the Node.js event loop, timers like setInterval and setTimeout\n // run *before* I/O events coming from network sockets or file reads/writes.\n // When this process gets starved of CPU resources for long periods of time,\n // for example when other processes are monopolizing all available cores,\n // pathological behavior can emerge:\n // - keepalive network request comes in, but is queued in Node internals waiting\n // for time on the event loop\n // - CPU is starved/monopolized by other processes for longer than the time\n // configured via this.#stopInterval\n // - When CPU becomes available and the event loop wakes up, this stop interval\n // check is run *before* the keepalive request is processed. The value of\n // this.#lastHeartbeat is now very stale, and erroneously triggers a shutdown\n // even though keepalive requests were about to be processed and update\n // this.#lastHeartbeat. Downtime ensues.\n //\n // To avoid this, we push the check out to a phase of the event loop *after*\n // I/O events are processed, using setImmediate():\n // https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#setimmediate-vs-settimeout\n //\n // This ensures we see a value for this.#lastHeartbeat that reflects\n // any keepalive requests that came in during the current event loop turn.\n this.#checkImmediateTimer = setImmediate(() => {\n this.#checkImmediateTimer = undefined;\n const timeSinceLastHeartbeat = Date.now() - this.#lastHeartbeat;\n if (timeSinceLastHeartbeat >= this.#stopInterval) {\n this.#lc.info?.(\n `last heartbeat received ${\n timeSinceLastHeartbeat / 1000\n } seconds ago. draining.`,\n );\n process.kill(process.pid, GRACEFUL_SHUTDOWN[0]);\n }\n });\n };\n\n stop() {\n clearTimeout(this.#checkIntervalTimer);\n if (this.#checkImmediateTimer) {\n clearImmediate(this.#checkImmediateTimer);\n }\n }\n}\n"],"mappings":";;;;;;;;;;AAuCA,IAAa,oBAAoB,CAAC,WAAW,QAAQ;AACrD,IAAa,oBAAoB,CAAC,WAAW,SAAS;AAItD,SAAS,wBAAwB,MAAc;CAC7C,IAAI,SAAS,cACX;CAEF,IAAI,KAAK,WAAW,iBAAiB,GACnC,OAAO;CAET,IAAI,KAAK,WAAW,YAAY,GAAG;EACjC,IAAI,KAAK,SAAS,UAAU,GAC1B,OAAO;E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aAAa,YAIf,GAAG,OAAO,wBAAwB,OAAO,CAAC,KAAK,CAAC;OAEhD,GAAG,QAAQ,uBAAuB,OAAO,CAAC,KAAK,CAAC;EAElD,IAAI;GACF,yCAAyC,IAAI,CAAC;EAChD,SAAS,iBAAiB;GACxB,GAAG,QAAQ,mDAAmD,EAC5D,OAAO,gBACT,CAAC;EACH;EACA,IAAI;GACF,MAAM,GAAG,MAAM;EACjB,UAAU;GACR,QAAQ,KAAK,EAAE;EACjB;CACF;AACF;;;;;;;;;;;;AAaA,IAAa,mBAAb,MAA8B;CAC5B;CAEA;CACA;CACA;CACA,iBAAiB;CAEjB,YAAY,IAAgB,cAAsB;EAChD,KAAK,MAAM;EACX,KAAK,gBAAgB;CACvB;CAEA,YAAY,YAAiC;EAC3C,KAAK,iBAAiB,KAAK,IAAI;EAC/B,IAAI,KAAK,wBAAwB,KAAA,GAAW;GAC1C,KAAK,IAAI,OACP,iCACE,KAAK,gBAAgB,IACtB,mBACD,UACF;GAGA,KAAK,sBAAsB,YACzB,KAAK,oBACL,KAAK,gBAAgB,CACvB;EACF;CACF;CAEA,2BAA2B;EAsBzB,KAAK,uBAAuB,mBAAmB;GAC7C,KAAK,uBAAuB,KAAA;GAC5B,MAAM,yBAAyB,KAAK,IAAI,IAAI,KAAK;GACjD,IAAI,0BAA0B,KAAK,eAAe;IAChD,KAAK,IAAI,OACP,2BACE,yBAAyB,IAC1B,wBACH;IACA,QAAQ,KAAK,QAAQ,KAAK,kBAAkB,EAAE;GAChD;EACF,CAAC;CACH;CAEA,OAAO;EACL,aAAa,KAAK,mBAAmB;EACrC,IAAI,KAAK,sBACP,eAAe,KAAK,oBAAoB;CAE5C;AACF"}
1
+ {"version":3,"file":"life-cycle.js","names":["NodeEventEmitter"],"sources":["../../../../../zero-cache/src/services/life-cycle.ts"],"sourcesContent":["import {EventEmitter as NodeEventEmitter} from 'node:events';\nimport type {IncomingHttpHeaders} from 'node:http';\nimport {pid} from 'node:process';\nimport type {EventEmitter} from 'stream';\nimport type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../shared/src/abort-error.ts';\nimport {resolver} from '../../../shared/src/resolver.ts';\nimport {logLastChanceSQLiteCorruptionDiagnostics} from '../db/sqlite-corruption.ts';\nimport {\n getOrCreateHistogram,\n LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n} from '../observability/metrics.ts';\nimport {ConfigurationError} from '../types/configuration-error.ts';\nimport {\n singleProcessMode,\n type Subprocess,\n type Worker,\n} from '../types/processes.ts';\nimport {orTimeout} from '../types/timeout.ts';\nimport {RunningState} from './running-state.ts';\nimport type {SingletonService} from './service.ts';\n\n/**\n * * `user-facing` workers serve external requests and are the first to\n * receive a `SIGTERM` or `SIGINT` signal for graceful shutdown.\n *\n * * `supporting` workers support `user-facing` workers and are sent\n * the `SIGTERM` signal only after all `user-facing` workers have\n * exited.\n *\n * For other kill signals, such as `SIGQUIT` and `SIGABRT`, all workers\n * are stopped without draining. `SIGQUIT` is used to represent an\n * intentional shutdown (for which draining is not beneficial), whereas\n * `SIGABRT` is used for unexpected process exits.\n *\n * A clean exit (i.e. code 0) of a worker of either type that is not the\n * result of a drain is treated as an intentional shutdown of the server.\n */\nexport type WorkerType = 'user-facing' | 'supporting';\n\nexport const GRACEFUL_SHUTDOWN = ['SIGTERM', 'SIGINT'] as const;\nexport const FORCEFUL_SHUTDOWN = ['SIGQUIT', 'SIGABRT'] as const;\n\ntype GracefulShutdownSignal = (typeof GRACEFUL_SHUTDOWN)[number];\n\nfunction workerStartupMetricName(name: string) {\n if (name === 'zero-cache') {\n return undefined;\n }\n if (name.startsWith('change-streamer')) {\n return 'change_streamer';\n }\n if (name.startsWith('replicator')) {\n if (name.includes('(backup)')) {\n return 'backup_replicator';\n }\n if (name.includes('(serving-copy)')) {\n return 'serving_copy_replicator';\n }\n if (name.includes('(serving)')) {\n return 'serving_replicator';\n }\n return 'other';\n }\n if (name.startsWith('syncer')) {\n return 'syncer';\n }\n if (name.startsWith('reaper')) {\n return 'reaper';\n }\n if (name.startsWith('shadow-syncer')) {\n return 'shadow_syncer';\n }\n if (name.startsWith('mutator')) {\n return 'mutator';\n }\n return 'other';\n}\n\nfunction startupDuration() {\n return getOrCreateHistogram('server', 'startup_duration', {\n description: 'Duration from starting zero-cache to its ready signal.',\n unit: 's',\n bucketBoundaries: LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n });\n}\n\nexport function recordStartupDurationMs(durationMs: number) {\n startupDuration().recordMs(durationMs, {component: 'dispatcher'});\n}\n\nfunction workerStartupDuration() {\n return getOrCreateHistogram('server', 'worker_startup_duration', {\n description: 'Duration from starting a worker to its ready signal.',\n unit: 's',\n bucketBoundaries: LONG_DURATION_HISTOGRAM_BOUNDARIES_S,\n });\n}\n\n// An internal error code used to indicate that a message has already been\n// logged at level ERRROR. When a process exits with this error code, the\n// parent process logs the exit at level WARN instead of ERROR.\nexport const UNHANDLED_EXCEPTION_ERROR_CODE = 13;\n\n// An internal error code used to indicate that the server should exit\n// without draining (e.g. due to a worker exiting cleanly outside of a drain,\n// such as for an auto reset), but the exit is otherwise intentional.\nexport const INTENTIONAL_SHUTDOWN_ERROR_CODE = 14;\n\n/**\n * Handles readiness, termination signals, and coordination of graceful\n * shutdown.\n */\nexport class ProcessManager {\n readonly #lc: LogContext;\n readonly #userFacing = new Set<Subprocess>();\n readonly #all = new Set<Subprocess>();\n readonly #exitImpl: (code: number) => never;\n readonly #start = Date.now();\n readonly #ready: Promise<void>[] = [];\n\n #runningState = new RunningState('process-manager');\n #drainStart = 0;\n\n constructor(lc: LogContext, proc: EventEmitter) {\n this.#lc = lc.withContext('component', 'process-manager');\n\n // Propagate `SIGTERM` and `SIGINT` to all user-facing workers,\n // initiating a graceful shutdown. The parent process will\n // exit once all user-facing workers have exited ...\n for (const signal of GRACEFUL_SHUTDOWN) {\n proc.on(signal, () => this.#startDrain(signal));\n }\n\n // ... which will result in sending `SIGTERM` to the remaining workers.\n proc.on('exit', code =>\n this.#kill(\n this.#all,\n code === 0\n ? 'SIGTERM' // graceful, drained shutdown\n : code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'SIGQUIT' // intentional abort without drain\n : 'SIGABRT', // unintentional shutdown, alertable error\n ),\n );\n\n // For other (catchable) kill signals, exit with a non-zero error code\n // to send a `SIGQUIT` (intentional shutdown) or `SIGABRT` (unexpected\n // shutdown) to all workers. For these signals, workers are stopped\n // immediately without draining, since there is no merit to slowly draining\n // when supporting workers have stopped.\n //\n // The logic for handling these signals is in `runUntilKilled()`.\n for (const signal of FORCEFUL_SHUTDOWN) {\n proc.on(signal, () =>\n this.#exit(signal === 'SIGQUIT' ? INTENTIONAL_SHUTDOWN_ERROR_CODE : -1),\n );\n }\n\n this.#exitImpl = (code: number) => {\n if (singleProcessMode()) {\n return proc.emit('exit', code) as never; // For unit / integration tests.\n }\n process.exit(code);\n };\n }\n\n async shutdown() {\n if (this.#drainStart === 0) {\n this.#startDrain(GRACEFUL_SHUTDOWN[0]);\n }\n await this.#runningState.stopped();\n }\n\n #exit(code: number) {\n this.#lc.info?.('exiting with code', code);\n this.#runningState.stop(this.#lc);\n void this.#lc.flush().finally(() => this.#exitImpl(code));\n }\n\n #startDrain(signal: GracefulShutdownSignal) {\n if (this.#all.size === 0) {\n // Shutdown if a signal is received before any subprocesses are added.\n this.#lc.info?.(`exiting on ${signal}`);\n this.#exit(0);\n }\n this.#lc.info?.(`initiating drain (${signal})`);\n this.#drainStart = Date.now();\n if (this.#userFacing.size) {\n this.#kill(this.#userFacing, signal);\n } else {\n this.#kill(this.#all, signal);\n }\n }\n\n addSubprocess(proc: Subprocess, type: WorkerType, name: string) {\n if (type === 'user-facing') {\n this.#userFacing.add(proc);\n }\n this.#all.add(proc);\n\n let isOpen = true;\n proc.on('close', (code, signal) => {\n isOpen = false;\n this.#onExit(code, signal, null, type, name, proc);\n });\n\n // As per https://nodejs.org/api/child_process.html#event-error\n // 'error' events can happen when sending a message to a child process\n // fails. This is not really an error when the server is shutting down,\n // so log any post-close errors at 'warn'.\n proc.on('error', err =>\n this.#lc[!isOpen || this.#drainStart > 0 ? 'warn' : 'error']?.(\n `error from ${name} ${proc.pid}`,\n err,\n ),\n );\n }\n\n readonly #initializing = new Map<number, string>();\n #nextID = 0;\n\n addWorker(worker: Worker, type: WorkerType, name: string): Worker {\n this.addSubprocess(worker, type, name);\n\n const start = performance.now();\n const id = ++this.#nextID;\n this.#initializing.set(id, name);\n const {promise, resolve} = resolver();\n this.#ready.push(promise);\n\n worker.onceMessageType('ready', () => {\n const elapsed = performance.now() - start;\n const workerMetricName = workerStartupMetricName(name);\n if (workerMetricName) {\n workerStartupDuration().recordMs(elapsed, {\n worker: workerMetricName,\n type,\n });\n }\n this.#lc.debug?.(`${name} ready (${Date.now() - this.#start} ms)`);\n this.#initializing.delete(id);\n resolve();\n });\n\n return worker;\n }\n\n initializing(): string[] {\n return [...this.#initializing.values()];\n }\n\n async allWorkersReady() {\n await Promise.all(this.#ready);\n }\n\n logErrorAndExit(err: unknown, name: string) {\n // only accessible by the main (i.e. user-facing) process.\n this.#onExit(-1, null, err, 'user-facing', name, undefined);\n }\n\n #onExit(\n code: number,\n sig: NodeJS.Signals | null,\n err: unknown,\n type: WorkerType,\n name: string,\n worker: Subprocess | undefined,\n ) {\n // Remove the worker from maps to avoid attempting to send more signals to it.\n if (worker) {\n this.#userFacing.delete(worker);\n this.#all.delete(worker);\n }\n\n const pid = worker?.pid ?? process.pid;\n\n // A clean exit outside of a drain is an intentional shutdown, e.g. the\n // replication-manager shutting down for protocol-specific instructions\n // (like auto reset), or a configuration error. In this case, a special\n // error code is used to signal that the server should be shut down\n // without draining, but it is otherwise not considered an\n // unexpected/alertable error. The same applies to a supporting worker\n // that exits while user-facing workers are still draining.\n if (\n code === 0 &&\n (this.#drainStart === 0 ||\n (type === 'supporting' && this.#userFacing.size > 0))\n ) {\n code = INTENTIONAL_SHUTDOWN_ERROR_CODE;\n }\n\n if (type === 'supporting') {\n const log =\n code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'info'\n : 'warn';\n this.#lc[log]?.(`${name} (${pid}) exited with code (${code})`, err ?? '');\n return this.#exit(code);\n }\n\n const log =\n code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'info'\n : this.#drainStart > 0 || code === UNHANDLED_EXCEPTION_ERROR_CODE\n ? 'warn'\n : 'error';\n this.#lc[log]?.(\n sig\n ? `${name} (${pid}) killed with (${sig})`\n : `${name} (${pid}) exited with code (${code})`,\n err ?? '',\n );\n\n // user-facing workers exited or finished draining.\n if (this.#userFacing.size === 0) {\n this.#lc.info?.(\n this.#drainStart\n ? `all user-facing workers drained (${\n Date.now() - this.#drainStart\n } ms)`\n : `all user-facing workers exited`,\n );\n return this.#exit(this.#drainStart ? 0 : code);\n }\n\n if (this.#drainStart === 0) {\n // If a user-facing worker exits without receiving a drain signal,\n // shutdown the server.\n return this.#exit(code);\n }\n\n return undefined;\n }\n\n #kill(workers: Iterable<Subprocess>, signal: NodeJS.Signals) {\n for (const worker of workers) {\n try {\n worker.kill(signal);\n } catch (e) {\n this.#lc.error?.(e);\n }\n }\n }\n}\n\n/**\n * Runs the supporting subprocesses of a worker (e.g. the litestream process\n * of the backup-replicator) as a {@link SingletonService}.\n *\n * Unlike a {@link ProcessManager}, it does not forward `SIGTERM` or `SIGINT`\n * to its subprocesses. Instead, {@link runUntilKilled} stops it along with the\n * worker's other services, i.e. after they have drained, so that the\n * subprocesses keep supporting them until then. Forceful signals (`SIGQUIT`\n * and `SIGABRT`), on the other hand, are forwarded immediately once the\n * service is running, and the subprocesses are killed if the worker exits\n * before they are stopped.\n *\n * `run()` settles when a subprocess exits before {@link stop()} is called,\n * which stops the worker: a clean exit resolves (and the parent in turn treats\n * the worker's clean exit outside of a drain as an intentional shutdown),\n * while any other exit rejects.\n */\nexport class SupportingProcesses implements SingletonService {\n readonly id = 'supporting-processes';\n readonly #lc: LogContext;\n readonly #parent: EventEmitter;\n readonly #procs = new Map<Subprocess, string>();\n readonly #done = resolver<void>();\n #signaled: NodeJS.Signals | undefined;\n\n constructor(lc: LogContext, parent: EventEmitter) {\n this.#lc = lc.withContext('component', 'supporting-processes');\n this.#parent = parent;\n // run() may never be called if the worker fails before running services.\n this.#done.promise.catch(() => {});\n\n parent.on('exit', code =>\n this.#kill(\n code === 0\n ? 'SIGTERM'\n : code === INTENTIONAL_SHUTDOWN_ERROR_CODE\n ? 'SIGQUIT'\n : 'SIGABRT',\n ),\n );\n }\n\n add<P extends Subprocess>(proc: P, name: string): P {\n this.#procs.set(proc, name);\n proc.on('close', (code: number | null, sig: NodeJS.Signals | null) =>\n this.#onClose(proc, name, code, sig),\n );\n // As with the ProcessManager, 'error' events after a signal (e.g. failing\n // to send a message to an exiting process) are logged at 'warn'.\n proc.on('error', err =>\n this.#lc[this.#signaled ? 'warn' : 'error']?.(\n `error from ${name} ${proc.pid}`,\n err,\n ),\n );\n return proc;\n }\n\n run(): Promise<void> {\n // Signal listeners are only installed when running, i.e. alongside those\n // of runUntilKilled(). Installing them any earlier would consume signals\n // received during worker initialization, which would otherwise terminate\n // the worker (or be handled by the initialization logic).\n for (const signal of FORCEFUL_SHUTDOWN) {\n this.#parent.on(signal, () => this.#kill(signal));\n }\n return this.#done.promise;\n }\n\n async stop(): Promise<void> {\n if (!this.#signaled) {\n this.#kill('SIGTERM');\n }\n if (this.#procs.size === 0) {\n this.#done.resolve();\n }\n await this.#done.promise.catch(() => {});\n }\n\n #onClose(\n proc: Subprocess,\n name: string,\n code: number | null,\n sig: NodeJS.Signals | null,\n ) {\n this.#procs.delete(proc);\n const exit = sig\n ? `${name} (${proc.pid}) killed with (${sig})`\n : `${name} (${proc.pid}) exited with code (${code})`;\n\n if (this.#signaled) {\n this.#lc.info?.(exit);\n if (this.#procs.size === 0) {\n this.#done.resolve();\n }\n } else if (code === 0 || code === INTENTIONAL_SHUTDOWN_ERROR_CODE) {\n this.#lc.info?.(exit);\n this.#done.resolve();\n } else {\n this.#done.reject(new Error(exit));\n }\n }\n\n #kill(signal: NodeJS.Signals) {\n this.#signaled = signal;\n for (const proc of this.#procs.keys()) {\n try {\n proc.kill(signal);\n } catch (e) {\n this.#lc.error?.(e);\n }\n }\n }\n}\n\nexport type SelfDrainOptions = {\n /**\n * The time after a self drain is requested at which the server sends itself\n * a `SIGTERM`, unless one was received sooner (e.g. from the container\n * runner).\n */\n delayMs: number;\n\n /**\n * Called when a self drain is requested. This should fail health checks\n * (both readiness and liveness) so that the container runner stops routing\n * to the server and initiates its normal shutdown process.\n */\n onRequested: () => void;\n};\n\n/**\n * Coordinates a drain that is initiated by the server itself (e.g. by a\n * view-syncer that has been granted permission to restore a new replica),\n * as opposed to one initiated by the container runner with a `SIGTERM`.\n *\n * Health checks start failing immediately, upon which an orchestrator such\n * as Kubernetes stops routing to the server and shuts it down as usual\n * (i.e. running any pre-stop hooks before sending `SIGTERM`). As a backstop,\n * and for environments without liveness checks, the server sends itself a\n * `SIGTERM` after the configured delay.\n */\nexport class SelfDrain {\n readonly #lc: LogContext;\n readonly #proc: EventEmitter;\n readonly #delayMs: number;\n readonly #onRequested: () => void;\n #requested = false;\n #signaled = false;\n #timer: ReturnType<typeof setTimeout> | undefined;\n\n constructor(lc: LogContext, proc: EventEmitter, opts: SelfDrainOptions) {\n this.#lc = lc.withContext('component', 'self-drain');\n this.#proc = proc;\n this.#delayMs = opts.delayMs;\n this.#onRequested = opts.onRequested;\n\n for (const signal of GRACEFUL_SHUTDOWN) {\n proc.once(signal, () => {\n this.#signaled = true;\n clearTimeout(this.#timer);\n });\n }\n }\n\n request() {\n if (this.#requested || this.#signaled) {\n return; // Already draining.\n }\n this.#requested = true;\n this.#lc.info?.(\n `self drain requested. sending SIGTERM in ${this.#delayMs} ms`,\n );\n this.#onRequested();\n this.#timer = setTimeout(() => {\n if (!this.#signaled) {\n this.#lc.info?.(`initiating self drain`);\n this.#proc.emit('SIGTERM', 'SIGTERM');\n }\n }, this.#delayMs);\n }\n}\n\n/**\n * The event that a {@link PreDrain} emits immediately upon the first graceful\n * shutdown signal, with that signal as its argument, before the delay.\n */\nexport const PRE_DRAIN = 'preDrain';\n\n/**\n * Delays graceful shutdown signals (`SIGTERM`, `SIGINT`), e.g. so that load\n * balancers can observe a failed readiness check before the server begins\n * to drain. The first graceful signal is announced immediately with a\n * {@link PRE_DRAIN} event, and is re-emitted to listeners of the signal, such\n * as those of the {@link ProcessManager} and {@link runUntilKilled}, once the\n * delay has elapsed. Repeated graceful signals are dropped. All other events,\n * including forceful signals, are forwarded immediately.\n */\nexport class PreDrain extends NodeEventEmitter {\n readonly #lc: LogContext;\n readonly #proc: EventEmitter;\n readonly #delayMs: number;\n readonly #forwarded = new Set<string | symbol>(['newListener', PRE_DRAIN]);\n #signaled = false;\n #timer: ReturnType<typeof setTimeout> | undefined;\n\n constructor(lc: LogContext, proc: EventEmitter, delayMs: number) {\n super();\n this.#lc = lc.withContext('component', 'pre-drain');\n this.#proc = proc;\n this.#delayMs = delayMs;\n\n for (const signal of GRACEFUL_SHUTDOWN) {\n this.#forwarded.add(signal);\n proc.on(signal, () => this.#onGracefulSignal(signal));\n }\n this.on('newListener', (event: string | symbol) => this.#forward(event));\n }\n\n #forward(event: string | symbol) {\n if (this.#forwarded.has(event)) {\n return;\n }\n this.#forwarded.add(event);\n this.#proc.on(event, (...args: unknown[]) => {\n if (\n event === 'disconnect' ||\n (FORCEFUL_SHUTDOWN as readonly (string | symbol)[]).includes(event)\n ) {\n clearTimeout(this.#timer); // Stopping without a drain.\n }\n this.emit(event, ...args);\n });\n }\n\n #onGracefulSignal(signal: GracefulShutdownSignal) {\n if (this.#signaled) {\n this.#lc.info?.(`already shutting down. ignoring ${signal}`);\n return;\n }\n this.#signaled = true;\n this.#lc.info?.(`received ${signal}. draining in ${this.#delayMs} ms`);\n this.emit(PRE_DRAIN, signal);\n this.#timer = setTimeout(() => this.emit(signal, signal), this.#delayMs);\n }\n}\n\n/**\n * The maximum time that {@link runUntilKilled} waits for its services to stop\n * (i.e. for their `run()` to settle) after stopping them.\n */\nexport const STOP_TIMEOUT_MS = 60_000;\n\n/**\n * Runs the specified services, stopping them on `SIGTERM` or `SIGINT` with\n * an optional {@link SingletonService.drain drain()}, or stopping them\n * without draining for `SIGQUIT`, or when the parent process disconnects.\n * Repeated graceful signals are ignored. If a service stops (or throws) on its\n * own, the remaining services are stopped without draining.\n *\n * @returns a Promise that settles once all services have stopped (or\n * {@link STOP_TIMEOUT_MS} has elapsed), resolving or rejecting\n * with the outcome of the first service to stop.\n */\nexport async function runUntilKilled(\n lc: LogContext,\n parent: EventEmitter,\n ...services: SingletonService[]\n): Promise<void> {\n if (services.length === 0) {\n return;\n }\n const stopped = new Set<SingletonService>();\n const runs = services.map(svc => svc.run().finally(() => stopped.add(svc)));\n\n const allStopped = resolver<void>();\n let stopping: Promise<void> | undefined;\n const stopAll = (reason: string) =>\n (stopping ??= stopServices(lc, services, runs, stopped, reason).then(\n allStopped.resolve,\n ));\n\n const GRACEFUL_SIGNALS = GRACEFUL_SHUTDOWN as readonly NodeJS.Signals[];\n let draining = false;\n const onSignal = async (signal: NodeJS.Signals) => {\n if (GRACEFUL_SIGNALS.includes(signal)) {\n if (draining || stopping) {\n // e.g. A SIGINT from both the terminal and a wrapping process.\n lc.info?.(`already shutting down. ignoring ${signal}`);\n return;\n }\n draining = true;\n // Drain all services before stopping any of them. Stopping a service\n // resolves its run() and thus ends runUntilKilled(), so a service\n // without a drain() must not be stopped while others are draining.\n const results = await Promise.allSettled(\n services.map(async svc => {\n if (svc.drain) {\n lc.info?.(`draining ${svc.constructor.name} ${svc.id} (${signal})`);\n await svc.drain();\n }\n }),\n );\n for (const result of results) {\n if (result.status === 'rejected') {\n lc.warn?.(`error while draining (${signal})`, result.reason);\n }\n }\n }\n // A forceful signal stops the services without waiting for (or after)\n // a drain.\n await stopAll(signal);\n };\n // The listeners remain installed for the lifetime of the process, as a\n // signal without a listener would terminate the process (e.g. a repeated\n // SIGINT during a drain).\n for (const signal of [...GRACEFUL_SHUTDOWN, ...FORCEFUL_SHUTDOWN]) {\n parent.on(signal, () => void onSignal(signal));\n }\n // A forked worker whose parent has exited (e.g. was killed by a signal,\n // in which case it cannot signal its workers) stops without draining, so\n // that it (and in turn its own subprocesses) is not orphaned.\n parent.once('disconnect', () => {\n lc.warn?.(`parent process disconnected. stopping without draining`);\n void stopAll('disconnect');\n });\n\n try {\n // Run all services and resolve when any of them stops, or when stopping\n // them (after a signal) has timed out.\n const svc = await Promise.race([\n ...runs.map((run, i) => run.then(() => services[i])),\n allStopped.promise,\n ]);\n if (svc) {\n lc.info?.(`${svc.constructor.name} (${svc.id}) stopped`);\n }\n } catch (e) {\n lc.error?.(`exiting on error`, e);\n throw e;\n } finally {\n await stopAll('stopped');\n }\n}\n\nasync function stopServices(\n lc: LogContext,\n services: SingletonService[],\n runs: Promise<void>[],\n stopped: Set<SingletonService>,\n reason: string,\n) {\n for (const svc of services) {\n if (!stopped.has(svc)) {\n lc.info?.(`stopping ${svc.constructor.name} ${svc.id} (${reason})`);\n void svc\n .stop()\n .catch(e =>\n lc.warn?.(`error stopping ${svc.constructor.name} ${svc.id}`, e),\n );\n }\n }\n if (\n (await orTimeout(Promise.allSettled(runs), STOP_TIMEOUT_MS)) === 'timed-out'\n ) {\n const running = services.filter(svc => !stopped.has(svc));\n lc.warn?.(\n `services did not stop within ${STOP_TIMEOUT_MS} ms: ` +\n running.map(svc => `${svc.constructor.name} ${svc.id}`).join(', '),\n );\n }\n}\n\nexport async function exitAfter(\n logContext: LogContext | (() => LogContext),\n run: () => Promise<void>,\n) {\n const getLogContext =\n typeof logContext === 'function' ? logContext : () => logContext;\n try {\n await run();\n const lc = getLogContext();\n lc.info?.(`pid ${pid} exiting normally`);\n process.exit(0);\n } catch (e) {\n const lc = getLogContext();\n if (e instanceof ConfigurationError) {\n lc.error?.(`exiting with configuration error: ${String(e)}`, e);\n process.exit(0);\n }\n if (e instanceof AbortError) {\n // AbortErrors signal an intentional shutdown or restart (e.g. SIGTERM\n // during a rollout, AutoResetSignal) and should not raise alerts. The\n // non-zero exit code is kept so that restart semantics are unchanged.\n lc.warn?.(`exiting after abort: ${String(e)}`, e);\n } else {\n lc.error?.(`exiting with error: ${String(e)}`, e);\n }\n try {\n logLastChanceSQLiteCorruptionDiagnostics(lc, e);\n } catch (diagnosticError) {\n lc.error?.('SQLite corruption last-chance diagnostic failed', {\n error: diagnosticError,\n });\n }\n try {\n await lc.flush();\n } finally {\n process.exit(-1);\n }\n }\n}\n\n/**\n * The HeartbeatMonitor monitors the cadence heartbeats (e.g. \"/keepalive\"\n * health checks made to HttpServices) that signal that the server\n * should continue processing requests. When a configurable `stopInterval`\n * elapses without receiving these heartbeats, the monitor initiates a\n * graceful shutdown of the server. This works with common load balancing\n * frameworks such as AWS Elastic Load Balancing.\n *\n * The HeartbeatMonitor is **opt-in** in that it only kicks in after it\n * starts receiving keepalives.\n */\nexport class HeartbeatMonitor {\n readonly #stopInterval: number;\n\n #lc: LogContext;\n #checkIntervalTimer: NodeJS.Timeout | undefined;\n #checkImmediateTimer: NodeJS.Immediate | undefined;\n #lastHeartbeat = 0;\n\n constructor(lc: LogContext, stopInterval: number) {\n this.#lc = lc;\n this.#stopInterval = stopInterval;\n }\n\n onHeartbeat(reqHeaders: IncomingHttpHeaders) {\n this.#lastHeartbeat = Date.now();\n if (this.#checkIntervalTimer === undefined) {\n this.#lc.info?.(\n `starting heartbeat monitor at ${\n this.#stopInterval / 1000\n } second interval`,\n reqHeaders,\n );\n // e.g. check every 5 seconds to see if it's been over 20 seconds\n // since the last heartbeat.\n this.#checkIntervalTimer = setInterval(\n this.#checkStopInterval,\n this.#stopInterval / 4,\n );\n }\n }\n\n #checkStopInterval = () => {\n // In the Node.js event loop, timers like setInterval and setTimeout\n // run *before* I/O events coming from network sockets or file reads/writes.\n // When this process gets starved of CPU resources for long periods of time,\n // for example when other processes are monopolizing all available cores,\n // pathological behavior can emerge:\n // - keepalive network request comes in, but is queued in Node internals waiting\n // for time on the event loop\n // - CPU is starved/monopolized by other processes for longer than the time\n // configured via this.#stopInterval\n // - When CPU becomes available and the event loop wakes up, this stop interval\n // check is run *before* the keepalive request is processed. The value of\n // this.#lastHeartbeat is now very stale, and erroneously triggers a shutdown\n // even though keepalive requests were about to be processed and update\n // this.#lastHeartbeat. Downtime ensues.\n //\n // To avoid this, we push the check out to a phase of the event loop *after*\n // I/O events are processed, using setImmediate():\n // https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#setimmediate-vs-settimeout\n //\n // This ensures we see a value for this.#lastHeartbeat that reflects\n // any keepalive requests that came in during the current event loop turn.\n this.#checkImmediateTimer = setImmediate(() => {\n this.#checkImmediateTimer = undefined;\n const timeSinceLastHeartbeat = Date.now() - this.#lastHeartbeat;\n if (timeSinceLastHeartbeat >= this.#stopInterval) {\n this.#lc.info?.(\n `last heartbeat received ${\n timeSinceLastHeartbeat / 1000\n } seconds ago. draining.`,\n );\n process.kill(process.pid, GRACEFUL_SHUTDOWN[0]);\n }\n });\n };\n\n stop() {\n clearTimeout(this.#checkIntervalTimer);\n if (this.#checkImmediateTimer) {\n clearImmediate(this.#checkImmediateTimer);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAwCA,IAAa,oBAAoB,CAAC,WAAW,QAAQ;AACrD,IAAa,oBAAoB,CAAC,WAAW,SAAS;AAItD,SAAS,wBAAwB,MAAc;CAC7C,IAAI,SAAS,cACX;CAEF,IAAI,KAAK,WAAW,iBAAiB,GACnC,OAAO;CAET,IAAI,KAAK,WAAW,YAAY,GAAG;EACjC,IAAI,KAAK,SAAS,UAAU,GAC1B,OAAO;EAET,IAAI,KAAK,SAAS,gBAAgB,GAChC,OAAO;EAET,IAAI,KAAK,SAAS,WAAW,GAC3B,OAAO;EAET,OAAO;CACT;CACA,IAAI,KAAK,WAAW,QAAQ,GAC1B,OAAO;CAET,IAAI,KAAK,WAAW,QAAQ,GAC1B,OAAO;CAET,IAAI,KAAK,WAAW,eAAe,GACjC,OAAO;CAET,IAAI,KAAK,W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YAAY,YAAiC;EAC3C,KAAK,iBAAiB,KAAK,IAAI;EAC/B,IAAI,KAAK,wBAAwB,KAAA,GAAW;GAC1C,KAAK,IAAI,OACP,iCACE,KAAK,gBAAgB,IACtB,mBACD,UACF;GAGA,KAAK,sBAAsB,YACzB,KAAK,oBACL,KAAK,gBAAgB,CACvB;EACF;CACF;CAEA,2BAA2B;EAsBzB,KAAK,uBAAuB,mBAAmB;GAC7C,KAAK,uBAAuB,KAAA;GAC5B,MAAM,yBAAyB,KAAK,IAAI,IAAI,KAAK;GACjD,IAAI,0BAA0B,KAAK,eAAe;IAChD,KAAK,IAAI,OACP,2BACE,yBAAyB,IAC1B,wBACH;IACA,QAAQ,KAAK,QAAQ,KAAK,kBAAkB,EAAE;GAChD;EACF,CAAC;CACH;CAEA,OAAO;EACL,aAAa,KAAK,mBAAmB;EACrC,IAAI,KAAK,sBACP,eAAe,KAAK,oBAAoB;CAE5C;AACF"}
@@ -4,6 +4,7 @@ import { AbortError } from "../../../../shared/src/abort-error.js";
4
4
  import { must } from "../../../../shared/src/must.js";
5
5
  import { mapEntries } from "../../../../shared/src/objects.js";
6
6
  import { stringify } from "../../../../shared/src/bigint-json.js";
7
+ import { id } from "../../types/sql.js";
7
8
  import { ZERO_VERSION_COLUMN_NAME } from "./schema/constants.js";
8
9
  import { liteRow } from "../../types/lite.js";
9
10
  import { liteTableName } from "../../types/names.js";
@@ -14,7 +15,6 @@ import { computeZqlSpecs, listTables } from "../../db/lite-tables.js";
14
15
  import { BackfillingTracker } from "./schema/backfilling.js";
15
16
  import { ChangeLog } from "./schema/change-log.js";
16
17
  import { updateReplicationWatermark } from "./schema/replication-state.js";
17
- import { id } from "../../types/sql.js";
18
18
  import { createLiteIndexStatement, createLiteTableStatement, liteColumnDef } from "../../db/create.js";
19
19
  import { acceptBackfill } from "../change-source/protocol/backfill-progress.js";
20
20
  import { SqliteError } from "@rocicorp/zero-sqlite3";