@malloy-publisher/server 0.0.198-dev4 → 0.0.198-dev6
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.
- package/build.ts +17 -13
- package/dist/instrumentation.mjs +21 -0
- package/dist/package_load_worker.mjs +12213 -0
- package/dist/server.mjs +2026 -2622
- package/package.json +2 -3
- package/src/health.ts +5 -3
- package/src/instrumentation.ts +50 -0
- package/src/package_load/package_load_pool.spec.ts +252 -0
- package/src/package_load/package_load_pool.ts +920 -0
- package/src/{compile/compile_worker.ts → package_load/package_load_worker.ts} +505 -246
- package/src/package_load/protocol.ts +336 -0
- package/src/server.ts +12 -0
- package/src/service/environment_store.ts +24 -3
- package/src/service/given.ts +80 -0
- package/src/service/model.ts +255 -291
- package/src/service/package.spec.ts +10 -0
- package/src/service/package.ts +268 -259
- package/src/service/package_worker_path.spec.ts +196 -0
- package/dist/compile_worker.mjs +0 -633
- package/src/compile/compile_pool.spec.ts +0 -292
- package/src/compile/compile_pool.ts +0 -796
- package/src/compile/protocol.ts +0 -270
- package/src/service/model_worker_path.spec.ts +0 -133
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloy-publisher/server",
|
|
3
3
|
"description": "Malloy Publisher Server",
|
|
4
|
-
"version": "0.0.198-
|
|
4
|
+
"version": "0.0.198-dev6",
|
|
5
5
|
"main": "dist/server.mjs",
|
|
6
6
|
"bin": {
|
|
7
7
|
"malloy-publisher": "dist/server.mjs"
|
|
@@ -51,7 +51,6 @@
|
|
|
51
51
|
"@opentelemetry/sdk-metrics": "^2.0.0",
|
|
52
52
|
"@opentelemetry/sdk-node": "^0.200.0",
|
|
53
53
|
"@opentelemetry/sdk-trace-node": "^2.0.0",
|
|
54
|
-
"adm-zip": "^0.5.16",
|
|
55
54
|
"async-mutex": "^0.5.0",
|
|
56
55
|
"aws-sdk": "^2.1692.0",
|
|
57
56
|
"body-parser": "^1.20.2",
|
|
@@ -61,6 +60,7 @@
|
|
|
61
60
|
"cors": "^2.8.5",
|
|
62
61
|
"duckdb": "1.4.4",
|
|
63
62
|
"express": "^4.21.0",
|
|
63
|
+
"extract-zip": "^2.0.1",
|
|
64
64
|
"globals": "^15.9.0",
|
|
65
65
|
"handlebars": "^4.7.8",
|
|
66
66
|
"http-proxy-middleware": "^3.0.5",
|
|
@@ -76,7 +76,6 @@
|
|
|
76
76
|
"@eslint/eslintrc": "^3.3.1",
|
|
77
77
|
"@eslint/js": "^9.23.0",
|
|
78
78
|
"@faker-js/faker": "^9.4.0",
|
|
79
|
-
"@types/adm-zip": "^0.5.7",
|
|
80
79
|
"@types/bun": "^1.2.20",
|
|
81
80
|
"@types/cors": "^2.8.12",
|
|
82
81
|
"@types/express": "^4.17.14",
|
package/src/health.ts
CHANGED
|
@@ -149,9 +149,11 @@ export async function performGracefulShutdownAfterDrain(
|
|
|
149
149
|
// processes. Lazy-imported to avoid pulling the pool module
|
|
150
150
|
// into the health.ts dep graph for tests that don't exercise
|
|
151
151
|
// the compile path.
|
|
152
|
-
const {
|
|
153
|
-
|
|
154
|
-
|
|
152
|
+
const { getPackageLoadPool } = await import(
|
|
153
|
+
"./package_load/package_load_pool"
|
|
154
|
+
);
|
|
155
|
+
await getPackageLoadPool().shutdown();
|
|
156
|
+
logger.info("Package-load worker pool shut down");
|
|
155
157
|
} catch (_error) {
|
|
156
158
|
/* do nothing */
|
|
157
159
|
}
|
package/src/instrumentation.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { monitorEventLoopDelay } from "node:perf_hooks";
|
|
1
2
|
import { metrics } from "@opentelemetry/api";
|
|
2
3
|
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
|
|
3
4
|
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
|
|
@@ -116,6 +117,55 @@ const httpRequestCount = meter.createCounter("http_server_requests_total", {
|
|
|
116
117
|
description: "Total number of HTTP requests",
|
|
117
118
|
});
|
|
118
119
|
|
|
120
|
+
// Event-loop-delay metrics. A blocked event loop is the only way the
|
|
121
|
+
// /health/liveness probe (a pure synchronous 200 handler) can fail under K8s,
|
|
122
|
+
// so we surface p50/p99/max so an operator can correlate liveness restarts
|
|
123
|
+
// with sustained event-loop pressure (large Malloy compiles, GC, etc.).
|
|
124
|
+
const eventLoopHistogram = monitorEventLoopDelay({ resolution: 20 });
|
|
125
|
+
eventLoopHistogram.enable();
|
|
126
|
+
|
|
127
|
+
const eventLoopLagP50 = meter.createObservableGauge(
|
|
128
|
+
"publisher_event_loop_lag_p50_ms",
|
|
129
|
+
{
|
|
130
|
+
description:
|
|
131
|
+
"Event loop delay p50 since the last scrape, in milliseconds",
|
|
132
|
+
unit: "ms",
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
const eventLoopLagP99 = meter.createObservableGauge(
|
|
136
|
+
"publisher_event_loop_lag_p99_ms",
|
|
137
|
+
{
|
|
138
|
+
description:
|
|
139
|
+
"Event loop delay p99 since the last scrape, in milliseconds",
|
|
140
|
+
unit: "ms",
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
const eventLoopLagMax = meter.createObservableGauge(
|
|
144
|
+
"publisher_event_loop_lag_max_ms",
|
|
145
|
+
{
|
|
146
|
+
description:
|
|
147
|
+
"Event loop delay max since the last scrape, in milliseconds",
|
|
148
|
+
unit: "ms",
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// Sample all three in one batch so the histogram reset can't race the reads.
|
|
153
|
+
meter.addBatchObservableCallback(
|
|
154
|
+
(observableResult) => {
|
|
155
|
+
observableResult.observe(
|
|
156
|
+
eventLoopLagP50,
|
|
157
|
+
eventLoopHistogram.percentile(50) / 1e6,
|
|
158
|
+
);
|
|
159
|
+
observableResult.observe(
|
|
160
|
+
eventLoopLagP99,
|
|
161
|
+
eventLoopHistogram.percentile(99) / 1e6,
|
|
162
|
+
);
|
|
163
|
+
observableResult.observe(eventLoopLagMax, eventLoopHistogram.max / 1e6);
|
|
164
|
+
eventLoopHistogram.reset();
|
|
165
|
+
},
|
|
166
|
+
[eventLoopLagP50, eventLoopLagP99, eventLoopLagMax],
|
|
167
|
+
);
|
|
168
|
+
|
|
119
169
|
const IGNORED_PATHS = new Set([
|
|
120
170
|
"/health",
|
|
121
171
|
"/health/liveness",
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit + light integration tests for the PackageLoadPool.
|
|
3
|
+
*
|
|
4
|
+
* The pool runs real `worker_threads` workers under the hood. These
|
|
5
|
+
* tests intentionally exercise that path so we catch regressions in
|
|
6
|
+
* RPC routing, queueing, lifecycle, and error propagation that
|
|
7
|
+
* wouldn't surface in a pure mock.
|
|
8
|
+
*
|
|
9
|
+
* Pool reuse strategy
|
|
10
|
+
* -------------------
|
|
11
|
+
* The "real worker" tests share a single `PackageLoadPool` across
|
|
12
|
+
* cases via Bun's `beforeAll`/`afterAll`. The worker itself owns no
|
|
13
|
+
* native handles (all duckdb work runs on the main thread); we still
|
|
14
|
+
* share to keep per-test overhead low and to match the production
|
|
15
|
+
* deployment, where the pool spawns workers up-front and reuses them.
|
|
16
|
+
*/
|
|
17
|
+
import {
|
|
18
|
+
afterAll,
|
|
19
|
+
afterEach,
|
|
20
|
+
beforeAll,
|
|
21
|
+
beforeEach,
|
|
22
|
+
describe,
|
|
23
|
+
expect,
|
|
24
|
+
it,
|
|
25
|
+
} from "bun:test";
|
|
26
|
+
import * as fs from "fs";
|
|
27
|
+
import * as os from "os";
|
|
28
|
+
import * as path from "path";
|
|
29
|
+
import {
|
|
30
|
+
PackageLoadPool,
|
|
31
|
+
__setPackageLoadPoolForTests,
|
|
32
|
+
getPackageLoadWorkerCount,
|
|
33
|
+
} from "./package_load_pool";
|
|
34
|
+
|
|
35
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
36
|
+
// getPackageLoadWorkerCount — env var parsing
|
|
37
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
describe("getPackageLoadWorkerCount", () => {
|
|
40
|
+
const ORIGINAL = process.env.PACKAGE_LOAD_WORKERS;
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
if (ORIGINAL === undefined) {
|
|
44
|
+
delete process.env.PACKAGE_LOAD_WORKERS;
|
|
45
|
+
} else {
|
|
46
|
+
process.env.PACKAGE_LOAD_WORKERS = ORIGINAL;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("defaults to 1 worker when env unset", () => {
|
|
51
|
+
delete process.env.PACKAGE_LOAD_WORKERS;
|
|
52
|
+
expect(getPackageLoadWorkerCount()).toBe(1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("throws when env value is non-numeric", () => {
|
|
56
|
+
process.env.PACKAGE_LOAD_WORKERS = "not-a-number";
|
|
57
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("throws when env value is negative", () => {
|
|
61
|
+
process.env.PACKAGE_LOAD_WORKERS = "-2";
|
|
62
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("throws when env value is 0 (no in-process fallback)", () => {
|
|
66
|
+
process.env.PACKAGE_LOAD_WORKERS = "0";
|
|
67
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("honors positive overrides", () => {
|
|
71
|
+
process.env.PACKAGE_LOAD_WORKERS = "4";
|
|
72
|
+
expect(getPackageLoadWorkerCount()).toBe(4);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
77
|
+
// PackageLoadPool constructor validation
|
|
78
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
describe("PackageLoadPool constructor", () => {
|
|
81
|
+
it("throws when maxWorkers is 0", () => {
|
|
82
|
+
expect(() => new PackageLoadPool(0)).toThrow(/maxWorkers >= 1/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("throws when maxWorkers is negative", () => {
|
|
86
|
+
expect(() => new PackageLoadPool(-1)).toThrow(/maxWorkers >= 1/);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
91
|
+
// PackageLoadPool — real worker loads a package
|
|
92
|
+
//
|
|
93
|
+
// One shared pool across the describe; each test gets its own temp
|
|
94
|
+
// package directory and its own DuckDB connection.
|
|
95
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
describe("PackageLoadPool (real worker)", () => {
|
|
98
|
+
let pool: PackageLoadPool;
|
|
99
|
+
let tempDir: string;
|
|
100
|
+
|
|
101
|
+
beforeAll(() => {
|
|
102
|
+
pool = new PackageLoadPool(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
afterAll(async () => {
|
|
106
|
+
await pool.shutdown();
|
|
107
|
+
await __setPackageLoadPoolForTests(null);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "publisher-compile-"));
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
afterEach(() => {
|
|
115
|
+
if (tempDir) {
|
|
116
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
117
|
+
tempDir = "";
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
function writeManifest(dir: string, name = "pkg"): void {
|
|
122
|
+
fs.writeFileSync(
|
|
123
|
+
path.join(dir, "publisher.json"),
|
|
124
|
+
JSON.stringify({ name, description: "test package" }),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function buildConfig(): Promise<{
|
|
129
|
+
malloyConfig: import("@malloydata/malloy").MalloyConfig;
|
|
130
|
+
duckdb: { close: () => Promise<void> };
|
|
131
|
+
}> {
|
|
132
|
+
const { MalloyConfig, FixedConnectionMap } = await import(
|
|
133
|
+
"@malloydata/malloy"
|
|
134
|
+
);
|
|
135
|
+
const { DuckDBConnection } = await import("@malloydata/db-duckdb");
|
|
136
|
+
const duckdb = new DuckDBConnection("duckdb", ":memory:");
|
|
137
|
+
const connections = new FixedConnectionMap(
|
|
138
|
+
new Map([["duckdb", duckdb]]),
|
|
139
|
+
"duckdb",
|
|
140
|
+
);
|
|
141
|
+
const malloyConfig = new MalloyConfig({ connections: {} });
|
|
142
|
+
malloyConfig.wrapConnections(() => connections);
|
|
143
|
+
return { malloyConfig, duckdb };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
it("loads a trivial single-model package and returns a hydratable modelDef", async () => {
|
|
147
|
+
writeManifest(tempDir);
|
|
148
|
+
fs.writeFileSync(
|
|
149
|
+
path.join(tempDir, "trivial.malloy"),
|
|
150
|
+
`source: nums is duckdb.sql("select 1 as a, 2 as b") extend {
|
|
151
|
+
measure: total is a.sum()
|
|
152
|
+
}`,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
156
|
+
try {
|
|
157
|
+
const outcome = await pool.loadPackage({
|
|
158
|
+
packagePath: tempDir,
|
|
159
|
+
packageName: "pkg",
|
|
160
|
+
malloyConfig,
|
|
161
|
+
defaultConnectionName: "duckdb",
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(outcome.packageMetadata.name).toBe("pkg");
|
|
165
|
+
expect(outcome.models).toHaveLength(1);
|
|
166
|
+
const m = outcome.models[0];
|
|
167
|
+
expect(m.modelPath).toBe("trivial.malloy");
|
|
168
|
+
expect(m.modelType).toBe("model");
|
|
169
|
+
expect(m.compilationError).toBeUndefined();
|
|
170
|
+
expect(m.modelDef).toBeDefined();
|
|
171
|
+
expect(Array.isArray(m.sources)).toBe(true);
|
|
172
|
+
expect(outcome.loadDurationMs).toBeGreaterThan(0);
|
|
173
|
+
} finally {
|
|
174
|
+
await duckdb.close();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("propagates a per-model compile failure in-band (not as a rejected Promise)", async () => {
|
|
179
|
+
writeManifest(tempDir);
|
|
180
|
+
fs.writeFileSync(
|
|
181
|
+
path.join(tempDir, "broken.malloy"),
|
|
182
|
+
`source: bad is duckdb.sql("select 1 as a") extend {
|
|
183
|
+
measure: oops is THIS_FUNC_DOES_NOT_EXIST(a)
|
|
184
|
+
}`,
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
188
|
+
try {
|
|
189
|
+
const outcome = await pool.loadPackage({
|
|
190
|
+
packagePath: tempDir,
|
|
191
|
+
packageName: "pkg",
|
|
192
|
+
malloyConfig,
|
|
193
|
+
defaultConnectionName: "duckdb",
|
|
194
|
+
});
|
|
195
|
+
expect(outcome.models).toHaveLength(1);
|
|
196
|
+
const m = outcome.models[0];
|
|
197
|
+
expect(m.compilationError).toBeDefined();
|
|
198
|
+
expect(m.modelDef).toBeUndefined();
|
|
199
|
+
} finally {
|
|
200
|
+
await duckdb.close();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("ignores embedded csv/parquet files (database probing is a main-thread concern)", async () => {
|
|
205
|
+
// The worker is pure-CPU: it reads the manifest and compiles
|
|
206
|
+
// .malloy files only. Database probing stays on the main thread
|
|
207
|
+
// (Package.readDatabases) so the worker doesn't need to dlopen
|
|
208
|
+
// duckdb-native. Verify that dropping a .csv next to the model
|
|
209
|
+
// doesn't crash the worker and doesn't show up in the result.
|
|
210
|
+
writeManifest(tempDir);
|
|
211
|
+
fs.writeFileSync(path.join(tempDir, "rows.csv"), "a,b\n1,2\n3,4\n5,6\n");
|
|
212
|
+
fs.writeFileSync(
|
|
213
|
+
path.join(tempDir, "trivial.malloy"),
|
|
214
|
+
`source: nums is duckdb.sql("select 1 as a") extend {\n measure: total is a.sum()\n}`,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
218
|
+
try {
|
|
219
|
+
const outcome = await pool.loadPackage({
|
|
220
|
+
packagePath: tempDir,
|
|
221
|
+
packageName: "pkg",
|
|
222
|
+
malloyConfig,
|
|
223
|
+
defaultConnectionName: "duckdb",
|
|
224
|
+
});
|
|
225
|
+
expect(outcome.models).toHaveLength(1);
|
|
226
|
+
expect(outcome.models[0].compilationError).toBeUndefined();
|
|
227
|
+
} finally {
|
|
228
|
+
await duckdb.close();
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
234
|
+
// PackageLoadPool — shutdown rejects new submissions
|
|
235
|
+
// Separate describe so the shutdown doesn't poison the shared pool above.
|
|
236
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
237
|
+
|
|
238
|
+
describe("PackageLoadPool (shutdown)", () => {
|
|
239
|
+
it("rejects loadPackage() after shutdown()", async () => {
|
|
240
|
+
const { MalloyConfig } = await import("@malloydata/malloy");
|
|
241
|
+
const pool = new PackageLoadPool(1);
|
|
242
|
+
await pool.shutdown();
|
|
243
|
+
await expect(
|
|
244
|
+
pool.loadPackage({
|
|
245
|
+
packagePath: "/tmp/nowhere",
|
|
246
|
+
packageName: "nowhere",
|
|
247
|
+
malloyConfig: new MalloyConfig({ connections: {} }),
|
|
248
|
+
defaultConnectionName: "duckdb",
|
|
249
|
+
}),
|
|
250
|
+
).rejects.toThrow("shutting down");
|
|
251
|
+
});
|
|
252
|
+
});
|