module TrinoEngineSupervisor
Event-driven lifecycle of the engine, driven by demand (maintenance executions).
Every transition happens under a row lock (SELECT … FOR UPDATE on the single trino_engine_states row), which closes three races at once: the destroy-during-acceptance window, concurrent drains and concurrent starts.
State machine: down -> starting -> up -> draining -> stopping -> down. “stopping” is the point of no return: destruction is in progress, and demand that arrives there must wait for down and trigger a fresh start.
Constants
- DRAIN_GRACE
- ENGINE_RESET_QUEUES
-
Hard reset for a frozen engine: fails everything in flight, clears the engine + maintenance job queues, tears the Trino cluster down, and returns the engine to “down”. Used from the activity screen when the engine stops making progress and a plain restart is not enough.
Only clears engine and maintenance queues — leaving sync and default queues untouched so unrelated work continues.
- MAX_START_ATTEMPTS
- READY_TIMEOUT
Public Instance Methods
Source
# File app/services/trino_engine_supervisor.rb, line 71 def begin_stopping! current = state current.with_lock do return false unless current.status == "draining" if TrinoDemand.any? transition!(current, "up", drain_started_at: nil) release_pending! return false end transition!(current, "stopping") true end rescue ConcurrentTransitionError false ensure broadcast_deferred! end
Called by DrainEngineJob immediately before destroying. Returns false if demand appeared - and then the destruction does NOT happen.
Source
# File app/services/trino_engine_supervisor.rb, line 157 def broadcast_deferred! ActivityBroadcaster.broadcast! end
Source
# File app/services/trino_engine_supervisor.rb, line 242 def coordinator_identity TrinoProvisioner.target rescue StandardError nil end
The coordinator this session runs on, captured AT TRANSITION TIME.
The history panel previously attributed every historical session to whatever the Deployment happens to point at now, because the only coordinator label on the page was resolved live at render time. Recording it into the lifecycle event’s context needs no schema change: that hash is already persisted and already read when sessions are reconstructed.
@return [String, nil] the namespace-qualified Deployment, or nil when it cannot be resolved (never let this break a transition)
# File app/services/trino_engine_supervisor.rb, line 25 def demand_arrived!(dispatch: nil) current = state current.with_lock do case current.status when "up" dispatch&.call when "starting" # The start in progress releases everything pending when ready. nil when "draining" # Still in time: back up without destroying anything. transition!(current, "up", drain_started_at: nil) dispatch&.call when "stopping" # Destruction already in progress. Do NOT dispatch: the engine is # dying. When it reaches down, the pending demand triggers a new start. nil else # down | failed transition!(current, "starting", start_attempts: 0, last_error: nil) MaintenanceOrchestrator.supervise_engine_start end end rescue ConcurrentTransitionError nil ensure broadcast_deferred! end
Single entry point for ANY demand source. Callers go through the same method - there is no parallel path that could diverge.
Source
# File app/services/trino_engine_supervisor.rb, line 54 def demand_finished! current = state current.with_lock do next if TrinoDemand.any? next unless current.status == "up" transition!(current, "draining", drain_started_at: Time.current) MaintenanceOrchestrator.drain_engine end rescue ConcurrentTransitionError nil ensure broadcast_deferred! end
Marks the engine draining when demand ends while it is up.
Source
# File app/services/trino_engine_supervisor.rb, line 163 def fail_in_flight! ExecutionHistory.where(status: %w[pending running]).find_each do |execution| TableLock.release(execution) execution.update!(status: :failed, current_step: "start", finished_at: Time.current, error_message: "engine hard reset") execution.execution_steps.where(status: "pending").update_all( status: "blocked", skip_reason: "engine hard reset", updated_at: Time.current ) end end
Frees every execution currently in flight, releasing their table locks so nothing is left dangling after a hard reset.
Source
# File app/services/trino_engine_supervisor.rb, line 92 def finish_stopping! current = state current.with_lock do raise ConcurrentTransitionError, "finish_stopping! called in status #{current.status}" unless current.status == "stopping" transition!(current, "down", drain_started_at: nil) # Demand that arrived during stopping: start again now. if TrinoDemand.any? transition!(current, "starting", start_attempts: 0) MaintenanceOrchestrator.supervise_engine_start end end rescue ConcurrentTransitionError nil ensure broadcast_deferred! end
Called after the destruction finished.
Source
# File app/services/trino_engine_supervisor.rb, line 137 def hard_reset! state.with_lock do fail_in_flight! begin SolidQueue::Job.where(finished_at: nil, queue_name: ENGINE_RESET_QUEUES).delete_all rescue StandardError nil # queue DB may not be provisioned in dev/test end unless state.status == "down" transition!(state, "down", start_attempts: 0, last_error: nil, drain_started_at: nil) end end rescue ConcurrentTransitionError nil ensure TrinoProvisioner.destroy! rescue nil TrinoProvisioner.wait_gone!(timeout: 2.minutes) rescue nil broadcast_deferred! end
# File app/services/trino_engine_supervisor.rb, line 221 def record_lifecycle!(record, status) ErrorEvent.record( catalog: nil, schema: "engine", operation: "engine-lifecycle", source_system: "engine", severity: "info", status: "resolved", message: "engine #{status} (generation #{record.generation})", context: { attempts: record.start_attempts, generation: record.generation, state: status, coordinator: coordinator_identity } ) end
Records the transition as an info-level engine event so the activity screen can show when the cluster went up, down, draining, etc. Genuine failures are recorded separately with severity “error” by the jobs that detect them.
These are informational markers, not actionable errors, so they are recorded already resolved - otherwise every start/stop would pile up as an “open error” on the error surface.
The generation is part of the message: ErrorEvent dedupes by message, and the timeline needs every up/down to be its own event rather than one counter.
@param record [TrinoEngineState] the state row after the transition @param status [String] the new status
Source
# File app/services/trino_engine_supervisor.rb, line 175 def release_pending! ExecutionHistory.where(status: "pending").find_each do |execution| MaintenanceOrchestrator.start_execution_on_engine(execution.id) end end
Releases everything that was waiting for the engine.
Source
# File app/services/trino_engine_supervisor.rb, line 112 def restart! current = state current.with_lock do if current.status == "starting" || current.status == "stopping" return end transition!(current, "starting", start_attempts: 0, last_error: nil) MaintenanceOrchestrator.supervise_engine_start end rescue ConcurrentTransitionError nil ensure broadcast_deferred! end
Operator-forced restart from the activity screen.
Source
# File app/services/trino_engine_supervisor.rb, line 21 def state = TrinoEngineState.instance(status: "down", status_changed_at: Time.current)
Returns the single engine-state row, creating it on first use.
@return [TrinoEngineState] the state row
# File app/services/trino_engine_supervisor.rb, line 187 def transition!(record, status, **extra) new_gen = record.generation + 1 # The generation predicate is what makes this a compare-and-set. Without it # the UPDATE always matched its own id, `updated` was always 1, and the # ConcurrentTransitionError below was unreachable - the row lock in the # callers was carrying the whole guarantee, and any caller that transitions # from a stale record (the watchdog re-reads state outside the lock) would # have overwritten a concurrent transition silently. updated = TrinoEngineState.where(id: record.id, generation: record.generation) .update_all(status: status, status_changed_at: Time.current, generation: new_gen, **extra) unless updated == 1 raise ConcurrentTransitionError, "CAS lost: expected generation #{record.generation}, status #{record.status}, target #{status}" end record.reload record_lifecycle!(record, status) end
Persists a state transition, records it as an engine lifecycle event, and broadcasts the new engine activity.
@param record [TrinoEngineState] the state row @param status [String] the target status @param extra [Hash] additional attributes to write