|
| 1 | +# Copyright 1999-2021 Alibaba Group Holding Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import logging |
| 17 | +import time |
| 18 | +from collections import defaultdict |
| 19 | +from typing import List, DefaultDict, Dict, Tuple |
| 20 | + |
| 21 | +from .... import oscar as mo |
| 22 | +from ....resource import Resource, ZeroResource |
| 23 | +from ....typing import BandType |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class GlobalResourceManagerActor(mo.Actor): |
| 29 | + # {(address, resource_type): {(session_id, subtask_id): Resource(...)}} |
| 30 | + _band_stid_resources: DefaultDict[BandType, Dict[Tuple[str, str], Resource]] |
| 31 | + _band_used_resources: Dict[BandType, Resource] |
| 32 | + _band_total_resources: Dict[BandType, Resource] |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + self._band_stid_resources = defaultdict(dict) |
| 36 | + self._band_used_resources = defaultdict(lambda: ZeroResource) |
| 37 | + self._band_idle_start_time = dict() |
| 38 | + self._band_idle_events = dict() |
| 39 | + self._band_total_resources = dict() |
| 40 | + self._cluster_api = None |
| 41 | + self._band_watch_task = None |
| 42 | + |
| 43 | + async def __post_create__(self): |
| 44 | + from ...cluster.api import ClusterAPI |
| 45 | + |
| 46 | + self._cluster_api = await ClusterAPI.create(self.address) |
| 47 | + |
| 48 | + async def watch_bands(): |
| 49 | + async for bands in self._cluster_api.watch_all_bands(): |
| 50 | + old_bands = set(self._band_total_resources.keys()) |
| 51 | + await self._refresh_bands(bands) |
| 52 | + new_bands = set(bands.keys()) - old_bands |
| 53 | + for band in new_bands: |
| 54 | + self._update_band_usage(band, ZeroResource) |
| 55 | + |
| 56 | + self._band_watch_task = asyncio.create_task(watch_bands()) |
| 57 | + |
| 58 | + async def __pre_destroy__(self): |
| 59 | + self._band_watch_task.cancel() |
| 60 | + |
| 61 | + async def refresh_bands(self): |
| 62 | + bands = await self._cluster_api.get_all_bands() |
| 63 | + await self._refresh_bands(bands) |
| 64 | + |
| 65 | + async def _refresh_bands(self, bands): |
| 66 | + # TODO add `num_mem_bytes` after supported report worker memory |
| 67 | + band_total_resources = {} |
| 68 | + for band, slot in bands.items(): |
| 69 | + if band[1].startswith("gpu"): |
| 70 | + band_total_resources[band] = Resource(num_gpus=slot) |
| 71 | + elif band[1].startswith("numa"): |
| 72 | + band_total_resources[band] = Resource(num_cpus=slot) |
| 73 | + else: |
| 74 | + raise NotImplementedError(f"Unsupported band type {band}") |
| 75 | + self._band_total_resources = band_total_resources |
| 76 | + |
| 77 | + @mo.extensible |
| 78 | + async def apply_subtask_resources( |
| 79 | + self, |
| 80 | + band: BandType, |
| 81 | + session_id: str, |
| 82 | + subtask_ids: List[str], |
| 83 | + subtask_resources: List[Resource], |
| 84 | + ) -> List[str]: |
| 85 | + if ( |
| 86 | + not self._band_total_resources or band not in self._band_total_resources |
| 87 | + ): # pragma: no cover |
| 88 | + await self.refresh_bands() |
| 89 | + idx = 0 |
| 90 | + # only ready bands will pass |
| 91 | + if band in self._band_total_resources: |
| 92 | + total_resource = self._band_total_resources[band] |
| 93 | + for stid, subtask_resource in zip(subtask_ids, subtask_resources): |
| 94 | + band_used_resource = self._band_used_resources[band] |
| 95 | + if band_used_resource + subtask_resource > total_resource: |
| 96 | + break |
| 97 | + self._band_stid_resources[band][(session_id, stid)] = subtask_resource |
| 98 | + self._update_band_usage(band, subtask_resource) |
| 99 | + idx += 1 |
| 100 | + if idx == 0: |
| 101 | + logger.debug( |
| 102 | + "No resources available, status: %r, request: %r", |
| 103 | + self._band_used_resources, |
| 104 | + subtask_resources, |
| 105 | + ) |
| 106 | + return subtask_ids[:idx] |
| 107 | + |
| 108 | + @mo.extensible |
| 109 | + def update_subtask_resources( |
| 110 | + self, band: BandType, session_id: str, subtask_id: str, resource: Resource |
| 111 | + ): |
| 112 | + session_subtask_id = (session_id, subtask_id) |
| 113 | + subtask_resources = self._band_stid_resources[band] |
| 114 | + if session_subtask_id not in subtask_resources: |
| 115 | + return |
| 116 | + |
| 117 | + resource_delta = resource - subtask_resources[session_subtask_id] |
| 118 | + subtask_resources[session_subtask_id] = resource |
| 119 | + self._update_band_usage(band, resource_delta) |
| 120 | + |
| 121 | + @mo.extensible |
| 122 | + def release_subtask_resource( |
| 123 | + self, band: BandType, session_id: str, subtask_id: str |
| 124 | + ): |
| 125 | + # todo ensure slots released when subtasks ends in all means |
| 126 | + resource_delta = self._band_stid_resources[band].pop( |
| 127 | + (session_id, subtask_id), ZeroResource |
| 128 | + ) |
| 129 | + self._update_band_usage(band, -resource_delta) |
| 130 | + |
| 131 | + def _update_band_usage(self, band: BandType, band_usage_delta: Resource): |
| 132 | + self._band_used_resources[band] += band_usage_delta |
| 133 | + # some code path doesn't call `apply_subtask_resources` |
| 134 | + band_total_resource = self._band_total_resources.get(band) |
| 135 | + if ( |
| 136 | + band_total_resource is not None |
| 137 | + and self._band_used_resources[band] > band_total_resource |
| 138 | + ): # pragma: no cover |
| 139 | + raise Exception( |
| 140 | + f"Resource exceed: band used resource {self._band_used_resources[band]} " |
| 141 | + f"band total resource {self._band_total_resources[band]}" |
| 142 | + ) |
| 143 | + if self._band_used_resources[band] <= ZeroResource: |
| 144 | + self._band_used_resources.pop(band) |
| 145 | + self._band_idle_start_time[band] = time.time() |
| 146 | + if band in self._band_idle_events: |
| 147 | + self._band_idle_events.pop(band).set() |
| 148 | + else: |
| 149 | + self._band_idle_start_time[band] = -1 |
| 150 | + |
| 151 | + def get_used_resources(self) -> Dict[BandType, Resource]: |
| 152 | + return self._band_used_resources |
| 153 | + |
| 154 | + def get_remaining_resources(self) -> Dict[BandType, Resource]: |
| 155 | + resources = {} |
| 156 | + for band, resource in self._band_total_resources.items(): |
| 157 | + used_resource = self.get_used_resources()[band] |
| 158 | + resources[band] = resource - used_resource |
| 159 | + return resources |
| 160 | + |
| 161 | + async def get_idle_bands(self, idle_duration: int): |
| 162 | + """Return a band list which all bands has been idle for at least `idle_duration` seconds.""" |
| 163 | + now = time.time() |
| 164 | + idle_bands = [] |
| 165 | + for band in self._band_total_resources.keys(): |
| 166 | + idle_start_time = self._band_idle_start_time.get(band) |
| 167 | + if idle_start_time is None: # pragma: no cover |
| 168 | + # skip new requested band for this round scale in. |
| 169 | + self._band_idle_start_time[band] = now |
| 170 | + elif idle_start_time > 0 and now >= idle_start_time + idle_duration: |
| 171 | + idle_bands.append(band) |
| 172 | + return idle_bands |
| 173 | + |
| 174 | + async def wait_band_idle(self, band: BandType): |
| 175 | + if self._band_idle_start_time[band] <= 0: |
| 176 | + if band in self._band_idle_events: |
| 177 | + event = self._band_idle_events[band] |
| 178 | + else: |
| 179 | + event = asyncio.Event() |
| 180 | + self._band_idle_events[band] = event |
| 181 | + return event.wait() |
0 commit comments