blob: bd98dee2716849c015eb3aa80985a01931bb5fbf [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
koder aka kdanilov783b4542015-04-23 18:57:04 +030010
koder aka kdanilove2de58c2015-04-24 22:59:36 +030011from wally.utils import (ssize_to_b, open_for_append_or_create,
12 sec_to_str, StopTestError)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030013
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030014from wally.ssh_utils import (copy_paths, run_over_ssh,
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030015 save_to_remote,
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030016 # delete_file,
17 connect, read_from_remote, Local)
18
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030019from . import postgres
20from .io import agent as io_agent
21from .io import formatter as io_formatter
22from .io.results_loader import parse_output
koder aka kdanilov652cd802015-04-13 12:21:07 +030023
koder aka kdanilov4643fd62015-02-10 16:20:13 -080024
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030025logger = logging.getLogger("wally")
koder aka kdanilove21d7472015-02-14 19:02:04 -080026
27
koder aka kdanilov4643fd62015-02-10 16:20:13 -080028class IPerfTest(object):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030029 def __init__(self, options, is_primary, on_result_cb, test_uuid, node,
koder aka kdanilov2066daf2015-04-23 21:05:41 +030030 log_directory=None,
31 coordination_queue=None,
32 remote_dir="/tmp/wally"):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030033 self.options = options
koder aka kdanilov4643fd62015-02-10 16:20:13 -080034 self.on_result_cb = on_result_cb
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030035 self.log_directory = log_directory
36 self.node = node
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030037 self.test_uuid = test_uuid
koder aka kdanilovec1b9732015-04-23 20:43:29 +030038 self.coordination_queue = coordination_queue
koder aka kdanilov2066daf2015-04-23 21:05:41 +030039 self.remote_dir = remote_dir
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030040 self.is_primary = is_primary
koder aka kdanilove2de58c2015-04-24 22:59:36 +030041 self.stop_requested = False
42
43 def request_stop(self):
44 self.stop_requested = True
koder aka kdanilov2066daf2015-04-23 21:05:41 +030045
46 def join_remote(self, path):
47 return os.path.join(self.remote_dir, path)
koder aka kdanilovec1b9732015-04-23 20:43:29 +030048
49 def coordinate(self, data):
50 if self.coordination_queue is not None:
koder aka kdanilove2de58c2015-04-24 22:59:36 +030051 self.coordination_queue.put((self.node.get_conn_id(), data))
koder aka kdanilov4643fd62015-02-10 16:20:13 -080052
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030053 def pre_run(self):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080054 pass
55
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030056 def cleanup(self):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030057 pass
58
koder aka kdanilov4643fd62015-02-10 16:20:13 -080059 @abc.abstractmethod
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030060 def run(self, barrier):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080061 pass
62
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030063 @classmethod
64 def format_for_console(cls, data):
65 msg = "{0}.format_for_console".format(cls.__name__)
66 raise NotImplementedError(msg)
67
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030068 def run_over_ssh(self, cmd, **kwargs):
69 return run_over_ssh(self.node.connection, cmd,
70 node=self.node.get_conn_id(), **kwargs)
71
koder aka kdanilovec1b9732015-04-23 20:43:29 +030072 @classmethod
73 def coordination_th(cls, coord_q, barrier, num_threads):
74 pass
75
koder aka kdanilov4643fd62015-02-10 16:20:13 -080076
Yulia Portnova7ddfa732015-02-24 17:32:58 +020077class TwoScriptTest(IPerfTest):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030078 def __init__(self, *dt, **mp):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030079 IPerfTest.__init__(self, *dt, **mp)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020080
koder aka kdanilovabd6ead2015-04-24 02:03:07 +030081 if 'run_script' in self.options:
82 self.run_script = self.options['run_script']
83 self.prepare_script = self.options['prepare_script']
Yulia Portnova7ddfa732015-02-24 17:32:58 +020084
85 def get_remote_for_script(self, script):
86 return os.path.join(self.tmp_dir, script.rpartition('/')[2])
87
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030088 def copy_script(self, src):
Yulia Portnova7ddfa732015-02-24 17:32:58 +020089 remote_path = self.get_remote_for_script(src)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030090 copy_paths(self.node.connection, {src: remote_path})
Yulia Portnova7ddfa732015-02-24 17:32:58 +020091 return remote_path
92
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030093 def pre_run(self):
94 remote_script = self.copy_script(self.node.connection,
95 self.pre_run_script)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020096 cmd = remote_script
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030097 self.run_over_ssh(cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020098
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030099 def run(self, barrier):
100 remote_script = self.copy_script(self.node.connection, self.run_script)
Yulia Portnova886a2562015-04-07 11:16:13 +0300101 cmd_opts = ' '.join(["%s %s" % (key, val) for key, val
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300102 in self.options.items()])
Yulia Portnova886a2562015-04-07 11:16:13 +0300103 cmd = remote_script + ' ' + cmd_opts
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300104 out_err = self.run_over_ssh(cmd)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300105 self.on_result(out_err, cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200106
107 def parse_results(self, out):
108 for line in out.split("\n"):
109 key, separator, value = line.partition(":")
110 if key and value:
111 self.on_result_cb((key, float(value)))
112
koder aka kdanilov66839a92015-04-11 13:22:31 +0300113 def on_result(self, out_err, cmd):
114 try:
115 self.parse_results(out_err)
116 except Exception as exc:
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300117 msg_templ = "Error during postprocessing results: {0!s}. {1}"
118 raise RuntimeError(msg_templ.format(exc, out_err))
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200119
120
121class PgBenchTest(TwoScriptTest):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300122 root = os.path.dirname(postgres.__file__)
123 prepare_script = os.path.join(root, "prepare.sh")
124 run_script = os.path.join(root, "run.sh")
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300125
126
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800127class IOPerfTest(IPerfTest):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300128 tcp_conn_timeout = 30
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300129 max_pig_timeout = 5
130 soft_runcycle = 5 * 60
koder aka kdanilov2c473092015-03-29 17:12:13 +0300131
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300132 def __init__(self, *dt, **mp):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300133 IPerfTest.__init__(self, *dt, **mp)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300134 self.config_fname = self.options['cfg']
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300135
136 if '/' not in self.config_fname and '.' not in self.config_fname:
137 cfgs_dir = os.path.dirname(io_agent.__file__)
138 self.config_fname = os.path.join(cfgs_dir,
139 self.config_fname + '.cfg')
140
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300141 self.alive_check_interval = self.options.get('alive_check_interval')
142 self.config_params = self.options.get('params', {})
143 self.tool = self.options.get('tool', 'fio')
koder aka kdanilovda45e882015-04-06 02:24:42 +0300144 self.raw_cfg = open(self.config_fname).read()
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300145 self.configs = list(io_agent.parse_all_in_1(self.raw_cfg,
146 self.config_params))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800147
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300148 cmd_log = os.path.join(self.log_directory, "task_compiled.cfg")
149 raw_res = os.path.join(self.log_directory, "raw_results.txt")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300150
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300151 self.io_py_remote = self.join_remote("agent.py")
152 self.log_fl = self.join_remote("log.txt")
153 self.pid_file = self.join_remote("pid")
154 self.task_file = self.join_remote("task.cfg")
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300155 self.use_sudo = self.options.get("use_sudo", True)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300156 self.test_logging = self.options.get("test_logging", False)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300157
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300158 fio_command_file = open_for_append_or_create(cmd_log)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300159
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300160 if self.test_logging:
161 soft_runcycle = self.soft_runcycle
162 else:
163 soft_runcycle = None
164
165 self.fio_configs = io_agent.parse_and_slice_all_in_1(
166 self.raw_cfg,
167 self.config_params,
168 soft_runcycle=soft_runcycle)
169
170 self.fio_configs = list(self.fio_configs)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300171 splitter = "\n\n" + "-" * 60 + "\n\n"
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300172
173 cfg = splitter.join(
174 map(io_agent.fio_config_to_str,
175 self.fio_configs))
176
177 fio_command_file.write(cfg)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300178 self.fio_raw_results_file = open_for_append_or_create(raw_res)
179
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300180 def __str__(self):
181 return "{0}({1})".format(self.__class__.__name__,
182 self.node.get_conn_id())
183
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300184 def cleanup(self):
185 # delete_file(conn, self.io_py_remote)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300186 # Need to remove tempo files, used for testing
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300187 pass
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300188
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300189 def prefill_test_files(self):
190 files = {}
191
192 for section in self.configs:
193 sz = ssize_to_b(section.vals['size'])
194 msz = sz / (1024 ** 2)
195
196 if sz % (1024 ** 2) != 0:
197 msz += 1
198
199 fname = section.vals['filename']
200
201 # if already has other test with the same file name
202 # take largest size
203 files[fname] = max(files.get(fname, 0), msz)
204
205 cmd_templ = "dd oflag=direct " + \
206 "if=/dev/zero of={0} bs={1} count={2}"
207
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300208 if self.use_sudo:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300209 cmd_templ = "sudo " + cmd_templ
210
211 ssize = 0
212 stime = time.time()
213
214 for fname, curr_sz in files.items():
215 cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz)
216 ssize += curr_sz
217 self.run_over_ssh(cmd, timeout=curr_sz)
218
219 ddtime = time.time() - stime
220 if ddtime > 1E-3:
221 fill_bw = int(ssize / ddtime)
222 mess = "Initiall dd fill bw is {0} MiBps for this vm"
223 logger.info(mess.format(fill_bw))
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300224 self.coordinate(('init_bw', fill_bw))
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300225
226 def install_utils(self, max_retry=3, timeout=5):
227 need_install = []
228 for bin_name, package in (('fio', 'fio'), ('screen', 'screen')):
229 try:
230 self.run_over_ssh('which ' + bin_name, nolog=True)
231 except OSError:
232 need_install.append(package)
233
koder aka kdanilovafd98742015-04-24 01:27:22 +0300234 if len(need_install) == 0:
235 return
236
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300237 cmd = "sudo apt-get -y install " + " ".join(need_install)
238
239 for i in range(max_retry):
240 try:
241 self.run_over_ssh(cmd)
242 break
243 except OSError as err:
244 time.sleep(timeout)
245 else:
246 raise OSError("Can't install - " + str(err))
247
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300248 def pre_run(self):
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300249 try:
250 cmd = 'mkdir -p "{0}"'.format(self.remote_dir)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300251 if self.use_sudo:
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300252 cmd = "sudo " + cmd
253 cmd += " ; sudo chown {0} {1}".format(self.node.get_user(),
254 self.remote_dir)
255
256 self.run_over_ssh(cmd)
257 except Exception as exc:
258 msg = "Failed to create folder {0} on remote {1}. Error: {2!s}"
259 msg = msg.format(self.remote_dir, self.node.get_conn_id(), exc)
260 logger.error(msg)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300261 raise StopTestError(msg, exc)
koder aka kdanilov783b4542015-04-23 18:57:04 +0300262
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300263 self.install_utils()
koder aka kdanilovda45e882015-04-06 02:24:42 +0300264
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300265 local_fname = os.path.splitext(io_agent.__file__)[0] + ".py"
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300266 files_to_copy = {local_fname: self.io_py_remote}
267 copy_paths(self.node.connection, files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800268
koder aka kdanilove87ae652015-04-20 02:14:35 +0300269 if self.options.get('prefill_files', True):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300270 self.prefill_test_files()
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300271 elif self.is_primary:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300272 logger.warning("Prefilling of test files is disabled")
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800273
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300274 def check_process_is_running(self, sftp, pid):
275 try:
276 sftp.stat("/proc/{0}".format(pid))
277 return True
koder aka kdanilova855f902015-04-26 14:31:45 +0300278 except (OSError, IOError, NameError):
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300279 return False
280
281 def kill_remote_process(self, conn, pid, soft=True):
282 try:
283 if soft:
284 cmd = "kill {0}"
285 else:
286 cmd = "kill -9 {0}"
287
288 if self.use_sudo:
289 cmd = "sudo " + cmd
290
291 self.run_over_ssh(cmd.format(pid))
292 return True
293 except OSError:
294 return False
295
296 def get_test_status(self, die_timeout=3):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300297 is_connected = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300298 is_running = None
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300299 pid = None
300 err = None
301
302 try:
303 conn = connect(self.node.conn_url,
304 conn_timeout=self.tcp_conn_timeout)
305 with conn:
306 with conn.open_sftp() as sftp:
307 try:
308 pid = read_from_remote(sftp, self.pid_file)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300309 is_running = True
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300310 except (NameError, IOError, OSError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300311 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300312 is_running = False
313
314 if is_running:
315 if not self.check_process_is_running(sftp, pid):
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300316 try:
317 sftp.remove(self.pid_file)
318 except (IOError, NameError, OSError):
319 pass
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300320 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300321
322 is_connected = True
323
koder aka kdanilova855f902015-04-26 14:31:45 +0300324 except (socket.error, SSHException, EOFError, SFTPError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300325 err = str(exc)
326 is_connected = False
327
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300328 return is_connected, is_running, pid, err
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300329
koder aka kdanilova855f902015-04-26 14:31:45 +0300330 def wait_till_finished(self, soft_timeout, timeout):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300331 conn_id = self.node.get_conn_id()
332 end_of_wait_time = timeout + time.time()
koder aka kdanilova855f902015-04-26 14:31:45 +0300333 soft_end_of_wait_time = soft_timeout + time.time()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300334
335 # time_till_check = random.randint(30, 90)
336 time_till_check = 5
337 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300338 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300339 pid_get_timeout = self.max_pig_timeout + time.time()
340 curr_connected = True
341
342 while end_of_wait_time > time.time():
343 time.sleep(time_till_check)
344
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300345 is_connected, is_running, npid, err = self.get_test_status()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300346
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300347 if is_connected and not is_running:
348 if pid is None:
349 if time.time() > pid_get_timeout:
350 msg = ("On node {0} pid file doesn't " +
351 "appears in time")
352 logger.error(msg.format(conn_id))
353 raise StopTestError("Start timeout")
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300354 else:
355 # execution finished
356 break
357
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300358 if npid is not None:
359 pid = npid
360
koder aka kdanilova855f902015-04-26 14:31:45 +0300361 if is_connected and pid is not None and is_running:
362 if time.time() < soft_end_of_wait_time:
363 time.sleep(soft_end_of_wait_time - time.time())
364
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300365 if is_connected and not curr_connected:
366 msg = "Connection with {0} is restored"
367 logger.debug(msg.format(conn_id))
368 elif not is_connected and curr_connected:
369 msg = "Lost connection with " + conn_id + ". Error: " + err
370 logger.debug(msg)
371
372 curr_connected = is_connected
373
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300374 def run(self, barrier):
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300375 try:
koder aka kdanilova323b302015-04-26 00:40:22 +0300376 if len(self.fio_configs) > 1 and self.is_primary:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300377
378 exec_time = 0
379 for test in self.fio_configs:
380 exec_time += io_agent.calculate_execution_time(test)
381
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300382 # +5% - is a rough estimation for additional operations
383 # like sftp, etc
384 exec_time = int(exec_time * 1.05)
385
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300386 exec_time_s = sec_to_str(exec_time)
koder aka kdanilova855f902015-04-26 14:31:45 +0300387 now_dt = datetime.datetime.now()
388 end_dt = now_dt + datetime.timedelta(0, exec_time)
389 msg = "Entire test should takes aroud: {0} and finished at {1}"
390 logger.info(msg.format(exec_time_s,
391 end_dt.strftime("%H:%M:%S")))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300392
393 for pos, fio_cfg_slice in enumerate(self.fio_configs):
394 names = [i.name for i in fio_cfg_slice]
395 msgs = []
396 already_processed = set()
397 for name in names:
398 if name not in already_processed:
399 already_processed.add(name)
400
401 if 1 == names.count(name):
402 msgs.append(name)
403 else:
404 frmt = "{0} * {1}"
405 msgs.append(frmt.format(name,
406 names.count(name)))
407
koder aka kdanilova323b302015-04-26 00:40:22 +0300408 if self.is_primary:
409 logger.info("Will run tests: " + ", ".join(msgs))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300410
koder aka kdanilova323b302015-04-26 00:40:22 +0300411 nolog = (pos != 0) or not self.is_primary
412 out_err = self.do_run(barrier, fio_cfg_slice, nolog=nolog)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300413
414 try:
415 for data in parse_output(out_err):
416 data['__meta__']['raw_cfg'] = self.raw_cfg
417 self.on_result_cb(data)
418 except (OSError, StopTestError):
419 raise
420 except Exception as exc:
421 msg_templ = "Error during postprocessing results: {0!s}"
422 raise RuntimeError(msg_templ.format(exc))
423
424 finally:
425 barrier.exit()
426
427 def do_run(self, barrier, cfg, nolog=False):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300428 conn_id = self.node.get_conn_id()
429
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300430 cmd_templ = "screen -S {screen_name} -d -m " + \
431 "env python2 {0} -p {pid_file} -o {results_file} " + \
432 "--type {1} {2} --json {3}"
433
434 if self.options.get("use_sudo", True):
435 cmd_templ = "sudo " + cmd_templ
koder aka kdanilov66839a92015-04-11 13:22:31 +0300436
437 params = " ".join("{0}={1}".format(k, v)
438 for k, v in self.config_params.items())
439
440 if "" != params:
441 params = "--params " + params
442
koder aka kdanilov783b4542015-04-23 18:57:04 +0300443 with self.node.connection.open_sftp() as sftp:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300444 save_to_remote(sftp, self.task_file,
445 io_agent.fio_config_to_str(cfg))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300446
447 screen_name = self.test_uuid
448 cmd = cmd_templ.format(self.io_py_remote,
449 self.tool,
450 params,
451 self.task_file,
452 pid_file=self.pid_file,
453 results_file=self.log_fl,
454 screen_name=screen_name)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300455
456 exec_time = io_agent.calculate_execution_time(cfg)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300457 exec_time_str = sec_to_str(exec_time)
458
koder aka kdanilova855f902015-04-26 14:31:45 +0300459 timeout = int(exec_time + max(300, exec_time))
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300460 soft_tout = exec_time
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300461 barrier.wait()
koder aka kdanilova323b302015-04-26 00:40:22 +0300462 self.run_over_ssh(cmd, nolog=nolog)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300463
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300464 if self.is_primary:
465 templ = "Test should takes about {0}." + \
466 " Should finish at {1}," + \
467 " will wait at most till {2}"
468 now_dt = datetime.datetime.now()
469 end_dt = now_dt + datetime.timedelta(0, exec_time)
470 wait_till = now_dt + datetime.timedelta(0, timeout)
koder aka kdanilovea22c3d2015-04-21 03:42:22 +0300471
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300472 logger.info(templ.format(exec_time_str,
473 end_dt.strftime("%H:%M:%S"),
474 wait_till.strftime("%H:%M:%S")))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300475
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300476 if not nolog:
477 msg = "Tests started in screen {1} on each testnode"
478 logger.debug(msg.format(conn_id, screen_name))
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300479
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300480 # TODO: add monitoring socket
481 if self.node.connection is not Local:
482 self.node.connection.close()
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300483
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300484 self.wait_till_finished(soft_tout, timeout)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300485 if not nolog:
486 logger.debug("Test on node {0} is finished".format(conn_id))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300487
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300488 if self.node.connection is not Local:
489 conn_timeout = self.tcp_conn_timeout * 3
490 self.node.connection = connect(self.node.conn_url,
491 conn_timeout=conn_timeout)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300492
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300493 with self.node.connection.open_sftp() as sftp:
494 return read_from_remote(sftp, self.log_fl)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300495
496 def merge_results(self, results):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300497 if len(results) == 0:
498 return None
499
koder aka kdanilov66839a92015-04-11 13:22:31 +0300500 merged_result = results[0]
501 merged_data = merged_result['res']
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300502 mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300503
504 for res in results[1:]:
505 assert res['__meta__'] == merged_result['__meta__']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300506 data = res['res']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300507
508 for testname, test_data in data.items():
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300509 if testname not in merged_data:
510 merged_data[testname] = test_data
511 continue
512
koder aka kdanilov66839a92015-04-11 13:22:31 +0300513 res_test_data = merged_data[testname]
514
515 diff = set(test_data.keys()).symmetric_difference(
516 res_test_data.keys())
517
518 msg = "Difference: {0}".format(",".join(diff))
519 assert len(diff) == 0, msg
520
521 for k, v in test_data.items():
522 if k in mergable_fields:
523 res_test_data[k].extend(v)
524 else:
525 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
526 assert res_test_data[k] == v, msg
527
528 return merged_result
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300529
530 @classmethod
531 def format_for_console(cls, data):
532 return io_formatter.format_results_for_console(data)