Class: Pwrake::Master
- Inherits:
-
Object
- Object
- Pwrake::Master
- Defined in:
- lib/pwrake/master/master.rb
Instance Attribute Summary collapse
-
#current_flow ⇒ Object
readonly
current_flow = task.property.subflow.
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#task_queue ⇒ Object
readonly
Returns the value of attribute task_queue.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
- #create_fiber(channels, &blk) ⇒ Object
- #drop_host(host_info) ⇒ Object
- #ending? ⇒ Boolean
- #finish ⇒ Object
- #handle_failed_target(name) ⇒ Object
-
#initialize ⇒ Master
constructor
A new instance of Master.
- #invoke(t, args) ⇒ Object
- #kill_end(sig) ⇒ Object
- #retire(hid) ⇒ Object
- #send_task_to_idle_core ⇒ Object
- #setup_branch_handler(sub_host) ⇒ Object
- #setup_branches ⇒ Object
- #setup_fiber(t) ⇒ Object
- #setup_postprocess ⇒ Object
- #setup_postprocess0 ⇒ Object
- #setup_postprocess1 ⇒ Object
- #signal_trap(sig) ⇒ Object
- #task_end(tw, host_info) ⇒ Object
Constructor Details
#initialize ⇒ Master
Returns a new instance of Master.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pwrake/master/master.rb', line 14 def initialize @selector = NBIO::Selector.new @hostinfo_by_taskname = {} @hdl_set = [] @channel_by_hostid = {} @channels = [] @hostinfo_by_id = {} @current_flow = {} # init @option = Option.new Log.set_logger(@option) TaskWrapper.init_task_logger(@option) at_exit{TaskWrapper.close_task_logger} # moved from Option#init @option.put_log if @option['LOG_DIR'] && @option['GC_LOG_FILE'] GC::Profiler.enable end end |
Instance Attribute Details
#current_flow ⇒ Object (readonly)
current_flow = task.property.subflow
37 38 39 |
# File 'lib/pwrake/master/master.rb', line 37 def current_flow @current_flow end |
#option ⇒ Object (readonly)
Returns the value of attribute option.
35 36 37 |
# File 'lib/pwrake/master/master.rb', line 35 def option @option end |
#task_queue ⇒ Object (readonly)
Returns the value of attribute task_queue.
34 35 36 |
# File 'lib/pwrake/master/master.rb', line 34 def task_queue @task_queue end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
36 37 38 |
# File 'lib/pwrake/master/master.rb', line 36 def thread @thread end |
Instance Method Details
#create_fiber(channels, &blk) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/pwrake/master/master.rb', line 188 def create_fiber(channels,&blk) channels.each do |chan| fb = Fiber.new(&blk) fb.resume(chan) end end |
#drop_host(host_info) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/pwrake/master/master.rb', line 423 def drop_host(host_info) Log.debug "drop_host: #{host_info.name}" hid = host_info.id if @hostinfo_by_id[hid] s = "drop:#{hid}" @channel_by_hostid[hid].put_line(s) @task_queue.drop_host(host_info) @hostinfo_by_id.delete(hid) if @hostinfo_by_id.empty? if @finished Log.debug "drop_host: @finished and @hostinfo_by_id.empty?" else Log.error "drop_host: All workers retired." $stderr.puts "All workers retired." @failed = true end end end end |
#ending? ⇒ Boolean
388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/pwrake/master/master.rb', line 388 def ending? if @no_more_run || @task_queue.empty? || @hostinfo_by_id.empty? case @hostinfo_by_taskname.size when 1..2 Log.debug " @no_more_run=#{@no_more_run.inspect}" if @no_more_run Log.debug " @task_queue.empty?=#{@task_queue.empty?}" if @task_queue.empty? Log.debug " @hostinfo_by_id.empty?=#{@hostinfo_by_id.empty?}" if @hostinfo_by_id.empty? Log.debug " @hostinfo_by_taskname.keys=#{@hostinfo_by_taskname.keys.inspect}" end @hostinfo_by_taskname.empty? else false end end |
#finish ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/pwrake/master/master.rb', line 443 def finish Log.debug "Master#finish begin" @branch_setup_thread.join # continues running fibers Log.debug "Master#finish @selector.run begin" begin Timeout.timeout(30){@selector.run} rescue end Log.debug "Master#finish @selector.run end" if !@exited @exited = true Log.debug "Master#finish Handler.exit begin" @selector.clear NBIO::Handler.exit(@hdl_set) begin Timeout.timeout(30) do @selector.run @thread.join if @thread end rescue end Log.debug "Master#finish Handler.exit end" end Log.debug "Master#finish end" @failed end |
#handle_failed_target(name) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/pwrake/master/master.rb', line 403 def handle_failed_target(name) case @option['FAILED_TARGET'] # when /rename/i, NilClass dst = name+"._fail_" ::FileUtils.mv(name,dst) msg = "Rename output file '#{name}' to '#{dst}'" $stderr.puts(msg) Log.warn(msg) # when /delete/i ::FileUtils.rm(name) msg = "Delete output file '#{name}'" $stderr.puts(msg) Log.warn(msg) # when /leave/i end end |
#invoke(t, args) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/pwrake/master/master.rb', line 195 def invoke(t, args) Log.debug "Master#invoke start: #{t.class}[#{t.name}]" @failed = false t.pw_search_tasks(args) return if @running @running = true if @option['GRAPH_PARTITION'] setup_postprocess0 @branch_setup_thread.join @task_queue.deq_noaction_task do |tw| tw.preprocess tw.status = "end" @post_pool.enq(tw) end @selector.run @post_pool.finish Log.debug "@post_pool.finish" require 'pwrake/misc/mcgp' MCGP.graph_partition(@option.host_map) end setup_postprocess1 @branch_setup_thread.join [:TERM,:INT].each do |sig| Signal.trap(sig) do signal_trap(sig) end end send_task_to_idle_core if ending? @post_pool.finish # need? else setup_fiber(t) end end |
#kill_end(sig) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pwrake/master/master.rb', line 82 def kill_end(sig) # log writing failed. can't be called from trap context $stderr.puts "Exiting..." @no_more_run = true @failed = true @selector.clear NBIO::Handler.kill(@hdl_set,sig) begin Timeout.timeout(30) do @selector.run @thread.join if @thread end rescue end Kernel.exit(false) end |
#retire(hid) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/pwrake/master/master.rb', line 172 def retire(hid) host_info = @hostinfo_by_id[hid.to_i] if host_info && host_info.decrease(1) # all retired if !@exited m = "retired: host #{host_info.name}" Log.warn(m) $stderr.puts(m) drop_host(host_info) # delete from hostinfo_by_id if @hostinfo_by_id.empty? raise RuntimeError,"no worker host" end end end end |
#send_task_to_idle_core ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/pwrake/master/master.rb', line 308 def send_task_to_idle_core #Log.debug "#{self.class}#send_task_to_idle_core start" count = 0 # @idle_cores.decrease(.. @task_queue.deq_task do |tw,host_info,ncore| host_info.busy(ncore) count += 1 @hostinfo_by_taskname[tw.name] = host_info tw.preprocess if tw.has_action? hid = host_info.id s = "#{hid}:#{tw.task_id}:#{tw.name}" @channel_by_hostid[hid].put_line(s) tw.exec_host = host_info.name tw.exec_host_id = hid else tw.status = "end" task_end(tw,host_info) # @idle_cores.increase(.. @post_pool.enq(tw) end end if count == 0 && !@task_queue.empty? && @hostinfo_by_taskname.empty? m="No task was invoked while unexecuted tasks remain" Log.error m Log.error "count=#{count} @hostinfo_by_taskname.empty?=#{@hostinfo_by_taskname.empty?} @task_queue=\n"+@task_queue.inspect_q raise RuntimeError,m end #Log.debug "#{self.class}#send_task_to_idle_core end time=#{Time.now-tm}" end |
#setup_branch_handler(sub_host) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pwrake/master/master.rb', line 39 def setup_branch_handler(sub_host) ior,w0 = IO.pipe r2,iow = IO.pipe if sub_host == "localhost" && /^(n|f)/i !~ ENV['T'] @thread = Thread.new(r2,w0,@option) do |r,w,o| Rake.application.run_branch_in_thread(r,w,o) end else dir = File.absolute_path(File.dirname($PROGRAM_NAME)) cmd = "ssh -x -T -q #{sub_host} '" + "cd \"#{Dir.pwd}\";"+ "PATH=#{dir}:${PATH} exec pwrake_branch'" Log.debug("BranchCommunicator cmd=#{cmd}") spawn(cmd,:pgroup=>true,:out=>w0,:in=>r2) w0.close r2.close Marshal.dump(@option,iow) iow.flush s = ior.gets if !s or s.chomp != "pwrake_branch start" raise RuntimeError,"pwrake_branch start failed: receive #{s.inspect}" end end rd = NBIO::Reader.new(@selector,ior) wt = NBIO::Writer.new(@selector,iow) return NBIO::Handler.new(rd,wt,sub_host) end |
#setup_branches ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/pwrake/master/master.rb', line 99 def setup_branches sum_ncore = 0 @option.host_map.each do |sub_host, wk_hosts| @hdl_set << hdl = setup_branch_handler(sub_host) Fiber.new do hdl.put_line "host_list_begin" wk_hosts.each do |host_info| name = host_info.name ncore = host_info.ncore host_id = host_info.id Log.debug "connecting #{name} ncore=#{ncore} id=#{host_id}" hdl.put_line "host:#{host_id} #{name} #{ncore}" @channel_by_hostid[host_id] = hdl @hostinfo_by_id[host_id] = host_info end hdl.put_line "host_list_end" while s = hdl.get_line case s when /^ncore:done$/ break when /^ncore:(\d+):(\d+)$/ id, ncore = $1.to_i, $2.to_i Log.debug "worker_id=#{id} ncore=#{ncore}" @hostinfo_by_id[id].set_ncore(ncore) sum_ncore += ncore when /^ip:(\d+):(\S+)$/ id, ipa = $1.to_i, $2 Log.debug "worker_id=#{id} ip=#{ipa}" @hostinfo_by_id[id].set_ip(ipa) when /^exited$/ raise RuntimeError,"Unexpected branch exit" else msg = "#{hdl.host}:#{s.inspect}" raise RuntimeError,"invalid return: #{msg}" end end end.resume end @selector.run Log.info "num_cores=#{sum_ncore}" @option.total_cores = sum_ncore @hostinfo_by_id.each do |id,host| if ncore = @hostinfo_by_id[id].idle_cores Log.info "#{host.name} id=#{id} ncore=#{ncore}" else @hostinfo_by_id.delete(id) end end if @hostinfo_by_id.empty? raise RuntimeError,"no worker host" end queue_class = Pwrake.const_get(@option.queue_class) @task_queue = queue_class.new(@hostinfo_by_id) @branch_setup_thread = Thread.new do create_fiber(@hdl_set) do |hdl| while s = hdl.get_line case s when /^retire:(\d+)$/ retire($1.to_i) when /^branch_setup:done$/ break else raise RuntimeError,"branch_setup failed: s=#{s.inspect}" end end end @selector.run end end |
#setup_fiber(t) ⇒ Object
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/pwrake/master/master.rb', line 233 def setup_fiber(t) n_retry = @option["RETRY"] create_fiber(@hdl_set) do |hdl| while s = hdl.get_line Log.debug "Master:recv #{s.inspect} from branch[#{hdl.host}]" case s when /^task(\w+):(\d*):(.*)$/o status, shell_id, task_name = $1, $2.to_i, $3 tw = Rake.application[task_name].wrapper tw.shell_id = shell_id tw.status = status host_info = @hostinfo_by_taskname[task_name] if host_info.nil? m = "unknown hostid: task_name=#{task_name} s=#{s.inspect}"+ " @hostinfo_by_taskname.keys=#{@hostinfo_by_taskname.keys.inspect}" Log.error(m) $stderr.puts(m) end task_end(tw,host_info) # @idle_cores.increase(.. # check failure if tw.status == "fail" $stderr.puts %[task "#{tw.name}" failed.] if host_info continuous_fail = host_info.task_result(tw.status) Log.debug "task=#{tw.name} continuous_fail=#{continuous_fail}" if continuous_fail > n_retry && @hostinfo_by_id.size > 1 # retire this host drop_host(host_info) Log.warn("retired host:#{host_info.name} due to continuous fail") end end if tw.no_more_retry && !@failed @failed = true case @option['FAILURE_TERMINATION'] when 'kill' NBIO::Handler.kill(@hdl_set,"INT") @selector.run @no_more_run = true $stderr.puts "... Kill running tasks." when 'continue' $stderr.puts "... Continue runable tasks." else # 'wait' @no_more_run = true $stderr.puts "... Wait for running tasks." end end if tw.has_output_file? && File.exist?(tw.name) handle_failed_target(tw.name) end end # postprocess @post_pool.enq(tw) # must be after @no_more_run = true break if @finished when /^retire:(\d+)$/ retire($1.to_i) when /^exited$/o @exited = true Log.debug "receive #{s.chomp} from branch" break else Log.error "unknown result: #{s.inspect}" $stderr.puts(s) end end Log.debug "Master#invoke: fiber end" end if !ending? Log.debug "@selector.run begin" @selector.run Log.debug "@selector.run end" end @post_pool.finish Log.debug "Master#invoke: end of task=#{t.name}" end |
#setup_postprocess ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/pwrake/master/master.rb', line 338 def setup_postprocess i = 0 n = @option.max_postprocess_pool @post_pool = FiberPool.new(n) do |pool| postproc = @option.postprocess(@selector) i += 1 Log.debug "New postprocess fiber ##{i}" Fiber.new do j = i while tw = pool.deq() #Log.debug "postproc##{j} deq=#{tw.name}" tw.postprocess(postproc) pool.count_down @hostinfo_by_taskname.delete(tw.name) tw.retry_or_subsequent unless @exited break if yield(pool,j) end postproc.close #Log.debug "postproc##{j} end" end end end |
#setup_postprocess0 ⇒ Object
369 370 371 |
# File 'lib/pwrake/master/master.rb', line 369 def setup_postprocess0 setup_postprocess{false} end |
#setup_postprocess1 ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/pwrake/master/master.rb', line 373 def setup_postprocess1 setup_postprocess do |pool,j| #Log.debug " pool.empty?=#{pool.empty?}" if ending? Log.debug "postproc##{j} closing" @finished = true @selector.halt true elsif !@no_more_run send_task_to_idle_core false end end end |
#signal_trap(sig) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pwrake/master/master.rb', line 67 def signal_trap(sig) $stderr.puts "\nSignal trapped. (sig=#{sig} pid=#{Process.pid})" if Rake.application..debug $stderr.print "in master thread #{Thread.current}:\n " $stderr.puts caller.join("\n ") if @thread $stderr.print "in branch thread #{@thread}:\n " if bt = @thread.backtrace $stderr.puts bt.join("\n ") end end end kill_end(sig) end |
#task_end(tw, host_info) ⇒ Object
361 362 363 364 365 366 367 |
# File 'lib/pwrake/master/master.rb', line 361 def task_end(tw,host_info) if host_info && host_info.idle(tw.n_used_cores(host_info)) # all retired Log.warn("retired host:#{host_info.name} because all core retired") drop_host(host_info) end end |