Create one service instance on an idle GPU and add it to scheduling pool.
(self, instance_type: str)
| 1319 | """Create one service instance on an idle GPU and add it to scheduling pool.""" |
| 1320 | if instance_type not in {"encoder", "transformer", "decoder"}: |
| 1321 | raise ValueError("instance_type must be one of: encoder, transformer, decoder") |
| 1322 | if self._runtime_config is None: |
| 1323 | raise RuntimeError("controller runtime config is not initialized") |
| 1324 | |
| 1325 | with self._instance_lock: |
| 1326 | use_static_slots = bool(self._static_instance_slots) |
| 1327 | selected_slot: dict[str, Any] | None = None |
| 1328 | |
| 1329 | if use_static_slots: |
| 1330 | if not self._free_slot_ids: |
| 1331 | raise RuntimeError("no idle static slot available") |
| 1332 | |
| 1333 | now = time.time() |
| 1334 | for slot_id in sorted(self._free_slot_ids): |
| 1335 | slot = self._static_instance_slots[slot_id] |
| 1336 | if slot.get("instance_type") != instance_type: |
| 1337 | continue |
| 1338 | if now < self._slot_reuse_block_until.get(slot_id, 0.0): |
| 1339 | continue |
| 1340 | |
| 1341 | host = str(slot["host"]) |
| 1342 | engine_rank = int(slot["engine_rank"]) |
| 1343 | monitor_port = MONITOR_POLLING_PORT + engine_rank |
| 1344 | if self._is_tcp_port_open(host, monitor_port): |
| 1345 | self.logger.warning( |
| 1346 | "Skip static slot=%s host=%s rank=%s for %s creation because monitor port %s is still in use", |
| 1347 | slot_id, |
| 1348 | host, |
| 1349 | engine_rank, |
| 1350 | instance_type, |
| 1351 | monitor_port, |
| 1352 | ) |
| 1353 | continue |
| 1354 | |
| 1355 | selected_slot = slot |
| 1356 | break |
| 1357 | |
| 1358 | if selected_slot is None: |
| 1359 | raise RuntimeError(f"no idle static slot available for {instance_type}: all candidates cooling down or port is in use") |
| 1360 | |
| 1361 | engine_rank = int(selected_slot["engine_rank"]) |
| 1362 | host = str(selected_slot["host"]) |
| 1363 | cuda_device = str(selected_slot["cuda_device"]) |
| 1364 | else: |
| 1365 | if not self._free_gpus: |
| 1366 | raise RuntimeError("no idle GPU available") |
| 1367 | |
| 1368 | now = time.time() |
| 1369 | engine_rank: int | None = None |
| 1370 | host = self._bootstrap_addr |
| 1371 | for candidate_gpu in sorted(self._free_gpus): |
| 1372 | if now < self._gpu_reuse_block_until.get(candidate_gpu, 0.0): |
| 1373 | continue |
| 1374 | |
| 1375 | monitor_port = MONITOR_POLLING_PORT + candidate_gpu |
| 1376 | if self._is_tcp_port_open(self._bootstrap_addr, monitor_port): |
| 1377 | self.logger.warning( |
| 1378 | "Skip gpu=%s for %s creation because monitor port %s is still in use", |
no test coverage detected