Class: ScbiMapreduce::WorkerLauncher

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

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.



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

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_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



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

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



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

def launch_external_workers(workers)
  puts "Launching #{workers.count} external workers: #{workers}"
  worker_id=0
  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

  log_file=File.join(init_dir,'logs','launchers')

  workers.each do |machine|

    # 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



59
60
61
62
63
64
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 59

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 39

def launch_workers
  # 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(i,@server_ip,@server_port)
        $LAUNCHER_LOG.info "Worker #{i} launched [#{@server_ip}:#{@server_port}]"
      }

      #threads.each { |aThread|  aThread.join }
    end
    #Process.waitall
    $LAUNCHER_LOG.info "All workers launched"
  end
end

#launch_workers_and_waitObject



34
35
36
37
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 34

def launch_workers_and_wait
  launch_workers
  Process.waitall
end

#left_largest_common_substr(s1, s2) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 68

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



82
83
84
85
86
87
88
89
90
# File 'lib/scbi_mapreduce/worker_launcher.rb', line 82

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