|
28 | 28 | from collections.abc import Mapping, Sequence |
29 | 29 | from enum import Enum |
30 | 30 | from functools import cache |
| 31 | +import subprocess |
31 | 32 | from typing import ( |
32 | 33 | Any, |
33 | 34 | Callable, |
@@ -109,6 +110,39 @@ def live_devices(): |
109 | 110 | def live_slice_indices() -> set[int]: |
110 | 111 | return {d.slice_index for d in live_devices()} |
111 | 112 |
|
| 113 | +def clean_up_checkpoints(checkpoint_dir: str): |
| 114 | + |
| 115 | + print(f"Checking for incomplete checkpoint after an elastic event...Check dir: {checkpoint_dir}") |
| 116 | + |
| 117 | + # 1. List the directory |
| 118 | + new_checkpoint_dir = f"{checkpoint_dir}/checkpoints/" |
| 119 | + result = subprocess.run(['gsutil', 'ls', new_checkpoint_dir], capture_output=True, text=True) |
| 120 | + |
| 121 | + if result.returncode != 0: |
| 122 | + print("Failed to inspect checkpoint dir. Continuing") |
| 123 | + return |
| 124 | + print(f"Checkpoints==> {[line for line in result.stdout.splitlines()]}") |
| 125 | + checkpoints = [line for line in result.stdout.splitlines()] |
| 126 | + |
| 127 | + if not checkpoints: |
| 128 | + print("Found no existing checkpoints. Continuing") |
| 129 | + return |
| 130 | + |
| 131 | + # Sort naturally (Version sort) and get the last one |
| 132 | + checkpoints.sort(key=lambda x: [int(c) if c.isdigit() else c for c in re.split(r'(\d+)', x)]) |
| 133 | + latest_checkpoint = checkpoints[-1] |
| 134 | + |
| 135 | + print(f"Checking latest checkpoint: {latest_checkpoint}") |
| 136 | + |
| 137 | + # 3. Check for commit_success file |
| 138 | + # gsutil -q stat returns 0 if found, non-zero if not |
| 139 | + stat_check = subprocess.run(['gsutil', '-q', 'stat', f"{latest_checkpoint}commit_success*"]) |
| 140 | + |
| 141 | + if stat_check.returncode != 0: |
| 142 | + print(f"No commit_success file found. Deleting {latest_checkpoint}...") |
| 143 | + subprocess.run(['gsutil', '-m', 'rm', '-rf', latest_checkpoint]) |
| 144 | + else: |
| 145 | + print(f"Found commit_success file. Keeping {latest_checkpoint}.") |
112 | 146 |
|
113 | 147 | @dataclasses.dataclass |
114 | 148 | class HybridMeshShape: |
|
0 commit comments