koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 2 | import time |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame] | 3 | import socket |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 4 | import random |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 5 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 6 | import logging |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 7 | import datetime |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 8 | |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 9 | from paramiko import SSHException, SFTPError |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 10 | import texttable |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame] | 11 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 12 | from wally.utils import (ssize_to_b, open_for_append_or_create, |
| 13 | sec_to_str, StopTestError) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 14 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 15 | from wally.ssh_utils import (copy_paths, run_over_ssh, |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 16 | save_to_remote, |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 17 | # delete_file, |
| 18 | connect, read_from_remote, Local) |
| 19 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 20 | from . import postgres |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 21 | from . import mysql |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 22 | from .io import agent as io_agent |
| 23 | from .io import formatter as io_formatter |
| 24 | from .io.results_loader import parse_output |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 25 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 26 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 27 | logger = logging.getLogger("wally") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 28 | |
| 29 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 30 | class IPerfTest(object): |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 31 | def __init__(self, options, is_primary, on_result_cb, test_uuid, node, |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 32 | log_directory=None, |
| 33 | coordination_queue=None, |
| 34 | remote_dir="/tmp/wally"): |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 35 | self.options = options |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 36 | self.on_result_cb = on_result_cb |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 37 | self.log_directory = log_directory |
| 38 | self.node = node |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 39 | self.test_uuid = test_uuid |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 40 | self.coordination_queue = coordination_queue |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 41 | self.remote_dir = remote_dir |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 42 | self.is_primary = is_primary |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 43 | self.stop_requested = False |
| 44 | |
| 45 | def request_stop(self): |
| 46 | self.stop_requested = True |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 47 | |
| 48 | def join_remote(self, path): |
| 49 | return os.path.join(self.remote_dir, path) |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 50 | |
| 51 | def coordinate(self, data): |
| 52 | if self.coordination_queue is not None: |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 53 | self.coordination_queue.put((self.node.get_conn_id(), data)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 54 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 55 | def pre_run(self): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 56 | pass |
| 57 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 58 | def cleanup(self): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 59 | pass |
| 60 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 61 | @abc.abstractmethod |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 62 | def run(self, barrier): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 63 | pass |
| 64 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 65 | @classmethod |
| 66 | def format_for_console(cls, data): |
| 67 | msg = "{0}.format_for_console".format(cls.__name__) |
| 68 | raise NotImplementedError(msg) |
| 69 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 70 | 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 kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 74 | @classmethod |
| 75 | def coordination_th(cls, coord_q, barrier, num_threads): |
| 76 | pass |
| 77 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 78 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 79 | class TwoScriptTest(IPerfTest): |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 80 | def __init__(self, *dt, **mp): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 81 | IPerfTest.__init__(self, *dt, **mp) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 82 | |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 83 | if 'scripts_path' in self.options: |
| 84 | self.root = self.options['scripts_path'] |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 85 | self.run_script = self.options['run_script'] |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 86 | self.prerun_script = self.options['prerun_script'] |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 87 | |
| 88 | def get_remote_for_script(self, script): |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 89 | return os.path.join(self.remote_dir, script.rpartition('/')[2]) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 90 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 91 | def pre_run(self): |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 92 | 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 Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 95 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 96 | def run(self, barrier): |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 97 | remote_script = self.get_remote_for_script(self.run_script) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 98 | cmd_opts = ' '.join(["%s %s" % (key, val) for key, val |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 99 | in self.options.items()]) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 100 | cmd = remote_script + ' ' + cmd_opts |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 101 | out_err = self.run_over_ssh(cmd, timeout=6000) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 102 | self.on_result(out_err, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 103 | |
| 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 kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 110 | def on_result(self, out_err, cmd): |
| 111 | try: |
| 112 | self.parse_results(out_err) |
| 113 | except Exception as exc: |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 114 | msg_templ = "Error during postprocessing results: {0!s}. {1}" |
| 115 | raise RuntimeError(msg_templ.format(exc, out_err)) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 116 | |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 117 | def merge_results(self, results): |
| 118 | tpcm = sum([val[1] for val in results]) |
| 119 | return {"res": {"TpmC": tpcm}} |
| 120 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 121 | |
| 122 | class PgBenchTest(TwoScriptTest): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 123 | root = os.path.dirname(postgres.__file__) |
Yulia Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 124 | pre_run_script = os.path.join(root, "prepare.sh") |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 125 | run_script = os.path.join(root, "run.sh") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 126 | |
| 127 | |
Yulia Portnova | 1f12396 | 2015-05-06 18:48:11 +0300 | [diff] [blame] | 128 | @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 Portnova | b1a1507 | 2015-05-06 14:59:25 +0300 | [diff] [blame] | 137 | class 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 kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 151 | class IOPerfTest(IPerfTest): |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 152 | tcp_conn_timeout = 30 |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 153 | max_pig_timeout = 5 |
| 154 | soft_runcycle = 5 * 60 |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 155 | |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 156 | def __init__(self, *dt, **mp): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 157 | IPerfTest.__init__(self, *dt, **mp) |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 158 | self.config_fname = self.options['cfg'] |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 159 | |
| 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 kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 165 | 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 kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 168 | self.raw_cfg = open(self.config_fname).read() |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 169 | self.configs = list(io_agent.parse_all_in_1(self.raw_cfg, |
| 170 | self.config_params)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 171 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 172 | 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 kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 174 | |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 175 | 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 kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 179 | self.use_sudo = self.options.get("use_sudo", True) |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 180 | self.test_logging = self.options.get("test_logging", False) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 181 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 182 | fio_command_file = open_for_append_or_create(cmd_log) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 183 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 184 | 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 kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 195 | splitter = "\n\n" + "-" * 60 + "\n\n" |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 196 | |
| 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 kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 202 | self.fio_raw_results_file = open_for_append_or_create(raw_res) |
| 203 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 204 | def __str__(self): |
| 205 | return "{0}({1})".format(self.__class__.__name__, |
| 206 | self.node.get_conn_id()) |
| 207 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 208 | def cleanup(self): |
| 209 | # delete_file(conn, self.io_py_remote) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 210 | # Need to remove tempo files, used for testing |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 211 | pass |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 212 | |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 213 | def prefill_test_files(self): |
| 214 | files = {} |
| 215 | |
| 216 | for section in self.configs: |
| 217 | sz = ssize_to_b(section.vals['size']) |
| 218 | msz = sz / (1024 ** 2) |
| 219 | |
| 220 | if sz % (1024 ** 2) != 0: |
| 221 | msz += 1 |
| 222 | |
| 223 | fname = section.vals['filename'] |
| 224 | |
| 225 | # if already has other test with the same file name |
| 226 | # take largest size |
| 227 | files[fname] = max(files.get(fname, 0), msz) |
| 228 | |
| 229 | cmd_templ = "dd oflag=direct " + \ |
| 230 | "if=/dev/zero of={0} bs={1} count={2}" |
| 231 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 232 | if self.use_sudo: |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 233 | 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 kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 248 | self.coordinate(('init_bw', fill_bw)) |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 249 | |
| 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 kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 258 | if len(need_install) == 0: |
| 259 | return |
| 260 | |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 261 | 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 kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 272 | def pre_run(self): |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 273 | try: |
| 274 | cmd = 'mkdir -p "{0}"'.format(self.remote_dir) |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 275 | if self.use_sudo: |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 276 | 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 kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 285 | raise StopTestError(msg, exc) |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame] | 286 | |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 287 | self.install_utils() |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 288 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 289 | local_fname = os.path.splitext(io_agent.__file__)[0] + ".py" |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 290 | files_to_copy = {local_fname: self.io_py_remote} |
| 291 | copy_paths(self.node.connection, files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 292 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 293 | if self.options.get('prefill_files', True): |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 294 | self.prefill_test_files() |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 295 | elif self.is_primary: |
koder aka kdanilov | 2066daf | 2015-04-23 21:05:41 +0300 | [diff] [blame] | 296 | logger.warning("Prefilling of test files is disabled") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 297 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 298 | def check_process_is_running(self, sftp, pid): |
| 299 | try: |
| 300 | sftp.stat("/proc/{0}".format(pid)) |
| 301 | return True |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 302 | except (OSError, IOError, NameError): |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 303 | 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 kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 321 | is_connected = None |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 322 | is_running = None |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 323 | 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 kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 333 | is_running = True |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 334 | except (NameError, IOError, OSError) as exc: |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 335 | pid = None |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 336 | is_running = False |
| 337 | |
| 338 | if is_running: |
| 339 | if not self.check_process_is_running(sftp, pid): |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 340 | try: |
| 341 | sftp.remove(self.pid_file) |
| 342 | except (IOError, NameError, OSError): |
| 343 | pass |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 344 | is_running = False |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 345 | |
| 346 | is_connected = True |
| 347 | |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 348 | except (socket.error, SSHException, EOFError, SFTPError) as exc: |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 349 | err = str(exc) |
| 350 | is_connected = False |
| 351 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 352 | return is_connected, is_running, pid, err |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 353 | |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 354 | def wait_till_finished(self, soft_timeout, timeout): |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 355 | conn_id = self.node.get_conn_id() |
| 356 | end_of_wait_time = timeout + time.time() |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 357 | soft_end_of_wait_time = soft_timeout + time.time() |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 358 | |
| 359 | # time_till_check = random.randint(30, 90) |
| 360 | time_till_check = 5 |
| 361 | pid = None |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 362 | is_running = False |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 363 | 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 kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 369 | is_connected, is_running, npid, err = self.get_test_status() |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 370 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 371 | 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 kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 378 | else: |
| 379 | # execution finished |
| 380 | break |
| 381 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 382 | if npid is not None: |
| 383 | pid = npid |
| 384 | |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 385 | 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 kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 389 | 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 kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 398 | def run(self, barrier): |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 399 | try: |
koder aka kdanilov | a323b30 | 2015-04-26 00:40:22 +0300 | [diff] [blame] | 400 | if len(self.fio_configs) > 1 and self.is_primary: |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 401 | |
| 402 | exec_time = 0 |
| 403 | for test in self.fio_configs: |
| 404 | exec_time += io_agent.calculate_execution_time(test) |
| 405 | |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 406 | # +5% - is a rough estimation for additional operations |
| 407 | # like sftp, etc |
| 408 | exec_time = int(exec_time * 1.05) |
| 409 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 410 | exec_time_s = sec_to_str(exec_time) |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 411 | 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 kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 416 | |
| 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 kdanilov | a323b30 | 2015-04-26 00:40:22 +0300 | [diff] [blame] | 432 | if self.is_primary: |
| 433 | logger.info("Will run tests: " + ", ".join(msgs)) |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 434 | |
koder aka kdanilov | a323b30 | 2015-04-26 00:40:22 +0300 | [diff] [blame] | 435 | nolog = (pos != 0) or not self.is_primary |
| 436 | out_err = self.do_run(barrier, fio_cfg_slice, nolog=nolog) |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 437 | |
| 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 kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 452 | conn_id = self.node.get_conn_id() |
| 453 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 454 | 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 kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 460 | |
| 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 kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame] | 467 | with self.node.connection.open_sftp() as sftp: |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 468 | save_to_remote(sftp, self.task_file, |
| 469 | io_agent.fio_config_to_str(cfg)) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 470 | |
| 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 kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 479 | |
| 480 | exec_time = io_agent.calculate_execution_time(cfg) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 481 | exec_time_str = sec_to_str(exec_time) |
| 482 | |
koder aka kdanilov | a855f90 | 2015-04-26 14:31:45 +0300 | [diff] [blame] | 483 | timeout = int(exec_time + max(300, exec_time)) |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 484 | soft_tout = exec_time |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 485 | barrier.wait() |
koder aka kdanilov | a323b30 | 2015-04-26 00:40:22 +0300 | [diff] [blame] | 486 | self.run_over_ssh(cmd, nolog=nolog) |
koder aka kdanilov | abd6ead | 2015-04-24 02:03:07 +0300 | [diff] [blame] | 487 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 488 | if self.is_primary: |
| 489 | templ = "Test should takes about {0}." + \ |
| 490 | " Should finish at {1}," + \ |
| 491 | " will wait at most till {2}" |
| 492 | now_dt = datetime.datetime.now() |
| 493 | end_dt = now_dt + datetime.timedelta(0, exec_time) |
| 494 | wait_till = now_dt + datetime.timedelta(0, timeout) |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 495 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 496 | logger.info(templ.format(exec_time_str, |
| 497 | end_dt.strftime("%H:%M:%S"), |
| 498 | wait_till.strftime("%H:%M:%S"))) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 499 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 500 | if not nolog: |
| 501 | msg = "Tests started in screen {1} on each testnode" |
| 502 | logger.debug(msg.format(conn_id, screen_name)) |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 503 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 504 | # TODO: add monitoring socket |
| 505 | if self.node.connection is not Local: |
| 506 | self.node.connection.close() |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 507 | |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 508 | self.wait_till_finished(soft_tout, timeout) |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 509 | if not nolog: |
| 510 | logger.debug("Test on node {0} is finished".format(conn_id)) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 511 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 512 | if self.node.connection is not Local: |
| 513 | conn_timeout = self.tcp_conn_timeout * 3 |
| 514 | self.node.connection = connect(self.node.conn_url, |
| 515 | conn_timeout=conn_timeout) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 516 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 517 | with self.node.connection.open_sftp() as sftp: |
| 518 | return read_from_remote(sftp, self.log_fl) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 519 | |
| 520 | def merge_results(self, results): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 521 | if len(results) == 0: |
| 522 | return None |
| 523 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 524 | merged_result = results[0] |
| 525 | merged_data = merged_result['res'] |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 526 | mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 527 | |
| 528 | for res in results[1:]: |
| 529 | assert res['__meta__'] == merged_result['__meta__'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 530 | data = res['res'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 531 | |
| 532 | for testname, test_data in data.items(): |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 533 | if testname not in merged_data: |
| 534 | merged_data[testname] = test_data |
| 535 | continue |
| 536 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 537 | res_test_data = merged_data[testname] |
| 538 | |
| 539 | diff = set(test_data.keys()).symmetric_difference( |
| 540 | res_test_data.keys()) |
| 541 | |
| 542 | msg = "Difference: {0}".format(",".join(diff)) |
| 543 | assert len(diff) == 0, msg |
| 544 | |
| 545 | for k, v in test_data.items(): |
| 546 | if k in mergable_fields: |
| 547 | res_test_data[k].extend(v) |
| 548 | else: |
| 549 | msg = "{0!r} != {1!r}".format(res_test_data[k], v) |
| 550 | assert res_test_data[k] == v, msg |
| 551 | |
| 552 | return merged_result |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 553 | |
| 554 | @classmethod |
| 555 | def format_for_console(cls, data): |
| 556 | return io_formatter.format_results_for_console(data) |