Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Static viz node renderer (#77419)

Alexander Polyankin6eff4789f0e5
diff --git a/Dockerfile b/Dockerfile
index 02f0ac26170..a791686b026 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -49,7 +49,7 @@ ENV FC_LANG=en-US LC_CTYPE=en_US.UTF-8
COPY bin/docker/DigiCertGlobalRootG2.crt.pem /app/certs/DigiCertGlobalRootG2.crt.pem
# dependencies
-RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew font-noto-cjk java-cacerts && \
+RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew font-noto-cjk java-cacerts nodejs && \
apk upgrade && \
rm -rf /var/cache/apk/* && \
mkdir -p /app/certs && \
diff --git a/bin/docker/Dockerfile b/bin/docker/Dockerfile
index e5a8483116d..f6dd3296219 100644
--- a/bin/docker/Dockerfile
+++ b/bin/docker/Dockerfile
@@ -9,7 +9,7 @@ ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA}
COPY DigiCertGlobalRootG2.crt.pem /app/certs/DigiCertGlobalRootG2.crt.pem
# dependencies
-RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew font-noto-cjk font-noto-thai java-cacerts && \
+RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew font-noto-cjk font-noto-thai java-cacerts nodejs && \
apk upgrade && \
rm -rf /var/cache/apk/* && \
mkdir -p /app/certs && \
diff --git a/bin/docker/Dockerfile_ubuntu b/bin/docker/Dockerfile_ubuntu
index c013bb51dfd..3fa3cae342b 100644
--- a/bin/docker/Dockerfile_ubuntu
+++ b/bin/docker/Dockerfile_ubuntu
@@ -11,7 +11,7 @@ COPY DigiCertGlobalRootG2.crt.pem /usr/local/share/ca-certificates/DigiCertGloba
# Dependencies
RUN apt-get update && \
apt-get upgrade -y && \
- apt-get install -y ca-certificates ca-certificates-java fonts-noto && \
+ apt-get install -y ca-certificates ca-certificates-java fonts-noto nodejs && \
apt-get clean && \
curl https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -o /usr/local/share/ca-certificates/rds-combined-ca-bundle.pem && \
update-ca-certificates && \
diff --git a/dev/src/dev/readme.md b/dev/src/dev/readme.md
index d860100950a..161de30bda3 100644
--- a/dev/src/dev/readme.md
+++ b/dev/src/dev/readme.md
@@ -6,7 +6,7 @@ In this namespace, you can run `(help)` to get a bit of help. It's principle usa
### NOTE!
-You must build the js bundle used to create the svgs with visx. Run `bun run build-static-viz` to ensure that this bundle is created and the file `resources/frontend_client/app/dist/lib-static-viz.bundle.js` exists
+You must build the js bundle used to create the svgs with visx. Run `bun run build-static-viz` to ensure that this bundle is created and the file `resources/frontend_client/app/dist/app-static-viz.bundle.js` exists
Example usage below:
diff --git a/frontend/lint/module-boundaries.mjs b/frontend/lint/module-boundaries.mjs
index b0196aff22d..8916cabfe4a 100644
--- a/frontend/lint/module-boundaries.mjs
+++ b/frontend/lint/module-boundaries.mjs
@@ -180,6 +180,7 @@ const elements = [
"frontend/src/metabase/app-embed-mcp-public-path.unit.spec.ts",
"frontend/src/metabase/app-public.ts",
"frontend/src/metabase/app-static-viz.ts",
+ "frontend/src/metabase/app-static-viz-cli.ts",
"frontend/src/metabase/AppComponent.tsx",
"frontend/src/metabase/App.styled.tsx",
"frontend/src/metabase/AppKBarProvider.tsx",
diff --git a/frontend/src/metabase/app-static-viz-cli.ts b/frontend/src/metabase/app-static-viz-cli.ts
new file mode 100644
index 00000000000..d447ea188ca
--- /dev/null
+++ b/frontend/src/metabase/app-static-viz-cli.ts
@@ -0,0 +1,60 @@
+// Entry point of the self-contained Node bundle (`app-static-viz-cli.bundle.js`) run by the `:node`
+// static-viz renderer (see `metabase.channel.render.js.node`). It answers newline-delimited JSON render
+// requests over stdin/stdout.
+//
+// ESM imports are hoisted above statements but execute in import order, so the console redirect lives in
+// its own dependency-free module imported first: it must run *before* the viz code loads — stdout carries
+// the line protocol, so anything the bundle logs during init has to go to stderr instead.
+import "metabase/static-viz/console";
+import "metabase/static-viz/polyfill";
+
+import readline from "readline";
+
+import {
+ type CellBackgroundColorsInput,
+ type RenderChartInput,
+ getCellBackgroundColors,
+ renderChart,
+} from "metabase/static-viz";
+
+type RenderChartData = {
+ fn: "renderChart";
+ arg: RenderChartInput;
+};
+
+type GetCellBackgroundColorsData = {
+ fn: "getCellBackgroundColors";
+ arg: CellBackgroundColorsInput;
+};
+
+type LineData = RenderChartData | GetCellBackgroundColorsData;
+
+const rl = readline.createInterface({ input: process.stdin });
+rl.on("line", (line: string) => {
+ const lineClean = line.trim();
+ if (!lineClean) {
+ return;
+ }
+ let out;
+ try {
+ const lineData: LineData = JSON.parse(lineClean);
+ const { fn, arg } = lineData;
+ if (fn === "renderChart") {
+ out = JSON.stringify({ ok: true, result: renderChart(arg) });
+ } else if (fn === "getCellBackgroundColors") {
+ out = JSON.stringify({ ok: true, result: getCellBackgroundColors(arg) });
+ } else {
+ throw new Error(`unknown function: ${fn}`);
+ }
+ } catch (e) {
+ out = JSON.stringify({
+ ok: false,
+ error: String(e instanceof Error ? (e.stack ?? e) : e),
+ });
+ }
+ process.stdout.write(out + "\n");
+});
+
+rl.on("close", () => process.exit(0));
+
+process.stdout.write(JSON.stringify({ ready: true }) + "\n");
diff --git a/frontend/src/metabase/app-static-viz.ts b/frontend/src/metabase/app-static-viz.ts
index 339ffbc7ea4..78db8279741 100644
--- a/frontend/src/metabase/app-static-viz.ts
+++ b/frontend/src/metabase/app-static-viz.ts
@@ -1,14 +1,11 @@
import "metabase/static-viz/polyfill";
-import {
- getCellBackgroundColors as getCellBackgroundColorsImpl,
- renderChart as renderChartImpl,
-} from "metabase/static-viz";
+import { getCellBackgroundColors, renderChart } from "metabase/static-viz";
-export function renderChart(inputJSON: string): string {
- return JSON.stringify(renderChartImpl(JSON.parse(inputJSON)));
+export function renderChartJSON(inputJSON: string): string {
+ return JSON.stringify(renderChart(JSON.parse(inputJSON)));
}
-export function getCellBackgroundColors(inputJSON: string): string {
- return JSON.stringify(getCellBackgroundColorsImpl(JSON.parse(inputJSON)));
+export function getCellBackgroundColorsJSON(inputJSON: string): string {
+ return JSON.stringify(getCellBackgroundColors(JSON.parse(inputJSON)));
}
diff --git a/frontend/src/metabase/static-viz/console.ts b/frontend/src/metabase/static-viz/console.ts
new file mode 100644
index 00000000000..af3b5db1a6a
--- /dev/null
+++ b/frontend/src/metabase/static-viz/console.ts
@@ -0,0 +1,17 @@
+// Imported FIRST by the node CLI entry (`app-static-viz-cli.ts`): stdout carries the render line
+// protocol, so console output must be routed to stderr before any static-viz module gets a chance to
+// log during initialization. This module has no imports of its own, so its body is guaranteed to run
+// before every other module in the bundle (ESM imports are hoisted, but execute in import order).
+/* eslint-disable no-console */
+
+for (const method of [
+ "log",
+ "info",
+ "warn",
+ "error",
+ "debug",
+ "trace",
+] as const) {
+ console[method] = (...args: unknown[]) =>
+ process.stderr.write(args.map((a) => String(a)).join(" ") + "\n");
+}
diff --git a/frontend/src/metabase/static-viz/lib/svg.ts b/frontend/src/metabase/static-viz/lib/svg.ts
index 55ba82d0aa2..93ad3522758 100644
--- a/frontend/src/metabase/static-viz/lib/svg.ts
+++ b/frontend/src/metabase/static-viz/lib/svg.ts
@@ -31,7 +31,8 @@ const transformSvgForOutline = (svgString: string) => {
export function patchDominantBaseline(svgString: string) {
return svgString.replace(
/<text\b[^>]*\bdominant-baseline="central"[^>]*>/g,
- (tag) => (/\bdy=/.test(tag) ? tag : tag.replace(/>$/, ' dy="0.5em">')),
+ (tag) =>
+ /\bdy=/.test(tag) ? tag : tag.replace(/(\/?)>$/, ' dy="0.5em"$1>'),
);
}
diff --git a/rspack.static-viz.config.js b/rspack.static-viz.config.js
index 74b09fc6d1c..0bd5da16c07 100644
--- a/rspack.static-viz.config.js
+++ b/rspack.static-viz.config.js
@@ -20,8 +20,94 @@ const ENTERPRISE_SRC_PATH =
const devMode = WEBPACK_BUNDLE !== "production";
+const moduleConfig = {
+ rules: [
+ {
+ test: /\.css$/i,
+ use: "null-loader",
+ },
+ {
+ test: /\.(tsx?|jsx?)$/,
+ exclude: /node_modules|cljs|css\/core\/fonts\.styled\.ts/,
+ use: [
+ {
+ loader: "builtin:swc-loader",
+ options: {
+ jsc: {
+ loose: true,
+ transform: {
+ react: {
+ runtime: "automatic",
+ refresh: false,
+ },
+ },
+ parser: {
+ syntax: "typescript",
+ tsx: true,
+ },
+ experimental: {
+ plugins: [["@swc/plugin-emotion", { sourceMap: devMode }]],
+ },
+ },
+
+ sourceMaps: false,
+ minify: true,
+ env: {
+ targets: ["defaults"],
+ },
+ },
+ },
+ ],
+ },
+ {
+ test: /\.svg/,
+ type: "asset/source",
+ resourceQuery: /source/, // *.svg?source
+ },
+ {
+ test: /\.svg$/i,
+ issuer: /\.[jt]sx?$/,
+ resourceQuery: /component/, // *.svg?component
+ use: [
+ {
+ loader: "@svgr/webpack",
+ options: {
+ ref: true,
+ svgoConfig: SVGO_CONFIG,
+ },
+ },
+ ],
+ },
+ {
+ test: /\.svg$/,
+ type: "asset/resource",
+ resourceQuery: { not: [/component|source/] },
+ },
+ ],
+};
+
+const resolveAlias = {
+ assets: ASSETS_PATH,
+ metabase: SRC_PATH,
+ "metabase-enterprise": ENTERPRISE_SRC_PATH,
+ cljs: devMode ? CLJS_SRC_PATH_DEV : CLJS_SRC_PATH,
+ "metabase-lib": LIB_SRC_PATH,
+ "metabase-types": TYPES_SRC_PATH,
+ embedding: EMBEDDING_SRC_PATH,
+ "embedding-sdk-bundle": SDK_BUNDLE_SRC_PATH,
+ "embedding-sdk-shared": SDK_SHARED_SRC_PATH,
+ "process/browser": require.resolve("process/browser"),
+ "ee-overrides":
+ process.env.MB_EDITION === "ee"
+ ? ENTERPRISE_SRC_PATH + "/static-viz-overrides"
+ : SRC_PATH + "/utils/noop",
+};
+
+const resolveExtensions = [".web.js", ".js", ".jsx", ".ts", ".tsx"];
+
module.exports = (env) => {
- return {
+ const graalConfig = {
+ name: "graal",
mode: "production",
context: SRC_PATH,
@@ -30,7 +116,7 @@ module.exports = (env) => {
},
entry: {
- "lib-static-viz": {
+ "app-static-viz": {
import: "./app-static-viz.ts",
library: {
name: "MetabaseStaticViz",
@@ -46,89 +132,10 @@ module.exports = (env) => {
globalObject: "globalThis",
},
- module: {
- rules: [
- {
- test: /\.css$/i,
- use: "null-loader",
- },
- {
- test: /\.(tsx?|jsx?)$/,
- exclude: /node_modules|cljs|css\/core\/fonts\.styled\.ts/,
- use: [
- {
- loader: "builtin:swc-loader",
- options: {
- jsc: {
- loose: true,
- transform: {
- react: {
- runtime: "automatic",
- refresh: false,
- },
- },
- parser: {
- syntax: "typescript",
- tsx: true,
- },
- experimental: {
- plugins: [["@swc/plugin-emotion", { sourceMap: devMode }]],
- },
- },
-
- sourceMaps: false,
- minify: true,
- env: {
- targets: ["defaults"],
- },
- },
- },
- ],
- },
- {
- test: /\.svg/,
- type: "asset/source",
- resourceQuery: /source/, // *.svg?source
- },
- {
- test: /\.svg$/i,
- issuer: /\.[jt]sx?$/,
- resourceQuery: /component/, // *.svg?component
- use: [
- {
- loader: "@svgr/webpack",
- options: {
- ref: true,
- svgoConfig: SVGO_CONFIG,
- },
- },
- ],
- },
- {
- test: /\.svg$/,
- type: "asset/resource",
- resourceQuery: { not: [/component|source/] },
- },
- ],
- },
+ module: moduleConfig,
resolve: {
- extensions: [".web.js", ".js", ".jsx", ".ts", ".tsx"],
- alias: {
- assets: ASSETS_PATH,
- metabase: SRC_PATH,
- "metabase-enterprise": ENTERPRISE_SRC_PATH,
- cljs: devMode ? CLJS_SRC_PATH_DEV : CLJS_SRC_PATH,
- "metabase-lib": LIB_SRC_PATH,
- "metabase-types": TYPES_SRC_PATH,
- embedding: EMBEDDING_SRC_PATH,
- "embedding-sdk-bundle": SDK_BUNDLE_SRC_PATH,
- "embedding-sdk-shared": SDK_SHARED_SRC_PATH,
- "process/browser": require.resolve("process/browser"),
- "ee-overrides":
- process.env.MB_EDITION === "ee"
- ? ENTERPRISE_SRC_PATH + "/static-viz-overrides"
- : SRC_PATH + "/utils/noop",
- },
+ extensions: resolveExtensions,
+ alias: resolveAlias,
fallback: {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
@@ -172,9 +179,46 @@ module.exports = (env) => {
)
.map((module) =>
module.nameForCondition.replace(`${__dirname}/`, ""),
- ),
+ )
+ .concat(["frontend/src/metabase/app-static-viz-cli.ts"]),
}),
}),
],
};
+
+ const nodeCliConfig = {
+ name: "node-cli",
+ mode: "production",
+ context: SRC_PATH,
+ target: "node",
+
+ performance: {
+ hints: false,
+ },
+
+ entry: {
+ "app-static-viz-cli": "./app-static-viz-cli.ts",
+ },
+
+ output: {
+ path: BUILD_PATH + "/app/dist",
+ filename: "[name].bundle.js",
+ },
+
+ module: moduleConfig,
+ resolve: {
+ extensions: resolveExtensions,
+ alias: resolveAlias,
+ },
+ optimization: {
+ minimize: true,
+ },
+ plugins: [
+ new rspack.EnvironmentPlugin({
+ IS_EMBEDDING_SDK_BUILD: false,
+ }),
+ ],
+ };
+
+ return [graalConfig, nodeCliConfig];
};
diff --git a/src/metabase/channel/render/js/common.clj b/src/metabase/channel/render/js/common.clj
new file mode 100644
index 00000000000..96ecf96f5b4
--- /dev/null
+++ b/src/metabase/channel/render/js/common.clj
@@ -0,0 +1,46 @@
+(ns metabase.channel.render.js.common
+ "Shared helpers for the pooled static-viz renderers ([[metabase.channel.render.js.graal]] and
+ [[metabase.channel.render.js.node]]): the graal bundle's classpath path, the test-init guard, and the
+ dirigiste worker-pool factory they both use."
+ (:require
+ [metabase.config.core :as config])
+ (:import
+ (io.aleph.dirigiste IPool$Generator Pool Pools)
+ (java.util.concurrent TimeUnit)))
+
+(set! *warn-on-reflection* true)
+
+(def bundle-resource-path
+ "Classpath path of the built static-viz bundle the graal renderer evaluates in-process. (The node
+ renderer runs its own self-contained bundle from disk instead — see
+ [[metabase.channel.render.js.node]].)"
+ "frontend_client/app/dist/app-static-viz.bundle.js")
+
+(defn assert-tests-not-initializing!
+ "Guard against loading the static-viz bundle as a side effect of loading namespaces: it might not have
+ been built yet. If it hasn't, we want a meaningful error (see the fixture in
+ [[metabase.channel.render.js.svg-test]]) rather than a meaningless failure at test-runner startup."
+ []
+ (when config/tests-available?
+ ((requiring-resolve 'mb.hawk.init/assert-tests-are-not-initializing) "(mt/id ...) or (data/id ...)")))
+
+(defn create-pool
+ "Build a dirigiste `Pool` of static-viz workers, each held exclusively per render (so at most `:max-size`
+ renders run at once). `:max-size` is the maximum number of concurrent workers. The utilization
+ controller targets 100% utilization with a min of 0, so when nothing is rendering the pool shrinks to 0
+ and `destroy` is called on each idle worker; it rechecks every 1 minute, so a worker lingers up to 1
+ minute before being reaped (keeping it warm through gaps between renders). `(generate)` mints a worker;
+ `(destroy worker)` tears one down. The other constructor args (queue size, sampling interval) don't
+ matter much."
+ ^Pool [generate destroy {:keys [max-size]}]
+ (let [max-queued-acquires 65000
+ sample-period-ms (.toMillis TimeUnit/MILLISECONDS 25)
+ control-period-ms (.toMillis TimeUnit/MINUTES 1)]
+ (Pool. (reify IPool$Generator
+ (generate [_ _] (generate))
+ (destroy [_ _ worker] (destroy worker)))
+ (Pools/utilizationController 1.0 max-size max-size)
+ max-queued-acquires
+ sample-period-ms
+ control-period-ms
+ TimeUnit/MILLISECONDS)))
diff --git a/src/metabase/channel/render/js/graal.clj b/src/metabase/channel/render/js/graal.clj
index a3ad14e1468..d3efb9abea6 100644
--- a/src/metabase/channel/render/js/graal.clj
+++ b/src/metabase/channel/render/js/graal.clj
@@ -15,14 +15,14 @@
neither context nor engine on GC). The first render after an idle gap rebuilds them."
(:require
[clojure.java.io :as io]
+ [metabase.channel.render.js.common :as common]
[metabase.channel.render.js.protocol :as js.protocol]
[metabase.config.core :as config]
[metabase.util.i18n :refer [trs]]
[metabase.util.json :as json]
[metabase.util.malli :as mu])
(:import
- (io.aleph.dirigiste IPool$Generator Pool Pools)
- (java.util.concurrent TimeUnit)
+ (io.aleph.dirigiste Pool)
(org.graalvm.polyglot Context Engine HostAccess Source Value)))
(set! *warn-on-reflection* true)
@@ -93,17 +93,6 @@
;;; ---------------------------------------- shared engine + contexts -------------------------------------
-(def ^:private bundle-resource-path "frontend_client/app/dist/lib-static-viz.bundle.js")
-
-(defn- assert-tests-not-initializing!
- "Guard against loading the static-viz bundle as a side effect of loading namespaces: it might not have
- been built yet. If it hasn't, we want a meaningful error (see the fixture in
- [[metabase.channel.render.js.svg-test]]) rather than a meaningless compilation error at test-runner
- startup."
- []
- (when config/tests-available?
- ((requiring-resolve 'mb.hawk.init/assert-tests-are-not-initializing) "(mt/id ...) or (data/id ...)")))
-
(def ^:private engine-lock (Object.))
(def ^:private shared-engine
@@ -119,7 +108,7 @@
[]
(locking engine-lock
(let [state (or @shared-engine
- {:source (build-source bundle-resource-path)
+ {:source (build-source common/bundle-resource-path)
:engine (create-engine)
:refs 0})]
(reset! shared-engine (update state :refs inc))
@@ -139,7 +128,7 @@
"Build a context on the shared engine and evaluate the bundle into it (creating the engine + parsing the
bundle if this is the first context)."
^Context []
- (assert-tests-not-initializing!)
+ (common/assert-tests-not-initializing!)
(let [{:keys [^Engine engine ^Source source]} (acquire-engine!)]
(try
(doto (create-context engine)
@@ -161,29 +150,11 @@
:static-viz)
(def ^:private ^Pool static-viz-context-pool
- "A pool of up to two static-viz contexts (change the controller's max to run 1 or 3), each held
- exclusively from acquire to release, so at most two renders run at once — one per context, on the
- shared engine.
-
- The utilization controller targets 100% utilization with a max of 2 and a min of 0, so when nothing is
- rendering it shrinks to 0 and the generator's `destroy` closes the context (and, on the last one, the
- shared engine). It rechecks once a minute, so an idle context lingers up to ~1 minute before being
- reaped (keeping it warm through short gaps between renders). The other constructor args (queue size,
- sampling interval) don't matter much."
- (let [max-pool-size 2
- max-queued-acquires 65000
- sample-period-ms (.toMillis TimeUnit/MILLISECONDS 25)
- control-period-ms (.toMillis TimeUnit/MINUTES 1)]
- (Pool. (reify IPool$Generator
- (generate [_ _]
- (generate-context!))
- (destroy [_ _ context]
- (destroy-context! context)))
- (Pools/utilizationController 1.0 max-pool-size max-pool-size)
- max-queued-acquires
- sample-period-ms
- control-period-ms
- TimeUnit/MILLISECONDS)))
+ "A pool of up to two static-viz contexts, each held exclusively from acquire to release, so at most two
+ renders run at once — one per context, on the shared engine. When idle the pool shrinks to 0 and the
+ generator's `destroy` closes the context (and, on the last one, the shared engine); the first render
+ after an idle gap rebuilds them. See [[metabase.channel.render.js.common/create-pool]]."
+ (common/create-pool generate-context! destroy-context! {:max-size 2}))
(defn- do-with-static-viz-context
"Borrow a pooled static-viz context and call `f` with it, held exclusively for the call (never let it —
@@ -216,6 +187,6 @@
[]
(reify js.protocol/StaticVizRenderer
(chart [_ input]
- (json/decode+kw (call-js "renderChart" [(json/encode input)])))
+ (json/decode+kw (call-js "renderChartJSON" [(json/encode input)])))
(cell-background-colors [_ input]
- (json/decode (call-js "getCellBackgroundColors" [(json/encode input)])))))
+ (json/decode (call-js "getCellBackgroundColorsJSON" [(json/encode input)])))))
diff --git a/src/metabase/channel/render/js/node.clj b/src/metabase/channel/render/js/node.clj
new file mode 100644
index 00000000000..29ed97422d9
--- /dev/null
+++ b/src/metabase/channel/render/js/node.clj
@@ -0,0 +1,182 @@
+(ns metabase.channel.render.js.node
+ "The `:node` [[metabase.channel.render.js.protocol/StaticVizRenderer]]: runs the static-viz JS in a pool
+ of external Node.js child processes rather than in-process on GraalVM. Each process runs the
+ self-contained node entrypoint (`app-static-viz-cli.bundle.js`, built by `rspack.static-viz.config.js`),
+ which answers newline-delimited JSON render requests over stdin/stdout.
+
+ The script is written to a fresh temp file from the classpath at every process spawn, executed
+ immediately, and deleted when the process is destroyed. So what `node` executes always comes straight
+ out of the jar — there is no long-lived on-disk copy to drift out of sync or be tampered with, and no
+ deploy-time provisioning.
+
+ The pool holds a single process, held exclusively per render (so renders serialize onto it); with a min
+ of 0 it shrinks to 0 when idle, killing the process after 1 minute with no work. We cap it at one
+ process — spawning a fresh `node` (runtime init + bundle load) is costly, and a warm process renders
+ fast enough that queuing a second render behind it beats paying that startup cost for concurrency.
+
+ Requires a `node` binary on the host's PATH."
+ (:require
+ [clojure.java.io :as io]
+ [metabase.channel.render.js.common :as common]
+ [metabase.channel.render.js.protocol :as js.protocol]
+ [metabase.util.json :as json])
+ (:import
+ (io.aleph.dirigiste Pool)
+ (java.io BufferedReader BufferedWriter File IOException InputStreamReader OutputStreamWriter)
+ (java.lang ProcessBuilder$Redirect)
+ (java.nio.charset StandardCharsets)
+ (java.nio.file Files)
+ (java.nio.file.attribute FileAttribute)))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------------ process ----------------------------------------------
+
+(def ^:private render-timeout-ms
+ "How long to wait for a render process to answer before assuming it has wedged: we kill it and fail the
+ render, so a stuck process can't hold its pool slot forever."
+ (* 60 1000))
+
+(defrecord ^:private NodeProcess [^Process process ^BufferedWriter writer ^BufferedReader reader ^File script-file])
+
+(defn- read-response-line
+ "Read one line from the process, killing it and throwing if it doesn't answer within
+ [[render-timeout-ms]] (so a wedged process can't hold its pool slot forever). `destroyForcibly`
+ (SIGKILL) rather than the polite `destroy`: a process wedged hard enough to miss the deadline may
+ ignore SIGTERM, and killing it also closes its stdout, which unblocks the reader future."
+ ^String [^BufferedReader reader ^Process process]
+ (let [fut (future (.readLine reader))
+ line (deref fut render-timeout-ms ::timeout)]
+ (if (identical? ::timeout line)
+ (do (.destroyForcibly process)
+ (future-cancel fut)
+ (throw (ex-info "static-viz node render timed out" {})))
+ line)))
+
+(def ^:private script-resource-path
+ "Classpath path of the node entrypoint bundle written to a temp file at each process spawn."
+ "frontend_client/app/dist/app-static-viz-cli.bundle.js")
+
+(defn- create-script-file!
+ "Write the node entrypoint bundle from the classpath to a fresh temp file, to be executed immediately
+ and deleted when its process is destroyed (`deleteOnExit` is only a backstop for JVM death)."
+ ^File []
+ (let [resource (io/resource script-resource-path)]
+ (when (nil? resource)
+ (throw (ex-info (str "static-viz resource not found: " script-resource-path
+ " — build it with `bun run build-static-viz`")
+ {:resource script-resource-path})))
+ (let [file (.toFile (Files/createTempFile "mb-static-viz" ".js" (into-array FileAttribute [])))]
+ (.deleteOnExit file)
+ (with-open [in (io/input-stream resource)]
+ (io/copy in file))
+ file)))
+
+(defn- start-process!
+ "Write the script to a temp file, spawn `node` on it, and block until it reports it has loaded. The
+ process's stderr is inherited (for debugging); stdout carries the line protocol. On any failure to
+ reach a ready state the process is destroyed and the file deleted, so nothing leaks."
+ ^NodeProcess []
+ (common/assert-tests-not-initializing!)
+ (let [script (create-script-file!)
+ ^Process process (try
+ (.. (ProcessBuilder. ^"[Ljava.lang.String;" (into-array String ["node" (.getAbsolutePath script)]))
+ (redirectError ProcessBuilder$Redirect/INHERIT)
+ (start))
+ (catch Throwable t
+ (.delete script)
+ (if (instance? IOException t)
+ (throw (ex-info (str "could not start `node` for static-viz rendering — is node"
+ " installed and on PATH? (otherwise set MB_STATIC_VIZ_MODE"
+ " to graalvm)")
+ {} t))
+ (throw t))))]
+ (try
+ (let [writer (BufferedWriter. (OutputStreamWriter. (.getOutputStream process) StandardCharsets/UTF_8))
+ reader (BufferedReader. (InputStreamReader. (.getInputStream process) StandardCharsets/UTF_8))
+ ready (read-response-line reader process)]
+ (when-not (some-> ready json/decode+kw :ready)
+ (throw (ex-info "static-viz node process failed to start" {:line ready})))
+ (->NodeProcess process writer reader script))
+ (catch Throwable t
+ (.destroy process)
+ (.delete script)
+ (throw t)))))
+
+(defn- stop-process!
+ [{:keys [^Process process ^BufferedWriter writer ^BufferedReader reader ^File script-file]}]
+ (try (.close writer) (catch Exception _))
+ (try (.close reader) (catch Exception _))
+ (try (.destroy process) (catch Exception _))
+ (try (.delete script-file) (catch Exception _)))
+
+(defn- render-on-process
+ "Send one render request to `node-process` and read its response. `arg` is a Clojure data structure sent
+ as a JS object (not a JSON string), so the bundle serializes only once — at this process boundary rather
+ than again inside the bundle. Returns the render result parsed into Clojure data. Not thread-safe on its
+ own — the pool hands a process to one render at a time. A write failure (broken pipe) means the pooled
+ process had already died; it is thrown as `:retryable` so [[call-node]] can get a fresh one."
+ [{:keys [^BufferedWriter writer ^BufferedReader reader ^Process process]} fn-name arg]
+ (try
+ (.write writer ^String (str (json/encode {:fn fn-name :arg arg}) "\n"))
+ (.flush writer)
+ (catch IOException e
+ (throw (ex-info "static-viz node process is not writable" {:retryable true} e))))
+ (let [line (read-response-line reader process)]
+ (when (nil? line)
+ (throw (ex-info "static-viz node process closed unexpectedly" {:retryable true})))
+ (let [{:keys [ok result error]} (json/decode+kw line)]
+ (if ok
+ result
+ ;; :render-error — the process answered the protocol correctly, so it is healthy; only this
+ ;; render failed. [[call-node]] releases (rather than disposes) the process on these.
+ (throw (ex-info (str "static-viz node render failed: " error) {:render-error true}))))))
+
+;;; ------------------------------------------------ pool -------------------------------------------------
+
+(def ^:private pool-key
+ "Dirigiste pools are keyed; the key is arbitrary, it just has to be the same for every operation."
+ :static-viz-node)
+
+(def ^:private ^Pool node-process-pool
+ "A pool of a single static-viz Node.js process, held exclusively per render (so renders serialize onto
+ it); when idle it shrinks to 0 and the generator's `destroy` kills the process. Capped at one because a
+ warm process is fast and spawning another is costly. See
+ [[metabase.channel.render.js.common/create-pool]]."
+ (common/create-pool start-process! stop-process! {:max-size 1}))
+
+(defn- call-node
+ "Run static-viz bundle function `fn-name` with `arg` (a Clojure data structure) on a pooled Node process,
+ returning the render result parsed into Clojure data. A `:render-error` (the process answered `ok:
+ false` — it is healthy, only the render failed) releases the process back for reuse, like the graal
+ renderer does with its context; any other failure may have left the process in a bad state, so it is
+ disposed (dropped from the pool). Retries once if the acquired process turned out to be already dead
+ (e.g. reaped or killed while parked in the pool), since a fresh one should succeed."
+ [fn-name arg]
+ (loop [retries 1]
+ (let [node-process (.acquire node-process-pool pool-key)
+ outcome (try
+ (let [result (render-on-process node-process fn-name arg)]
+ (.release node-process-pool pool-key node-process)
+ {:result result})
+ (catch Throwable t
+ (if (:render-error (ex-data t))
+ (.release node-process-pool pool-key node-process)
+ (.dispose node-process-pool pool-key node-process))
+ (if (and (:retryable (ex-data t)) (pos? retries))
+ {:retry true}
+ (throw t))))]
+ (if (:retry outcome)
+ (recur (dec retries))
+ (:result outcome)))))
+
+(defn renderer
+ "The `:node` [[metabase.channel.render.js.protocol/StaticVizRenderer]] — runs the static-viz JS in a pool
+ of external Node.js processes. Passes each `input` map to the bundle's object-in/object-out functions, so
+ it is serialized once (at the process boundary) rather than JSON-encoded again inside the bundle."
+ []
+ (reify js.protocol/StaticVizRenderer
+ (chart [_ input]
+ (call-node "renderChart" input))
+ (cell-background-colors [_ input]
+ (call-node "getCellBackgroundColors" input))))
diff --git a/src/metabase/channel/render/js/renderer.clj b/src/metabase/channel/render/js/renderer.clj
index 4649718883a..655a46d8f9f 100644
--- a/src/metabase/channel/render/js/renderer.clj
+++ b/src/metabase/channel/render/js/renderer.clj
@@ -1,13 +1,19 @@
(ns metabase.channel.render.js.renderer
- "Hands callers the static-viz [[metabase.channel.render.js.protocol/StaticVizRenderer]] implementation:
- the in-process GraalVM renderer. Kept behind the protocol so an alternative backend can be slotted in
- without touching the callers ([[metabase.channel.render.js.svg]], [[metabase.channel.render.js.color]])."
+ "Hands callers the static-viz [[metabase.channel.render.js.protocol/StaticVizRenderer]] implementation
+ for the configured [[metabase.channel.settings/static-viz-mode]]: the in-process GraalVM renderer
+ (default) or a pool of external Node.js processes. Kept behind the protocol so the callers
+ ([[metabase.channel.render.js.svg]], [[metabase.channel.render.js.color]]) don't care which backend
+ runs."
(:require
- [metabase.channel.render.js.graal :as graal]))
+ [metabase.channel.render.js.graal :as graal]
+ [metabase.channel.render.js.node :as node]
+ [metabase.channel.settings :as channel.settings]))
(set! *warn-on-reflection* true)
(defn renderer
- "The static-viz renderer."
+ "The static-viz renderer for the configured mode."
[]
- (graal/renderer))
+ (if (= :node (channel.settings/static-viz-mode))
+ (node/renderer)
+ (graal/renderer)))
diff --git a/src/metabase/channel/settings.clj b/src/metabase/channel/settings.clj
index b09b0bc6943..6adbc7edd82 100644
--- a/src/metabase/channel/settings.clj
+++ b/src/metabase/channel/settings.clj
@@ -337,3 +337,17 @@
:getter (fn [] (boolean (slack-app-token)))
:export? false
:setter :none)
+
+(defsetting static-viz-mode
+ "How static visualizations (subscription/alert charts and pulse table cell colors) are rendered:
+ `graalvm` runs the JavaScript in-process on a pooled GraalVM context (default), or `node` runs it in a
+ pool of external Node.js child processes (requires a `node` binary on the host's PATH)."
+ :type :keyword
+ :visibility :internal
+ :default :graalvm
+ :export? false
+ :setter (fn [new-value]
+ (when (some? new-value)
+ (assert (#{:graalvm :node} (keyword new-value))
+ (tru "Invalid static-viz-mode! Only values of graalvm and node are allowed.")))
+ (setting/set-value-of-type! :keyword :static-viz-mode new-value)))