Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch component and package dependencies in parallel #844

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commodore/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def render_target(

classes = [f"params.{inv.bootstrap_target}"]

for c in components:
for c in sorted(components):
if inv.defaults_file(c).is_file():
classes.append(f"defaults.{c}")
else:
Expand Down
32 changes: 32 additions & 0 deletions commodore/dependency_mgmt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

import itertools
from concurrent.futures import ThreadPoolExecutor

import click

from commodore.config import Config
Expand Down Expand Up @@ -69,6 +72,8 @@ def fetch_components(cfg: Config):
cfg.register_component_aliases(component_aliases)
cspecs = _read_components(cfg, component_names)
click.secho("Fetching components...", bold=True)

deps: dict[str, list] = {}
for cn in component_names:
cspec = cspecs[cn]
if cfg.debug:
Expand All @@ -86,11 +91,28 @@ def fetch_components(cfg: Config):
f"Component {cn} has uncommitted changes. "
+ "Please specify `--force` to discard them"
)
deps.setdefault(cdep.url, []).append(c)
fetch_parallel(fetch_component, cfg, deps.values())


def fetch_component(cfg, dependencies):
"""
Fetch all components of a MultiDependency object.
"""
for c in dependencies:
c.checkout()
cfg.register_component(c)
create_component_symlinks(cfg, c)


def fetch_parallel(fetch_fun, cfg, to_fetch):
"""
Fetch dependencies in parallel threads with ThreadPoolExecutor.
"""
with ThreadPoolExecutor() as exe:
exe.map(fetch_fun, itertools.repeat(cfg), to_fetch)


def register_components(cfg: Config):
"""
Discover components in the inventory, and register them if the
Expand Down Expand Up @@ -152,6 +174,7 @@ def fetch_packages(cfg: Config):
pkgs = _discover_packages(cfg)
pspecs = _read_packages(cfg, pkgs)

deps: dict[str, list] = {}
for p in pkgs:
pspec = pspecs[p]
pdep = cfg.register_dependency_repo(pspec.url)
Expand All @@ -167,6 +190,15 @@ def fetch_packages(cfg: Config):
f"Package {p} has uncommitted changes. "
+ "Please specify `--force` to discard them"
)
deps.setdefault(pdep.url, []).append((p, pkg))
fetch_parallel(fetch_package, cfg, deps.values())


def fetch_package(cfg, dependencies):
"""
Fetch all package dependencies of a MultiDependency object.
"""
for p, pkg in dependencies:
pkg.checkout()
cfg.register_package(p, pkg)
create_package_symlink(cfg, p, pkg)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_render_bootstrap_target(tmp_path: P):

classes = [
"params.cluster",
"defaults.foo",
"defaults.bar",
"defaults.foo",
"global.commodore",
]
assert target != ""
Expand Down Expand Up @@ -83,8 +83,8 @@ def test_render_target(tmp_path: P):

classes = [
"params.cluster",
"defaults.foo",
"defaults.bar",
"defaults.foo",
"global.commodore",
"components.foo",
]
Expand Down Expand Up @@ -115,8 +115,8 @@ def test_render_aliased_target(tmp_path: P):

classes = [
"params.cluster",
"defaults.foo",
"defaults.bar",
"defaults.foo",
"global.commodore",
"components.foo",
]
Expand Down Expand Up @@ -148,8 +148,8 @@ def test_render_aliased_target_with_dash(tmp_path: P):

classes = [
"params.cluster",
"defaults.foo-comp",
"defaults.bar",
"defaults.foo-comp",
"global.commodore",
"components.foo-comp",
]
Expand Down
Loading