class ChartTrinoProvisioner
Provisions Trino through the Helm chart in Kubernetes (the production default). Health and idleness come from the coordinator itself: the engine is ephemeral, so create!/destroy! scale the Deployments 1<->0 and health is read from GET /v1/info.
The engine is either a single-node pod (coordinator also runs tasks) or a coordinator with separate workers, driven by TrinoEngineConfig. Resources and topology are applied from that config on every create!.
Constants
- TRINO_QUERY_ID
-
Trino query ids have a fixed shape: <yyyyMMdd><HHmmss><seq>_<coordinator>. Validating before interpolation prevents a crafted value (e.g. ../../v1/info) from escaping the /v1/query/ prefix into an arbitrary coordinator path (#211).
Public Class Methods
# File app/services/chart_trino_provisioner.rb, line 16 def initialize(k8s: TrinoK8sClient.new, worker_k8s: nil, transport: HttpTransport.new, base_url: ENV.fetch("TRINO_URL")) @k8s = k8s @worker_k8s = worker_k8s || TrinoK8sClient.new(client: K8sClientFactory.build_worker) @transport = transport @base_url = base_url end
Creates the provisioner with injectable k8s clients, transport and base URL.
@param k8s [TrinoK8sClient] the coordinator Kubernetes client @param worker_k8s [TrinoK8sClient] the worker Kubernetes client @param transport [HttpTransport] the HTTP transport @param base_url [String] the Trino coordinator base URL
Public Instance Methods
Source
# File app/services/chart_trino_provisioner.rb, line 113 def active_queries body = @transport.get("#{@base_url}/v1/query", headers: { "X-Trino-User" => trino_user }) (body.is_a?(Array) ? body : []).sort_by { |q| q["queryId"].to_s }.reverse end
The queries currently known to the Trino coordinator, most recent first.
Trino’s /v1/query requires an authenticated principal: without the X-Trino-User header the coordinator answers 401 and every listing (and, through it, idle?) is dead. Same header TrinoRestClient sends for query submission, so the value is consistent.
@return [Array<Hash>] the raw /v1/query entries
Source
# File app/services/chart_trino_provisioner.rb, line 148 def cancel_query(query_id) return false unless query_id.to_s.match?(TRINO_QUERY_ID) @transport.delete("#{@base_url}/v1/query/#{query_id}") true rescue StandardError false end
Cancels a running or queued query on the coordinator. Tolerates a query that already finished (returns false) without raising.
@param query_id [String] the Trino query id to cancel @return [Boolean] true when the DELETE was accepted
Source
# File app/services/chart_trino_provisioner.rb, line 27 def config TrinoEngineConfig.instance end
The active engine config (singleton).
@return [TrinoEngineConfig]
Source
# File app/services/chart_trino_provisioner.rb, line 159 def create! wrap_k8s_errors("coordinator") { apply_coordinator_spec } wrap_k8s_errors("worker") { apply_worker_spec } if config.cluster? end
Applies the engine config and scales the coordinator up (and workers, in cluster topology).
Source
# File app/services/chart_trino_provisioner.rb, line 172 def destroy! first_error = nil begin wrap_k8s_errors("coordinator") { @k8s.scale(0) } rescue TrinoProvisioner::Error => e first_error = e Rails.logger.warn("Trino coordinator scale-down failed: #{e.message}") end begin @worker_k8s.scale(0) rescue Kubeclient::ResourceNotFoundError Rails.logger.info("Trino worker Deployment absent; nothing to scale down") rescue Kubeclient::HttpError => e first_error ||= TrinoProvisioner::Error.new(k8s_message("worker", e)) Rails.logger.warn("Trino worker scale-down failed: #{k8s_message("worker", e)}") end raise first_error if first_error end
Scales both the coordinator and workers down to 0 replicas.
The worker Deployment may legitimately not exist in single topology, so a missing one is not a failure here - there is nothing to scale down.
Both scale-downs are always attempted: a coordinator failure must not leak worker pods that stay up holding their CPU/memory reservations. The first error is collected and re-raised after the workers were handled.
Source
# File app/services/chart_trino_provisioner.rb, line 34 def exists? @k8s.deployment_exists? end
Whether the Trino Deployment currently exists.
@return [Boolean] true if the Deployment is present
Source
# File app/services/chart_trino_provisioner.rb, line 52 def healthy? body = @transport.get("#{@base_url}/v1/info") return false unless body["starting"] == false return true unless config.cluster? check_cluster_nodes rescue StandardError false end
Healthy means HTTP 200 and starting:false on /v1/info. In cluster topology, also verifies that at least one worker has registered AND that every registered node has finished starting - a coordinator that is up before any worker joins must not read as healthy. Two paths are tried in order:
-
GET /v1/node on the coordinator (requires MANAGEMENT_READ; works when http-server.authentication.type includes management-read).
-
If /v1/node 404s (management endpoint not registered — the common case for plain internal clusters), fall back to querying each worker’s own /v1/info directly.
If neither path is reachable, the coordinator’s own /v1/info is already confirmed — workers are deemed healthy to avoid a destroy/recreate loop.
Source
# File app/services/chart_trino_provisioner.rb, line 97 def idle? active_queries.none? { |q| %w[RUNNING QUEUED].include?(q["state"]) } rescue StandardError # Coordinator unreachable/erroring: unknown state. Treat as busy so the # engine is never torn down while a query may still be running. false end
No queries running or queued, per GET /v1/query.
An unreachable coordinator must NOT read as idle: DrainEngineJob uses this to decide whether it is safe to tear the engine down, and “no data” from an error is indistinguishable from “nothing running” — killing a busy engine because the coordinator was briefly unreachable abandons in-flight queries.
Source
# File app/services/chart_trino_provisioner.rb, line 76 def replicas @k8s.replicas end
The coordinator’s desired replica count (the authority for the engine).
@return [Integer] the spec.replicas count
Source
# File app/services/chart_trino_provisioner.rb, line 67 def rollout_complete? return false unless @k8s.ready? config.cluster? ? @worker_k8s.ready? : true end
Whether the Deployments are fully rolled out and ready: the coordinator and, in cluster topology, the workers.
@return [Boolean] true when every required node is ready
Source
# File app/services/chart_trino_provisioner.rb, line 124 def safe_active_queries active_queries rescue StandardError => e Rails.logger.warn("active_queries unavailable: #{e.message}") [] end
Best-effort query listing for DISPLAY. Never raises: an unreachable or erroring coordinator renders as an empty list — it must not take down the Activity page. The drain decision (idle?) deliberately uses the raising active_queries so it can still fail closed.
@return [Array<Hash>] the queries, or [] when the coordinator is unreachable
Source
# File app/services/chart_trino_provisioner.rb, line 87 def target @k8s.target end
The addressed coordinator Deployment as “namespace/name”.
TrinoProvisioner delegates :target to the adapter, but no adapter defined it - so every call raised NoMethodError, which engine_target_label’s bare rescue StandardError swallowed into the literal string “engine”.
@return [String] the namespace-qualified Deployment name
Source
# File app/services/chart_trino_provisioner.rb, line 134 def trino_user ENV.fetch("TRINO_USER", "deepdiver") end
The Trino principal sent with coordinator calls, defaulting to “deepdiver”.
@return [String] the X-Trino-User value
Source
# File app/services/chart_trino_provisioner.rb, line 200 def wait_gone!(timeout:) deadline = Time.current + timeout sleep 1 until wrap_k8s_errors("coordinator") { @k8s.live_replicas }.zero? || Time.current > deadline end
Blocks until the coordinator is scaled down or the timeout elapses.
destroy! only scales to 0 (the Deployment is not deleted), so “gone” here means no replicas remain.
@param timeout [ActiveSupport::Duration] how long to wait