Class: ScbiMapreduce::WorkerLauncher

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_mapreduce/worker_launcher.rb

Constant Summary collapse

@@worker_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_ip, server_port, server_ip_list, workers, worker_file, log_file = nil, init_env_file = nil) ⇒ WorkerLauncher

Returns a new instance of WorkerLauncher.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 15

def initialize(server_ip,server_port, server_ip_list,workers, worker_file, log_file=nil, init_env_file=nil)
  @server_ip = server_ip
  @server_port = server_port
  @worker_file = worker_file
  @workers=workers
  @init_env_file=init_env_file
  @server_ip_list=server_ip_list



  if log_file.nil?
    log_file = "logs/launcher_global_log.txt"
  end

  FileUtils.mkdir_p(File.dirname(log_file)) if ((log_file!=STDOUT) && (!File.exists?(File.dirname(log_file))))


  $LAUNCHER_LOG = Logger.new(log_file)

  $LAUNCHER_LOG.datetime_format = "%Y-%m-%d %H:%M:%S"
end

Instance Attribute Details

#server_ipObject

Returns the value of attribute server_ip.



11
12
13
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 11

def server_ip
  @server_ip
end

#server_portObject

Returns the value of attribute server_port.



11
12
13
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 11

def server_port
  @server_port
end

Instance Method Details

#find_common_ip(machine_ip, ip_list) ⇒ Object



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
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 97

def find_common_ip(machine_ip,ip_list)

  def left_largest_common_substr(s1,s2)
    res=''

    s2.scan(/./).each_with_index do |l1,i|
      if s1[i]==l1
        res << l1
      else
        break
      end
    end
    res

  end

  def remove_final_dot(s)
    res=s
    # remove final dot
    if res[res.length-1]=='.'
      res=res[0,res.length-1]
    end

    return res
  end


  res=''
  common_ip=''

  ip_list.each do |ip|

    res=left_largest_common_substr(ip,machine_ip)
    res=remove_final_dot(res)

    if res.length>common_ip.length
      common_ip=res
    end
  end

  return common_ip
end

#launch_external_workers(workers) ⇒ Object



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
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
206
207
208
209
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 141

def launch_external_workers(workers)

  #skip if slurm detected
  if system("which srun > /dev/null 2>&1")
    return
  end

  puts "Launching #{workers.count} external workers: #{workers}"
  puts "INIT_ENV_FILE: #{@init_env_file}"
  
  # This sleep is necessary to leave time to lustre fylesystems to sync the log folder between all nodes. If not, external workers will not be launched.
  if !workers.empty?
    puts "SLEEP 10 for logs folder sync in lustre fs"
    sleep 10
  end
  
  init=''
  if @init_env_file
    init_path = File.expand_path(@init_env_file)
    # path = File.join($ROOT_PATH)
    # puts "init_env file: #{path}"
    if File.exists?(init_path)
      puts "File #{init_path} exists, using it"
      init=". #{init_path}; "
    end
  end

  init_dir=Dir.pwd

  cd =''

  if File.exists?(init_dir)
    cd = "cd #{init_dir}; "
  end

  

  workers.each do |machine|
    
    log_file=File.join(init_dir,'logs',"launcher_#{@@worker_id}")
    log_dir=File.join(init_dir,'logs')
    
    # if server_ip is not in valid ips
    if !@server_ip_list.include?(@server_ip)
      # find matching ip between server and worker
      machine_ip = Resolv.getaddress(machine)
      matching_ip=find_common_ip(machine_ip,@server_ip_list)
      found_ip=@server_ip_list.select{|one_ip| one_ip.index(matching_ip)==0}.first
    else
      found_ip=@server_ip
    end

    if !found_ip.nil?
      # cmd = "ssh #{machine} \"#{init} #{cd} #{INTERPRETER} #{File.join(File.dirname(__FILE__),'main_worker.rb')} #{worker_id.to_s} #{@server_ip} #{@server_port} #{@worker_file}\""
      # cmd = "ssh #{machine} \"nohup #{File.join(File.dirname(__FILE__),'launcher.sh')} #{worker_id.to_s} #{@server_ip} #{@server_port} #{@worker_file} #{init_dir} #{init_path}  </dev/null >> #{log_file} 2>> #{log_file} & \""
      cmd = "ssh #{machine} \"nohup  #{File.join(File.dirname(__FILE__),'launcher.sh')} #{@@worker_id.to_s} #{found_ip} #{@server_port} #{@worker_file} #{init_dir} #{init_path}  </dev/null >> #{log_file} 2>> #{log_file} & \""

      $LAUNCHER_LOG.info cmd

      pid=fork{
        exec(cmd)
      }

      @@worker_id+=1
    else
      $LAUNCHER_LOG.error("Couldn't find a matching ip between worker (#{machine_ip}) and server #{ip_list.to_json}")
    end
  end
end

#launch_worker(worker_id, server_ip, server_port) ⇒ Object

override this



90
91
92
93
94
95
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 90

def launch_worker(worker_id, server_ip, server_port)

  cmd = "#{INTERPRETER} #{File.join(File.dirname(__FILE__),'main_worker.rb')} #{worker_id.to_s} #{server_ip} #{server_port} #{@worker_file}"
  puts cmd
  exec(cmd)
end

#launch_workersObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 42

def launch_workers

  if system("which srun > /dev/null 2>&1") && (!ENV['SLURM_PROCID'].to_s.empty?)
      $LAUNCHER_LOG.info "SLURM DETECTED"
      $LAUNCHER_LOG.info "Launching #{@workers} workers via srun"
      launch_workers_srun
  else
      $LAUNCHER_LOG.info "Launching #{@workers} workers via SSH"
      launch_workers_ssh
  end

end

#launch_workers_and_waitObject



37
38
39
40
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 37

def launch_workers_and_wait
  launch_workers
  Process.waitall
end

#launch_workers_srunObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 55

def launch_workers_srun
  # TODO - si aqui falla algo, no peta, se bloquea
  $LAUNCHER_LOG.info "Launching #{@workers} srun workers"
    
    pid=fork{
      $LAUNCHER_LOG.info "Connecting #{@workers} srun workers to #{@server_ip}:#{@server_port}"
      cmd = "srun #{File.join(File.dirname(__FILE__),'main_worker.rb')} auto #{server_ip} #{server_port} #{@worker_file}"
      $LAUNCHER_LOG.info cmd
      exec(cmd)
    }

    $LAUNCHER_LOG.info "All workers launched"

end

#launch_workers_sshObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 70

def launch_workers_ssh
  # TODO - si aqui falla algo, no peta, se bloquea
  $LAUNCHER_LOG.info "Launching #{@workers} local workers"
  if @workers > 0
    $LAUNCHER_LOG.info "Connecting #{@workers} local workers to #{@server_ip}:#{@server_port}"
    threads = []
    @workers.times do |i|
      pid=fork{
        launch_worker(@@worker_id,@server_ip,@server_port)
        $LAUNCHER_LOG.info "Worker #{i} launched [#{@server_ip}:#{@server_port}]"
      }
      @@worker_id+=1
      #threads.each { |aThread|  aThread.join }
    end
    #Process.waitall
    $LAUNCHER_LOG.info "All workers launched"
  end
end

#left_largest_common_substr(s1, s2) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 99

def left_largest_common_substr(s1,s2)
  res=''

  s2.scan(/./).each_with_index do |l1,i|
    if s1[i]==l1
      res << l1
    else
      break
    end
  end
  res

end

#remove_final_dot(s) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 113

def remove_final_dot(s)
  res=s
  # remove final dot
  if res[res.length-1]=='.'
    res=res[0,res.length-1]
  end

  return res
end