| 1 | import sys |
| 2 | import signal |
| 3 | from random import randbytes |
| 4 | from pathlib import Path |
| 5 | import threading |
| 6 | file = Path(__file__).resolve() |
| 7 | parent, root = file.parent, file.parents[1] |
| 8 | sys.path.append(str(root)) |
| 9 | |
| 10 | # Additionally remove the current file's directory from sys.path |
| 11 | from contextlib import suppress |
| 12 | |
| 13 | with suppress(ValueError): |
| 14 | sys.path.remove(str(parent)) |
| 15 | |
| 16 | import logging |
| 17 | |
| 18 | logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(message)s") |
| 19 | |
| 20 | import time |
| 21 | import os |
| 22 | from omnistrate_tests.classes.omnistrate_fleet_instance import OmnistrateFleetInstance |
| 23 | from omnistrate_tests.classes.omnistrate_fleet_api import OmnistrateFleetAPI |
| 24 | from omnistrate_tests.classes.falkordb_cluster import FalkorDBCluster |
| 25 | import argparse |
| 26 | |
| 27 | parser = argparse.ArgumentParser() |
| 28 | parser.add_argument("omnistrate_user") |
| 29 | parser.add_argument("omnistrate_password") |
| 30 | parser.add_argument("cloud_provider", choices=["aws", "gcp"]) |
| 31 | parser.add_argument("region") |
| 32 | |
| 33 | parser.add_argument( |
| 34 | "--subscription-id", required=False, default=os.getenv("SUBSCRIPTION_ID") |
| 35 | ) |
| 36 | parser.add_argument("--ref-name", required=False, |
| 37 | default=os.getenv("REF_NAME")) |
| 38 | parser.add_argument("--service-id", required=True) |
| 39 | parser.add_argument("--environment-id", required=True) |
| 40 | parser.add_argument("--resource-key", required=True) |
| 41 | |
| 42 | parser.add_argument("--instance-name", required=True) |
| 43 | parser.add_argument("--instance-description", |
| 44 | required=False, default="test-standalone") |
| 45 | parser.add_argument("--instance-type", required=True) |
| 46 | parser.add_argument("--storage-size", required=False, default="30") |
| 47 | parser.add_argument("--tls", action="store_true") |
| 48 | parser.add_argument("--rdb-config", required=False, default="medium") |
| 49 | parser.add_argument("--aof-config", required=False, default="always") |
| 50 | parser.add_argument("--host-count", required=False, default="6") |
| 51 | parser.add_argument("--cluster-replicas", required=False, default="1") |
| 52 | parser.add_argument("--shards", required=False, default="3") |
| 53 | |
| 54 | parser.add_argument("--ensure-mz-distribution", action="store_true") |
| 55 | |
| 56 | parser.set_defaults(tls=False) |
| 57 | args = parser.parse_args() |
| 58 | |
| 59 | instance: OmnistrateFleetInstance = None |
| 60 | |
| 61 | |
| 62 | # Intercept exit signals so we can delete the instance before exiting |
| 63 | def signal_handler(sig, frame): |
| 64 | if instance: |
| 65 | instance.delete(False) |
| 66 | sys.exit(0) |
| 67 | |
| 68 | |
| 69 | signal.signal(signal.SIGINT, signal_handler) |
| 70 | signal.signal(signal.SIGTERM, signal_handler) |
| 71 | |
| 72 | current_host_count = int(args.host_count) |
| 73 | current_replicas_count = int(args.cluster_replicas) |
| 74 | |
| 75 | |
| 76 | def test_cluster_replicas(): |
| 77 | global instance |
| 78 | |
| 79 | omnistrate = OmnistrateFleetAPI( |
| 80 | email=args.omnistrate_user, |
| 81 | password=args.omnistrate_password, |
| 82 | ) |
| 83 | |
| 84 | service = omnistrate.get_service(args.service_id) |
| 85 | product_tier = omnistrate.get_product_tier( |
| 86 | service_id=args.service_id, |
| 87 | environment_id=args.environment_id, |
| 88 | tier_name=args.ref_name, |
| 89 | ) |
| 90 | service_model = omnistrate.get_service_model( |
| 91 | args.service_id, product_tier.service_model_id |
| 92 | ) |
| 93 | |
| 94 | logging.info(f"Product tier id: {product_tier.product_tier_id} for {args.ref_name}") |
| 95 | |
| 96 | instance = omnistrate.instance( |
| 97 | service_id=args.service_id, |
| 98 | service_provider_id=service.service_provider_id, |
| 99 | service_key=service.key, |
| 100 | service_environment_id=args.environment_id, |
| 101 | service_environment_key=service.get_environment( |
| 102 | args.environment_id).key, |
| 103 | service_model_key=service_model.key, |
| 104 | service_api_version="v1", |
| 105 | product_tier_key=product_tier.product_tier_key, |
| 106 | resource_key=args.resource_key, |
| 107 | subscription_id=args.subscription_id, |
| 108 | deployment_create_timeout_seconds=2400, |
| 109 | deployment_delete_timeout_seconds=2400, |
| 110 | deployment_failover_timeout_seconds=2400 |
| 111 | ) |
| 112 | |
| 113 | try: |
| 114 | password = randbytes(16).hex() |
| 115 | instance.create( |
| 116 | wait_for_ready=True, |
| 117 | deployment_cloud_provider=args.cloud_provider, |
| 118 | deployment_region=args.region, |
| 119 | name=args.instance_name, |
| 120 | description=args.instance_description, |
| 121 | falkordb_user="falkordb", |
| 122 | falkordb_password=password, |
| 123 | nodeInstanceType=args.instance_type, |
| 124 | storageSize=args.storage_size, |
| 125 | enableTLS=args.tls, |
| 126 | RDBPersistenceConfig=args.rdb_config, |
| 127 | AOFPersistenceConfig=args.aof_config, |
| 128 | hostCount=args.host_count, |
| 129 | clusterReplicas=args.cluster_replicas, |
| 130 | ) |
| 131 | |
| 132 | add_data(instance) |
| 133 | |
| 134 | # Start a new thread and signal for zero_downtime test |
| 135 | thread_signal = threading.Event() |
| 136 | error_signal = threading.Event() |
| 137 | thread = threading.Thread( |
| 138 | target=test_zero_downtime, args=(thread_signal, error_signal, instance, args.tls) |
| 139 | ) |
| 140 | thread.start() |
| 141 | |
| 142 | change_replica_count(instance, int(args.cluster_replicas) + 1) |
| 143 | |
| 144 | if args.ensure_mz_distribution: |
| 145 | test_ensure_mz_distribution(instance, password) |
| 146 | |
| 147 | check_data(instance) |
| 148 | |
| 149 | change_replica_count(instance, int(args.cluster_replicas)) |
| 150 | |
| 151 | # Wait for the zero_downtime |
| 152 | thread_signal.set() |
| 153 | thread.join() |
| 154 | |
| 155 | check_data(instance) |
| 156 | |
| 157 | except Exception as e: |
| 158 | logging.exception(e) |
| 159 | instance.delete(False) |
| 160 | raise e |
| 161 | |
| 162 | # Delete instance |
| 163 | instance.delete(False) |
| 164 | |
| 165 | if error_signal.is_set(): |
| 166 | raise ValueError("Test failed") |
| 167 | else: |
| 168 | logging.info("Test passed") |
| 169 | |
| 170 | |
| 171 | def change_replica_count(instance: OmnistrateFleetInstance, new_replicas_count: int): |
| 172 | global current_replicas_count, current_host_count |
| 173 | |
| 174 | diff = new_replicas_count - current_replicas_count |
| 175 | |
| 176 | new_host_count = int(current_host_count) + (diff * int(args.shards)) |
| 177 | |
| 178 | logging.info( |
| 179 | f"Changing clusterReplicas to {new_replicas_count} and hostCount to {new_host_count}" |
| 180 | ) |
| 181 | instance.update_params( |
| 182 | hostCount=f"{new_host_count}", |
| 183 | clusterReplicas=f"{new_replicas_count}", |
| 184 | wait_for_ready=True, |
| 185 | ) |
| 186 | |
| 187 | |
| 188 | current_host_count = new_host_count |
| 189 | current_replicas_count = new_replicas_count |
| 190 | |
| 191 | instance_details = instance.get_instance_details() |
| 192 | |
| 193 | params = ( |
| 194 | instance_details["result_params"] |
| 195 | if "result_params" in instance_details |
| 196 | else None |
| 197 | ) |
| 198 | |
| 199 | if not params: |
| 200 | raise Exception("No result_params found in instance details") |
| 201 | |
| 202 | host_count = int(params["hostCount"]) if "hostCount" in params else None |
| 203 | |
| 204 | if not host_count: |
| 205 | raise Exception("No hostCount found in instance details") |
| 206 | |
| 207 | if host_count != current_host_count: |
| 208 | raise Exception("Host count does not match new host count") |
| 209 | |
| 210 | cluster_replicas = ( |
| 211 | int(params["clusterReplicas"]) if "clusterReplicas" in params else None |
| 212 | ) |
| 213 | |
| 214 | if not cluster_replicas: |
| 215 | raise Exception("No clusterReplicas found in instance details") |
| 216 | |
| 217 | if cluster_replicas != current_replicas_count: |
| 218 | raise Exception( |
| 219 | "Cluster replicas count does not match new replicas count") |
| 220 | |
| 221 | |
| 222 | def test_ensure_mz_distribution(instance: OmnistrateFleetInstance, password: str): |
| 223 | """This function should ensure that each shard is distributed across multiple availability zones""" |
| 224 | |
| 225 | network_topology: dict = instance.get_network_topology(force_refresh=True) |
| 226 | instance_details = instance.get_instance_details() |
| 227 | |
| 228 | params = ( |
| 229 | instance_details["result_params"] |
| 230 | if "result_params" in instance_details |
| 231 | else None |
| 232 | ) |
| 233 | |
| 234 | if not params: |
| 235 | raise Exception("No result_params found in instance details") |
| 236 | |
| 237 | resource_key = next( |
| 238 | (k for [k, v] in network_topology.items() |
| 239 | if v["resourceName"] == "cluster-mz"), |
| 240 | None, |
| 241 | ) |
| 242 | |
| 243 | resource = network_topology[resource_key] |
| 244 | |
| 245 | nodes = resource["nodes"] |
| 246 | |
| 247 | if len(nodes) == 0: |
| 248 | raise Exception("No nodes found in network topology") |
| 249 | |
| 250 | if len(nodes) != current_host_count: |
| 251 | raise Exception(f"Host count does not match number of nodes. Current host count: {current_host_count}; Number of nodes: {len(nodes)}") |
| 252 | |
| 253 | cluster = FalkorDBCluster( |
| 254 | host=resource["clusterEndpoint"], |
| 255 | port=resource["clusterPorts"][0], |
| 256 | username="falkordb", |
| 257 | password=password, |
| 258 | ssl=params["enableTLS"] == "true" if "enableTLS" in params else False, |
| 259 | ) |
| 260 | |
| 261 | groups = cluster.groups(current_replicas_count) |
| 262 | |
| 263 | for group in groups: |
| 264 | group_azs = set() |
| 265 | for node in group: |
| 266 | omnistrateNode = next( |
| 267 | (n for n in nodes if n["endpoint"] == node.hostname), None |
| 268 | ) |
| 269 | if not omnistrateNode: |
| 270 | logging.warning( |
| 271 | f"Node {node.hostname} not found in network topology") |
| 272 | continue |
| 273 | |
| 274 | group_azs.add(omnistrateNode["availabilityZone"]) |
| 275 | |
| 276 | if len(group_azs) == 1: |
| 277 | raise Exception( |
| 278 | "Group is not distributed across multiple availability zones" |
| 279 | ) |
| 280 | |
| 281 | logging.info( |
| 282 | f"Group {group} is distributed across availability zones {group_azs}" |
| 283 | ) |
| 284 | |
| 285 | logging.info("Shards are distributed across multiple availability zones") |
| 286 | |
| 287 | |
| 288 | def add_data(instance: OmnistrateFleetInstance): |
| 289 | """This function should retrieve the instance host and port for connection, write some data to the DB, then check that the data is there""" |
| 290 | |
| 291 | # Get instance host and port |
| 292 | db = instance.create_connection( |
| 293 | ssl=args.tls, |
| 294 | ) |
| 295 | |
| 296 | graph = db.select_graph("test") |
| 297 | |
| 298 | # Write some data to the DB |
| 299 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 300 | |
| 301 | result = graph.query("MATCH (n:Person) RETURN n") |
| 302 | |
| 303 | if len(result.result_set) == 0: |
| 304 | raise Exception("Data not found after write") |
| 305 | |
| 306 | |
| 307 | def check_data(instance: OmnistrateFleetInstance): |
| 308 | |
| 309 | # Get instance host and port |
| 310 | db = instance.create_connection( |
| 311 | ssl=args.tls, |
| 312 | ) |
| 313 | |
| 314 | graph = db.select_graph("test") |
| 315 | |
| 316 | result = graph.query("MATCH (n:Person) RETURN n") |
| 317 | |
| 318 | if len(result.result_set) == 0: |
| 319 | raise Exception("Data did not persist after host count change") |
| 320 | |
| 321 | def test_zero_downtime( |
| 322 | thread_signal: threading.Event, |
| 323 | error_signal: threading.Event, |
| 324 | instance: OmnistrateFleetInstance, |
| 325 | ssl=False, |
| 326 | ): |
| 327 | """This function should test the ability to read and write while a memory update happens""" |
| 328 | try: |
| 329 | db = instance.create_connection(ssl=ssl, force_reconnect=True) |
| 330 | |
| 331 | graph = db.select_graph("test") |
| 332 | |
| 333 | while not thread_signal.is_set(): |
| 334 | # Write some data to the DB |
| 335 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 336 | graph.ro_query("MATCH (n:Person {name: 'Alice'}) RETURN n") |
| 337 | |
| 338 | time.sleep(3) |
| 339 | except Exception as e: |
| 340 | logging.exception(e) |
| 341 | error_signal.set() |
| 342 | raise e |
| 343 | |
| 344 | |
| 345 | if __name__ == "__main__": |
| 346 | test_cluster_replicas() |