-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark_cluster.rb
405 lines (373 loc) · 10.7 KB
/
spark_cluster.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
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
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
210
211
212
213
214
215
216
217
218
219
220
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
295
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
ssh_lib = File.join(ENV["HOME"], "/net-ssh/lib")
$LOAD_PATH.unshift(ssh_lib)
require 'net/ssh'
require 'ping'
require 'rexml/document'
VM_NUMBER = 14
Throttler_port = 20000
Files = {
:cluster_nodes => File.join(ENV["HOME"], "/host_list"),
:throttler_ports => File.join(ENV["HOME"], "/thrott_ports")
}
Usernames = {
:root_usr => 'root',
:usr_name => 'ioi600'
}
include REXML
def provision_vms
vm_ids = []
File.truncate("/home/ioi600/set_file", 0) unless File.zero?("/home/ioi600/set_file")
f = File.open("/home/ioi600/set_file", 'w')
VM_NUMBER.times do
output = `onevm create /home/ioi600/spark_vm.template`
vm_id = output.chomp!.split(':')[1].strip!
vm_ids << vm_id
f.puts(vm_id)
puts "VM with id #{vm_id} created"
end
2.times do
output = `onevm create /home/ioi600/spark2_vm.template`
vm_id = output.chomp!.split(':')[1].strip!
vm_ids << vm_id
f.puts(vm_id)
puts "VM with id #{vm_id} created"
end
f.close
vm_num = VM_NUMBER + 2
check_status_of_vms(vm_num)
get_ip_address_for_vms(vm_ids)
end
def get_ip_address_for_vms(vms)
ip_list = []
unless vms.nil? || vms.empty?
vms.each do |vm_id|
result = `onevm show "#{vm_id}" | grep IP`
ip = result.chomp.split('=')[1].sub(',', '').strip.chomp('"').reverse.chomp('"').reverse
ip_list << ip
end
end
ip_list
end
def configure_and_start_spark_cluster (vm_pool)
set_passwordless_ssh (vm_pool)
delete_files_and_folders (vm_pool)
configure_spark (vm_pool)
configure_spark_workers (vm_pool)
vm_pool.each{|vm| start_bandwidth_throttler vm}
edit_master_hosts_file (vm_pool)
edit_slaves_hosts_file (vm_pool)
configure_hadoop (vm_pool)
format_hdfs (vm_pool.first)
start_hdfs_and_yarn (vm_pool)
start_spark (vm_pool.first)
configure_hibench (vm_pool)
end
def delete_files_and_folders vm_pool
vm_pool.each do |vm|
cmd = <<-eos
cd /home/ioi600
rm -rf bin/
rm examples.desktop
rm -rf lib/
rm -rf spark-perf/
rm -rf Weasel/
rm contextualization.log
touch monitor_in.txt
touch monitor_out.txt
python bandwidth-throttler/monitor_bandwidth.py eth0 monitor_out.txt monitor_in.txt proc > ~/ssh.stdout 2>&1 & disown
eos
ssh = Net::SSH.start("#{vm}", Usernames[:root_usr])
res = ssh.exec!(cmd)
puts res
ssh.close
end
end
def configure_spark vm_pool
vm_pool.each do |vm|
cmd = <<-eos
cd spark-2.0.2/conf
touch slaves
cat slaves.template >> slaves
echo #{vm} >> slaves
eos
ssh = Net::SSH.start("#{vm_pool.first}", Usernames[:usr_name])
res = ssh.exec!(cmd)
puts res
ssh.close
end
end
def configure_spark_workers vm_pool
vm_pool.each do |vm|
cmd = <<-eos
cd spark-2.0.2/conf
touch spark-env.sh
touch spark-defaults.conf
cat spark-env.sh.template >> spark-env.sh
cat spark-defaults.conf.template >> spark-defaults.conf
echo SPARK_MASTER_HOST=#{vm_pool.first} >> spark-env.sh
echo SPARK_WORKER_MEMORY=10g >> spark-env.sh
echo SPARK_WORKER_CORES=4 >> spark-env.sh
echo SPARK_WORKER_INSTANCES=2 >> spark-env.sh
echo SPARK_LOCAL_DIRS=/mnt >> spark-env.sh
echo spark.rpc.askTimeout 600s >> spark-defaults.conf
echo spark.network.timeout 600s >> spark-defaults.conf
eos
ssh = Net::SSH.start("#{vm}", Usernames[:usr_name])
res = ssh.exec!(cmd)
ssh.close
end
end
def configure_hibench vm_pool
vm_pool.each do |vm|
cmd = <<-eos
cd /mnt/HiBench/conf
touch hadoop.conf
echo hibench.hadoop.home /home/ioi600/hadoop-2.7.3 >> hadoop.conf
echo hibench.hadoop.executable /home/ioi600/hadoop-2.7.3/bin/hadoop >> hadoop.conf
echo hibench.hadoop.configure.dir /home/ioi600/hadoop-2.7.3/etc/hadoop >> hadoop.conf
echo hibench.hdfs.master hdfs://#{vm_pool.first}:9000 >> hadoop.conf
echo hibench.hadoop.release apache >> hadoop.conf
touch spark.conf
echo hibench.spark.home /home/ioi600/spark-2.0.2 >> spark.conf
echo hibench.spark.master spark://#{vm_pool.first}:7077 >> spark.conf
echo hibench.spark.version spark2.0 >> spark.conf
echo spark.executor.memory 10g >> spark.conf
echo spark.driver.memory 4g >> spark.conf
sed -i '3d' hibench.conf
sed -i '3i\hibench.scale.profile large' hibench.conf
sed -i '6d' hibench.conf
sed -i '6i\hibench.default.map.parallelism 64' hibench.conf
sed -i '9d' hibench.conf
sed -i '9i\hibench.default.shuffle.parallelism 64' hibench.conf
sed -i '57, 58d' hibench.conf
sed -i '57i\hibench.masters.hostnames #{vm_pool.first}' hibench.conf
sed -i '58i\hibench.slaves.hostnames #{vm_pool[1..-1].join(' ')}' hibench.conf
sed -i '32d' hibench.conf
sed -i '32i\hibench.hdfs.data.dir ${hibench.hdfs.master}/mnt/HiBenchData' hibench.conf
eos
ssh = Net::SSH.start("#{vm}", Usernames[:usr_name])
ssh.exec!(cmd)
ssh.close
end
end
def format_hdfs master_ip
cmd = <<-eos
cd hadoop-2.7.3/bin
./hdfs namenode -format
eos
ssh = Net::SSH.start("#{master_ip}", Usernames[:usr_name])
res = ssh.exec!(cmd)
puts res
ssh.close
end
def start_spark master_ip
cmd = <<-eos
cd spark-2.0.2/sbin
./start-all.sh
eos
ssh = Net::SSH.start("#{master_ip}", Usernames[:usr_name])
res = ssh.exec!(cmd)
puts res
ssh.close
end
def configure_hadoop vm_pool
source = "<configuration><property><name>fs.default.name</name><value>hdfs://#{vm_pool.first}:9000</value></property><property><name>hadoop.tmp.dir</name><value>/mnt</value></property></configuration>"
source_2 = "<configuration><property><name>dfs.replication</name><value>2</value></property></configuration>"
fs = ""
fs_2 = ""
doc_2 = Document.new(source_2)
doc = Document.new(source)
formatter = REXML::Formatters::Pretty.new
formatter_2 = REXML::Formatters::Pretty.new
formatter_2.compact = true
formatter.compact = true
formatter.write(doc, fs)
formatter_2.write(doc_2, fs_2)
vm_pool.each do |vm|
cmd = <<-eos
mkdir /mnt/hadoop-ioi600/dfs/name
cd hadoop-2.7.3/etc/hadoop
sed -i '19d' core-site.xml
sed -i '$d' core-site.xml
echo "#{fs}" | tee -a core-site.xml
eos
cmd_2 = <<-eos
cd hadoop-2.7.3/etc/hadoop
sed -i '19d' hdfs-site.xml
sed -i '$d' hdfs-site.xml
echo "#{fs_2}" | tee -a hdfs-site.xml
eos
ssh = Net::SSH.start("#{vm}", Usernames[:usr_name])
res = ssh.exec!(cmd)
res_1 = ssh.exec!(cmd_2)
puts res
puts res_1
ssh.close
end
end
def start_hdfs_and_yarn vm_pool
prepare_slaves_file vm_pool.first
vm_pool.each do |vm|
cmd = <<-eos
cd hadoop-2.7.3/etc/hadoop
echo #{vm} >> slaves
eos
ssh = Net::SSH.start("#{vm_pool.first}", Usernames[:usr_name])
ssh.exec!(cmd)
ssh.close
end
lunch_hdfs_demons vm_pool.first
end
def lunch_hdfs_demons master_ip
cmd = <<-eos
cd hadoop-2.7.3/sbin
./start-dfs.sh
./start-yarn.sh
eos
ssh = Net::SSH.start("#{master_ip}", Usernames[:usr_name])
ssh.exec!(cmd)
ssh.close
end
def prepare_slaves_file master_ip
cmd = <<-eos
cd hadoop-2.7.3/etc/hadoop
truncate -s 0 slaves
eos
ssh = Net::SSH.start("#{master_ip}", Usernames[:usr_name])
ssh.exec!(cmd)
ssh.close
end
def start_bandwidth_throttler vm
vm_port = Throttler_port
File.open(Files[:throttler_ports], 'a') do |f|
f.puts "#{vm}\t#{vm_port}"
end
cmd = <<-eos
touch ssh.stdout
cd /home/ioi600/bandwidth-throttler
python shape_traffic_server.py --port #{vm_port} > ~/ssh.stdout 2>&1 & disown
eos
ssh = Net::SSH.start("#{vm}", Usernames[:root_usr])
ssh.exec!(cmd)
ssh.close
puts "bandwidth-throttler started on cluster host #{vm}"
end
def edit_master_hosts_file vm_pool
cmd_1 = <<-eos
touch /tmp/test.txt
sed -i '1, 2d' /etc/hosts
eos
ssh = Net::SSH.start("#{vm_pool.first}", Usernames[:root_usr])
ssh.exec!(cmd_1)
ssh.close
vm_pool[1..-1].each do |vm|
cmd_2 = <<-eos
cd /etc
echo '#{vm} Spark-vm' | cat - hosts > /tmp/test.txt && mv /tmp/test.txt hosts
eos
ssh = Net::SSH.start("#{vm_pool.first}", Usernames[:root_usr])
ssh.exec!(cmd_2)
ssh.close
end
cmd_3 = <<-eos
cd /etc
echo '#{vm_pool.first} Spark-vm' | cat - hosts > /tmp/test.txt && mv /tmp/test.txt hosts
eos
ssh = Net::SSH.start("#{vm_pool.first}", Usernames[:root_usr])
ssh.exec!(cmd_3)
ssh.close
end
def edit_slaves_hosts_file vm_pool
vm_pool[1..-1].each do |vm|
cmd = <<-eos
touch /tmp/test.txt
sed -i '1, 2d' /etc/hosts
eos
ssh = Net::SSH.start("#{vm}", Usernames[:root_usr])
ssh.exec!(cmd)
end
vm_pool[1..-1].each do |v|
vm_pool[1..-1].each do |vm|
unless vm == v
cmd = <<-eos
cd /etc
echo '#{vm} Spark-vm' | cat - hosts > /tmp/test.txt && mv /tmp/test.txt hosts
eos
ssh = Net::SSH.start("#{v}", Usernames[:root_usr])
ssh.exec!(cmd)
ssh.close
end
end
end
vm_pool[1..-1].each do |v|
cmd = <<-eos
cd /etc
echo '#{v} Spark-vm' | cat - hosts > /tmp/test.txt && mv /tmp/test.txt hosts
eos
ssh = Net::SSH.start("#{v}", Usernames[:root_usr])
ssh.exec!(cmd)
ssh.close
end
vm_pool[1..-1].each_with_index do |v, idx|
cmd = <<-eos
cd /etc
echo '#{vm_pool.first} Spark-vm' | cat - hosts > /tmp/test.txt && mv /tmp/test.txt hosts
eos
ssh = Net::SSH.start("#{v}", Usernames[:root_usr])
ssh.exec!(cmd)
ssh.close
end
end
def start
vm_pool = provision_vms
reset_environment vm_pool
vm_reachable? vm_pool
configure_and_start_spark_cluster vm_pool
puts "spark cluster configured and started successfully"
exit(0)
end
def check_status_of_vms vm_num
loop do
print "\rinitializing VMs........"
vm_list = `onevm list`
running_vms = vm_list.each_line.select{|l| l.include? "runn"}
if running_vms.count == vm_num
break
end
end
end
def vm_reachable? vm_pool
loop do
print "\rhecking reachability of VMs"
reachable_vms = []
vm_pool.each do |host|
reachable_vms << Ping.pingecho("#{host}")
end
break if reachable_vms.all? { |e| e == true }
end
end
def set_passwordless_ssh vm_pool
vm_pool.each do |vm|
cmd = <<-eos
cd .ssh
sshpass -p '1234' ssh-copy-id ioi600@#{vm}
eos
ssh = Net::SSH.start "#{vm_pool.first}" , "#{Usernames[:usr_name]}"
puts "#{ssh.exec!(cmd)}"
end
end
def reset_environment vm_list
File.truncate(Files[:cluster_nodes], 0) unless File.zero?(Files[:cluster_nodes])
File.open(Files[:cluster_nodes], 'a') do |f|
f.puts "#{vm_list.first}\tmaster and worker"
end
vm_list[1..-1].each do |ip|
Thread.new do
File.open(Files[:cluster_nodes], 'a'){|f| f.puts "#{ip}\tworker"}
end.join
end
File.truncate(Files[:throttler_ports], 0) unless File.zero?(Files[:throttler_ports])
end
if __FILE__ == $PROGRAM_NAME
start
end