blob: dd52f33b8b3b37c8d84841be930b5a98226970b6 [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 kdanilovf86d7af2015-05-06 04:01:54 +030012from wally.utils import (ssize2b, open_for_append_or_create,
koder aka kdanilove2de58c2015-04-24 22:59:36 +030013 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,
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300192 soft_runcycle=soft_runcycle,
193 split_on_names=self.test_logging)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300194
195 self.fio_configs = list(self.fio_configs)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300196 splitter = "\n\n" + "-" * 60 + "\n\n"
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300197
198 cfg = splitter.join(
199 map(io_agent.fio_config_to_str,
200 self.fio_configs))
201
202 fio_command_file.write(cfg)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300203 self.fio_raw_results_file = open_for_append_or_create(raw_res)
204
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300205 def __str__(self):
206 return "{0}({1})".format(self.__class__.__name__,
207 self.node.get_conn_id())
208
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300209 def cleanup(self):
210 # delete_file(conn, self.io_py_remote)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300211 # Need to remove tempo files, used for testing
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300212 pass
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300213
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300214 def prefill_test_files(self):
215 files = {}
216
217 for section in self.configs:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300218 sz = ssize2b(section.vals['size'])
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300219 msz = sz / (1024 ** 2)
220
221 if sz % (1024 ** 2) != 0:
222 msz += 1
223
224 fname = section.vals['filename']
225
226 # if already has other test with the same file name
227 # take largest size
228 files[fname] = max(files.get(fname, 0), msz)
229
230 cmd_templ = "dd oflag=direct " + \
231 "if=/dev/zero of={0} bs={1} count={2}"
232
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300233 if self.use_sudo:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300234 cmd_templ = "sudo " + cmd_templ
235
236 ssize = 0
237 stime = time.time()
238
239 for fname, curr_sz in files.items():
240 cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz)
241 ssize += curr_sz
242 self.run_over_ssh(cmd, timeout=curr_sz)
243
244 ddtime = time.time() - stime
245 if ddtime > 1E-3:
246 fill_bw = int(ssize / ddtime)
247 mess = "Initiall dd fill bw is {0} MiBps for this vm"
248 logger.info(mess.format(fill_bw))
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300249 self.coordinate(('init_bw', fill_bw))
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300250
251 def install_utils(self, max_retry=3, timeout=5):
252 need_install = []
253 for bin_name, package in (('fio', 'fio'), ('screen', 'screen')):
254 try:
255 self.run_over_ssh('which ' + bin_name, nolog=True)
256 except OSError:
257 need_install.append(package)
258
koder aka kdanilovafd98742015-04-24 01:27:22 +0300259 if len(need_install) == 0:
260 return
261
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300262 cmd = "sudo apt-get -y install " + " ".join(need_install)
263
264 for i in range(max_retry):
265 try:
266 self.run_over_ssh(cmd)
267 break
268 except OSError as err:
269 time.sleep(timeout)
270 else:
271 raise OSError("Can't install - " + str(err))
272
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300273 def pre_run(self):
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300274 try:
275 cmd = 'mkdir -p "{0}"'.format(self.remote_dir)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300276 if self.use_sudo:
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300277 cmd = "sudo " + cmd
278 cmd += " ; sudo chown {0} {1}".format(self.node.get_user(),
279 self.remote_dir)
280
281 self.run_over_ssh(cmd)
282 except Exception as exc:
283 msg = "Failed to create folder {0} on remote {1}. Error: {2!s}"
284 msg = msg.format(self.remote_dir, self.node.get_conn_id(), exc)
285 logger.error(msg)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300286 raise StopTestError(msg, exc)
koder aka kdanilov783b4542015-04-23 18:57:04 +0300287
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300288 self.install_utils()
koder aka kdanilovda45e882015-04-06 02:24:42 +0300289
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300290 local_fname = os.path.splitext(io_agent.__file__)[0] + ".py"
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300291 files_to_copy = {local_fname: self.io_py_remote}
292 copy_paths(self.node.connection, files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800293
koder aka kdanilove87ae652015-04-20 02:14:35 +0300294 if self.options.get('prefill_files', True):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300295 self.prefill_test_files()
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300296 elif self.is_primary:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300297 logger.warning("Prefilling of test files is disabled")
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800298
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300299 def check_process_is_running(self, sftp, pid):
300 try:
301 sftp.stat("/proc/{0}".format(pid))
302 return True
koder aka kdanilova855f902015-04-26 14:31:45 +0300303 except (OSError, IOError, NameError):
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300304 return False
305
306 def kill_remote_process(self, conn, pid, soft=True):
307 try:
308 if soft:
309 cmd = "kill {0}"
310 else:
311 cmd = "kill -9 {0}"
312
313 if self.use_sudo:
314 cmd = "sudo " + cmd
315
316 self.run_over_ssh(cmd.format(pid))
317 return True
318 except OSError:
319 return False
320
321 def get_test_status(self, die_timeout=3):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300322 is_connected = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300323 is_running = None
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300324 pid = None
325 err = None
326
327 try:
328 conn = connect(self.node.conn_url,
329 conn_timeout=self.tcp_conn_timeout)
330 with conn:
331 with conn.open_sftp() as sftp:
332 try:
333 pid = read_from_remote(sftp, self.pid_file)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300334 is_running = True
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300335 except (NameError, IOError, OSError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300336 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300337 is_running = False
338
339 if is_running:
340 if not self.check_process_is_running(sftp, pid):
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300341 try:
342 sftp.remove(self.pid_file)
343 except (IOError, NameError, OSError):
344 pass
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300345 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300346
347 is_connected = True
348
koder aka kdanilova855f902015-04-26 14:31:45 +0300349 except (socket.error, SSHException, EOFError, SFTPError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300350 err = str(exc)
351 is_connected = False
352
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300353 return is_connected, is_running, pid, err
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300354
koder aka kdanilova855f902015-04-26 14:31:45 +0300355 def wait_till_finished(self, soft_timeout, timeout):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300356 conn_id = self.node.get_conn_id()
357 end_of_wait_time = timeout + time.time()
koder aka kdanilova855f902015-04-26 14:31:45 +0300358 soft_end_of_wait_time = soft_timeout + time.time()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300359
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300360 time_till_check = random.randint(5, 10)
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300361 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 kdanilov57ce4db2015-04-25 21:25:51 +0300487 if self.is_primary:
488 templ = "Test should takes about {0}." + \
489 " Should finish at {1}," + \
490 " will wait at most till {2}"
491 now_dt = datetime.datetime.now()
492 end_dt = now_dt + datetime.timedelta(0, exec_time)
493 wait_till = now_dt + datetime.timedelta(0, timeout)
koder aka kdanilovea22c3d2015-04-21 03:42:22 +0300494
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300495 logger.info(templ.format(exec_time_str,
496 end_dt.strftime("%H:%M:%S"),
497 wait_till.strftime("%H:%M:%S")))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300498
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300499 if not nolog:
500 msg = "Tests started in screen {1} on each testnode"
501 logger.debug(msg.format(conn_id, screen_name))
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300502
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300503 # TODO: add monitoring socket
504 if self.node.connection is not Local:
505 self.node.connection.close()
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300506
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300507 self.wait_till_finished(soft_tout, timeout)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300508 if not nolog:
509 logger.debug("Test on node {0} is finished".format(conn_id))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300510
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300511 if self.node.connection is not Local:
512 conn_timeout = self.tcp_conn_timeout * 3
513 self.node.connection = connect(self.node.conn_url,
514 conn_timeout=conn_timeout)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300515
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300516 with self.node.connection.open_sftp() as sftp:
517 return read_from_remote(sftp, self.log_fl)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300518
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300519 @classmethod
520 def merge_results(cls, 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:]:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300529 mm = merged_result['__meta__']
530 assert mm['raw_cfg'] == res['__meta__']['raw_cfg']
531 assert mm['params'] == res['__meta__']['params']
532 mm['timings'].extend(res['__meta__']['timings'])
koder aka kdanilov66839a92015-04-11 13:22:31 +0300533
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300534 data = res['res']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300535 for testname, test_data in data.items():
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300536 if testname not in merged_data:
537 merged_data[testname] = test_data
538 continue
539
koder aka kdanilov66839a92015-04-11 13:22:31 +0300540 res_test_data = merged_data[testname]
541
542 diff = set(test_data.keys()).symmetric_difference(
543 res_test_data.keys())
544
545 msg = "Difference: {0}".format(",".join(diff))
546 assert len(diff) == 0, msg
547
548 for k, v in test_data.items():
549 if k in mergable_fields:
550 res_test_data[k].extend(v)
551 else:
552 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
553 assert res_test_data[k] == v, msg
554
555 return merged_result
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300556
557 @classmethod
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300558 def format_for_console(cls, data, dinfo):
559 return io_formatter.format_results_for_console(data, dinfo)