Class: Droonga::Dispatcher
- Inherits:
-
Object
- Object
- Droonga::Dispatcher
show all
- Includes:
- Loggable
- Defined in:
- lib/droonga/dispatcher.rb
Defined Under Namespace
Classes: MissingDatasetParameter, SessionPlanner, UnknownDataset, UnknownType
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(engine_state, cluster, catalog) ⇒ Dispatcher
Returns a new instance of Dispatcher.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/droonga/dispatcher.rb', line 52
def initialize(engine_state, cluster, catalog)
@engine_state = engine_state
@cluster = cluster
@forwarder = @engine_state.forwarder
@replier = @engine_state.replier
@catalog = catalog
@adapter_runners = create_adapter_runners
@farm = Farm.new(@engine_state.name, @catalog, @engine_state.loop,
:engine_state => @engine_state,
:cluster => @cluster,
:dispatcher => self,
:forwarder => @forwarder,
:internal_connection_lifetime =>
@engine_state.internal_connection_lifetime)
@engine_state.wait_until_ready(@farm)
@collector_runners = create_collector_runners
@step_runners = create_step_runners
end
|
Instance Attribute Details
#cluster ⇒ Object
Returns the value of attribute cluster.
50
51
52
|
# File 'lib/droonga/dispatcher.rb', line 50
def cluster
@cluster
end
|
#engine_state ⇒ Object
Returns the value of attribute engine_state.
50
51
52
|
# File 'lib/droonga/dispatcher.rb', line 50
def engine_state
@engine_state
end
|
Instance Method Details
#direct_route?(route) ⇒ Boolean
307
308
309
310
|
# File 'lib/droonga/dispatcher.rb', line 307
def direct_route?(route)
receiver = route["to"]
not @cluster.engine_node_names.include?(receiver)
end
|
#dispatch(message, destination) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/droonga/dispatcher.rb', line 207
def dispatch(message, destination)
logger.trace("dispatch: start", :message => message, :destination => destination)
if local_route?(destination)
process_internal_message(message)
else
forward_message = @message.merge("body" => message)
forward_destination = {
"type" => "dispatcher",
"to" => destination,
}
if direct_route?(forward_destination)
@forwarder.forward(forward_message, forward_destination)
else
@cluster.forward(forward_message, forward_destination)
end
end
logger.trace("dispatch: done")
end
|
#dispatch_steps(steps) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/droonga/dispatcher.rb', line 226
def dispatch_steps(steps)
logger.trace("dispatch_steps: start", :steps => steps)
id = @engine_state.generate_id
destinations = []
timeout = nil
steps.each do |step|
calculated_timeout = timeout_from_step(step)
if calculated_timeout
timeout = calculated_timeout
end
dataset = @catalog.dataset(step["dataset"])
if dataset
if write_step?(step)
step["write"] = true
target_nodes = @cluster.writable_nodes
if target_nodes.empty?
logger.error("there is no node to dispath a write step!",
:my_role => NodeRole.mine,
:all_nodes => @cluster.engine_nodes.collect(&:to_json),
:step => step)
end
else
target_nodes = @cluster.readable_nodes
if target_nodes.empty?
logger.error("there is no node to dispath a read step!",
:my_role => NodeRole.mine,
:all_nodes => @cluster.engine_nodes.collect(&:to_json),
:step => step)
end
end
routes = dataset.compute_routes(step, target_nodes)
step["routes"] = routes.collect do |route|
internal_route(route)
end
else
step["routes"] ||= [id]
end
destinations += step["routes"].collect do |route|
internal_farm_path(route)
end
end
dispatch_message = {
"id" => id,
"steps" => steps,
"timeout" => timeout,
}
destinations.uniq.each do |destination|
dispatch(dispatch_message, destination)
end
logger.trace("dispatch_steps: done")
end
|
#forward(message, destination) ⇒ Object
127
128
129
130
131
132
133
134
135
|
# File 'lib/droonga/dispatcher.rb', line 127
def forward(message, destination)
logger.trace("forward start")
if local_route?(destination) or direct_route?(destination)
@forwarder.forward(message, destination)
else
@cluster.forward(message, destination)
end
logger.trace("forward done")
end
|
#local_route?(route) ⇒ Boolean
303
304
305
|
# File 'lib/droonga/dispatcher.rb', line 303
def local_route?(route)
@engine_state.local_route?(route)
end
|
#process_internal_message(message) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/droonga/dispatcher.rb', line 170
def process_internal_message(message)
logger.trace("process_internal_message: start", :message => message)
id = message["id"]
session = @engine_state.find_session(id)
if session
session.receive(message["input"], message["value"])
else
logger.trace("process_internal_message: no session")
steps = message["steps"]
if steps
session_planner = SessionPlanner.new(@engine_state, @cluster, steps)
dataset = message["dataset"] || @message["dataset"]
collector_runner = @collector_runners[dataset]
session = session_planner.create_session(id, self, collector_runner)
if session.need_result?
timeout = message["timeout"] ||
@engine_state.internal_connection_lifetime
@engine_state.register_session(id, session,
:timeout => timeout)
session.start
logger.trace("process_internal_message: waiting for results")
else
session.start
session.finish
session = nil
logger.trace("process_internal_message: no need to wait for results")
end
else
logger.error("no steps error", :id => id, :message => message)
return
end
end
@engine_state.unregister_session(id) if session and session.done?
logger.trace("process_internal_message: done")
end
|
#process_local_message(local_message) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
# File 'lib/droonga/dispatcher.rb', line 283
def process_local_message(local_message)
logger.trace("process_local_message: start", :steps => local_message)
task = local_message["task"]
slice_name = task["route"]
slice_name = public_route(slice_name)
step = task["step"]
command = step["command"]
descendants = {}
step["descendants"].each do |name, routes|
descendants[name] = routes.collect do |route|
internal_farm_path(route)
end
end
local_message["descendants"] = descendants
farm_message = @message.merge("body" => local_message,
"type" => command)
@farm.process(slice_name, farm_message)
logger.trace("process_local_message: done")
end
|
#process_message(message) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/droonga/dispatcher.rb', line 105
def process_message(message)
logger.trace("process_message: start", :message => message)
@message = message
if message["type"] == "dispatcher"
process_internal_message(message["body"])
else
begin
assert_valid_message(message)
process_input_message(message)
rescue ErrorMessage => error
reply("statusCode" => error.status_code,
"body" => error.response_body)
rescue StandardError, LoadError, SyntaxError => error
logger.exception("failed to process input message", error)
formatted_error = ErrorMessages::InternalServerError.new("Unknown internal error")
reply("statusCode" => formatted_error.status_code,
"body" => formatted_error.response_body)
end
end
logger.trace("process_message: done")
end
|
#refresh_node_reference ⇒ Object
101
102
103
|
# File 'lib/droonga/dispatcher.rb', line 101
def refresh_node_reference
@farm.refresh_node_reference
end
|
#reply(message) ⇒ void
This method returns an undefined value.
Replies response to replyTo.
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/droonga/dispatcher.rb', line 149
def reply(message)
adapted_message = @message.merge(message)
adapter_runner = @adapter_runners[adapted_message["dataset"]]
if adapter_runner
adapted_message = adapter_runner.adapt_output(adapted_message)
end
if adapted_message["replyTo"].nil?
status_code = adapted_message["statusCode"] || 200
if status_code != 200
dataset = adapted_message["dataset"]
body = adapted_message["body"] || {}
name = body["name"] || "Unknown"
message = body["message"] || "unknown error"
logger.error("orphan error: " +
"<#{dataset}>[#{name}](#{status_code}): #{message}")
end
else
@replier.reply(adapted_message)
end
end
|
#start ⇒ Object
71
72
73
|
# File 'lib/droonga/dispatcher.rb', line 71
def start
@farm.start
end
|
#stop_gracefully(&block) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/droonga/dispatcher.rb', line 75
def stop_gracefully(&block)
logger.trace("stop_gracefully: start")
@collector_runners.each_value do |collector_runner|
collector_runner.shutdown
end
@adapter_runners.each_value do |adapter_runner|
adapter_runner.shutdown
end
@farm.stop_gracefully do
yield
logger.trace("stop_gracefully: done")
end
end
|
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/droonga/dispatcher.rb', line 89
def stop_immediately
logger.trace("stop_immediately: start")
@collector_runners.each_value do |collector_runner|
collector_runner.shutdown
end
@adapter_runners.each_value do |adapter_runner|
adapter_runner.shutdown
end
@farm.stop_immediately
logger.trace("stop_immediately: done")
end
|
#timeout_from_step(step) ⇒ Object
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/droonga/dispatcher.rb', line 324
def timeout_from_step(step)
return nil unless step["dataset"]
step_runner = @step_runners[step["dataset"]]
return nil unless step_runner
step_definition = step_runner.find(step["command"])
return nil unless step_definition
step_definition.timeout_for_step(step)
end
|
#write_step?(step) ⇒ Boolean
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/droonga/dispatcher.rb', line 312
def write_step?(step)
return false unless step["dataset"]
step_runner = @step_runners[step["dataset"]]
return false unless step_runner
step_definition = step_runner.find(step["command"])
return false unless step_definition
step_definition.write?
end
|