Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/temporal/activity/poller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'securerandom'
require 'temporal/activity/task_processor'
require 'temporal/connection'
require 'temporal/error_handler'
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/temporal/metric_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
65 changes: 64 additions & 1 deletion lib/temporal/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -44,6 +59,8 @@ def schedule(&block)
end

def shutdown
stop_metrics_reporter

size.times do
schedule { throw EXIT_SYMBOL }
end
Expand All @@ -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
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
end

def poll
Thread.current.abort_on_exception = true

Expand Down
9 changes: 9 additions & 0 deletions lib/temporal/workflow/poller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'securerandom'
require 'grpc/errors'
require 'temporal/connection'
require 'temporal/thread_pool'
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/lib/temporal/activity/poller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions spec/unit/lib/temporal/thread_pool_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'temporal/thread_pool'
require 'timeout'

describe Temporal::ThreadPool do
before do
Expand All @@ -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
Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/unit/lib/temporal/workflow/poller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading