Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
Remove unused slurmdbd_available and slurmd_avilable stored state and
remove is_joined from slurmdbd and slurmd interfaces.
  • Loading branch information
jamesbeedy committed Jun 5, 2024
1 parent 9d2ba22 commit f413371
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 64 deletions.
34 changes: 7 additions & 27 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from interface_slurmrestd import (
Slurmrestd,
SlurmrestdAvailableEvent,
SlurmrestdUnavailableEvent,
)
from ops import (
ActionEvent,
Expand Down Expand Up @@ -61,9 +60,7 @@ def __init__(self, *args):
new_nodes=[],
nhc_params=str(),
slurm_installed=False,
slurmdbd_available=False,
slurmdbd_host=str(),
slurmrestd_available=False,
user_supplied_slurm_conf_params=str(),
)

Expand Down Expand Up @@ -91,9 +88,8 @@ def __init__(self, *args):
self._slurmd.on.partition_unavailable: self._on_write_slurm_conf,
self._slurmd.on.slurmd_available: self._on_write_slurm_conf,
self._slurmd.on.slurmd_departed: self._on_write_slurm_conf,
# slurmrestd lifecycle hook events
# slurmrestd available
self._slurmrestd.on.slurmrestd_available: self._on_slurmrestd_available,
self._slurmrestd.on.slurmrestd_unavailable: self._on_slurmrestd_unavailable,
# NOTE: a second slurmctld should get the jwt/munge keys and configure them
# fluentbit
self.on["fluentbit"].relation_created: self._on_fluentbit_relation_created,
Expand Down Expand Up @@ -177,25 +173,17 @@ def _on_slurmrestd_available(self, event: SlurmrestdAvailableEvent) -> None:
"""Check that we have slurm_config when slurmrestd available otherwise defer the event."""
if self.model.unit.is_leader():
if self._check_status():
slurm_config = self._assemble_slurm_conf()
self._stored.slurmrestd_available = True
self._slurmrestd.set_slurm_config_on_app_relation_data(slurm_config)
self._slurmrestd.set_slurm_config_on_app_relation_data(self._assemble_slurm_conf())
return
logger.debug("Cluster not ready yet, deferring event.")
event.defer()

def _on_slurmrestd_unavailable(self, event: SlurmrestdUnavailableEvent) -> None:
"""Set slurmrestd_available to False."""
self._stored.slurmrestd_available = False

def _on_slurmdbd_available(self, event: SlurmdbdAvailableEvent) -> None:
self._stored.slurmdbd_host = event.slurmdbd_host
self._stored.slurmdbd_available = True
self._on_write_slurm_conf(event)

def _on_slurmdbd_unavailable(self, event: SlurmdbdUnavailableEvent) -> None:
self._stored.slurmdbd_host = ""
self._stored.slurmdbd_available = False
self._check_status()

def _on_drain_nodes_action(self, event: ActionEvent) -> None:
Expand Down Expand Up @@ -284,7 +272,7 @@ def _on_write_slurm_conf(
self.new_nodes = new_nodes_from_slurm_config.copy()

# slurmrestd needs the slurm.conf file, so send it every time it changes.
if self._stored.slurmrestd_available is True:
if self._slurmrestd.is_joined is not False:
self._slurmrestd.set_slurm_config_on_app_relation_data(slurm_config)
else:
logger.debug("## Should write slurm.conf, but we don't have it. " "Deferring.")
Expand Down Expand Up @@ -315,9 +303,9 @@ def _assemble_slurmctld_parameters() -> str:
return ",".join(slurmctld_param_config)

accounting_params = {}
if self._slurmdbd.is_joined:
if (slurmdbd_host := self._stored.slurmdbd_host) != "":
accounting_params = {
"AccountingStorageHost": self._stored.slurmdbd_host,
"AccountingStorageHost": slurmdbd_host,
"AccountingStorageType": "accounting_storage/slurmdbd",
"AccountingStoragePass": "/var/run/munge/munge.socket.2",
"AccountingStoragePort": "6819",
Expand Down Expand Up @@ -364,12 +352,9 @@ def _check_status(self) -> bool: # noqa C901
"""Check for all relations and set appropriate status.
This charm needs these conditions to be satisfied in order to be ready:
- Slurm components installed.
- Munge running.
- Slurmctld component installed
- Munge running
"""
# NOTE: slurmd and slurmrestd are not needed for slurmctld to work,
# only for the cluster to operate. But we need slurmd inventory
# to assemble slurm.conf
if self.slurm_installed is not True:
self.unit.status = BlockedStatus("Error installing slurmctld")
return False
Expand Down Expand Up @@ -439,11 +424,6 @@ def slurm_installed(self, slurm_installed: bool) -> None:
"""Set slurm_installed in stored state."""
self._stored.slurm_installed = slurm_installed

@property
def slurmdbd_available(self) -> bool:
"""Return slurmdbd_available from stored state."""
return True if self._stored.slurmdbd_available is True else False


if __name__ == "__main__":
main.main(SlurmctldCharm)
28 changes: 9 additions & 19 deletions src/interface_slurmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Interface slurmd."""
import json
import logging
from typing import Any, Dict, List, Union
from typing import Any, Dict

from ops import (
EventBase,
Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(self, charm, relation_name):
def _on_relation_created(self, event: RelationCreatedEvent) -> None:
"""Set our data on the relation."""
# Need to wait until the charm has installed slurm before we can proceed.
if not (self._charm.slurm_installed and self._charm.slurmdbd_available):
if not self._charm.slurm_installed:
event.defer()
return

Expand Down Expand Up @@ -149,13 +149,12 @@ def set_nhc_params(self, params: str = "#") -> None:
# so we set it to something that behaves like empty
logger.debug(f"## set_nhc_params: {params}")

if self.is_joined:
if relations := self._relations:
for relation in relations:
app = self.model.app
cluster_info = json.loads(relation.data[app]["cluster_info"])
cluster_info["nhc_params"] = params
relation.data[app]["cluster_info"] = json.dumps(cluster_info)
if relations := self.framework.model.relations.get(self._relation_name):
for relation in relations:
app = self.model.app
cluster_info = json.loads(relation.data[app]["cluster_info"])
cluster_info["nhc_params"] = params
relation.data[app]["cluster_info"] = json.dumps(cluster_info)
else:
logger.debug("## slurmd not joined")

Expand Down Expand Up @@ -194,7 +193,7 @@ def get_new_nodes_and_nodes_and_partitions(self) -> Dict[str, Any]:
nodes = {}
new_nodes = []

if relations := self._relations:
if relations := self.framework.model.relations.get(self._relation_name):
for relation in relations:

partition_as_dict = self._get_partition_from_relation(relation)
Expand Down Expand Up @@ -246,12 +245,3 @@ def get_new_nodes_and_nodes_and_partitions(self) -> Dict[str, Any]:
else []
)
return {"down_nodes": new_node_down_nodes, "nodes": nodes, "partitions": partitions}

@property
def _relations(self) -> Union[List[Relation], None]:
return self.framework.model.relations.get(self._relation_name)

@property
def is_joined(self) -> bool:
"""Return True if self._relation is not None."""
return True if self._relations else False
12 changes: 0 additions & 12 deletions src/interface_slurmdbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"""Slurmdbd."""
import json
import logging
from typing import Union

from ops import (
EventBase,
EventSource,
Object,
ObjectEvents,
Relation,
RelationBrokenEvent,
RelationChangedEvent,
RelationCreatedEvent,
Expand Down Expand Up @@ -114,13 +112,3 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
if self.framework.model.unit.is_leader():
event.relation.data[self.model.app]["cluster_info"] = ""
self.on.slurmdbd_unavailable.emit()

@property
def _relation(self) -> Union[Relation, None]:
"""Return the relation or None."""
return self.framework.model.get_relation(self._relation_name)

@property
def is_joined(self) -> bool:
"""Return True if the relation to slurmdbd exists."""
return True if self._charm.framework.model.relations.get(self._relation_name) else False
5 changes: 1 addition & 4 deletions src/interface_slurmrestd.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def __init__(self, charm, relation_name):
@property
def is_joined(self) -> bool:
"""Return True if relation is joined."""
if self._charm.framework.model.relations.get(self._relation_name):
return True
else:
return False
return True if self.model.relations.get(self._relation_name) else False

def _on_relation_created(self, event: RelationCreatedEvent) -> None:
# Check that slurm has been installed so that we know the munge key is
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ def test_on_slurmrestd_available_if_available(self, *_) -> None:
def test_on_slurmdbd_available(self) -> None:
"""Test that the on_slurmdbd_method works."""
self.harness.charm._slurmdbd.on.slurmdbd_available.emit("slurmdbdhost")
self.assertEqual(self.harness.charm._stored.slurmdbd_available, True)
self.assertEqual(self.harness.charm._stored.slurmdbd_host, "slurmdbdhost")

def test_on_slurmdbd_unavailable(self) -> None:
"""Test that the on_slurmdbd_unavailable method works."""
self.harness.charm._slurmdbd.on.slurmdbd_unavailable.emit()
self.assertEqual(self.harness.charm._stored.slurmdbd_available, False)
self.assertEqual(self.harness.charm._stored.slurmdbd_host, "")

0 comments on commit f413371

Please sign in to comment.