docstr: Converts autoscaler scale request into a RayCluster CR patch payload.
docstr_tok: ["Converts","autoscaler","scale","request","into","a","RayCluster","CR","patch","payload","."]
code: def _scale_request_to_patch_payload(
      self, scale_request: ScaleRequest, raycluster: Dict[str, Any]
  ) -> List[Dict[str, Any]]:
      """Converts autoscaler scale request into a RayCluster CR patch payload."""
      patch_payload = []
      # Collect patches for replica counts.
      for (
          node_type,
          target_replicas,
      ) in scale_request.desired_num_workers.items():
          group_index = _worker_group_index(raycluster, node_type)
          group_max_replicas = _worker_group_max_replicas(
              raycluster, group_index
          )
          # Cap the replica count to maxReplicas.
          if (
              group_max_replicas is not None
              and group_max_replicas < target_replicas
          ):
              logger.warning(
                  "Autoscaler attempted to create "
                  + "more than maxReplicas pods of type {}.".format(
                      node_type
                  )
              )
              target_replicas = group_max_replicas
          # Check if we need to change the target count.
          if target_replicas == _worker_group_replicas(
              raycluster, group_index
          ):
              # No patch required.
              continue
          # Need to patch replica count. Format the patch and add it to the payload.
          patch = worker_replica_patch(group_index, target_replicas)
          patch_payload.append(patch)
  
      # Maps node_type to nodes to delete for that group.
      deletion_groups = defaultdict(list)
      for worker in scale_request.workers_to_delete:
          node_type = self.node_tags(worker)[TAG_RAY_USER_NODE_TYPE]
          deletion_groups[node_type].append(worker)
  
      for node_type, workers_to_delete in deletion_groups.items():
          group_index = _worker_group_index(raycluster, node_type)
          patch = worker_delete_patch(group_index, workers_to_delete)
          patch_payload.append(patch)
  
      return patch_payload
  
imports: import json
  import logging
  import os
  from collections import defaultdict
  from typing import Any, Dict, List, Optional, Tuple
  import requests
  from ray.autoscaler._private.constants import WORKER_LIVENESS_CHECK_KEY, WORKER_RPC_DRAIN_KEY
  from ray.autoscaler._private.util import NodeID, NodeIP, NodeKind, NodeStatus, NodeType
  from ray.autoscaler.batching_node_provider import BatchingNodeProvider, NodeData, ScaleRequest
  from ray.autoscaler.tags import NODE_KIND_HEAD, NODE_KIND_WORKER, STATUS_UP_TO_DATE, STATUS_UPDATE_FAILED, TAG_RAY_USER_NODE_TYPE
label_desc: Use image node_id 6 for calling the KubeRayNodeProvider obj's underlying member method code with example usage: obj._scale_request_to_patch_payload(scale_request, raycluster) and returns: patch_payload