|
| 1 | +from contextlib import contextmanager |
| 2 | +from datetime import datetime, timezone |
| 3 | +import json |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from os import path |
| 7 | +import subprocess |
| 8 | +from subprocess import Popen |
| 9 | +import tempfile |
| 10 | + |
| 11 | +import hydra |
| 12 | +from omegaconf import DictConfig |
| 13 | + |
| 14 | +logging.basicConfig( |
| 15 | + level=os.environ.get('LOGLEVEL', 'INFO').upper() |
| 16 | +) |
| 17 | + |
| 18 | +log = logging.getLogger(__name__) |
| 19 | + |
| 20 | +MOUNT_DIRECTORY = "s3" |
| 21 | +MP_LOGS_DIRECTORY = "mp_logs/" |
| 22 | + |
| 23 | + |
| 24 | +@contextmanager |
| 25 | +def _mounted_bucket( |
| 26 | + cfg: DictConfig, |
| 27 | + ): |
| 28 | + """ |
| 29 | + Mounts the S3 bucket, providing metadata about the successful mount. |
| 30 | +
|
| 31 | + Context manager allows use of `with` clause, automatically unmounting the bucket. |
| 32 | + """ |
| 33 | + mount_dir = tempfile.mkdtemp(suffix=".mountpoint-s3") |
| 34 | + mount_metadata = _mount_mp(cfg, mount_dir) |
| 35 | + try: |
| 36 | + yield mount_metadata |
| 37 | + finally: |
| 38 | + try: |
| 39 | + subprocess.check_output(["umount", mount_dir]) |
| 40 | + log.debug(f"{mount_dir} unmounted") |
| 41 | + os.rmdir(mount_dir) |
| 42 | + except Exception: |
| 43 | + log.error(f"Error cleaning up Mountpoint at {mount_dir}:", exc_info=True) |
| 44 | + |
| 45 | + |
| 46 | +class MountError(Exception): |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +def _mount_mp( |
| 51 | + cfg: DictConfig, |
| 52 | + mount_dir: str, |
| 53 | + ) -> dict[str, any] | MountError | subprocess.CalledProcessError: |
| 54 | + """ |
| 55 | + Mount an S3 bucket using Mountpoint, |
| 56 | + using the configuration to apply Mountpoint arguments. |
| 57 | +
|
| 58 | + Returns Mountpoint version string. |
| 59 | + """ |
| 60 | + |
| 61 | + if cfg['mountpoint_binary'] is None: |
| 62 | + mountpoint_args = [ |
| 63 | + "cargo", |
| 64 | + "run", |
| 65 | + "--quiet", |
| 66 | + "--release", |
| 67 | + "--", |
| 68 | + ] |
| 69 | + else: |
| 70 | + mountpoint_args = [cfg['mountpoint_binary']] |
| 71 | + |
| 72 | + os.makedirs(MP_LOGS_DIRECTORY, exist_ok=True) |
| 73 | + |
| 74 | + bucket = cfg['s3_bucket'] |
| 75 | + |
| 76 | + mountpoint_version_output = subprocess \ |
| 77 | + .check_output([ |
| 78 | + *mountpoint_args, |
| 79 | + "--version" |
| 80 | + ]) \ |
| 81 | + .decode("utf-8") |
| 82 | + log.info("Mountpoint version: %s", mountpoint_version_output.strip()) |
| 83 | + |
| 84 | + subprocess_args = [ |
| 85 | + *mountpoint_args, |
| 86 | + bucket, |
| 87 | + mount_dir, |
| 88 | + "--log-metrics", |
| 89 | + "--allow-overwrite", |
| 90 | + "--allow-delete", |
| 91 | + f"--log-directory={MP_LOGS_DIRECTORY}", |
| 92 | + ] |
| 93 | + subprocess_env = { |
| 94 | + "PATH": os.environ["PATH"], |
| 95 | + } |
| 96 | + |
| 97 | + if cfg['s3_prefix'] is not None: |
| 98 | + subprocess_args.append(f"--prefix={cfg['s3_prefix']}") |
| 99 | + |
| 100 | + if cfg['mountpoint_debug']: |
| 101 | + subprocess_args.append("--debug") |
| 102 | + if cfg['mountpoint_debug_crt']: |
| 103 | + subprocess_args.append("--debug-crt") |
| 104 | + |
| 105 | + if cfg["read_part_size"]: |
| 106 | + subprocess_args.append(f"--read-part-size={cfg['read_part_size']}") |
| 107 | + if cfg["write_part_size"]: |
| 108 | + subprocess_args.append(f"--write-part-size={cfg['write_part_size']}") |
| 109 | + |
| 110 | + if cfg['metadata_ttl'] is not None: |
| 111 | + subprocess_args.append(f"--metadata-ttl={cfg['metadata_ttl']}") |
| 112 | + |
| 113 | + if cfg['upload_checksums'] is not None: |
| 114 | + subprocess_args.append(f"--upload-checksums={cfg['upload_checksums']}") |
| 115 | + |
| 116 | + if cfg['fuse_threads'] is not None: |
| 117 | + subprocess_args.append(f"--max-threads={cfg['fuse_threads']}") |
| 118 | + |
| 119 | + log.info(f"Mounting S3 bucket {bucket} with args: %s; env: %s", subprocess_args, subprocess_env) |
| 120 | + try: |
| 121 | + output = subprocess.check_output(subprocess_args, env=subprocess_env) |
| 122 | + except subprocess.CalledProcessError as e: |
| 123 | + log.error(f"Error during mounting: {e}") |
| 124 | + raise MountError() from e |
| 125 | + |
| 126 | + log.info("Mountpoint output: %s", output.decode("utf-8").strip()) |
| 127 | + |
| 128 | + return { |
| 129 | + "mount_dir": mount_dir, |
| 130 | + "mount_s3_command": " ".join(subprocess_args), |
| 131 | + "mount_s3_env": subprocess_env, |
| 132 | + "mp_version": mountpoint_version_output.strip(), |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +def _run_fio(cfg: DictConfig, mount_dir: str) -> None: |
| 137 | + """ |
| 138 | + Run the FIO workload against the file system. |
| 139 | + """ |
| 140 | + FIO_BINARY = "fio" |
| 141 | + fio_job_name = cfg["fio_benchmark"] |
| 142 | + fio_output_filepath = f"fio.{fio_job_name}.json" |
| 143 | + |
| 144 | + # TODO: Avoid duplicating/diverging the FIO jobs between `benchmark/fio/` and `mountpoint-s3/scripts/fio/` |
| 145 | + fio_job_filepath = hydra.utils.to_absolute_path(f"fio/{fio_job_name}.fio") |
| 146 | + subprocess_args = [ |
| 147 | + FIO_BINARY, |
| 148 | + "--eta=never", |
| 149 | + "--output-format=json", |
| 150 | + f"--output={fio_output_filepath}", |
| 151 | + f"--directory={mount_dir}", |
| 152 | + fio_job_filepath, |
| 153 | + ] |
| 154 | + subprocess_env = { |
| 155 | + "PATH": os.environ["PATH"], |
| 156 | + "APP_WORKERS": str(cfg['application_workers']), |
| 157 | + "SIZE_GIB": "100", |
| 158 | + "DIRECT": "1" if cfg['direct_io'] else "0", |
| 159 | + "UNIQUE_DIR": datetime.now(tz=timezone.utc).isoformat(), |
| 160 | + # TODO: Confirm assumption that `libaio` should make direct IO go faster. |
| 161 | + # TODO: Review if we should use sync or psync. We use `sync` in other benchmarks. |
| 162 | + "IO_ENGINE": "libaio" if cfg['direct_io'] else "psync", |
| 163 | + } |
| 164 | + log.info("Running FIO with args: %s; env: %s", subprocess_args, subprocess_env) |
| 165 | + |
| 166 | + # Use Popen instead of check_output, as we had some issues when trying to attach perf |
| 167 | + with Popen(subprocess_args, env=subprocess_env) as process: |
| 168 | + exit_code = process.wait() |
| 169 | + if exit_code != 0: |
| 170 | + log.error(f"FIO process failed with exit code {exit_code}") |
| 171 | + raise subprocess.CalledProcessError(exit_code, subprocess_args) |
| 172 | + else: |
| 173 | + log.info("FIO process completed successfully") |
| 174 | + |
| 175 | + |
| 176 | +def _collect_logs() -> None: |
| 177 | + """ |
| 178 | + Collect the Mountpoint log if it exists and move to the output directory. |
| 179 | + Mountpoint log filename will be normalized removing the date, etc.. |
| 180 | + The old log directory is removed. |
| 181 | +
|
| 182 | + Fails if more than one log file is found. |
| 183 | + """ |
| 184 | + logs_directory = path.join(os.getcwd(), MP_LOGS_DIRECTORY) |
| 185 | + dir_entries = os.listdir(logs_directory) |
| 186 | + |
| 187 | + if not dir_entries: |
| 188 | + log.debug(f"No Mountpoint log files in directory {logs_directory}") |
| 189 | + return |
| 190 | + |
| 191 | + assert len(dir_entries) <= 1, f"Expected no more than one log file in {logs_directory}" |
| 192 | + |
| 193 | + old_log_dir = path.join(logs_directory, dir_entries[0]) |
| 194 | + new_log_path = "mountpoint-s3.log" |
| 195 | + log.debug(f"Renaming {old_log_dir} to {new_log_path}") |
| 196 | + os.rename(old_log_dir, new_log_path) |
| 197 | + os.rmdir(logs_directory) |
| 198 | + |
| 199 | + |
| 200 | +def _write_metadata(metadata: dict[str, any]) -> None: |
| 201 | + with open("metadata.json", "w") as f: |
| 202 | + json.dump(metadata, f, default=str) |
| 203 | + |
| 204 | + |
| 205 | +def _postprocessing(metadata: dict[str, any]) -> None: |
| 206 | + _collect_logs() |
| 207 | + _write_metadata(metadata) |
| 208 | + |
| 209 | + |
| 210 | +@hydra.main(version_base=None, config_path="conf", config_name="config") |
| 211 | +def run_experiment(cfg: DictConfig) -> None: |
| 212 | + """ |
| 213 | + At a high level, we want to mount the S3 bucket using Mountpoint, |
| 214 | + run a synthetic workload against Mountpoint while capturing metrics and logs, |
| 215 | + then end the load and unmount the bucket. |
| 216 | +
|
| 217 | + We should collect all of the logs and metric and dump them in the output directory. |
| 218 | + """ |
| 219 | + log.debug("Experiment starting") |
| 220 | + metadata = { |
| 221 | + "start_time": datetime.now(tz=timezone.utc), |
| 222 | + "success": False, |
| 223 | + } |
| 224 | + |
| 225 | + with _mounted_bucket(cfg) as mount_metadata: |
| 226 | + metadata.update(mount_metadata) |
| 227 | + mount_dir = mount_metadata["mount_dir"] |
| 228 | + try: |
| 229 | + # TODO: Add resource monitoring during FIO job |
| 230 | + _run_fio(cfg, mount_dir) |
| 231 | + metadata["success"] = True |
| 232 | + except Exception as e: |
| 233 | + log.error(f"Error running experiment: {e}") |
| 234 | + |
| 235 | + metadata["end_time"] = datetime.now(tz=timezone.utc) |
| 236 | + |
| 237 | + _postprocessing(metadata) |
| 238 | + log.info("Experiment ended") |
| 239 | + |
| 240 | + |
| 241 | +if __name__ == "__main__": |
| 242 | + run_experiment() |
0 commit comments