Skip to content

Commit 5d1ad55

Browse files
ntlm1686facebook-github-bot
authored andcommitted
Replace partition by rpartition for parsing app_id (#523)
Summary: To parse a address like `"addr-of-cluster-app_id"`, the old code will fail since it takes `"addr"` as the address and `"of-cluster-app_id"` as the app id. After replace `partition` by `rpartition`, it should work as intended. Pulled By: d4l3k Pull Request resolved: #523 d4l3k Reviewed By: kurman Differential Revision: D37187555 fbshipit-source-id: f2dd94e9722aaf29ff1edd8b0588fd04a6493e65
1 parent efab8b4 commit 5d1ad55

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

torchx/schedulers/ray_scheduler.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import json
99
import logging
1010
import os
11+
import re
1112
import tempfile
1213
import time
1314
from dataclasses import dataclass, field
1415
from datetime import datetime
1516
from shutil import copy2, rmtree
16-
from typing import Any, cast, Dict, Iterable, List, Mapping, Optional, Set, Type # noqa
17+
from typing import Any, cast, Dict, Iterable, List, Optional, Tuple # noqa
1718

1819
from torchx.schedulers.api import (
1920
AppDryRunInfo,
@@ -322,13 +323,25 @@ def wait_until_finish(self, app_id: str, timeout: int = 30) -> None:
322323
break
323324
time.sleep(1)
324325

325-
def _cancel_existing(self, app_id: str) -> None: # pragma: no cover
326+
def _parse_app_id(self, app_id: str) -> Tuple[str, str]:
327+
# find index of '-' in the first :\d+-
328+
m = re.search(r":\d+-", app_id)
329+
if m:
330+
sep = m.span()[1]
331+
addr = app_id[: sep - 1]
332+
app_id = app_id[sep:]
333+
return addr, app_id
334+
326335
addr, _, app_id = app_id.partition("-")
336+
return addr, app_id
337+
338+
def _cancel_existing(self, app_id: str) -> None: # pragma: no cover
339+
addr, app_id = self._parse_app_id(app_id)
327340
client = JobSubmissionClient(f"http://{addr}")
328341
client.stop_job(app_id)
329342

330343
def _get_job_status(self, app_id: str) -> JobStatus:
331-
addr, _, app_id = app_id.partition("-")
344+
addr, app_id = self._parse_app_id(app_id)
332345
client = JobSubmissionClient(f"http://{addr}")
333346
status = client.get_job_status(app_id)
334347
if isinstance(status, str):
@@ -375,7 +388,7 @@ def log_iter(
375388
streams: Optional[Stream] = None,
376389
) -> Iterable[str]:
377390
# TODO: support tailing, streams etc..
378-
addr, _, app_id = app_id.partition("-")
391+
addr, app_id = self._parse_app_id(app_id)
379392
client: JobSubmissionClient = JobSubmissionClient(f"http://{addr}")
380393
logs: str = client.get_job_logs(app_id)
381394
iterator = split_lines(logs)

torchx/schedulers/test/ray_scheduler_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ def test_requirements(self) -> None:
298298
job = req.request
299299
self.assertEqual(job.requirements, reqs)
300300

301+
def test_parse_app_id(self) -> None:
302+
test_addr_appid = [
303+
(
304+
"0.0.0.0:1234-app_id",
305+
"0.0.0.0:1234",
306+
"app_id",
307+
), # (full address, address:port, app_id)
308+
("addr-of-cluster:1234-app-id", "addr-of-cluster:1234", "app-id"),
309+
("www.test.com:1234-app:id", "www.test.com:1234", "app:id"),
310+
("foo", "foo", ""),
311+
("foo-bar-bar", "foo", "bar-bar"),
312+
]
313+
for test_example, addr, app_id in test_addr_appid:
314+
parsed_addr, parsed_appid = self._scheduler._parse_app_id(test_example)
315+
self.assertEqual(parsed_addr, addr)
316+
self.assertEqual(parsed_appid, app_id)
317+
301318
class RayClusterSetup:
302319
_instance = None # pyre-ignore[4]
303320

0 commit comments

Comments
 (0)