Skip to content

Commit 114e116

Browse files
committed
wait only one minute and retry if worker is overloaded
1 parent 1a4acde commit 114e116

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

lilac

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ from lilac2.nomypy import BuildResult, BuildReason # type: ignore
4848
from lilac2.building import build_package, MissingDependencies
4949
from lilac2 import slogconf
5050
from lilac2 import intl
51-
from lilac2.workerman import WorkerManager
51+
from lilac2.workerman import WorkerManager, ResourceTemporarilyOverloaded
5252
from lilac2.typing import PkgToBuild, Rusages
5353
try:
5454
from lilac2 import db
@@ -265,7 +265,7 @@ def start_build(
265265
executor.map(lambda x: None, range(max_workers))
266266

267267
while True:
268-
pkgs = try_pick_some(
268+
pkgs, overloaded = try_pick_some(
269269
repo,
270270
buildsorter, failed,
271271
running = frozenset(x.pkgbase for x in futures.values()),
@@ -289,14 +289,16 @@ def start_build(
289289
# no more packages and no task is running: we're done
290290
break
291291

292-
done, pending = futures_wait(futures, return_when=FIRST_COMPLETED)
292+
timeout = 60 if overloaded else None
293+
done, pending = futures_wait(
294+
futures, timeout=timeout, return_when=FIRST_COMPLETED)
293295
for fu in done:
294296
pkg = futures.pop(fu)
295297
wm = cast(WorkerManager, pkg.workerman)
296298
wm.current_task_count -= 1
297299
fu.result()
298300

299-
# at least one task is done, try pick new tasks
301+
# at least one task is done or timed out, try pick new tasks
300302

301303
except KeyboardInterrupt:
302304
logger.info('keyboard interrupted, bye~')
@@ -308,17 +310,19 @@ def try_pick_some(
308310
running: Set[str],
309311
starving: bool,
310312
workermans: list[WorkerManager],
311-
) -> list[PkgToBuild]:
313+
) -> tuple[list[PkgToBuild], bool]:
314+
overloaded = False
315+
312316
if not buildsorter.is_active():
313-
return []
317+
return [], overloaded
314318

315319
ready = buildsorter.get_ready()
316320
if not ready:
317-
return []
321+
return [], overloaded
318322

319323
ready_to_build = [pkg for pkg in ready if pkg not in running]
320324
if not ready_to_build:
321-
return []
325+
return [], overloaded
322326

323327
if db.USE:
324328
rusages = db.get_pkgs_last_rusage(ready_to_build)
@@ -344,12 +348,17 @@ def try_pick_some(
344348
if not ready_to_build_wm:
345349
continue
346350

347-
to_builds = wm.try_accept_package(
348-
ready_to_build_wm,
349-
rusages,
350-
buildsorter.priority_func,
351-
lambda pkg: check_buildability(pkg,repo, buildsorter, failed),
352-
)
351+
try:
352+
to_builds = wm.try_accept_package(
353+
ready_to_build_wm,
354+
rusages,
355+
buildsorter.priority_func,
356+
lambda pkg: check_buildability(pkg,repo, buildsorter, failed),
357+
)
358+
except ResourceTemporarilyOverloaded:
359+
overloaded = True
360+
continue
361+
353362
ret.extend(to_builds)
354363
# remove picked packages from ready_to_build
355364
picked = {x.pkgbase for x in to_builds}
@@ -378,7 +387,7 @@ def try_pick_some(
378387
ret.append(to_build)
379388
break
380389

381-
return ret
390+
return ret, overloaded
382391

383392
def check_buildability(
384393
pkg: str,

lilac2/workerman.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
logger = logging.getLogger(__name__)
1414

15+
class ResourceTemporarilyOverloaded(Exception):
16+
pass
17+
1518
class WorkerManager:
1619
name: str
1720
max_concurrency: int
@@ -50,7 +53,7 @@ def try_accept_package(
5053

5154
if cpu_ratio > 1.0 and self.current_task_count > 0:
5255
logger.debug('[%s] high CPU usage (%.2f), idling', self.name, cpu_ratio)
53-
return []
56+
raise ResourceTemporarilyOverloaded
5457

5558
def sort_key(pkg):
5659
p = priority_func(pkg)

0 commit comments

Comments
 (0)