|
| 1 | +import json |
| 2 | +from dataclasses import dataclass |
| 3 | +from pathlib import Path |
| 4 | +from typing import Text, Union, Optional, Dict, List |
| 5 | + |
| 6 | +from benchmark.backend import Backend, PathLike, Server, Client, Container |
| 7 | +from docker.models import containers |
| 8 | + |
| 9 | +import logging |
| 10 | +import docker |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class DockerContainerConf: |
| 18 | + engine: Text |
| 19 | + image: Optional[Text] = None |
| 20 | + dockerfile: Optional[Text] = None |
| 21 | + environment: Optional[Dict[Text, Union[Text, int, bool]]] = None |
| 22 | + main: Optional[Text] = None |
| 23 | + hostname: Optional[Text] = None |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def from_file( |
| 27 | + cls, path: Text, engine: Text, container: Text = "server" |
| 28 | + ) -> "DockerContainerConf": |
| 29 | + with open(path, "r") as fp: |
| 30 | + conf = json.load(fp) |
| 31 | + return DockerContainerConf(engine=engine, **conf[container]) |
| 32 | + |
| 33 | + def dockerfile_path(self, root_dir: Path) -> Path: |
| 34 | + """ |
| 35 | + Calculates the absolute path to the directory containing the dockerfile, |
| 36 | + using given root directory as a base. |
| 37 | + :param root_dir: |
| 38 | + :return: |
| 39 | + """ |
| 40 | + return root_dir / "engine" / self.engine |
| 41 | + |
| 42 | + |
| 43 | +class DockerContainer(Container): |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + container_conf: DockerContainerConf, |
| 47 | + docker_backend: "DockerBackend", |
| 48 | + ): |
| 49 | + self.container_conf = container_conf |
| 50 | + self.docker_backend = docker_backend |
| 51 | + self.container: containers.Container = None |
| 52 | + self.volumes = [] |
| 53 | + |
| 54 | + def mount(self, source: PathLike, target: PathLike): |
| 55 | + self.volumes.append(f"{source}:{target}") |
| 56 | + |
| 57 | + def run(self): |
| 58 | + # Build the dockerfile if it was provided as a container image. This is |
| 59 | + # typically done for the clients, as they may require some custom setup |
| 60 | + if self.container_conf.dockerfile is not None: |
| 61 | + dockerfile_path = self.container_conf.dockerfile_path( |
| 62 | + self.docker_backend.root_dir |
| 63 | + ) |
| 64 | + image, logs = self.docker_backend.docker_client.images.build( |
| 65 | + path=str(dockerfile_path), |
| 66 | + dockerfile=self.container_conf.dockerfile, |
| 67 | + ) |
| 68 | + self.container_conf.image = image.id |
| 69 | + logger.info( |
| 70 | + "Built %s into a Docker image %s", |
| 71 | + self.container_conf.dockerfile, |
| 72 | + image.id, |
| 73 | + ) |
| 74 | + |
| 75 | + # Create the container either using the image or dockerfile, if that was |
| 76 | + # provided. The dockerfile has a preference over the image name. |
| 77 | + logger.debug("Running a container using image %s", self.container_conf.image) |
| 78 | + self.container = self.docker_backend.docker_client.containers.run( |
| 79 | + self.container_conf.image, |
| 80 | + detach=True, |
| 81 | + volumes=self.volumes, |
| 82 | + environment=self.container_conf.environment, |
| 83 | + hostname=self.container_conf.hostname, |
| 84 | + network=self.docker_backend.network.name, |
| 85 | + ) |
| 86 | + |
| 87 | + # TODO: remove the image on exit |
| 88 | + |
| 89 | + def logs(self): |
| 90 | + for log_entry in self.container.logs(stream=True, follow=True): |
| 91 | + yield log_entry |
| 92 | + |
| 93 | + def is_ready(self) -> bool: |
| 94 | + # TODO: implement the healthcheck |
| 95 | + return True |
| 96 | + |
| 97 | + |
| 98 | +class DockerServer(Server, DockerContainer): |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +class DockerClient(Client, DockerContainer): |
| 103 | + def load_data(self, filename: Text): |
| 104 | + command = f"{self.container_conf.main} load {filename}" |
| 105 | + _, generator = self.container.exec_run(command, stream=True) |
| 106 | + return generator |
| 107 | + |
| 108 | + |
| 109 | +class DockerBackend(Backend): |
| 110 | + """ |
| 111 | + A Docker based backend for the benchmarks, using separate containers for |
| 112 | + server and client/s. |
| 113 | + """ |
| 114 | + |
| 115 | + NETWORK_NAME = "vector-benchmark" |
| 116 | + |
| 117 | + def __init__( |
| 118 | + self, |
| 119 | + root_dir: Union[PathLike], |
| 120 | + docker_client: Optional[docker.DockerClient] = None, |
| 121 | + ): |
| 122 | + super().__init__(root_dir) |
| 123 | + if docker_client is None: |
| 124 | + docker_client = docker.from_env() |
| 125 | + self.docker_client = docker_client |
| 126 | + self.containers: List[DockerContainer] = [] |
| 127 | + |
| 128 | + def __enter__(self): |
| 129 | + super().__enter__() |
| 130 | + self.network = self.docker_client.networks.create(self.NETWORK_NAME) |
| 131 | + # self.data_volume = self.docker_client.volumes.create() |
| 132 | + return self |
| 133 | + |
| 134 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 135 | + super().__exit__(exc_type, exc_val, exc_tb) |
| 136 | + |
| 137 | + # Kill all the containers on the context manager exit, so there are no |
| 138 | + # orphaned containers once the benchmark is finished |
| 139 | + for container in self.containers: |
| 140 | + container.container.kill() |
| 141 | + |
| 142 | + # Remove the data volume as well, so there won't be any volume left |
| 143 | + # self.data_volume.remove() |
| 144 | + |
| 145 | + # Finally get rid of the network as well |
| 146 | + self.network.remove() |
| 147 | + |
| 148 | + def initialize_server(self, engine: Text) -> Server: |
| 149 | + server_conf = DockerContainerConf.from_file( |
| 150 | + self.root_dir / "engine" / engine / "config.json", |
| 151 | + engine=engine, |
| 152 | + container="server", |
| 153 | + ) |
| 154 | + logger.info("Initializing %s server: %s", engine, server_conf) |
| 155 | + server = DockerServer(server_conf, self) |
| 156 | + self.containers.append(server) |
| 157 | + return server |
| 158 | + |
| 159 | + def initialize_client(self, engine: Text) -> Client: |
| 160 | + # TODO: Create a docker volume so the data is available on client instances |
| 161 | + client_conf = DockerContainerConf.from_file( |
| 162 | + self.root_dir / "engine" / engine / "config.json", |
| 163 | + engine=engine, |
| 164 | + container="client", |
| 165 | + ) |
| 166 | + logger.info("Initializing %s client: %s", engine, client_conf) |
| 167 | + client = DockerClient(client_conf, self) |
| 168 | + self.containers.append(client) |
| 169 | + return client |
0 commit comments