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