|
| 1 | +import pytest |
| 2 | +import time |
| 3 | +import struct |
| 4 | +from valkey_search_test_case import ValkeySearchClusterTestCase |
| 5 | +from valkey import ResponseError |
| 6 | +from valkeytestframework.conftest import resource_port_tracker |
| 7 | + |
| 8 | +@pytest.mark.cluster |
| 9 | +class TestAsyncClientThrottling(ValkeySearchClusterTestCase): |
| 10 | + """ |
| 11 | + Test async client throttling during slot migration. |
| 12 | + |
| 13 | + Throttling has 3 phases: |
| 14 | + - Phase 1: Queue <= threshold → no blocking |
| 15 | + - Phase 2: Queue > threshold, sync=0 → block all async |
| 16 | + - Phase 3: Queue > threshold, sync>0 → ratio-based blocking |
| 17 | + |
| 18 | + Tests reduced to 1 writer thread to slow down processing and ensure sustained queue buildup. |
| 19 | + """ |
| 20 | + |
| 21 | + CLUSTER_SIZE = 2 |
| 22 | + # Disable JSON module to avoid atomic slot migration incompatibility |
| 23 | + LOAD_JSON_MODULE = False |
| 24 | + |
| 25 | + def append_startup_args(self, args): |
| 26 | + """Reduce threads to slow down backfill for testing.""" |
| 27 | + args = super().append_startup_args(args) |
| 28 | + args.update({ |
| 29 | + "search.writer-threads": "1", |
| 30 | + "search.reader-threads": "1" |
| 31 | + }) |
| 32 | + return args |
| 33 | + |
| 34 | + def test_threshold_async_client_throttle(self): |
| 35 | + """ |
| 36 | + Test threshold-based blocking with async mutations. |
| 37 | + Verifies blocking when async mutations exceed threshold. |
| 38 | + """ |
| 39 | + source_client = self.client_for_primary(0) |
| 40 | + target_client = self.client_for_primary(1) |
| 41 | + |
| 42 | + # Create index on source (may propagate via coordinator) |
| 43 | + source_client.execute_command( |
| 44 | + "FT.CREATE", "idx", "SCHEMA", |
| 45 | + "description", "VECTOR", "HNSW", "6", |
| 46 | + "TYPE", "FLOAT32", "DIM", "3", "DISTANCE_METRIC", "L2" |
| 47 | + ) |
| 48 | + |
| 49 | + # Wait for coordinator propagation |
| 50 | + time.sleep(1) |
| 51 | + |
| 52 | + # Enable throttling on BOTH nodes |
| 53 | + for client in [source_client, target_client]: |
| 54 | + client.execute_command( |
| 55 | + "CONFIG", "SET", "search.async-client-throttling-enabled", "yes" |
| 56 | + ) |
| 57 | + client.execute_command( |
| 58 | + "CONFIG", "SET", "search.async-clients-block-threshold", "1" |
| 59 | + ) |
| 60 | + |
| 61 | + # Add 100 documents with vector data to ensure throttling |
| 62 | + num_keys = 100 |
| 63 | + keys = [f"{{migrate}}:p{i}" for i in range(num_keys)] |
| 64 | + |
| 65 | + for i, key in enumerate(keys): |
| 66 | + # Create binary vector data (3 float32 values) |
| 67 | + vector_data = struct.pack('fff', float(i+1), float(i+2), float(i+3)) |
| 68 | + source_client.execute_command("HSET", key, "description", vector_data) |
| 69 | + |
| 70 | + # Get slot for migration |
| 71 | + slot = source_client.execute_command("CLUSTER", "KEYSLOT", keys[0]) |
| 72 | + |
| 73 | + # Get node IDs |
| 74 | + source_id = source_client.execute_command("CLUSTER", "MYID").decode('utf-8') |
| 75 | + target_id = target_client.execute_command("CLUSTER", "MYID").decode('utf-8') |
| 76 | + |
| 77 | + # Perform atomic slot migration |
| 78 | + source_client.execute_command( |
| 79 | + "CLUSTER", "MIGRATESLOTS", |
| 80 | + "SLOTSRANGE", str(slot), str(slot), |
| 81 | + "NODE", target_id |
| 82 | + ) |
| 83 | + |
| 84 | + # Wait for backfill to complete |
| 85 | + time.sleep(3) |
| 86 | + |
| 87 | + # Check post-migration metrics |
| 88 | + search_info = target_client.execute_command("INFO", "search") |
| 89 | + ft_info = target_client.execute_command("FT.INFO", "idx") |
| 90 | + |
| 91 | + # Parse FT.INFO |
| 92 | + ft_dict = {} |
| 93 | + for i in range(0, len(ft_info), 2): |
| 94 | + key = ft_info[i].decode('utf-8') if isinstance(ft_info[i], bytes) else ft_info[i] |
| 95 | + value = ft_info[i+1] |
| 96 | + if isinstance(value, bytes): |
| 97 | + value = value.decode('utf-8') |
| 98 | + ft_dict[key] = value |
| 99 | + |
| 100 | + num_docs = int(ft_dict.get("num_docs", 0)) |
| 101 | + last_blocked_duration = int(search_info.get("search_last_throttled_duration_us", 0)) |
| 102 | + |
| 103 | + # Validate migration completed successfully |
| 104 | + assert num_docs == num_keys, \ |
| 105 | + f"Expected num_docs == {num_keys}, got {num_docs}" |
| 106 | + |
| 107 | + # Validate that blocking occurred with measurable duration |
| 108 | + assert last_blocked_duration > 0, \ |
| 109 | + f"Expected blocking duration > 0, got {last_blocked_duration}us" |
| 110 | + |
| 111 | + # Validate queue counters |
| 112 | + async_queue = int(search_info.get("search_async_mutation_queue", -1)) |
| 113 | + sync_queue = int(search_info.get("search_sync_mutation_queue", -1)) |
| 114 | + assert async_queue >= 0, f"Invalid async_mutation_queue: {async_queue}" |
| 115 | + assert sync_queue == 0, f"Expected sync_queue=0 (Phase 2), got {sync_queue}" |
| 116 | + |
| 117 | + # Note: throttled_clients_count might be 0 after unblocking completes |
| 118 | + # The duration metric proves blocking occurred even if count is currently 0 |
| 119 | + |
| 120 | + # Verify all throttling metrics exist |
| 121 | + required_metrics = [ |
| 122 | + "search_async_mutation_queue", |
| 123 | + "search_sync_mutation_queue", |
| 124 | + "search_throttled_clients_count", |
| 125 | + "search_last_throttled_duration_us", |
| 126 | + "search_current_throttled_duration_us" |
| 127 | + ] |
| 128 | + for metric in required_metrics: |
| 129 | + assert metric in search_info, f"Missing required metric: {metric}" |
| 130 | + |
| 131 | + def test_ratio_based_throttle_with_sync_traffic(self): |
| 132 | + """ |
| 133 | + Test Phase 3 ratio-based blocking with concurrent async + sync traffic. |
| 134 | + """ |
| 135 | + import threading |
| 136 | + |
| 137 | + source_client = self.client_for_primary(0) |
| 138 | + target_client = self.client_for_primary(1) |
| 139 | + cluster_client = self.new_cluster_client() |
| 140 | + |
| 141 | + source_client.execute_command( |
| 142 | + "FT.CREATE", "idx_phase3", "SCHEMA", |
| 143 | + "description", "VECTOR", "HNSW", "6", |
| 144 | + "TYPE", "FLOAT32", "DIM", "3", "DISTANCE_METRIC", "L2" |
| 145 | + ) |
| 146 | + time.sleep(1) |
| 147 | + |
| 148 | + for client in [source_client, target_client]: |
| 149 | + client.execute_command("CONFIG", "SET", "search.async-client-throttling-enabled", "yes") |
| 150 | + client.execute_command("CONFIG", "SET", "search.async-clients-block-threshold", "3") |
| 151 | + |
| 152 | + # Sync keys on target |
| 153 | + sync_keys = [f"{{sync}}:s{i}" for i in range(50)] |
| 154 | + for i, key in enumerate(sync_keys): |
| 155 | + vector_data = struct.pack('fff', float(200+i), float(201+i), float(202+i)) |
| 156 | + target_client.execute_command("HSET", key, "description", vector_data) |
| 157 | + |
| 158 | + # Migration keys on source |
| 159 | + migration_keys = [f"{{phase3}}:p{i}" for i in range(150)] |
| 160 | + for i, key in enumerate(migration_keys): |
| 161 | + vector_data = struct.pack('fff', float(i+1), float(i+2), float(i+3)) |
| 162 | + source_client.execute_command("HSET", key, "description", vector_data) |
| 163 | + migration_slot = source_client.execute_command("CLUSTER", "KEYSLOT", migration_keys[0]) |
| 164 | + |
| 165 | + # Background sync threads |
| 166 | + stop_background = threading.Event() |
| 167 | + bg_count = [0] |
| 168 | + |
| 169 | + def sync_traffic(tid): |
| 170 | + my_keys = sync_keys[tid::5] |
| 171 | + while not stop_background.is_set(): |
| 172 | + for key in my_keys: |
| 173 | + try: |
| 174 | + vector_data = struct.pack('fff', float(300+tid), float(301+tid), float(302+tid)) |
| 175 | + cluster_client.execute_command("HSET", key, "description", vector_data) |
| 176 | + bg_count[0] += 1 |
| 177 | + except: |
| 178 | + pass |
| 179 | + time.sleep(0.002) |
| 180 | + |
| 181 | + bg_threads = [threading.Thread(target=lambda t=i: sync_traffic(t), daemon=True) for i in range(5)] |
| 182 | + for t in bg_threads: |
| 183 | + t.start() |
| 184 | + time.sleep(0.3) |
| 185 | + |
| 186 | + target_id = target_client.execute_command("CLUSTER", "MYID").decode('utf-8') |
| 187 | + source_client.execute_command("CLUSTER", "MIGRATESLOTS", "SLOTSRANGE", str(migration_slot), str(migration_slot), "NODE", target_id) |
| 188 | + time.sleep(1.5) |
| 189 | + |
| 190 | + stop_background.set() |
| 191 | + for t in bg_threads: |
| 192 | + t.join(timeout=1) |
| 193 | + time.sleep(1) |
| 194 | + |
| 195 | + search_info = target_client.execute_command("INFO", "search") |
| 196 | + duration = int(search_info.get("search_last_throttled_duration_us", 0)) |
| 197 | + count = target_client.execute_command("CLUSTER", "COUNTKEYSINSLOT", migration_slot) |
| 198 | + |
| 199 | + assert count == len(migration_keys) |
| 200 | + assert duration > 0 |
| 201 | + assert bg_count[0] > 0 |
0 commit comments