diff --git a/lib/temporal/activity/poller.rb b/lib/temporal/activity/poller.rb index 859fb688..5616d332 100644 --- a/lib/temporal/activity/poller.rb +++ b/lib/temporal/activity/poller.rb @@ -1,3 +1,4 @@ +require 'securerandom' require 'temporal/activity/task_processor' require 'temporal/connection' require 'temporal/error_handler' @@ -129,6 +130,14 @@ def thread_pool pool_name: 'activity_task_poller', namespace: namespace, task_queue: task_queue + }, + worker_metrics_tags: { + namespace: namespace, + task_queue: task_queue, + worker_type: 'ActivityWorker', + # Custom connection identities may be shared across processes. + # Keep each pool in a separate DogStatsD gauge context. + worker_id: "#{config.for_connection.identity.to_s.strip}:#{SecureRandom.uuid}" } ) end diff --git a/lib/temporal/metric_keys.rb b/lib/temporal/metric_keys.rb index e945f0b6..a99f2b35 100644 --- a/lib/temporal/metric_keys.rb +++ b/lib/temporal/metric_keys.rb @@ -12,5 +12,7 @@ module MetricKeys WORKFLOW_TASK_EXECUTION_FAILED = 'workflow_task.execution_failed'.freeze THREAD_POOL_AVAILABLE_THREADS = 'thread_pool.available_threads'.freeze + WORKER_TASK_SLOTS_AVAILABLE = 'worker_task_slots_available'.freeze + WORKER_TASK_SLOTS_USED = 'worker_task_slots_used'.freeze end end diff --git a/lib/temporal/thread_pool.rb b/lib/temporal/thread_pool.rb index 3febbf82..874e7e4b 100644 --- a/lib/temporal/thread_pool.rb +++ b/lib/temporal/thread_pool.rb @@ -9,11 +9,21 @@ # module Temporal class ThreadPool + DEFAULT_METRICS_REPORT_INTERVAL_SECONDS = 10 + attr_reader :size - def initialize(size, config, metrics_tags) + def initialize( + size, + config, + metrics_tags, + worker_metrics_tags: nil, + metrics_report_interval_seconds: DEFAULT_METRICS_REPORT_INTERVAL_SECONDS + ) @size = size @metrics_tags = metrics_tags + @worker_metrics_tags = worker_metrics_tags + @metrics_report_interval_seconds = metrics_report_interval_seconds @queue = Queue.new @mutex = Mutex.new @config = config @@ -22,6 +32,11 @@ def initialize(size, config, metrics_tags) @pool = Array.new(size) do |_i| Thread.new { poll } end + + # Task transitions can be shorter than a metrics aggregation interval. A + # periodic snapshot makes this a stable saturation signal and reports idle + # workers even when they have not received a task recently. + start_metrics_reporter if worker_metrics_tags end def report_metrics @@ -44,6 +59,8 @@ def schedule(&block) end def shutdown + stop_metrics_reporter + size.times do schedule { throw EXIT_SYMBOL } end @@ -55,6 +72,52 @@ def shutdown EXIT_SYMBOL = :exit + def report_worker_slot_metrics + available_threads = @mutex.synchronize { @available_threads } + + Temporal.metrics.gauge( + Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, + available_threads, + @worker_metrics_tags + ) + Temporal.metrics.gauge( + Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, + size - available_threads, + @worker_metrics_tags + ) + end + + def start_metrics_reporter + @metrics_reporter_mutex = Mutex.new + @metrics_reporter_condition = ConditionVariable.new + @metrics_reporter_shutdown = false + + report_worker_slot_metrics + @metrics_reporter_thread = Thread.new do + loop do + shutting_down = @metrics_reporter_mutex.synchronize do + unless @metrics_reporter_shutdown + @metrics_reporter_condition.wait(@metrics_reporter_mutex, @metrics_report_interval_seconds) + end + @metrics_reporter_shutdown + end + break if shutting_down + + report_worker_slot_metrics + end + end + end + + def stop_metrics_reporter + return unless @metrics_reporter_thread + + @metrics_reporter_mutex.synchronize do + @metrics_reporter_shutdown = true + @metrics_reporter_condition.signal + end + @metrics_reporter_thread.join + end + def poll Thread.current.abort_on_exception = true diff --git a/lib/temporal/workflow/poller.rb b/lib/temporal/workflow/poller.rb index 198f4502..011dda54 100644 --- a/lib/temporal/workflow/poller.rb +++ b/lib/temporal/workflow/poller.rb @@ -1,3 +1,4 @@ +require 'securerandom' require 'grpc/errors' require 'temporal/connection' require 'temporal/thread_pool' @@ -125,6 +126,14 @@ def thread_pool pool_name: 'workflow_task_poller', namespace: namespace, task_queue: task_queue + }, + worker_metrics_tags: { + namespace: namespace, + task_queue: task_queue, + worker_type: 'WorkflowWorker', + # Custom connection identities may be shared across processes. + # Keep each pool in a separate DogStatsD gauge context. + worker_id: "#{config.for_connection.identity.to_s.strip}:#{SecureRandom.uuid}" } ) end diff --git a/spec/unit/lib/temporal/activity/poller_spec.rb b/spec/unit/lib/temporal/activity/poller_spec.rb index 3e5d24c7..d0b882ce 100644 --- a/spec/unit/lib/temporal/activity/poller_spec.rb +++ b/spec/unit/lib/temporal/activity/poller_spec.rb @@ -54,6 +54,24 @@ def poll(task, times: 1) end describe '#start' do + it 'keeps metrics separate for pools with the same custom connection identity' do + config.identity = 'shared-worker' + ids = [] + allow(Temporal::ThreadPool).to receive(:new) do |*args| + ids << args.last.fetch(:worker_metrics_tags).fetch(:worker_id) + thread_pool + end + other = described_class.new(namespace, task_queue, lookup, config, middleware) + + subject.send(:thread_pool) + subject.send(:thread_pool) + other.send(:thread_pool) + + expect(ids.size).to eq(2) + expect(ids.uniq.size).to eq(2) + expect(config.for_connection.identity).to eq('shared-worker') + end + it 'measures time between polls' do # if it doesn't poll, this test will loop forever times = poll(nil, times: 2) diff --git a/spec/unit/lib/temporal/thread_pool_spec.rb b/spec/unit/lib/temporal/thread_pool_spec.rb index 5de5b03a..f31cb3c8 100644 --- a/spec/unit/lib/temporal/thread_pool_spec.rb +++ b/spec/unit/lib/temporal/thread_pool_spec.rb @@ -1,4 +1,5 @@ require 'temporal/thread_pool' +require 'timeout' describe Temporal::ThreadPool do before do @@ -9,6 +10,14 @@ let(:size) { 2 } let(:tags) { { foo: 'bar', bat: 'baz' } } let(:thread_pool) { described_class.new(size, config, tags) } + let(:worker_metrics_tags) do + { + namespace: 'test-namespace', + task_queue: 'test-task-queue', + worker_type: 'ActivityWorker', + worker_id: 'worker-1' + } + end describe '#new' do it 'executes one task on a thread and exits' do @@ -71,5 +80,80 @@ ) .at_least(:once) end + + it 'periodically reports available and used task slots for an individual worker' do + pool = described_class.new( + size, + config, + tags, + worker_metrics_tags: worker_metrics_tags, + metrics_report_interval_seconds: 0.01 + ) + + sleep 0.02 + pool.shutdown + + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, size, worker_metrics_tags) + .at_least(:twice) + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, 0, worker_metrics_tags) + .at_least(:twice) + end + + it 'shuts down promptly when the metrics thread has not started yet' do + start_threads = Queue.new + allow(Thread).to receive(:new).and_wrap_original do |original, &block| + original.call do + start_threads.pop + block.call + end + end + pool = described_class.new(size, config, tags, worker_metrics_tags: worker_metrics_tags) + allow(Thread).to receive(:new).and_call_original + + # Wait until shutdown is joining the reporter before allowing that + # reporter to run. Other threads, including Timeout's watchdog, run normally. + shutdown_thread = Thread.start { pool.shutdown } + Timeout.timeout(2) { Thread.pass until shutdown_thread.status == 'sleep' } + (size + 1).times { start_threads << true } + + expect(shutdown_thread.join(1)).to eq(shutdown_thread) + ensure + (size + 1).times { start_threads << true } + shutdown_thread&.join + end + + it 'reports task slot usage while a task is running' do + task_started = Queue.new + finish_task = Queue.new + pool = described_class.new( + size, + config, + tags, + worker_metrics_tags: worker_metrics_tags, + metrics_report_interval_seconds: 0.01 + ) + + pool.schedule do + task_started << true + finish_task.pop + end + task_started.pop + sleep 0.02 + finish_task << true + pool.shutdown + + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, size - 1, worker_metrics_tags) + .at_least(:once) + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, 1, worker_metrics_tags) + .at_least(:once) + end end end diff --git a/spec/unit/lib/temporal/workflow/poller_spec.rb b/spec/unit/lib/temporal/workflow/poller_spec.rb index 020e2e91..69f959c4 100644 --- a/spec/unit/lib/temporal/workflow/poller_spec.rb +++ b/spec/unit/lib/temporal/workflow/poller_spec.rb @@ -64,6 +64,24 @@ def poll(task, times: 1) end describe '#start' do + it 'keeps metrics separate for pools with the same custom connection identity' do + config.identity = 'shared-worker' + ids = [] + allow(Temporal::ThreadPool).to receive(:new) do |*args| + ids << args.last.fetch(:worker_metrics_tags).fetch(:worker_id) + instance_double(Temporal::ThreadPool) + end + other = described_class.new(namespace, task_queue, lookup, config, middleware, workflow_middleware) + + subject.send(:thread_pool) + subject.send(:thread_pool) + other.send(:thread_pool) + + expect(ids.size).to eq(2) + expect(ids.uniq.size).to eq(2) + expect(config.for_connection.identity).to eq('shared-worker') + end + it 'polls for workflow tasks' do subject.start times = poll(nil, times: 2)