Class: Fluent::Plugin::TailInput::WorkerPool
- Inherits:
-
Object
- Object
- Fluent::Plugin::TailInput::WorkerPool
show all
- Defined in:
- lib/fluent/plugin/in_tail/worker_pool.rb
Defined Under Namespace
Classes: Result, Task, WorkerStopped
Instance Method Summary
collapse
Constructor Details
#initialize(num_threads:, task_limit:, byte_limit:, thread_create:, notify:, &process) ⇒ WorkerPool
Returns a new instance of WorkerPool.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 29
def initialize(num_threads:, task_limit:, byte_limit:, thread_create:, notify:, &process)
unless num_threads > 0 && task_limit > 0 && byte_limit > 0
raise ArgumentError, 'worker and queue limits must be positive'
end
raise ArgumentError, 'processor is required' unless process
@num_threads = num_threads
@task_limit = task_limit
@byte_limit = byte_limit
@thread_create = thread_create
@notify = notify
@process = process
@mutex = Mutex.new
@jobs = Queue.new
@results = Queue.new
@active = {}
@bytes = 0
@regular_bytes = 0
@oversized_task = nil
@threads = []
@state = :new
@failure = nil
end
|
Instance Method Details
#acknowledge(result) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 109
def acknowledge(result)
@mutex.synchronize do
task = result.task
unless @active[task.key].equal?(task)
raise ArgumentError, 'result is no longer active'
end
@active.delete(task.key)
@bytes -= task.bytes
if task.bytes > @byte_limit
@oversized_task = nil
else
@regular_bytes -= task.bytes
end
end
end
|
#failure ⇒ Object
130
131
132
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 130
def failure
@mutex.synchronize { @failure }
end
|
#join(timeout: nil) ⇒ Object
The source joins after draining results; the event loop never joins.
142
143
144
145
146
147
148
149
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 142
def join(timeout: nil)
deadline = timeout && Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
@threads.each do |thread|
remaining = deadline && [deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max
return false unless thread.join(remaining)
end
true
end
|
#kill ⇒ Object
151
152
153
154
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 151
def kill
@threads.each(&:kill)
@threads.each(&:join)
end
|
#next_result ⇒ Object
103
104
105
106
107
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 103
def next_result
@results.pop(true)
rescue ThreadError
nil
end
|
#start ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 53
def start
@mutex.synchronize do
raise 'worker pool already started' unless @state == :new
@state = :starting
end
begin
@num_threads.times do |index|
@threads << @thread_create.call(:"in_tail_worker_#{index}") { run(index) }
end
@mutex.synchronize do
raise(@failure || WorkerStopped.new('pool stopped during start')) unless @state == :starting
@state = :running
end
self
rescue
stop
join
raise
end
end
|
#statistics ⇒ Object
126
127
128
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 126
def statistics
@mutex.synchronize { { tasks: @active.size, bytes: @bytes, state: @state }.freeze }
end
|
#stop ⇒ Object
134
135
136
137
138
139
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 134
def stop
@mutex.synchronize do
@state = :stopping unless @state == :failed
@jobs.close
end
end
|
#submit(key, payload, bytes:) ⇒ Object
Capacity remains reserved until emit and position handling acknowledge
the result, including while a parsed batch waits in the result queue.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/fluent/plugin/in_tail/worker_pool.rb', line 76
def submit(key, payload, bytes:)
raise ArgumentError, 'batch bytes must be nonnegative' if bytes < 0
@mutex.synchronize do
return false unless @state == :running
return false if @active.key?(key) || @active.size >= @task_limit
if bytes > @byte_limit
return false if @oversized_task
else
return false if @regular_bytes + bytes > @byte_limit
end
task = Task.new(key, bytes, payload).freeze
@active[key] = task
@bytes += bytes
if bytes > @byte_limit
@oversized_task = task
else
@regular_bytes += bytes
end
@jobs << task
true
end
end
|