blob: c3848a77e826769d42ea8b20c7573c60e10fa3c2 [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
Yulia Portnova7ddfa732015-02-24 17:32:58 +020091
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030092 def pre_run(self):
Yulia Portnovab1a15072015-05-06 14:59:25 +030093 copy_paths(self.node.connection, {self.root: self.remote_dir})
94 cmd = self.get_remote_for_script(self.pre_run_script)
95 self.run_over_ssh(cmd, timeout=2000)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020096
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030097 def run(self, barrier):
Yulia Portnovab1a15072015-05-06 14:59:25 +030098 remote_script = self.get_remote_for_script(self.run_script)
Yulia Portnova886a2562015-04-07 11:16:13 +030099 cmd_opts = ' '.join(["%s %s" % (key, val) for key, val
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300100 in self.options.items()])
Yulia Portnova886a2562015-04-07 11:16:13 +0300101 cmd = remote_script + ' ' + cmd_opts
Yulia Portnovab1a15072015-05-06 14:59:25 +0300102 out_err = self.run_over_ssh(cmd, timeout=6000)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300103 self.on_result(out_err, cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200104
105 def parse_results(self, out):
106 for line in out.split("\n"):
107 key, separator, value = line.partition(":")
108 if key and value:
109 self.on_result_cb((key, float(value)))
110
koder aka kdanilov66839a92015-04-11 13:22:31 +0300111 def on_result(self, out_err, cmd):
112 try:
113 self.parse_results(out_err)
114 except Exception as exc:
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300115 msg_templ = "Error during postprocessing results: {0!s}. {1}"
116 raise RuntimeError(msg_templ.format(exc, out_err))
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200117
Yulia Portnovab1a15072015-05-06 14:59:25 +0300118 def merge_results(self, results):
119 tpcm = sum([val[1] for val in results])
120 return {"res": {"TpmC": tpcm}}
121
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200122
123class PgBenchTest(TwoScriptTest):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300124 root = os.path.dirname(postgres.__file__)
Yulia Portnovab1a15072015-05-06 14:59:25 +0300125 pre_run_script = os.path.join(root, "prepare.sh")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300126 run_script = os.path.join(root, "run.sh")
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300127
128
Yulia Portnovab1a15072015-05-06 14:59:25 +0300129class MysqlTest(TwoScriptTest):
130 root = os.path.dirname(mysql.__file__)
131 pre_run_script = os.path.join(root, "prepare.sh")
132 run_script = os.path.join(root, "run.sh")
133
134 @classmethod
135 def format_for_console(cls, data):
136 tab = texttable.Texttable(max_width=120)
137 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
138 tab.header(["TpmC"])
139 tab.add_row([data['res']['TpmC']])
140 return tab.draw()
141
142
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800143class IOPerfTest(IPerfTest):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300144 tcp_conn_timeout = 30
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300145 max_pig_timeout = 5
146 soft_runcycle = 5 * 60
koder aka kdanilov2c473092015-03-29 17:12:13 +0300147
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300148 def __init__(self, *dt, **mp):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300149 IPerfTest.__init__(self, *dt, **mp)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300150 self.config_fname = self.options['cfg']
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300151
152 if '/' not in self.config_fname and '.' not in self.config_fname:
153 cfgs_dir = os.path.dirname(io_agent.__file__)
154 self.config_fname = os.path.join(cfgs_dir,
155 self.config_fname + '.cfg')
156
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300157 self.alive_check_interval = self.options.get('alive_check_interval')
158 self.config_params = self.options.get('params', {})
159 self.tool = self.options.get('tool', 'fio')
koder aka kdanilovda45e882015-04-06 02:24:42 +0300160 self.raw_cfg = open(self.config_fname).read()
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300161 self.configs = list(io_agent.parse_all_in_1(self.raw_cfg,
162 self.config_params))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800163
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300164 cmd_log = os.path.join(self.log_directory, "task_compiled.cfg")
165 raw_res = os.path.join(self.log_directory, "raw_results.txt")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300166
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300167 self.io_py_remote = self.join_remote("agent.py")
168 self.log_fl = self.join_remote("log.txt")
169 self.pid_file = self.join_remote("pid")
170 self.task_file = self.join_remote("task.cfg")
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300171 self.use_sudo = self.options.get("use_sudo", True)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300172 self.test_logging = self.options.get("test_logging", False)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300173
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300174 fio_command_file = open_for_append_or_create(cmd_log)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300175
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300176 if self.test_logging:
177 soft_runcycle = self.soft_runcycle
178 else:
179 soft_runcycle = None
180
181 self.fio_configs = io_agent.parse_and_slice_all_in_1(
182 self.raw_cfg,
183 self.config_params,
184 soft_runcycle=soft_runcycle)
185
186 self.fio_configs = list(self.fio_configs)
koder aka kdanilov0c598a12015-04-21 03:01:40 +0300187 splitter = "\n\n" + "-" * 60 + "\n\n"
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300188
189 cfg = splitter.join(
190 map(io_agent.fio_config_to_str,
191 self.fio_configs))
192
193 fio_command_file.write(cfg)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300194 self.fio_raw_results_file = open_for_append_or_create(raw_res)
195
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300196 def __str__(self):
197 return "{0}({1})".format(self.__class__.__name__,
198 self.node.get_conn_id())
199
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300200 def cleanup(self):
201 # delete_file(conn, self.io_py_remote)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300202 # Need to remove tempo files, used for testing
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300203 pass
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300204
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300205 def prefill_test_files(self):
206 files = {}
207
208 for section in self.configs:
209 sz = ssize_to_b(section.vals['size'])
210 msz = sz / (1024 ** 2)
211
212 if sz % (1024 ** 2) != 0:
213 msz += 1
214
215 fname = section.vals['filename']
216
217 # if already has other test with the same file name
218 # take largest size
219 files[fname] = max(files.get(fname, 0), msz)
220
221 cmd_templ = "dd oflag=direct " + \
222 "if=/dev/zero of={0} bs={1} count={2}"
223
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300224 if self.use_sudo:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300225 cmd_templ = "sudo " + cmd_templ
226
227 ssize = 0
228 stime = time.time()
229
230 for fname, curr_sz in files.items():
231 cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz)
232 ssize += curr_sz
233 self.run_over_ssh(cmd, timeout=curr_sz)
234
235 ddtime = time.time() - stime
236 if ddtime > 1E-3:
237 fill_bw = int(ssize / ddtime)
238 mess = "Initiall dd fill bw is {0} MiBps for this vm"
239 logger.info(mess.format(fill_bw))
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300240 self.coordinate(('init_bw', fill_bw))
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300241
242 def install_utils(self, max_retry=3, timeout=5):
243 need_install = []
244 for bin_name, package in (('fio', 'fio'), ('screen', 'screen')):
245 try:
246 self.run_over_ssh('which ' + bin_name, nolog=True)
247 except OSError:
248 need_install.append(package)
249
koder aka kdanilovafd98742015-04-24 01:27:22 +0300250 if len(need_install) == 0:
251 return
252
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300253 cmd = "sudo apt-get -y install " + " ".join(need_install)
254
255 for i in range(max_retry):
256 try:
257 self.run_over_ssh(cmd)
258 break
259 except OSError as err:
260 time.sleep(timeout)
261 else:
262 raise OSError("Can't install - " + str(err))
263
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300264 def pre_run(self):
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300265 try:
266 cmd = 'mkdir -p "{0}"'.format(self.remote_dir)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300267 if self.use_sudo:
koder aka kdanilova4a570f2015-04-23 22:11:40 +0300268 cmd = "sudo " + cmd
269 cmd += " ; sudo chown {0} {1}".format(self.node.get_user(),
270 self.remote_dir)
271
272 self.run_over_ssh(cmd)
273 except Exception as exc:
274 msg = "Failed to create folder {0} on remote {1}. Error: {2!s}"
275 msg = msg.format(self.remote_dir, self.node.get_conn_id(), exc)
276 logger.error(msg)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300277 raise StopTestError(msg, exc)
koder aka kdanilov783b4542015-04-23 18:57:04 +0300278
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300279 self.install_utils()
koder aka kdanilovda45e882015-04-06 02:24:42 +0300280
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300281 local_fname = os.path.splitext(io_agent.__file__)[0] + ".py"
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300282 files_to_copy = {local_fname: self.io_py_remote}
283 copy_paths(self.node.connection, files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800284
koder aka kdanilove87ae652015-04-20 02:14:35 +0300285 if self.options.get('prefill_files', True):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300286 self.prefill_test_files()
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300287 elif self.is_primary:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300288 logger.warning("Prefilling of test files is disabled")
koder aka kdanilov6e2ae792015-03-04 18:02:24 -0800289
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300290 def check_process_is_running(self, sftp, pid):
291 try:
292 sftp.stat("/proc/{0}".format(pid))
293 return True
koder aka kdanilova855f902015-04-26 14:31:45 +0300294 except (OSError, IOError, NameError):
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300295 return False
296
297 def kill_remote_process(self, conn, pid, soft=True):
298 try:
299 if soft:
300 cmd = "kill {0}"
301 else:
302 cmd = "kill -9 {0}"
303
304 if self.use_sudo:
305 cmd = "sudo " + cmd
306
307 self.run_over_ssh(cmd.format(pid))
308 return True
309 except OSError:
310 return False
311
312 def get_test_status(self, die_timeout=3):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300313 is_connected = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300314 is_running = None
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300315 pid = None
316 err = None
317
318 try:
319 conn = connect(self.node.conn_url,
320 conn_timeout=self.tcp_conn_timeout)
321 with conn:
322 with conn.open_sftp() as sftp:
323 try:
324 pid = read_from_remote(sftp, self.pid_file)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300325 is_running = True
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300326 except (NameError, IOError, OSError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300327 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300328 is_running = False
329
330 if is_running:
331 if not self.check_process_is_running(sftp, pid):
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300332 try:
333 sftp.remove(self.pid_file)
334 except (IOError, NameError, OSError):
335 pass
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300336 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300337
338 is_connected = True
339
koder aka kdanilova855f902015-04-26 14:31:45 +0300340 except (socket.error, SSHException, EOFError, SFTPError) as exc:
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300341 err = str(exc)
342 is_connected = False
343
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300344 return is_connected, is_running, pid, err
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300345
koder aka kdanilova855f902015-04-26 14:31:45 +0300346 def wait_till_finished(self, soft_timeout, timeout):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300347 conn_id = self.node.get_conn_id()
348 end_of_wait_time = timeout + time.time()
koder aka kdanilova855f902015-04-26 14:31:45 +0300349 soft_end_of_wait_time = soft_timeout + time.time()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300350
351 # time_till_check = random.randint(30, 90)
352 time_till_check = 5
353 pid = None
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300354 is_running = False
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300355 pid_get_timeout = self.max_pig_timeout + time.time()
356 curr_connected = True
357
358 while end_of_wait_time > time.time():
359 time.sleep(time_till_check)
360
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300361 is_connected, is_running, npid, err = self.get_test_status()
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300362
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300363 if is_connected and not is_running:
364 if pid is None:
365 if time.time() > pid_get_timeout:
366 msg = ("On node {0} pid file doesn't " +
367 "appears in time")
368 logger.error(msg.format(conn_id))
369 raise StopTestError("Start timeout")
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300370 else:
371 # execution finished
372 break
373
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300374 if npid is not None:
375 pid = npid
376
koder aka kdanilova855f902015-04-26 14:31:45 +0300377 if is_connected and pid is not None and is_running:
378 if time.time() < soft_end_of_wait_time:
379 time.sleep(soft_end_of_wait_time - time.time())
380
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300381 if is_connected and not curr_connected:
382 msg = "Connection with {0} is restored"
383 logger.debug(msg.format(conn_id))
384 elif not is_connected and curr_connected:
385 msg = "Lost connection with " + conn_id + ". Error: " + err
386 logger.debug(msg)
387
388 curr_connected = is_connected
389
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300390 def run(self, barrier):
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300391 try:
koder aka kdanilova323b302015-04-26 00:40:22 +0300392 if len(self.fio_configs) > 1 and self.is_primary:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300393
394 exec_time = 0
395 for test in self.fio_configs:
396 exec_time += io_agent.calculate_execution_time(test)
397
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300398 # +5% - is a rough estimation for additional operations
399 # like sftp, etc
400 exec_time = int(exec_time * 1.05)
401
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300402 exec_time_s = sec_to_str(exec_time)
koder aka kdanilova855f902015-04-26 14:31:45 +0300403 now_dt = datetime.datetime.now()
404 end_dt = now_dt + datetime.timedelta(0, exec_time)
405 msg = "Entire test should takes aroud: {0} and finished at {1}"
406 logger.info(msg.format(exec_time_s,
407 end_dt.strftime("%H:%M:%S")))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300408
409 for pos, fio_cfg_slice in enumerate(self.fio_configs):
410 names = [i.name for i in fio_cfg_slice]
411 msgs = []
412 already_processed = set()
413 for name in names:
414 if name not in already_processed:
415 already_processed.add(name)
416
417 if 1 == names.count(name):
418 msgs.append(name)
419 else:
420 frmt = "{0} * {1}"
421 msgs.append(frmt.format(name,
422 names.count(name)))
423
koder aka kdanilova323b302015-04-26 00:40:22 +0300424 if self.is_primary:
425 logger.info("Will run tests: " + ", ".join(msgs))
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300426
koder aka kdanilova323b302015-04-26 00:40:22 +0300427 nolog = (pos != 0) or not self.is_primary
428 out_err = self.do_run(barrier, fio_cfg_slice, nolog=nolog)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300429
430 try:
431 for data in parse_output(out_err):
432 data['__meta__']['raw_cfg'] = self.raw_cfg
433 self.on_result_cb(data)
434 except (OSError, StopTestError):
435 raise
436 except Exception as exc:
437 msg_templ = "Error during postprocessing results: {0!s}"
438 raise RuntimeError(msg_templ.format(exc))
439
440 finally:
441 barrier.exit()
442
443 def do_run(self, barrier, cfg, nolog=False):
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300444 conn_id = self.node.get_conn_id()
445
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300446 cmd_templ = "screen -S {screen_name} -d -m " + \
447 "env python2 {0} -p {pid_file} -o {results_file} " + \
448 "--type {1} {2} --json {3}"
449
450 if self.options.get("use_sudo", True):
451 cmd_templ = "sudo " + cmd_templ
koder aka kdanilov66839a92015-04-11 13:22:31 +0300452
453 params = " ".join("{0}={1}".format(k, v)
454 for k, v in self.config_params.items())
455
456 if "" != params:
457 params = "--params " + params
458
koder aka kdanilov783b4542015-04-23 18:57:04 +0300459 with self.node.connection.open_sftp() as sftp:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300460 save_to_remote(sftp, self.task_file,
461 io_agent.fio_config_to_str(cfg))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300462
463 screen_name = self.test_uuid
464 cmd = cmd_templ.format(self.io_py_remote,
465 self.tool,
466 params,
467 self.task_file,
468 pid_file=self.pid_file,
469 results_file=self.log_fl,
470 screen_name=screen_name)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300471
472 exec_time = io_agent.calculate_execution_time(cfg)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300473 exec_time_str = sec_to_str(exec_time)
474
koder aka kdanilova855f902015-04-26 14:31:45 +0300475 timeout = int(exec_time + max(300, exec_time))
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300476 soft_tout = exec_time
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300477 barrier.wait()
koder aka kdanilova323b302015-04-26 00:40:22 +0300478 self.run_over_ssh(cmd, nolog=nolog)
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300479
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300480 if self.is_primary:
481 templ = "Test should takes about {0}." + \
482 " Should finish at {1}," + \
483 " will wait at most till {2}"
484 now_dt = datetime.datetime.now()
485 end_dt = now_dt + datetime.timedelta(0, exec_time)
486 wait_till = now_dt + datetime.timedelta(0, timeout)
koder aka kdanilovea22c3d2015-04-21 03:42:22 +0300487
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300488 logger.info(templ.format(exec_time_str,
489 end_dt.strftime("%H:%M:%S"),
490 wait_till.strftime("%H:%M:%S")))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300491
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300492 if not nolog:
493 msg = "Tests started in screen {1} on each testnode"
494 logger.debug(msg.format(conn_id, screen_name))
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300495
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300496 # TODO: add monitoring socket
497 if self.node.connection is not Local:
498 self.node.connection.close()
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300499
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300500 self.wait_till_finished(soft_tout, timeout)
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300501 if not nolog:
502 logger.debug("Test on node {0} is finished".format(conn_id))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300503
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300504 if self.node.connection is not Local:
505 conn_timeout = self.tcp_conn_timeout * 3
506 self.node.connection = connect(self.node.conn_url,
507 conn_timeout=conn_timeout)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300508
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300509 with self.node.connection.open_sftp() as sftp:
510 return read_from_remote(sftp, self.log_fl)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300511
512 def merge_results(self, results):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300513 if len(results) == 0:
514 return None
515
koder aka kdanilov66839a92015-04-11 13:22:31 +0300516 merged_result = results[0]
517 merged_data = merged_result['res']
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300518 mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300519
520 for res in results[1:]:
521 assert res['__meta__'] == merged_result['__meta__']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300522 data = res['res']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300523
524 for testname, test_data in data.items():
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300525 if testname not in merged_data:
526 merged_data[testname] = test_data
527 continue
528
koder aka kdanilov66839a92015-04-11 13:22:31 +0300529 res_test_data = merged_data[testname]
530
531 diff = set(test_data.keys()).symmetric_difference(
532 res_test_data.keys())
533
534 msg = "Difference: {0}".format(",".join(diff))
535 assert len(diff) == 0, msg
536
537 for k, v in test_data.items():
538 if k in mergable_fields:
539 res_test_data[k].extend(v)
540 else:
541 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
542 assert res_test_data[k] == v, msg
543
544 return merged_result
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300545
546 @classmethod
547 def format_for_console(cls, data):
548 return io_formatter.format_results_for_console(data)