Class: Pwrake::Master

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/master/master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMaster

Returns a new instance of Master.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pwrake/master/master.rb', line 13

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)
  # moved from Option#init
  @option.put_log
  if @option['LOG_DIR'] && @option['GC_LOG_FILE']
    GC::Profiler.enable
  end
end

Instance Attribute Details

#current_flowObject (readonly)

current_flow = task.property.subflow



35
36
37
# File 'lib/pwrake/master/master.rb', line 35

def current_flow
  @current_flow
end

#optionObject (readonly)

Returns the value of attribute option.



33
34
35
# File 'lib/pwrake/master/master.rb', line 33

def option
  @option
end

#task_queueObject (readonly)

Returns the value of attribute task_queue.



32
33
34
# File 'lib/pwrake/master/master.rb', line 32

def task_queue
  @task_queue
end

#threadObject (readonly)

Returns the value of attribute thread.



34
35
36
# File 'lib/pwrake/master/master.rb', line 34

def thread
  @thread
end

Instance Method Details

#create_fiber(channels, &blk) ⇒ Object



180
181
182
183
184
185
# File 'lib/pwrake/master/master.rb', line 180

def create_fiber(channels,&blk)
  channels.each do |chan|
    fb = Fiber.new(&blk)
    fb.resume(chan)
  end
end

#drop_host(host_info) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/pwrake/master/master.rb', line 405

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

Returns:

  • (Boolean)


376
377
378
379
380
381
382
383
# File 'lib/pwrake/master/master.rb', line 376

def ending?
  #Log.debug " @no_more_run=#{@no_more_run.inspect}"
  #Log.debug " @task_queue.empty?=#{@task_queue.empty?}"
  #Log.debug " @hostinfo_by_id.empty?=#{@hostinfo_by_id.empty?}"
  #Log.debug " @hostinfo_by_taskname.keys=#{@hostinfo_by_taskname.keys.inspect}"
  (@no_more_run || @task_queue.empty? || @hostinfo_by_id.empty?) &&
    @hostinfo_by_taskname.empty?
end

#finishObject



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/pwrake/master/master.rb', line 425

def finish
  Log.debug "Master#finish begin"
  @branch_setup_thread.join
  # continues running fibers
  Log.debug "Master#finish @selector.run begin"
  @selector.run(60)
  Log.debug "Master#finish @selector.run end"
  if !@exited
    @exited = true
    Log.debug "Master#finish Handler.exit begin"
    NBIO::Handler.exit(@hdl_set)
    @selector.run(60)
    Log.debug "Master#finish Handler.exit end"
  end
  TaskWrapper.close_task_logger
  Log.debug "Master#finish end"
  @failed
end

#handle_failed_target(name) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/pwrake/master/master.rb', line 385

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



187
188
189
190
191
192
193
194
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
# File 'lib/pwrake/master/master.rb', line 187

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
    @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
  @killed = 0
  [:TERM,:INT].each do |sig|
    Signal.trap(sig) do
      signal_trap(sig)
    end
  end
  send_task_to_idle_core
  setup_fiber(t)
end

#retire(hid) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pwrake/master/master.rb', line 166

def retire(hid)
  host_info = @hostinfo_by_id[hid.to_i]
  if host_info && host_info.decrease(1)
    # all retired
    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

#send_task_to_idle_coreObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/pwrake/master/master.rb', line 296

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 "@task_queue="+@task_queue.inspect
    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



37
38
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
# File 'lib/pwrake/master/master.rb', line 37

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_branchesObject



93
94
95
96
97
98
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
# File 'lib/pwrake/master/master.rb', line 93

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



221
222
223
224
225
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
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/pwrake/master/master.rb', line 221

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_postprocessObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/pwrake/master/master.rb', line 326

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_postprocess0Object



357
358
359
# File 'lib/pwrake/master/master.rb', line 357

def setup_postprocess0
  setup_postprocess{false}
end

#setup_postprocess1Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/pwrake/master/master.rb', line 361

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pwrake/master/master.rb', line 65

def signal_trap(sig)
  case @killed
  when 0
    # log writing failed. can't be called from trap context
    $stderr.puts "\nSignal trapped. (sig=#{sig} pid=#{Process.pid} ##{@killed})"
    if Rake.application.options.debug
      $stderr.puts "in master thread #{Thread.current}:"
      $stderr.puts caller
      if @thread
        $stderr.puts "in branch thread #{@thread}:"
        $stderr.puts @thread.backtrace.join("\n")
      end
    end
    $stderr.puts "Exiting..."
    @no_more_run = true
    @failed = true
    NBIO::Handler.kill(@hdl_set,sig)
    # @selector.run : not required here
  when 1
    $stderr.puts "\nOnce more Ctrl-C (SIGINT) for exit."
  when 2
    @thread.kill if @thread
  else
    Kernel.exit(false) # must wait for nomral exit
  end
  @killed += 1
end

#task_end(tw, host_info) ⇒ Object



349
350
351
352
353
354
355
# File 'lib/pwrake/master/master.rb', line 349

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