Skip to content

Commit

Permalink
feat: merge DeploymentModel.from_stale_hosted_entities and _get_recon…
Browse files Browse the repository at this point in the history
…figure_operation_hosts
  • Loading branch information
PaulFarault committed Nov 20, 2024
1 parent d43cf8f commit 3c2f3d3
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions tdp/core/models/deployment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,33 @@ def from_stale_hosted_entities(
Raises:
NothingToReconfigureError: If no component needs to be reconfigured.
"""
operation_hosts = _get_reconfigure_operation_hosts(
stale_hosted_entity_statuses, collections
operation_hosts: set[OperationHostTuple] = set()
for status in stale_hosted_entity_statuses:
if status.to_config:
operation_hosts.add(
OperationHostTuple(
collections.operations[f"{status.entity.name}_config"],
status.entity.host,
)
)
if status.to_restart:
operation_hosts.add(
OperationHostTuple(
collections.operations[f"{status.entity.name}_start"],
status.entity.host,
)
)
if len(operation_hosts) == 0:
raise NothingToReconfigureError("No component needs to be reconfigured.")

# Sort by hosts to improve readability
operation_hosts_sorted = sorted(
operation_hosts, key=lambda x: f"{x.operation.name}_{x.host_name}"
)

# Sort operations using DAG topological sort.
reconfigure_operations_sorted = Dag(collections).topological_sort_key(
operation_hosts, key=lambda x: x.operation.name
operation_hosts_sorted, key=lambda x: x.operation.name
)

# Generate deployment
Expand Down Expand Up @@ -472,36 +492,3 @@ class OperationHostTuple(NamedTuple):

operation: Operation
host_name: Optional[str]


def _get_reconfigure_operation_hosts(
hosted_entity_statuses: list[HostedEntityStatus],
collections: Collections,
) -> list[OperationHostTuple]:
"""Generate a list of config and restart operation associated with their host.
Args:
hosted_entity_statuses: List of hosted entities statuses.
Returns: List of tuple (operation, host) ordered <operation-name>_<host>.
"""
operation_hosts: set[OperationHostTuple] = set()
for status in hosted_entity_statuses:
if status.to_config:
operation_hosts.add(
OperationHostTuple(
collections.operations[f"{status.entity.name}_config"],
status.entity.host,
)
)
if status.to_restart:
operation_hosts.add(
OperationHostTuple(
collections.operations[f"{status.entity.name}_start"],
status.entity.host,
)
)
if len(operation_hosts) == 0:
raise NothingToReconfigureError("No component needs to be reconfigured.")
# Sort by hosts to improve readability
return sorted(operation_hosts, key=lambda x: f"{x.operation.name}_{x.host_name}")

0 comments on commit 3c2f3d3

Please sign in to comment.