Retry paginated manifest and parallelize large downloads
Browse files- mirror_worker.py +37 -4
mirror_worker.py
CHANGED
|
@@ -33,6 +33,40 @@ def retry(action, label: str, attempts: int = 6):
|
|
| 33 |
time.sleep(min(180, 2 ** attempt * 5))
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def main() -> None:
|
| 37 |
WORKDIR.mkdir(parents=True, exist_ok=True)
|
| 38 |
source = HubApi(token=MODELSCOPE_TOKEN)
|
|
@@ -42,9 +76,7 @@ def main() -> None:
|
|
| 42 |
uploaded = set(target.list_repo_files(DEST_REPO, repo_type="dataset"))
|
| 43 |
# The ordinary repository listing is truncated for very large ModelScope
|
| 44 |
# datasets. Their dataset downloader uses this paginated tree endpoint.
|
| 45 |
-
files =
|
| 46 |
-
SOURCE_REPO, page_size=200
|
| 47 |
-
)
|
| 48 |
selected = [
|
| 49 |
item for number, item in enumerate(files)
|
| 50 |
if number % WORKER_COUNT == WORKER_INDEX
|
|
@@ -68,8 +100,9 @@ def main() -> None:
|
|
| 68 |
"dataset",
|
| 69 |
path,
|
| 70 |
local_dir=WORKDIR,
|
| 71 |
-
force=
|
| 72 |
expected_sha256=item.get("Sha256") or item.get("sha256"),
|
|
|
|
| 73 |
),
|
| 74 |
f"download {path}",
|
| 75 |
)
|
|
|
|
| 33 |
time.sleep(min(180, 2 ** attempt * 5))
|
| 34 |
|
| 35 |
|
| 36 |
+
def paginated_manifest(source: HubApi) -> list[dict]:
|
| 37 |
+
"""List the source tree page-by-page, reauthenticating on transient 400s."""
|
| 38 |
+
items: list[dict] = []
|
| 39 |
+
page = 1
|
| 40 |
+
while True:
|
| 41 |
+
def fetch_page():
|
| 42 |
+
response = source.legacy._request(
|
| 43 |
+
"GET",
|
| 44 |
+
f"datasets/{SOURCE_REPO}/repo/tree",
|
| 45 |
+
params={
|
| 46 |
+
"Revision": "master",
|
| 47 |
+
"Recursive": "True",
|
| 48 |
+
"PageNumber": page,
|
| 49 |
+
"PageSize": 200,
|
| 50 |
+
},
|
| 51 |
+
)
|
| 52 |
+
data = source.legacy._json_data(response)
|
| 53 |
+
return data if isinstance(data, list) else data.get("Files", [])
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
batch = retry(fetch_page, f"manifest page {page}")
|
| 57 |
+
except Exception:
|
| 58 |
+
# ModelScope occasionally loses the session while walking a large
|
| 59 |
+
# gated tree. Refresh it, then retry this same page.
|
| 60 |
+
source.login(MODELSCOPE_TOKEN)
|
| 61 |
+
batch = retry(fetch_page, f"manifest page {page} after login")
|
| 62 |
+
items.extend(batch)
|
| 63 |
+
if page % 100 == 0:
|
| 64 |
+
print(f"manifest page {page}: {len(items)} entries", flush=True)
|
| 65 |
+
if len(batch) < 200:
|
| 66 |
+
return items
|
| 67 |
+
page += 1
|
| 68 |
+
|
| 69 |
+
|
| 70 |
def main() -> None:
|
| 71 |
WORKDIR.mkdir(parents=True, exist_ok=True)
|
| 72 |
source = HubApi(token=MODELSCOPE_TOKEN)
|
|
|
|
| 76 |
uploaded = set(target.list_repo_files(DEST_REPO, repo_type="dataset"))
|
| 77 |
# The ordinary repository listing is truncated for very large ModelScope
|
| 78 |
# datasets. Their dataset downloader uses this paginated tree endpoint.
|
| 79 |
+
files = paginated_manifest(source)
|
|
|
|
|
|
|
| 80 |
selected = [
|
| 81 |
item for number, item in enumerate(files)
|
| 82 |
if number % WORKER_COUNT == WORKER_INDEX
|
|
|
|
| 100 |
"dataset",
|
| 101 |
path,
|
| 102 |
local_dir=WORKDIR,
|
| 103 |
+
force=False,
|
| 104 |
expected_sha256=item.get("Sha256") or item.get("sha256"),
|
| 105 |
+
file_size=int(item.get("Size") or item.get("size") or 0) or None,
|
| 106 |
),
|
| 107 |
f"download {path}",
|
| 108 |
)
|