Class: Pwrake::Branch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, r, w) ⇒ Branch

Returns a new instance of Branch.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pwrake/branch/branch.rb', line 5

def initialize(opts,r,w)
  #Thread.abort_on_exception = true
  @option = opts
  @task_q = {}  # worker_id => FiberQueue.new
  @shells = []
  @ior = r
  @iow = w
  @runner = Runner.new(@option['HEARTBEAT'])
  @master_hdl = Handler.new(@runner,@ior,@iow)
  @master_chan = Channel.new(@master_hdl)
  @wk_comm = {}
  @wk_hdl_set = HandlerSet.new
  @shell_start_interval = @option['SHELL_START_INTERVAL']
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



31
32
33
# File 'lib/pwrake/branch/branch.rb', line 31

def logger
  @logger
end

Instance Method Details

#finishObject



202
203
204
205
206
207
208
209
210
# File 'lib/pwrake/branch/branch.rb', line 202

def finish
  return if @finished
  @finished = true
  Log.debug "Branch#finish: begin"
  @wk_hdl_set.exit
  Log.debug "Branch#finish: worker exited"
  @master_hdl.put_line "exited"
  Log.debug "Branch#finish: sent 'exited' to master"
end

#init_loggerObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pwrake/branch/branch.rb', line 33

def init_logger
  if dir = @option['LOG_DIR']
    logfile = File.join(dir,@option['LOG_FILE'])
    @logger = Logger.new(logfile)
  else
    if @option['DEBUG']
      @logger = Logger.new($stderr)
    else
      @logger = Logger.new(File::NULL)
    end
  end

  if @option['DEBUG']
    @logger.level = Logger::DEBUG
  elsif @option['TRACE']
    @logger.level = Logger::INFO
  else
    @logger.level = Logger::WARN
  end
end

#kill(sig = "INT") ⇒ Object



197
198
199
200
# File 'lib/pwrake/branch/branch.rb', line 197

def kill(sig="INT")
  Log.warn "Branch#kill #{sig}"
  @wk_hdl_set.kill(sig)
end

#runObject

Rakefile is loaded after ‘init’ before ‘run’



22
23
24
25
26
27
28
29
# File 'lib/pwrake/branch/branch.rb', line 22

def run
  setup_worker
  setup_shell
  setup_fiber
  setup_master_channel
  @runner.run
  Log.debug "Brandh#run end"
end

#setup_fiberObject



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
# File 'lib/pwrake/branch/branch.rb', line 143

def setup_fiber
  # start fibers
  @shells.each do |shell|
    shell.create_fiber(@master_hdl).resume
  end
  Log.debug "all fiber started"

  @wk_comm.each_value do |comm|
    #comm.start_default_fiber
    Fiber.new do
      while s = comm.channel.get_line
        break unless comm.common_line(s)
      end
      Log.debug "Branch#setup_fiber: end of fiber for default channel"
    end.resume
  end

  # setup end
  @wk_comm.values.each do |comm|
    comm.handler.put_line "setup_end"
  end

  @master_hdl.put_line "branch_setup:done"
  Log.debug "Branch#setup_fiber: setup end"
end

#setup_master_channelObject



169
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
# File 'lib/pwrake/branch/branch.rb', line 169

def setup_master_channel
  Fiber.new do
    while s = @master_chan.get_line
      # receive command from main pwrake
      Log.debug "Branch:recv #{s.inspect} from master"
      case s
        #
      when /^(\d+):(.+)$/o
        id, tname = $1,$2
        @task_q[id].enq(tname)
        #
      when /^exit$/
        @task_q.each_value{|q| q.finish}
        @shells.each{|shell| shell.close}
        @runner.finish
        break
        #
      when /^kill:(.*)$/o
        sig = $1
        kill(sig)
      else
        Log.debug "Branch: invalid line from master: #{s}"
      end
    end
    Log.debug "Branch#setup_master_channel: end of fiber for master channel"
  end.resume
end

#setup_shellObject



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
# File 'lib/pwrake/branch/branch.rb', line 115

def setup_shell
  @shells = []
  errors = []
  shell_id = 0
  @wk_comm.each_value do |comm|
    comm.ncore.times do
      chan = Channel.new(comm.handler,shell_id)
      shell_id += 1
      shell = Shell.new(chan,@task_q[comm.id],@option.worker_option)
      @shells << shell
      # wait for remote shell open
      Fiber.new do
        if !shell.open
          errors << comm.host
        end
        Log.debug "Branch#setup_shells: end of fiber to open shell"
      end.resume
      sleep @shell_start_interval
    end
  end

  @runner.run

  if !errors.empty?
    raise RuntimeError,"Failed to start workers: #{errors.inspect}"
  end
end

#setup_workerObject



54
55
56
57
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pwrake/branch/branch.rb', line 54

def setup_worker
  s = @ior.gets
  if s.chomp != "host_list_begin"
    raise "Branch#setup_worker: recv=#{s.chomp} expected=host_list_begin"
  end

  if dir = @option['LOG_DIR']
    fn = File.join(dir,@option["COMMAND_CSV_FILE"])
    Shell.profiler.open(fn,@option['GNU_TIME'],@option['PLOT_PARALLELISM'])
  end

  worker_code = WorkerCommunicator.read_worker_progs(@option)

  while s = @ior.gets
    s.chomp!
    break if s == "host_list_end"
    if /^host:(\d+) (\S+) ([+-]?\d+)?$/ =~ s
      id, host, ncore = $1,$2,$3
      ncore &&= ncore.to_i
      comm = WorkerCommunicator.new(id,host,ncore,@runner,@option)
      comm.setup_connection(worker_code)
      @wk_comm[id] = comm
      @wk_hdl_set << comm.handler
      @task_q[id] = FiberQueue.new
    else
      raise "Branch#setup_worker: recv=#{s.chomp} expected=host:id hostname ncore"
    end
  end

  errors = []

  @wk_comm.values.each do |comm|
    Fiber.new do
      while true
        if s = comm.channel.get_line
          break unless comm.ncore_proc(s)
        else
          errors << comm
          break
        end
      end
      Log.debug "Branch#setup_worker: fiber end of ncore_proc"
    end.resume
  end

  @runner.run

  if !errors.empty?
    errors.each{|comm| @wk_hdl_set.delete(comm.handler)}
    hosts = errors.map{|comm| comm.host}.join(",")
    raise RuntimeError,"Failed to connect to workers: #{hosts}"
  end

  # ncore
  @wk_comm.each_value do |comm|
    # set WorkerChannel#ncore at Master
    @master_hdl.put_line "ncore:#{comm.id}:#{comm.ncore}"
  end
  @master_hdl.put_line "ncore:done"
end