blob: f0a1e8d9692fb147430221eb0204692913d7648c [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,
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:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300217 sz = ssize2b(section.vals['size'])
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300218 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
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300359 time_till_check = random.randint(5, 10)
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300360 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300361 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300362 pid_get_timeout = self.max_pig_timeout + time.time()
363 curr_connected = True
364
365 while end_of_wait_time > time.time():
366 time.sleep(time_till_check)
367
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300368 is_connected, is_running, npid, err = self.get_test_status()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300369
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300370 if is_connected and not is_running:
371 if pid is None:
372 if time.time() > pid_get_timeout:
373 msg = ("On node {0} pid file doesn't " +
374 "appears in time")
375 logger.error(msg.format(conn_id))
376 raise StopTestError("Start timeout")
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300377 else:
378 # execution finished
379 break
380
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300381 if npid is not None:
382 pid = npid
383
koder aka kdanilova855f902015-04-26 14:31:45 +0300384 if is_connected and pid is not None and is_running:
385 if time.time() < soft_end_of_wait_time:
386 time.sleep(soft_end_of_wait_time - time.time())
387
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300388 if is_connected and not curr_connected:
389 msg = "Connection with {0} is restored"
390 logger.debug(msg.format(conn_id))
391 elif not is_connected and curr_connected:
392 msg = "Lost connection with " + conn_id + ". Error: " + err
393 logger.debug(msg)
394
395 curr_connected = is_connected
396
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300397 def run(self, barrier):
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300398 try:
koder aka kdanilova323b302015-04-26 00:40:22 +0300399 if len(self.fio_configs) > 1 and self.is_primary:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300400
401 exec_time = 0
402 for test in self.fio_configs:
403 exec_time += io_agent.calculate_execution_time(test)
404
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300405 # +5% - is a rough estimation for additional operations
406 # like sftp, etc
407 exec_time = int(exec_time * 1.05)
408
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300409 exec_time_s = sec_to_str(exec_time)
koder aka kdanilova855f902015-04-26 14:31:45 +0300410 now_dt = datetime.datetime.now()
411 end_dt = now_dt + datetime.timedelta(0, exec_time)
412 msg = "Entire test should takes aroud: {0} and finished at {1}"
413 logger.info(msg.format(exec_time_s,
414 end_dt.strftime("%H:%M:%S")))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300415
416 for pos, fio_cfg_slice in enumerate(self.fio_configs):
417 names = [i.name for i in fio_cfg_slice]
418 msgs = []
419 already_processed = set()
420 for name in names:
421 if name not in already_processed:
422 already_processed.add(name)
423
424 if 1 == names.count(name):
425 msgs.append(name)
426 else:
427 frmt = "{0} * {1}"
428 msgs.append(frmt.format(name,
429 names.count(name)))
430
koder aka kdanilova323b302015-04-26 00:40:22 +0300431 if self.is_primary:
432 logger.info("Will run tests: " + ", ".join(msgs))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300433
koder aka kdanilova323b302015-04-26 00:40:22 +0300434 nolog = (pos != 0) or not self.is_primary
435 out_err = self.do_run(barrier, fio_cfg_slice, nolog=nolog)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300436
437 try:
438 for data in parse_output(out_err):
439 data['__meta__']['raw_cfg'] = self.raw_cfg
440 self.on_result_cb(data)
441 except (OSError, StopTestError):
442 raise
443 except Exception as exc:
444 msg_templ = "Error during postprocessing results: {0!s}"
445 raise RuntimeError(msg_templ.format(exc))
446
447 finally:
448 barrier.exit()
449
450 def do_run(self, barrier, cfg, nolog=False):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300451 conn_id = self.node.get_conn_id()
452
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300453 cmd_templ = "screen -S {screen_name} -d -m " + \
454 "env python2 {0} -p {pid_file} -o {results_file} " + \
455 "--type {1} {2} --json {3}"
456
457 if self.options.get("use_sudo", True):
458 cmd_templ = "sudo " + cmd_templ
koder aka kdanilov66839a92015-04-11 13:22:31 +0300459
460 params = " ".join("{0}={1}".format(k, v)
461 for k, v in self.config_params.items())
462
463 if "" != params:
464 params = "--params " + params
465
koder aka kdanilov783b4542015-04-23 18:57:04 +0300466 with self.node.connection.open_sftp() as sftp:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300467 save_to_remote(sftp, self.task_file,
468 io_agent.fio_config_to_str(cfg))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300469
470 screen_name = self.test_uuid
471 cmd = cmd_templ.format(self.io_py_remote,
472 self.tool,
473 params,
474 self.task_file,
475 pid_file=self.pid_file,
476 results_file=self.log_fl,
477 screen_name=screen_name)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300478
479 exec_time = io_agent.calculate_execution_time(cfg)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300480 exec_time_str = sec_to_str(exec_time)
481
koder aka kdanilova855f902015-04-26 14:31:45 +0300482 timeout = int(exec_time + max(300, exec_time))
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300483 soft_tout = exec_time
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300484 barrier.wait()
koder aka kdanilova323b302015-04-26 00:40:22 +0300485 self.run_over_ssh(cmd, nolog=nolog)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300486 if self.is_primary:
487 templ = "Test should takes about {0}." + \
488 " Should finish at {1}," + \
489 " will wait at most till {2}"
490 now_dt = datetime.datetime.now()
491 end_dt = now_dt + datetime.timedelta(0, exec_time)
492 wait_till = now_dt + datetime.timedelta(0, timeout)
koder aka kdanilovea22c3d2015-04-21 03:42:22 +0300493
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300494 logger.info(templ.format(exec_time_str,
495 end_dt.strftime("%H:%M:%S"),
496 wait_till.strftime("%H:%M:%S")))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300497
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300498 if not nolog:
499 msg = "Tests started in screen {1} on each testnode"
500 logger.debug(msg.format(conn_id, screen_name))
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300501
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300502 # TODO: add monitoring socket
503 if self.node.connection is not Local:
504 self.node.connection.close()
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300505
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300506 self.wait_till_finished(soft_tout, timeout)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300507 if not nolog:
508 logger.debug("Test on node {0} is finished".format(conn_id))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300509
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300510 if self.node.connection is not Local:
511 conn_timeout = self.tcp_conn_timeout * 3
512 self.node.connection = connect(self.node.conn_url,
513 conn_timeout=conn_timeout)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300514
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300515 with self.node.connection.open_sftp() as sftp:
516 return read_from_remote(sftp, self.log_fl)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300517
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300518 @classmethod
519 def merge_results(cls, results):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300520 if len(results) == 0:
521 return None
522
koder aka kdanilov66839a92015-04-11 13:22:31 +0300523 merged_result = results[0]
524 merged_data = merged_result['res']
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300525 mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300526
527 for res in results[1:]:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300528 mm = merged_result['__meta__']
529 assert mm['raw_cfg'] == res['__meta__']['raw_cfg']
530 assert mm['params'] == res['__meta__']['params']
531 mm['timings'].extend(res['__meta__']['timings'])
koder aka kdanilov66839a92015-04-11 13:22:31 +0300532
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300533 data = res['res']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300534 for testname, test_data in data.items():
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300535 if testname not in merged_data:
536 merged_data[testname] = test_data
537 continue
538
koder aka kdanilov66839a92015-04-11 13:22:31 +0300539 res_test_data = merged_data[testname]
540
541 diff = set(test_data.keys()).symmetric_difference(
542 res_test_data.keys())
543
544 msg = "Difference: {0}".format(",".join(diff))
545 assert len(diff) == 0, msg
546
547 for k, v in test_data.items():
548 if k in mergable_fields:
549 res_test_data[k].extend(v)
550 else:
551 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
552 assert res_test_data[k] == v, msg
553
554 return merged_result
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300555
556 @classmethod
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300557 def format_for_console(cls, data, dinfo):
558 return io_formatter.format_results_for_console(data, dinfo)