|
| 1 | +# Copyright OpenSearch Contributors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# The OpenSearch Contributors require contributions made to |
| 5 | +# this file be licensed under the Apache-2.0 license or a |
| 6 | +# compatible open source license. |
| 7 | + |
| 8 | + |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import subprocess |
| 13 | +from contextlib import contextmanager |
| 14 | +from typing import Any, Generator |
| 15 | + |
| 16 | +import requests |
| 17 | +from requests.auth import HTTPBasicAuth |
| 18 | +from retry.api import retry_call # type: ignore |
| 19 | + |
| 20 | +from manifests.bundle_manifest import BundleManifest |
| 21 | +from test_workflow.benchmark_test.benchmark_args import BenchmarkArgs |
| 22 | + |
| 23 | + |
| 24 | +class BenchmarkTestCluster: |
| 25 | + manifest: BundleManifest |
| 26 | + work_dir: str |
| 27 | + current_workspace: str |
| 28 | + args: BenchmarkArgs |
| 29 | + output_file: str |
| 30 | + params: str |
| 31 | + is_endpoint_public: bool |
| 32 | + cluster_endpoint: str |
| 33 | + cluster_endpoint_with_port: str |
| 34 | + |
| 35 | + """ |
| 36 | + Represents a performance test cluster. This class deploys the opensearch bundle with CDK. Supports both single |
| 37 | + and multi-node clusters |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + bundle_manifest: BundleManifest, |
| 43 | + config: dict, |
| 44 | + args: BenchmarkArgs, |
| 45 | + current_workspace: str |
| 46 | + ) -> None: |
| 47 | + self.manifest = bundle_manifest |
| 48 | + self.current_workspace = current_workspace |
| 49 | + self.args = args |
| 50 | + self.output_file = "output.json" |
| 51 | + role = config["Constants"]["Role"] |
| 52 | + params_dict = self.setup_cdk_params(config) |
| 53 | + params_list = [] |
| 54 | + for key, value in params_dict.items(): |
| 55 | + if value: |
| 56 | + ''' |
| 57 | + TODO: To send json input to typescript code from command line it needs to be enclosed in |
| 58 | + single-quotes, this is a temp fix to achieve that since the quoted string passed from command line in |
| 59 | + tesh.sh wrapper script gets un-quoted and we need to handle it here. |
| 60 | + ''' |
| 61 | + if key == 'additionalConfig': |
| 62 | + params_list.append(f" -c {key}=\'{value}\'") |
| 63 | + else: |
| 64 | + params_list.append(f" -c {key}={value}") |
| 65 | + role_params = ( |
| 66 | + f" --require-approval=never --plugin cdk-assume-role-credential-plugin" |
| 67 | + f" -c assume-role-credentials:writeIamRoleName={role} -c assume-role-credentials:readIamRoleName={role} " |
| 68 | + ) |
| 69 | + self.params = "".join(params_list) + role_params |
| 70 | + self.is_endpoint_public = False |
| 71 | + self.cluster_endpoint = None |
| 72 | + self.cluster_endpoint_with_port = None |
| 73 | + self.stack_name = f"opensearch-infra-stack-{self.args.stack_suffix}-{self.manifest.build.id}-{self.manifest.build.architecture}" |
| 74 | + |
| 75 | + def start(self) -> None: |
| 76 | + command = f"npm install && cdk deploy \"*\" {self.params} --outputs-file {self.output_file}" |
| 77 | + |
| 78 | + logging.info(f'Executing "{command}" in {os.getcwd()}') |
| 79 | + subprocess.check_call(command, cwd=os.getcwd(), shell=True) |
| 80 | + with open(self.output_file, "r") as read_file: |
| 81 | + load_output = json.load(read_file) |
| 82 | + self.create_endpoint(load_output) |
| 83 | + self.wait_for_processing() |
| 84 | + |
| 85 | + def create_endpoint(self, cdk_output: dict) -> None: |
| 86 | + loadbalancer_url = cdk_output[self.stack_name].get('loadbalancerurl', None) |
| 87 | + if loadbalancer_url is None: |
| 88 | + raise RuntimeError("Unable to fetch the cluster endpoint from cdk output") |
| 89 | + self.cluster_endpoint = loadbalancer_url |
| 90 | + self.cluster_endpoint_with_port = "".join([loadbalancer_url, ":", str(self.port)]) |
| 91 | + |
| 92 | + @property |
| 93 | + def endpoint(self) -> str: |
| 94 | + return self.cluster_endpoint |
| 95 | + |
| 96 | + @property |
| 97 | + def endpoint_with_port(self) -> str: |
| 98 | + return self.cluster_endpoint_with_port |
| 99 | + |
| 100 | + @property |
| 101 | + def port(self) -> int: |
| 102 | + return 80 if self.args.insecure else 443 |
| 103 | + |
| 104 | + def terminate(self) -> None: |
| 105 | + command = f"cdk destroy {self.stack_name} {self.params} --force" |
| 106 | + logging.info(f'Executing "{command}" in {os.getcwd()}') |
| 107 | + |
| 108 | + subprocess.check_call(command, cwd=os.getcwd(), shell=True) |
| 109 | + |
| 110 | + def wait_for_processing(self, tries: int = 3, delay: int = 15, backoff: int = 2) -> None: |
| 111 | + logging.info(f"Waiting for domain at {self.endpoint} to be up") |
| 112 | + protocol = "http://" if self.args.insecure else "https://" |
| 113 | + url = "".join([protocol, self.endpoint, "/_cluster/health"]) |
| 114 | + request_args = {"url": url} if self.args.insecure else {"url": url, "auth": HTTPBasicAuth("admin", "admin"), "verify": False} # type: ignore |
| 115 | + retry_call(requests.get, fkwargs=request_args, |
| 116 | + tries=tries, delay=delay, backoff=backoff) |
| 117 | + |
| 118 | + def setup_cdk_params(self, config: dict) -> dict: |
| 119 | + if self.args.stack_suffix: |
| 120 | + suffix = self.args.stack_suffix + '-' + self.manifest.build.id + '-' + self.manifest.build.architecture |
| 121 | + else: |
| 122 | + suffix = self.manifest.build.id + '-' + self.manifest.build.architecture |
| 123 | + return { |
| 124 | + "distributionUrl": self.manifest.build.location, |
| 125 | + "vpcId": config["Constants"]["VpcId"], |
| 126 | + "account": config["Constants"]["AccountId"], |
| 127 | + "region": config["Constants"]["Region"], |
| 128 | + "suffix": suffix, |
| 129 | + "securityDisabled": str(self.args.insecure).lower(), |
| 130 | + "cpuArch": self.manifest.build.architecture, |
| 131 | + "singleNodeCluster": str(self.args.single_node).lower(), |
| 132 | + "distVersion": self.manifest.build.version, |
| 133 | + "minDistribution": str(self.args.min_distribution).lower(), |
| 134 | + "serverAccessType": config["Constants"]["serverAccessType"], |
| 135 | + "restrictServerAccessTo": config["Constants"]["restrictServerAccessTo"], |
| 136 | + "additionalConfig": self.args.additional_config, |
| 137 | + "managerNodeCount": self.args.manager_node_count, |
| 138 | + "dataNodeCount": self.args.data_node_count, |
| 139 | + "clientNodeCount": self.args.client_node_count, |
| 140 | + "ingestNodeCount": self.args.ingest_node_count, |
| 141 | + "mlNodeCount": self.args.ml_node_count, |
| 142 | + "dataNodeStorage": self.args.data_node_storage, |
| 143 | + "mlNodeStorage": self.args.ml_node_storage, |
| 144 | + "jvmSysProps": self.args.jvm_sys_props |
| 145 | + } |
| 146 | + |
| 147 | + @classmethod |
| 148 | + @contextmanager |
| 149 | + def create(cls, *args: Any) -> Generator[Any, None, None]: |
| 150 | + """ |
| 151 | + Set up the cluster. When this method returns, the cluster must be available to take requests. |
| 152 | + Throws ClusterCreationException if the cluster could not start for some reason. If this exception is thrown, the caller does not need to call "destroy". |
| 153 | + """ |
| 154 | + cluster = cls(*args) |
| 155 | + |
| 156 | + try: |
| 157 | + cluster.start() |
| 158 | + yield cluster |
| 159 | + finally: |
| 160 | + cluster.terminate() |
0 commit comments