diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/pybind/mgr/mds_autoscaler | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pybind/mgr/mds_autoscaler')
-rw-r--r-- | src/pybind/mgr/mds_autoscaler/__init__.py | 6 | ||||
-rw-r--r-- | src/pybind/mgr/mds_autoscaler/module.py | 99 | ||||
-rw-r--r-- | src/pybind/mgr/mds_autoscaler/tests/__init__.py | 0 | ||||
-rw-r--r-- | src/pybind/mgr/mds_autoscaler/tests/test_autoscaler.py | 88 |
4 files changed, 193 insertions, 0 deletions
diff --git a/src/pybind/mgr/mds_autoscaler/__init__.py b/src/pybind/mgr/mds_autoscaler/__init__.py new file mode 100644 index 000000000..326792113 --- /dev/null +++ b/src/pybind/mgr/mds_autoscaler/__init__.py @@ -0,0 +1,6 @@ +import os + +if 'UNITTEST' in os.environ: + import tests + +from .module import MDSAutoscaler diff --git a/src/pybind/mgr/mds_autoscaler/module.py b/src/pybind/mgr/mds_autoscaler/module.py new file mode 100644 index 000000000..2f780059c --- /dev/null +++ b/src/pybind/mgr/mds_autoscaler/module.py @@ -0,0 +1,99 @@ +""" +Automatically scale MDSs based on status of the file-system using the FSMap +""" + +import logging +from typing import Any, Optional +from mgr_module import MgrModule, NotifyType +from orchestrator._interface import MDSSpec, ServiceSpec +import orchestrator +import copy + +log = logging.getLogger(__name__) + + +class MDSAutoscaler(orchestrator.OrchestratorClientMixin, MgrModule): + """ + MDS autoscaler. + """ + NOTIFY_TYPES = [NotifyType.fs_map] + + def __init__(self, *args: Any, **kwargs: Any) -> None: + MgrModule.__init__(self, *args, **kwargs) + self.set_mgr(self) + + def get_service(self, fs_name: str) -> Optional[orchestrator.ServiceDescription]: + service = f"mds.{fs_name}" + completion = self.describe_service(service_type='mds', + service_name=service, + refresh=True) + orchestrator.raise_if_exception(completion) + if completion.result: + return completion.result[0] + return None + + def update_daemon_count(self, spec: ServiceSpec, fs_name: str, abscount: int) -> MDSSpec: + ps = copy.deepcopy(spec.placement) + ps.count = abscount + newspec = MDSSpec(service_type=spec.service_type, + service_id=spec.service_id, + placement=ps) + return newspec + + def get_required_standby_count(self, fs_map: dict, fs_name: str) -> int: + assert fs_map is not None + for fs in fs_map['filesystems']: + if fs['mdsmap']['fs_name'] == fs_name: + return fs['mdsmap']['standby_count_wanted'] + assert False + + def get_required_max_mds(self, fs_map: dict, fs_name: str) -> int: + assert fs_map is not None + for fs in fs_map['filesystems']: + if fs['mdsmap']['fs_name'] == fs_name: + return fs['mdsmap']['max_mds'] + assert False + + def verify_and_manage_mds_instance(self, fs_map: dict, fs_name: str) -> None: + assert fs_map is not None + + try: + svc = self.get_service(fs_name) + if not svc: + self.log.info(f"fs {fs_name}: no service defined; skipping") + return + if not svc.spec.placement.count: + self.log.info(f"fs {fs_name}: service does not specify a count; skipping") + return + + standbys_required = self.get_required_standby_count(fs_map, fs_name) + max_mds = self.get_required_max_mds(fs_map, fs_name) + want = max_mds + standbys_required + + self.log.info(f"fs {fs_name}: " + f"max_mds={max_mds} " + f"standbys_required={standbys_required}, " + f"count={svc.spec.placement.count}") + + if want == svc.spec.placement.count: + return + + self.log.info(f"fs {fs_name}: adjusting daemon count from {svc.spec.placement.count} to {want}") + newspec = self.update_daemon_count(svc.spec, fs_name, want) + completion = self.apply_mds(newspec) + orchestrator.raise_if_exception(completion) + except orchestrator.OrchestratorError as e: + self.log.exception(f"fs {fs_name}: exception while updating service: {e}") + pass + + def notify(self, notify_type: NotifyType, notify_id: str) -> None: + if notify_type != NotifyType.fs_map: + return + fs_map = self.get('fs_map') + if not fs_map: + return + + # we don't know for which fs config has been changed + for fs in fs_map['filesystems']: + fs_name = fs['mdsmap']['fs_name'] + self.verify_and_manage_mds_instance(fs_map, fs_name) diff --git a/src/pybind/mgr/mds_autoscaler/tests/__init__.py b/src/pybind/mgr/mds_autoscaler/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/src/pybind/mgr/mds_autoscaler/tests/__init__.py diff --git a/src/pybind/mgr/mds_autoscaler/tests/test_autoscaler.py b/src/pybind/mgr/mds_autoscaler/tests/test_autoscaler.py new file mode 100644 index 000000000..2d6017d4a --- /dev/null +++ b/src/pybind/mgr/mds_autoscaler/tests/test_autoscaler.py @@ -0,0 +1,88 @@ +import pytest +from unittest import mock + +from ceph.deployment.service_spec import ServiceSpec, PlacementSpec +from orchestrator import DaemonDescription, OrchResult, ServiceDescription + +try: + from typing import Any, List +except ImportError: + pass + +from mds_autoscaler.module import MDSAutoscaler + + +@pytest.fixture() +def mds_autoscaler_module(): + + yield MDSAutoscaler('mds_autoscaler', 0, 0) + + +class TestCephadm(object): + + @mock.patch("mds_autoscaler.module.MDSAutoscaler.get") + @mock.patch("mds_autoscaler.module.MDSAutoscaler.list_daemons") + @mock.patch("mds_autoscaler.module.MDSAutoscaler.describe_service") + @mock.patch("mds_autoscaler.module.MDSAutoscaler.apply_mds") + def test_scale_up(self, _apply_mds, _describe_service, _list_daemons, _get, mds_autoscaler_module: MDSAutoscaler): + daemons = OrchResult(result=[ + DaemonDescription( + hostname='myhost', + daemon_type='mds', + daemon_id='fs_name.myhost.a' + ), + DaemonDescription( + hostname='myhost', + daemon_type='mds', + daemon_id='fs_name.myhost.b' + ), + ]) + _list_daemons.return_value = daemons + + services = OrchResult(result=[ + ServiceDescription( + spec=ServiceSpec( + service_type='mds', + service_id='fs_name', + placement=PlacementSpec( + count=2 + ) + ) + ) + ]) + _describe_service.return_value = services + + apply = OrchResult(result='') + _apply_mds.return_value = apply + + _get.return_value = { + 'filesystems': [ + { + 'mdsmap': { + 'fs_name': 'fs_name', + 'in': [ + { + 'name': 'mds.fs_name.myhost.a', + } + ], + 'standby_count_wanted': 2, + 'max_mds': 1 + } + } + ], + 'standbys': [ + { + 'name': 'mds.fs_name.myhost.b', + } + ], + + } + mds_autoscaler_module.notify('fs_map', None) + + _apply_mds.assert_called_with(ServiceSpec( + service_type='mds', + service_id='fs_name', + placement=PlacementSpec( + count=3 + ) + )) |