blob: db5dc367cd9a3038c5e4638c5c28affdff675a3e [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import abc
koder aka kdanilov66839a92015-04-11 13:22:31 +03002import time
koder aka kdanilov783b4542015-04-23 18:57:04 +03003import socket
koder aka kdanilov4d4771c2015-04-23 01:32:02 +03004import random
koder aka kdanilov4643fd62015-02-10 16:20:13 -08005import os.path
koder aka kdanilove21d7472015-02-14 19:02:04 -08006import logging
koder aka kdanilovea22c3d2015-04-21 03:42:22 +03007import datetime
koder aka kdanilove21d7472015-02-14 19:02:04 -08008
koder aka kdanilova855f902015-04-26 14:31:45 +03009from paramiko import SSHException, SFTPError
Yulia Portnovab1a15072015-05-06 14:59:25 +030010import texttable
koder aka kdanilov783b4542015-04-23 18:57:04 +030011
koder aka kdanilove2de58c2015-04-24 22:59:36 +030012from wally.utils import (ssize_to_b, open_for_append_or_create,
13 sec_to_str, StopTestError)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030014
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030015from wally.ssh_utils import (copy_paths, run_over_ssh,
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030016 save_to_remote,
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030017 # delete_file,
18 connect, read_from_remote, Local)
19
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030020from . import postgres
Yulia Portnovab1a15072015-05-06 14:59:25 +030021from . import mysql
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030022from .io import agent as io_agent
23from .io import formatter as io_formatter
24from .io.results_loader import parse_output
koder aka kdanilov652cd802015-04-13 12:21:07 +030025
koder aka kdanilov4643fd62015-02-10 16:20:13 -080026
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030027logger = logging.getLogger("wally")
koder aka kdanilove21d7472015-02-14 19:02:04 -080028
29
koder aka kdanilov4643fd62015-02-10 16:20:13 -080030class IPerfTest(object):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030031 def __init__(self, options, is_primary, on_result_cb, test_uuid, node,
koder aka kdanilov2066daf2015-04-23 21:05:41 +030032 log_directory=None,
33 coordination_queue=None,
34 remote_dir="/tmp/wally"):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030035 self.options = options
koder aka kdanilov4643fd62015-02-10 16:20:13 -080036 self.on_result_cb = on_result_cb
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030037 self.log_directory = log_directory
38 self.node = node
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030039 self.test_uuid = test_uuid
koder aka kdanilovec1b9732015-04-23 20:43:29 +030040 self.coordination_queue = coordination_queue
koder aka kdanilov2066daf2015-04-23 21:05:41 +030041 self.remote_dir = remote_dir
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030042 self.is_primary = is_primary
koder aka kdanilove2de58c2015-04-24 22:59:36 +030043 self.stop_requested = False
44
45 def request_stop(self):
46 self.stop_requested = True
koder aka kdanilov2066daf2015-04-23 21:05:41 +030047
48 def join_remote(self, path):
49 return os.path.join(self.remote_dir, path)
koder aka kdanilovec1b9732015-04-23 20:43:29 +030050
51 def coordinate(self, data):
52 if self.coordination_queue is not None:
koder aka kdanilove2de58c2015-04-24 22:59:36 +030053 self.coordination_queue.put((self.node.get_conn_id(), data))
koder aka kdanilov4643fd62015-02-10 16:20:13 -080054
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030055 def pre_run(self):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080056 pass
57
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030058 def cleanup(self):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030059 pass
60
koder aka kdanilov4643fd62015-02-10 16:20:13 -080061 @abc.abstractmethod
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030062 def run(self, barrier):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080063 pass
64
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030065 @classmethod
66 def format_for_console(cls, data):
67 msg = "{0}.format_for_console".format(cls.__name__)
68 raise NotImplementedError(msg)
69
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030070 def run_over_ssh(self, cmd, **kwargs):
71 return run_over_ssh(self.node.connection, cmd,
72 node=self.node.get_conn_id(), **kwargs)
73
koder aka kdanilovec1b9732015-04-23 20:43:29 +030074 @classmethod
75 def coordination_th(cls, coord_q, barrier, num_threads):
76 pass
77
koder aka kdanilov4643fd62015-02-10 16:20:13 -080078
Yulia Portnova7ddfa732015-02-24 17:32:58 +020079class TwoScriptTest(IPerfTest):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030080 def __init__(self, *dt, **mp):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030081 IPerfTest.__init__(self, *dt, **mp)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020082
Yulia Portnovab1a15072015-05-06 14:59:25 +030083 if 'scripts_path' in self.options:
84 self.root = self.options['scripts_path']
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030085 self.run_script = self.options['run_script']
Yulia Portnovab1a15072015-05-06 14:59:25 +030086 self.prerun_script = self.options['prerun_script']
Yulia Portnova7ddfa732015-02-24 17:32:58 +020087
88 def get_remote_for_script(self, script):
Yulia Portnovab1a15072015-05-06 14:59:25 +030089 return os.path.join(self.remote_dir, script.rpartition('/')[2])
Yulia Portnova7ddfa732015-02-24 17:32:58 +020090
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030091 def pre_run(self):
Yulia Portnovab1a15072015-05-06 14:59:25 +030092 copy_paths(self.node.connection, {self.root: self.remote_dir})
93 cmd = self.get_remote_for_script(self.pre_run_script)
94 self.run_over_ssh(cmd, timeout=2000)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020095
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030096 def run(self, barrier):
Yulia Portnovab1a15072015-05-06 14:59:25 +030097 remote_script = self.get_remote_for_script(self.run_script)
Yulia Portnova886a2562015-04-07 11:16:13 +030098 cmd_opts = ' '.join(["%s %s" % (key, val) for key, val
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030099 in self.options.items()])
Yulia Portnova886a2562015-04-07 11:16:13 +0300100 cmd = remote_script + ' ' + cmd_opts
Yulia Portnovab1a15072015-05-06 14:59:25 +0300101 out_err = self.run_over_ssh(cmd, timeout=6000)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300102 self.on_result(out_err, cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200103
104 def parse_results(self, out):
105 for line in out.split("\n"):
106 key, separator, value = line.partition(":")
107 if key and value:
108 self.on_result_cb((key, float(value)))
109
koder aka kdanilov66839a92015-04-11 13:22:31 +0300110 def on_result(self, out_err, cmd):
111 try:
112 self.parse_results(out_err)
113 except Exception as exc:
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300114 msg_templ = "Error during postprocessing results: {0!s}. {1}"
115 raise RuntimeError(msg_templ.format(exc, out_err))
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200116
Yulia Portnovab1a15072015-05-06 14:59:25 +0300117 def merge_results(self, results):
118 tpcm = sum([val[1] for val in results])
119 return {"res": {"TpmC": tpcm}}
120
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200121
122class PgBenchTest(TwoScriptTest):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300123 root = os.path.dirname(postgres.__file__)
Yulia Portnovab1a15072015-05-06 14:59:25 +0300124 pre_run_script = os.path.join(root, "prepare.sh")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300125 run_script = os.path.join(root, "run.sh")
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300126
127
Yulia Portnova1f123962015-05-06 18:48:11 +0300128 @classmethod
129 def format_for_console(cls, data):
130 tab = texttable.Texttable(max_width=120)
131 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
132 tab.header(["TpmC"])
133 tab.add_row([data['res']['TpmC']])
134 return tab.draw()
135
136
Yulia Portnovab1a15072015-05-06 14:59:25 +0300137class MysqlTest(TwoScriptTest):
138 root = os.path.dirname(mysql.__file__)
139 pre_run_script = os.path.join(root, "prepare.sh")
140 run_script = os.path.join(root, "run.sh")
141
142 @classmethod
143 def format_for_console(cls, data):
144 tab = texttable.Texttable(max_width=120)
145 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
146 tab.header(["TpmC"])
147 tab.add_row([data['res']['TpmC']])
148 return tab.draw()
149
150
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800151class IOPerfTest(IPerfTest):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300152 tcp_conn_timeout = 30
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300153 max_pig_timeout = 5
154 soft_runcycle = 5 * 60
koder aka kdanilov2c473092015-03-29 17:12:13 +0300155
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300156 def __init__(self, *dt, **mp):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300157 IPerfTest.__init__(self, *dt, **mp)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300158 self.config_fname = self.options['cfg']
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300159
160 if '/' not in self.config_fname and '.' not in self.config_fname:
161 cfgs_dir = os.path.dirname(io_agent.__file__)
162 self.config_fname = os.path.join(cfgs_dir,
163 self.config_fname + '.cfg')
164
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300165 self.alive_check_interval = self.options.get('alive_check_interval')
166 self.config_params = self.options.get('params', {})
167 self.tool = self.options.get('tool', 'fio')
koder aka kdanilovda45e882015-04-06 02:24:42 +0300168 self.raw_cfg = open(self.config_fname).read()
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300169 self.configs = list(io_agent.parse_all_in_1(self.raw_cfg,
170 self.config_params))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800171
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300172 cmd_log = os.path.join(self.log_directory, "task_compiled.cfg")
173 raw_res = os.path.join(self.log_directory, "raw_results.txt")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300174
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300175 self.io_py_remote = self.join_remote("agent.py")
176 self.log_fl = self.join_remote("log.txt")
177 self.pid_file = self.join_remote("pid")
178 self.task_file = self.join_remote("task.cfg")
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300179 self.use_sudo = self.options.get("use_sudo", True)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300180 self.test_logging = self.options.get("test_logging", False)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300181
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300182 fio_command_file = open_for_append_or_create(cmd_log)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300183
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300184 if self.test_logging:
185 soft_runcycle = self.soft_runcycle
186 else:
187 soft_runcycle = None
188
189 self.fio_configs = io_agent.parse_and_slice_all_in_1(
190 self.raw_cfg,
191 self.config_params,
192 soft_runcycle=soft_runcycle)
193
194 self.fio_configs = list(self.fio_configs)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300195 splitter = "\n\n" + "-" * 60 + "\n\n"
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300196
197 cfg = splitter.join(
198 map(io_agent.fio_config_to_str,
199 self.fio_configs))
200
201 fio_command_file.write(cfg)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300202 self.fio_raw_results_file = open_for_append_or_create(raw_res)
203
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300204 def __str__(self):
205 return "{0}({1})".format(self.__class__.__name__,
206 self.node.get_conn_id())
207
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300208 def cleanup(self):
209 # delete_file(conn, self.io_py_remote)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300210 # Need to remove tempo files, used for testing
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300211 pass
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300212
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300213 def prefill_test_files(self):
214 files = {}
215
216 for section in self.configs:
217 sz = ssize_to_b(section.vals['size'])
218 msz = sz / (1024 ** 2)
219
220 if sz % (1024 ** 2) != 0:
221 msz += 1
222
223 fname = section.vals['filename']
224
225 # if already has other test with the same file name
226 # take largest size
227 files[fname] = max(files.get(fname, 0), msz)
228
229 cmd_templ = "dd oflag=direct " + \
230 "if=/dev/zero of={0} bs={1} count={2}"
231
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300232 if self.use_sudo:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300233 cmd_templ = "sudo " + cmd_templ
234
235 ssize = 0
236 stime = time.time()
237
238 for fname, curr_sz in files.items():
239 cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz)
240 ssize += curr_sz
241 self.run_over_ssh(cmd, timeout=curr_sz)
242
243 ddtime = time.time() - stime
244 if ddtime > 1E-3:
245 fill_bw = int(ssize / ddtime)
246 mess = "Initiall dd fill bw is {0} MiBps for this vm"
247 logger.info(mess.format(fill_bw))
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300248 self.coordinate(('init_bw', fill_bw))
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300249
250 def install_utils(self, max_retry=3, timeout=5):
251 need_install = []
252 for bin_name, package in (('fio', 'fio'), ('screen', 'screen')):
253 try:
254 self.run_over_ssh('which ' + bin_name, nolog=True)
255 except OSError:
256 need_install.append(package)
257
koder aka kdanilovafd98742015-04-24 01:27:22 +0300258 if len(need_install) == 0:
259 return
260
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300261 cmd = "sudo apt-get -y install " + " ".join(need_install)
262
263 for i in range(max_retry):
264 try:
265 self.run_over_ssh(cmd)
266 break
267 except OSError as err:
268 time.sleep(timeout)
269 else:
270 raise OSError("Can't install - " + str(err))
271
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300272 def pre_run(self):
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300273 try:
274 cmd = 'mkdir -p "{0}"'.format(self.remote_dir)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300275 if self.use_sudo:
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300276 cmd = "sudo " + cmd
277 cmd += " ; sudo chown {0} {1}".format(self.node.get_user(),
278 self.remote_dir)
279
280 self.run_over_ssh(cmd)
281 except Exception as exc:
282 msg = "Failed to create folder {0} on remote {1}. Error: {2!s}"
283 msg = msg.format(self.remote_dir, self.node.get_conn_id(), exc)
284 logger.error(msg)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300285 raise StopTestError(msg, exc)
koder aka kdanilov783b4542015-04-23 18:57:04 +0300286
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300287 self.install_utils()
koder aka kdanilovda45e882015-04-06 02:24:42 +0300288
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300289 local_fname = os.path.splitext(io_agent.__file__)[0] + ".py"
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300290 files_to_copy = {local_fname: self.io_py_remote}
291 copy_paths(self.node.connection, files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800292
koder aka kdanilove87ae652015-04-20 02:14:35 +0300293 if self.options.get('prefill_files', True):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300294 self.prefill_test_files()
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300295 elif self.is_primary:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300296 logger.warning("Prefilling of test files is disabled")
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800297
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300298 def check_process_is_running(self, sftp, pid):
299 try:
300 sftp.stat("/proc/{0}".format(pid))
301 return True
koder aka kdanilova855f902015-04-26 14:31:45 +0300302 except (OSError, IOError, NameError):
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300303 return False
304
305 def kill_remote_process(self, conn, pid, soft=True):
306 try:
307 if soft:
308 cmd = "kill {0}"
309 else:
310 cmd = "kill -9 {0}"
311
312 if self.use_sudo:
313 cmd = "sudo " + cmd
314
315 self.run_over_ssh(cmd.format(pid))
316 return True
317 except OSError:
318 return False
319
320 def get_test_status(self, die_timeout=3):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300321 is_connected = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300322 is_running = None
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300323 pid = None
324 err = None
325
326 try:
327 conn = connect(self.node.conn_url,
328 conn_timeout=self.tcp_conn_timeout)
329 with conn:
330 with conn.open_sftp() as sftp:
331 try:
332 pid = read_from_remote(sftp, self.pid_file)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300333 is_running = True
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300334 except (NameError, IOError, OSError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300335 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300336 is_running = False
337
338 if is_running:
339 if not self.check_process_is_running(sftp, pid):
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300340 try:
341 sftp.remove(self.pid_file)
342 except (IOError, NameError, OSError):
343 pass
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300344 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300345
346 is_connected = True
347
koder aka kdanilova855f902015-04-26 14:31:45 +0300348 except (socket.error, SSHException, EOFError, SFTPError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300349 err = str(exc)
350 is_connected = False
351
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300352 return is_connected, is_running, pid, err
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300353
koder aka kdanilova855f902015-04-26 14:31:45 +0300354 def wait_till_finished(self, soft_timeout, timeout):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300355 conn_id = self.node.get_conn_id()
356 end_of_wait_time = timeout + time.time()
koder aka kdanilova855f902015-04-26 14:31:45 +0300357 soft_end_of_wait_time = soft_timeout + time.time()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300358
359 # time_till_check = random.randint(30, 90)
360 time_till_check = 5
361 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300362 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300363 pid_get_timeout = self.max_pig_timeout + time.time()
364 curr_connected = True
365
366 while end_of_wait_time > time.time():
367 time.sleep(time_till_check)
368
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300369 is_connected, is_running, npid, err = self.get_test_status()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300370
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300371 if is_connected and not is_running:
372 if pid is None:
373 if time.time() > pid_get_timeout:
374 msg = ("On node {0} pid file doesn't " +
375 "appears in time")
376 logger.error(msg.format(conn_id))
377 raise StopTestError("Start timeout")
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300378 else:
379 # execution finished
380 break
381
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300382 if npid is not None:
383 pid = npid
384
koder aka kdanilova855f902015-04-26 14:31:45 +0300385 if is_connected and pid is not None and is_running:
386 if time.time() < soft_end_of_wait_time:
387 time.sleep(soft_end_of_wait_time - time.time())
388
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300389 if is_connected and not curr_connected:
390 msg = "Connection with {0} is restored"
391 logger.debug(msg.format(conn_id))
392 elif not is_connected and curr_connected:
393 msg = "Lost connection with " + conn_id + ". Error: " + err
394 logger.debug(msg)
395
396 curr_connected = is_connected
397
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300398 def run(self, barrier):
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300399 try:
koder aka kdanilova323b302015-04-26 00:40:22 +0300400 if len(self.fio_configs) > 1 and self.is_primary:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300401
402 exec_time = 0
403 for test in self.fio_configs:
404 exec_time += io_agent.calculate_execution_time(test)
405
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300406 # +5% - is a rough estimation for additional operations
407 # like sftp, etc
408 exec_time = int(exec_time * 1.05)
409
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300410 exec_time_s = sec_to_str(exec_time)
koder aka kdanilova855f902015-04-26 14:31:45 +0300411 now_dt = datetime.datetime.now()
412 end_dt = now_dt + datetime.timedelta(0, exec_time)
413 msg = "Entire test should takes aroud: {0} and finished at {1}"
414 logger.info(msg.format(exec_time_s,
415 end_dt.strftime("%H:%M:%S")))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300416
417 for pos, fio_cfg_slice in enumerate(self.fio_configs):
418 names = [i.name for i in fio_cfg_slice]
419 msgs = []
420 already_processed = set()
421 for name in names:
422 if name not in already_processed:
423 already_processed.add(name)
424
425 if 1 == names.count(name):
426 msgs.append(name)
427 else:
428 frmt = "{0} * {1}"
429 msgs.append(frmt.format(name,
430 names.count(name)))
431
koder aka kdanilova323b302015-04-26 00:40:22 +0300432 if self.is_primary:
433 logger.info("Will run tests: " + ", ".join(msgs))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300434
koder aka kdanilova323b302015-04-26 00:40:22 +0300435 nolog = (pos != 0) or not self.is_primary
436 out_err = self.do_run(barrier, fio_cfg_slice, nolog=nolog)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300437
438 try:
439 for data in parse_output(out_err):
440 data['__meta__']['raw_cfg'] = self.raw_cfg
441 self.on_result_cb(data)
442 except (OSError, StopTestError):
443 raise
444 except Exception as exc:
445 msg_templ = "Error during postprocessing results: {0!s}"
446 raise RuntimeError(msg_templ.format(exc))
447
448 finally:
449 barrier.exit()
450
451 def do_run(self, barrier, cfg, nolog=False):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300452 conn_id = self.node.get_conn_id()
453
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300454 cmd_templ = "screen -S {screen_name} -d -m " + \
455 "env python2 {0} -p {pid_file} -o {results_file} " + \
456 "--type {1} {2} --json {3}"
457
458 if self.options.get("use_sudo", True):
459 cmd_templ = "sudo " + cmd_templ
koder aka kdanilov66839a92015-04-11 13:22:31 +0300460
461 params = " ".join("{0}={1}".format(k, v)
462 for k, v in self.config_params.items())
463
464 if "" != params:
465 params = "--params " + params
466
koder aka kdanilov783b4542015-04-23 18:57:04 +0300467 with self.node.connection.open_sftp() as sftp:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300468 save_to_remote(sftp, self.task_file,
469 io_agent.fio_config_to_str(cfg))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300470
471 screen_name = self.test_uuid
472 cmd = cmd_templ.format(self.io_py_remote,
473 self.tool,
474 params,
475 self.task_file,
476 pid_file=self.pid_file,
477 results_file=self.log_fl,
478 screen_name=screen_name)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300479
480 exec_time = io_agent.calculate_execution_time(cfg)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300481 exec_time_str = sec_to_str(exec_time)
482
koder aka kdanilova855f902015-04-26 14:31:45 +0300483 timeout = int(exec_time + max(300, exec_time))
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300484 soft_tout = exec_time
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300485 barrier.wait()
koder aka kdanilova323b302015-04-26 00:40:22 +0300486 self.run_over_ssh(cmd, nolog=nolog)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300487
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300488 if self.is_primary:
489 templ = "Test should takes about {0}." + \
490 " Should finish at {1}," + \
491 " will wait at most till {2}"
492 now_dt = datetime.datetime.now()
493 end_dt = now_dt + datetime.timedelta(0, exec_time)
494 wait_till = now_dt + datetime.timedelta(0, timeout)
koder aka kdanilovea22c3d2015-04-21 03:42:22 +0300495
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300496 logger.info(templ.format(exec_time_str,
497 end_dt.strftime("%H:%M:%S"),
498 wait_till.strftime("%H:%M:%S")))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300499
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300500 if not nolog:
501 msg = "Tests started in screen {1} on each testnode"
502 logger.debug(msg.format(conn_id, screen_name))
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300503
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300504 # TODO: add monitoring socket
505 if self.node.connection is not Local:
506 self.node.connection.close()
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300507
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300508 self.wait_till_finished(soft_tout, timeout)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300509 if not nolog:
510 logger.debug("Test on node {0} is finished".format(conn_id))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300511
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300512 if self.node.connection is not Local:
513 conn_timeout = self.tcp_conn_timeout * 3
514 self.node.connection = connect(self.node.conn_url,
515 conn_timeout=conn_timeout)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300516
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300517 with self.node.connection.open_sftp() as sftp:
518 return read_from_remote(sftp, self.log_fl)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300519
520 def merge_results(self, results):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300521 if len(results) == 0:
522 return None
523
koder aka kdanilov66839a92015-04-11 13:22:31 +0300524 merged_result = results[0]
525 merged_data = merged_result['res']
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300526 mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300527
528 for res in results[1:]:
529 assert res['__meta__'] == merged_result['__meta__']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300530 data = res['res']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300531
532 for testname, test_data in data.items():
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300533 if testname not in merged_data:
534 merged_data[testname] = test_data
535 continue
536
koder aka kdanilov66839a92015-04-11 13:22:31 +0300537 res_test_data = merged_data[testname]
538
539 diff = set(test_data.keys()).symmetric_difference(
540 res_test_data.keys())
541
542 msg = "Difference: {0}".format(",".join(diff))
543 assert len(diff) == 0, msg
544
545 for k, v in test_data.items():
546 if k in mergable_fields:
547 res_test_data[k].extend(v)
548 else:
549 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
550 assert res_test_data[k] == v, msg
551
552 return merged_result
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300553
554 @classmethod
555 def format_for_console(cls, data):
556 return io_formatter.format_results_for_console(data)