diff --git a/modules/python/clients/aks_machine_client.py b/modules/python/clients/aks_machine_client.py index 9772d1ecde..737de08c88 100644 --- a/modules/python/clients/aks_machine_client.py +++ b/modules/python/clients/aks_machine_client.py @@ -47,6 +47,14 @@ # consume the caller's whole ``timeout`` budget before the polling loop starts. # The remaining budget is reserved for ``_wait_for_agentpool_provisioning``. _PER_REQUEST_TIMEOUT_CAP = 60 +# Bounded 429 retry budget for ``_scale_machine_batch``. Total number of +# attempts (NOT additional retries on top of an initial call). The AKS RP +# throttles BatchPutMachine independently of normal PUTs; small bursts at +# the start of a scale-out are common, so a short exponential backoff +# (1s, 2s, 4s, 8s between the five attempts) usually clears them without +# ballooning the chunk's wall-time. +_BATCH_429_MAX_RETRIES = 5 +_BATCH_429_INITIAL_BACKOFF_SECONDS = 1.0 class AKSMachineClient(AKSClient): @@ -375,8 +383,10 @@ def scale_machine( Flow: 1. Snapshot the current Ready+labeled node count in the agentpool (``baseline_count``) so the readiness watcher counts only NEW nodes. - 2. Submit machine PUTs individually (per-machine ARM polling is - intentionally NOT performed). + 2. Submit machine PUTs -- individually by default, or sharded into + ``machine_workers`` chunks where each chunk is sent as a single + ``BatchPutMachine``-headered PUT when ``use_batch_api=True`` (per- + machine ARM polling is intentionally NOT performed in either path). 3. Wait ONCE on the agentpool-level provisioningState. 4. Wait for nodes labeled ``agentpool=`` to surpass ``baseline_count`` by enough to satisfy P50/P70/P90/P99/P100 targets. @@ -384,19 +394,14 @@ def scale_machine( ``tags`` is currently unused (Machine API rejects top-level tags on the machine PUT body; machines inherit tags from the parent agentpool). - The ``use_batch_api=True`` branch is stubbed on this PR and raises - ``NotImplementedError``; it will land in a follow-up PR. + See ``_create_batch_machines`` for the batch chunking contract and the + ``BatchPutMachine`` header schema. Raises: - NotImplementedError: ``use_batch_api=True``. RuntimeError: fewer machines landed than requested, agentpool did not reach Succeeded within ``timeout``, or P100 readiness did not complete within ``readiness_wait_timeout``. """ - if use_batch_api: - raise NotImplementedError( - "scale_machine batch path (use_batch_api=True) lands in a follow-up PR" - ) cluster_name = cluster_name or self.get_cluster_name() metadata = { "cluster_name": cluster_name, @@ -446,7 +451,12 @@ def scale_machine( names = [ f"{prefix}-machine-{i + 1}" for i in range(scale_machine_count) ] - successful = self._scale_machine_individually(request, names) + command_t0 = time.time() + if use_batch_api: + successful = self._scale_machine_batch(request, names) + else: + successful = self._scale_machine_individually(request, names) + op.add_metadata("command_execution_time", time.time() - command_t0) op.add_metadata("successful_machines", successful) # Fail fast on partial landing BEFORE waiting on the agentpool @@ -573,3 +583,192 @@ def _create_single_machine(self, name: str, request: SimpleNamespace) -> bool: logger.error(f"PUT machine {name} -> {resp.status_code} {resp.text[:500]}") return False return True + + # ---- Machine API: batch scale path ---- + def _scale_machine_batch( + self, + request: SimpleNamespace, + names: List[str], + ) -> List[str]: + """Dispatch ``names`` across ``machine_workers`` worker slices via the Batch API. + + Each worker submits exactly one ``BatchPutMachine``-headered PUT carrying a + contiguous slice of ``names``. Slices are computed by arithmetic from the + ``worker_id``. + + ``scale_machine_count`` MUST be an exact multiple of ``machine_workers``; + ``ValueError`` is raised otherwise so the failure surfaces at the + ``MachineCRUD`` boundary instead of producing a short final chunk that + would skew per-chunk latency dashboards. + + Per-worker exceptions are caught and logged; the worker's slice is + excluded from the returned ``successful`` list. The input ``request`` + is not mutated. + """ + n = len(names) + workers = request.machine_workers + if workers <= 0: + raise ValueError(f"machine_workers must be positive (got {workers})") + if n % workers != 0: + raise ValueError( + f"scale_machine_count ({n}) must be an exact multiple of " + f"machine_workers ({workers}) when use_batch_api=True; " + f"got remainder {n % workers}" + ) + per_worker = n // workers + successful: List[str] = [] + + def run_worker(worker_id: int) -> List[str]: + start = worker_id * per_worker + chunk = names[start:start + per_worker] + try: + return self._create_batch_machines(request, chunk, worker_id) + except Exception: + logger.exception(f"batch worker {worker_id} failed") + return [] + + with ThreadPoolExecutor(max_workers=workers) as ex: + futures = [ex.submit(run_worker, w) for w in range(workers)] + for fut in as_completed(futures): + try: + created = fut.result() + except Exception: + # run_worker already swallows; this is belt-and-suspenders. + logger.exception("unexpected error retrieving batch worker result") + continue + successful.extend(created) + return successful + + def _create_batch_machines( + self, + request: SimpleNamespace, + chunk: List[str], + chunk_idx: int, + ) -> List[str]: + """Submit a batch-PUT request creating every machine in ``chunk``. + + Uses the AKS Machine API ``BatchPutMachine`` HEADER pattern (implicit + contract with AKS NAP, not publicly available): a single PUT to the first + machine name in ``chunk`` carrying a ``BatchPutMachine`` header whose + value is JSON containing ``vmSkus`` and ``batchMachines`` lists. + + ``batchMachines`` lists ONLY the *additional* machines; the first one + is created from the URL + body. Each entry uses the key + ``machineName`` -- the wrong key (e.g. ``name``) causes the API to + silently ignore the extras and create only the URL-pointed machine. + + ``vmSkus`` is a ``{"value": []}`` envelope (not a flat + list); the inner object only needs ``name`` (the requested VM size) + and ``resourceType`` -- the AKS NAP Batch endpoint validates the + rest server-side. We deliberately omit speculative SKU shape fields + (family, locations, capabilities) so the header schema stays + consistent across all VM sizes. + + Returns the list of machine names submitted on 2xx. Raises + ``RuntimeError`` (from ``_make_batch_request``) if the request + exhausts 429 retries or returns a non-2xx. Caller catches and + excludes failed chunks from the success list. + """ + if not chunk: + return [] + sub = self.subscription_id + first_machine_name = chunk[0] + url = ( + f"{_ARM_BASE}/subscriptions/{sub}/resourceGroups/{request.resource_group}" + f"/providers/Microsoft.ContainerService/managedClusters/{request.cluster_name}" + f"/agentPools/{request.agentpool_name}/machines/{first_machine_name}" + f"?api-version={_MACHINE_API_VERSION}" + ) + body: Dict[str, Any] = {"properties": {"hardware": {"vmSize": request.vm_size}}} + remaining_machines = chunk[1:] + batch_machines = [{"machineName": name} for name in remaining_machines] + vm_sku = { + "name": request.vm_size, + "resourceType": "virtualMachines", + } + batch_header_value = json.dumps({ + "vmSkus": {"value": [vm_sku]}, + "batchMachines": batch_machines, + }) + logger.info( + f"chunk {chunk_idx}: submitting BatchPutMachine PUT for {len(chunk)} machines " + f"(target={first_machine_name})" + ) + # Cap the timeout value passed to this batch PUT attempt. This limits + # the per-attempt request timeout given to ``_make_batch_request``, + # but it does not bound total wall time for the chunk because retry + # and backoff handling can still extend the overall elapsed time. + put_timeout = min(request.timeout, _PER_REQUEST_TIMEOUT_CAP) + self._make_batch_request( + "PUT", + url, + body, + put_timeout, + batch_header_value=batch_header_value, + chunk_idx=chunk_idx, + first_machine_name=first_machine_name, + ) + return list(chunk) + + def _make_batch_request( + self, + method: str, + url: str, + data: Dict[str, Any], + timeout: int, + batch_header_value: str, + chunk_idx: Optional[int] = None, + first_machine_name: Optional[str] = None, + ) -> None: + """Send a ``BatchPutMachine``-headered request with bounded 429 backoff. + + Sends the request directly (NOT via ``make_request``) so we can attach + the ``BatchPutMachine`` header alongside the standard auth/content-type + headers. Calls the endpoint up to ``_BATCH_429_MAX_RETRIES`` total + times on HTTP 429, sleeping between attempts with exponential backoff + starting at ``_BATCH_429_INITIAL_BACKOFF_SECONDS`` and doubling each + time (1s, 2s, 4s, 8s by default). All other non-2xx responses raise + immediately. + """ + # Compact prefix so every error/log line is self-identifying in Kusto + # (chunk_idx + first machine name pinpoint the failing slice without + # cross-referencing the per-worker log). + ctx = ( + f"chunk={chunk_idx} target={first_machine_name} {method} {url}" + ) + backoff = _BATCH_429_INITIAL_BACKOFF_SECONDS + last_resp: Optional[requests.Response] = None + for attempt in range(_BATCH_429_MAX_RETRIES): + headers = { + "Authorization": f"Bearer {self._get_access_token()}", + "Content-Type": "application/json", + "BatchPutMachine": batch_header_value, + } + # Use the client's shared Session so this batch path reuses the + # same pooled TCP/TLS connections (and adapters/proxies) as the + # individual ``make_request`` path; avoids per-PUT handshakes + # during large scales. + resp = self._session.request( + method, url, headers=headers, json=data, timeout=timeout, + ) + last_resp = resp + if resp.status_code in (200, 201, 202, 204): + return + if resp.status_code != 429: + raise RuntimeError( + f"batch request failed [{ctx}]: " + f"{resp.status_code} {resp.text[:500]}" + ) + if attempt == _BATCH_429_MAX_RETRIES - 1: + break + logger.warning( + f"batch request 429 [{ctx}]; retrying in {backoff:.1f}s " + f"(attempt {attempt + 1}/{_BATCH_429_MAX_RETRIES})" + ) + time.sleep(backoff) + backoff *= 2 + text = last_resp.text[:500] if last_resp is not None else "" + raise RuntimeError( + f"batch request exceeded {_BATCH_429_MAX_RETRIES} attempts " + f"after HTTP 429 responses [{ctx}]; last text={text}" + ) diff --git a/modules/python/tests/test_aks_machine_client.py b/modules/python/tests/test_aks_machine_client.py index 6c6b3fadc9..0f97884834 100644 --- a/modules/python/tests/test_aks_machine_client.py +++ b/modules/python/tests/test_aks_machine_client.py @@ -7,9 +7,10 @@ the right keys - failure path raises (so the OperationContext records ``success=False``) -This revision adds tests for the non-batch scale path. The batch dispatch -(``use_batch_api=True``) is asserted to raise ``NotImplementedError`` until -the follow-up PR lands. +This revision adds tests for both scale paths (non-batch individual PUTs and +the BatchPutMachine-headered chunked path) plus the batch-path helpers +``_scale_machine_batch``, ``_create_batch_machines``, and +``_make_batch_request``. """ # pylint: disable=protected-access # Tests intentionally exercise private helpers directly; the leading underscore @@ -256,15 +257,46 @@ def test_scale_machine_passes_baseline_count_to_readiness( self.assertEqual(mock_wait_ready.call_args.kwargs["baseline_count"], 3) self.assertEqual(mock_wait_ready.call_args.kwargs["expected_count"], 1) - def test_scale_machine_batch_path_raises_not_implemented(self): - """The use_batch_api=True branch is stubbed until the follow-up PR.""" - with self.assertRaises(NotImplementedError): + def test_scale_machine_batch_path_dispatches_to_batch(self): + """The use_batch_api=True branch calls _scale_machine_batch (not _individually) + and records command_execution_time in operation metadata.""" + names = [f"scale2-machine-{i+1}" for i in range(2)] + with mock.patch.object( + AKSMachineClient, + "_scale_machine_batch", + return_value=names, + ) as mock_batch, mock.patch.object( + AKSMachineClient, "_scale_machine_individually" + ) as mock_individual, mock.patch.object( + AKSMachineClient, "_wait_for_agentpool_provisioning", return_value=True + ), mock.patch.object( + AKSMachineClient, + "_wait_for_machine_node_readiness", + return_value={ + f"P{p}": { + "target_nodes": 2, + "elapsed_time_seconds": 12.0, + "percentage": p, + "success": True, + } + for p in (50, 70, 90, 99, 100) + }, + ): + self.mock_k8s.get_ready_nodes.return_value = [] self.client.scale_machine( agentpool_name="apool", vm_size="Standard_D2_v3", scale_machine_count=2, use_batch_api=True, + machine_workers=2, ) + mock_batch.assert_called_once() + mock_individual.assert_not_called() + # command_execution_time must be added to operation metadata. + added_keys = { + call.args[0] for call in self.mock_operation.add_metadata.call_args_list + } + self.assertIn("command_execution_time", added_keys) # ---- _create_single_machine ---- @@ -477,6 +509,197 @@ def test_wait_readiness_no_k8s_client_short_circuits(self): self.assertFalse(env[f"P{p}"]["success"]) self.assertEqual(env[f"P{p}"]["target_nodes"], 0) + # ---- _scale_machine_batch ---- + + def test_scale_machine_batch_partitions_and_aggregates(self): + """Names sharded across machine_workers; all successful slices aggregated.""" + request = SimpleNamespace( + agentpool_name="apool", + cluster_name="fake-cluster", + resource_group="fake-rg", + vm_size="Standard_D2_v3", + timeout=60, + machine_workers=2, + ) + names = ["m-1", "m-2", "m-3", "m-4"] + with mock.patch.object( + AKSMachineClient, + "_create_batch_machines", + side_effect=lambda req, chunk, worker_id: list(chunk), + ) as mock_create_batch: + successful = self.client._scale_machine_batch(request, names) + self.assertEqual(set(successful), set(names)) + # 2 workers, each handling per_worker = 4 // 2 = 2 names + self.assertEqual(mock_create_batch.call_count, 2) + + def test_scale_machine_batch_per_worker_failure_isolated(self): + """One worker raising does not poison the other worker's success list.""" + request = SimpleNamespace( + agentpool_name="apool", + cluster_name="fake-cluster", + resource_group="fake-rg", + vm_size="Standard_D2_v3", + timeout=60, + machine_workers=2, + ) + names = ["m-1", "m-2", "m-3", "m-4"] + counter = itertools.count() + + def fake_create(req, chunk, worker_id): # pylint: disable=unused-argument + if next(counter) == 0: + raise RuntimeError("first worker boom") + return list(chunk) + + with mock.patch.object( + AKSMachineClient, "_create_batch_machines", side_effect=fake_create + ): + successful = self.client._scale_machine_batch(request, names) + # Exactly one worker's slice survives. + self.assertEqual(len(successful), 2) + + def test_scale_machine_batch_rejects_non_exact_multiple(self): + """scale_machine_count must be an exact multiple of machine_workers.""" + request = SimpleNamespace( + agentpool_name="apool", + cluster_name="fake-cluster", + resource_group="fake-rg", + vm_size="Standard_D2_v3", + timeout=60, + machine_workers=3, + ) + names = ["m-1", "m-2", "m-3", "m-4"] # 4 not divisible by 3 + with self.assertRaises(ValueError): + self.client._scale_machine_batch(request, names) + + # ---- _create_batch_machines ---- + + def test_create_batch_machines_empty_chunk_returns_empty(self): + """Empty chunk -> early return, no HTTP call.""" + request = SimpleNamespace( + agentpool_name="apool", + cluster_name="fake-cluster", + resource_group="fake-rg", + vm_size="Standard_D2_v3", + timeout=60, + ) + with mock.patch.object( + AKSMachineClient, "_make_batch_request" + ) as mock_make_batch: + result = self.client._create_batch_machines(request, [], 0) + self.assertEqual(result, []) + mock_make_batch.assert_not_called() + + def test_create_batch_machines_header_shape(self): + """BatchPutMachine header carries vmSkus envelope + batchMachines + using machineName (NOT name) keys for the *additional* machines only.""" + request = SimpleNamespace( + agentpool_name="apool", + cluster_name="fake-cluster", + resource_group="fake-rg", + vm_size="Standard_D2_v3", + timeout=60, + ) + with mock.patch.object( + AKSMachineClient, "_make_batch_request" + ) as mock_make_batch: + result = self.client._create_batch_machines( + request, ["m-1", "m-2", "m-3"], chunk_idx=0 + ) + self.assertEqual(result, ["m-1", "m-2", "m-3"]) + mock_make_batch.assert_called_once() + # Inspect the batch_header_value kwarg. + import json as _json # pylint: disable=import-outside-toplevel + kwargs = mock_make_batch.call_args.kwargs + header_value = kwargs["batch_header_value"] + parsed = _json.loads(header_value) + # vmSkus is wrapped in {"value": [...]} + self.assertIn("vmSkus", parsed) + self.assertIn("value", parsed["vmSkus"]) + self.assertEqual(len(parsed["vmSkus"]["value"]), 1) + self.assertEqual( + parsed["vmSkus"]["value"][0]["name"], "Standard_D2_v3" + ) + # First machine of chunk is created from the URL+body; only the rest + # appear in batchMachines. + self.assertEqual( + parsed["batchMachines"], + [{"machineName": "m-2"}, {"machineName": "m-3"}], + ) + + # ---- _make_batch_request ---- + + def test_make_batch_request_2xx_returns(self): + """2xx response returns without raising; one HTTP call made.""" + with mock.patch.object( + self.client._session, "request" + ) as mock_request: + mock_request.return_value.status_code = 200 + self.client._make_batch_request( + "PUT", + "https://fake/url", + {"k": "v"}, + timeout=30, + batch_header_value="{}", + ) + mock_request.assert_called_once() + + def test_make_batch_request_non_2xx_non_429_raises(self): + """500 -> RuntimeError, no retry.""" + with mock.patch.object( + self.client._session, "request" + ) as mock_request: + mock_request.return_value.status_code = 500 + mock_request.return_value.text = "boom" + with self.assertRaises(RuntimeError): + self.client._make_batch_request( + "PUT", + "https://fake/url", + {}, + timeout=30, + batch_header_value="{}", + ) + self.assertEqual(mock_request.call_count, 1) + + def test_make_batch_request_429_then_success(self): + """429 then 2xx (201) -> succeeds after 1 retry.""" + responses = [ + mock.MagicMock(status_code=429), + mock.MagicMock(status_code=201), + ] + with mock.patch.object( + self.client._session, "request", side_effect=responses, + ) as mock_request, mock.patch( + "clients.aks_machine_client.time.sleep" + ): + self.client._make_batch_request( + "PUT", + "https://fake/url", + {}, + timeout=30, + batch_header_value="{}", + ) + self.assertEqual(mock_request.call_count, 2) + + def test_make_batch_request_429_exhausted_raises(self): + """429 across the full retry budget -> RuntimeError.""" + with mock.patch.object( + self.client._session, "request" + ) as mock_request, mock.patch( + "clients.aks_machine_client.time.sleep" + ): + mock_request.return_value.status_code = 429 + mock_request.return_value.text = "rate limited" + with self.assertRaises(RuntimeError): + self.client._make_batch_request( + "PUT", + "https://fake/url", + {}, + timeout=30, + batch_header_value="{}", + ) + # _BATCH_429_MAX_RETRIES == 5 total attempts (no extra initial call) + self.assertEqual(mock_request.call_count, 5) + if __name__ == "__main__": unittest.main()