From ed7008ae8ca2c666c184e3d4f075fd04cb4ef407 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 22 Feb 2021 13:45:31 +0800 Subject: [PATCH] mgr/ceph_module: add type annotation to BaseMgrModule Signed-off-by: Kefu Chai (cherry picked from commit b67e4341a3c3f4ceb7d03a731dcda31f18237eb2) --- src/pybind/mgr/ceph_module.pyi | 111 +++++++++++++++++++++------------ src/pybind/mgr/mgr_module.py | 24 ++++--- 2 files changed, 86 insertions(+), 49 deletions(-) diff --git a/src/pybind/mgr/ceph_module.pyi b/src/pybind/mgr/ceph_module.pyi index 06b7725f86777..19ad8a4f8a54c 100644 --- a/src/pybind/mgr/ceph_module.pyi +++ b/src/pybind/mgr/ceph_module.pyi @@ -2,6 +2,14 @@ # Used by mypy to do proper type checking of mgr modules. # Without this file, all classes have undefined base classes. +from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union +try: + from typing import Protocol # Protocol was added in Python 3.8 +except ImportError: + class Protocol: # type: ignore + pass + + class BasePyOSDMap(object): def _get_epoch(self): ... def _get_crush_version(self): ... @@ -36,44 +44,67 @@ class BaseMgrStandbyModule(object): def _ceph_get_store(self, key):... def _ceph_get_active_uri(self):... + +OptionValue = Optional[Union[bool, int, float, str]] + + +class CompletionT(Protocol): + def complete(self, r: int, outb: str, outs: str) -> None: ... + + +ServerInfoT = Dict[str, Union[str, List[Dict[str, str]]]] +HealthCheckT = Mapping[str, Union[int, str, Sequence[str]]] +PerfCounterT = Dict[str, Any] + class BaseMgrModule(object): - def __init__(self, py_modules_ptr, this_ptr): pass - def _ceph_get_version(self):... - def _ceph_get_release_name(self):... - def _ceph_lookup_release_name(self, release):... - def _ceph_cluster_log(self, channel, priority, message):... - def _ceph_get_context(self):... - def _ceph_get(self, data_name):... - def _ceph_get_server(self, hostname):... - def _ceph_get_perf_schema(self, svc_type, svc_name):... - def _ceph_get_counter(self, svc_type, svc_name, path):... - def _ceph_get_latest_counter(self, svc_type, svc_name, path):... - def _ceph_get_metadata(self, svc_type, svc_id):... - def _ceph_get_daemon_status(self, svc_type, svc_id):... - def _ceph_send_command(self, result, svc_type, svc_id, command, tag, inbuf):... - def _ceph_set_health_checks(self, checks):... - def _ceph_get_mgr_id(self):... - def _ceph_get_option(self, key):... - def _ceph_get_foreign_option(self, entity, key):... - def _ceph_get_module_option(self, key, default, localized_prefix=""):... - def _ceph_get_store_prefix(self, key_prefix):... - def _ceph_set_module_option(self, module, key, val):... - def _ceph_set_store(self, key, val):... - def _ceph_get_store(self, key):... - def _ceph_get_osdmap(self):... - def _ceph_set_uri(self, uri):... - def _ceph_set_device_wear_level(self, devid, val):... - def _ceph_have_mon_connection(self):... - def _ceph_update_progress_event(self, evid, desc, progress, add_to_ceph_s):... - def _ceph_complete_progress_event(self, evid):... - def _ceph_clear_all_progress_events(self):... - def _ceph_dispatch_remote(self, module_name, method_name, *args, **kwargs):... - def _ceph_add_osd_perf_query(self, query):... - def _ceph_remove_osd_perf_query(self, query_id):... - def _ceph_get_osd_perf_counters(self, query_id):... - def _ceph_add_mds_perf_query(self, query):... - def _ceph_remove_mds_perf_query(self, query_id):... - def _ceph_get_mds_perf_counters(self, query_id):... - def _ceph_unregister_client(self, addrs):... - def _ceph_register_client(self, addrs):... - def _ceph_is_authorized(self, arguments):... + def __init__(self, py_modules_ptr: object, this_ptr: object) -> None: pass + def _ceph_get_version(self) -> str: ... + def _ceph_get_release_name(self) -> str: ... + def _ceph_lookup_release_name(self, release: int) -> str: ... + def _ceph_cluster_log(self, channel: str, priority: int, message: str) -> None: ... + def _ceph_get_context(self) -> object: ... + def _ceph_get(self, data_name: str) -> Dict[str, Any]: ... + def _ceph_get_server(self, hostname: Optional[str]) -> Union[ServerInfoT, + List[ServerInfoT]]: ... + def _ceph_get_perf_schema(self, svc_type: str, svc_name: str) -> Dict[str, Any]: ... + def _ceph_get_counter(self, svc_type: str, svc_name: str, path: str) -> Dict[str, List[Tuple[float, int]]]: ... + def _ceph_get_latest_counter(self, svc_type, svc_name, path): ... + def _ceph_get_metadata(self, svc_type, svc_id): ... + def _ceph_get_daemon_status(self, svc_type, svc_id): ... + def _ceph_send_command(self, + result: CompletionT, + svc_type: str, + svc_id: str, + command: str, + tag: str, + inbuf: Optional[str]) -> None: ... + def _ceph_set_health_checks(self, checks: Mapping[str, HealthCheckT]) -> None: ... + def _ceph_get_mgr_id(self) -> str: ... + def _ceph_get_option(self, key: str) -> OptionValue: ... + def _ceph_get_foreign_option(self, entity: str, key: str) -> OptionValue: ... + def _ceph_get_module_option(self, + key: str, + default: str, + localized_prefix: str = "") -> OptionValue: ... + def _ceph_get_store_prefix(self, key_prefix) -> Dict[str, str]: ... + def _ceph_set_module_option(self, module: str, key: str, val: Optional[str]) -> None: ... + def _ceph_set_store(self, key: str, val: Optional[str]) -> None: ... + def _ceph_get_store(self, key: str) -> Optional[str]: ... + # mgr actually imports OSDMap from mgr_module and constructs an OSDMap + def _ceph_get_osdmap(self) -> BasePyOSDMap: ... + def _ceph_set_uri(self, uri: str) -> None: ... + def _ceph_set_device_wear_level(self, devid: str, val: float) -> None: ... + def _ceph_have_mon_connection(self) -> bool: ... + def _ceph_update_progress_event(self, evid: str, desc: str, progress: float, add_to_ceph_s: bool) -> None: ... + def _ceph_complete_progress_event(self, evid: str) -> None: ... + def _ceph_clear_all_progress_events(self) -> None: ... + def _ceph_dispatch_remote(self, module_name: str, method_name: str, *args: Any, **kwargs: Any) -> Any: ... + def _ceph_add_osd_perf_query(self, query: Dict[str, Dict[str, Any]]) -> Optional[int]: ... + def _ceph_remove_osd_perf_query(self, query_id: int) -> None: ... + def _ceph_get_osd_perf_counters(self, query_id: int) -> Optional[Dict[str, List[PerfCounterT]]]: ... + def _ceph_add_mds_perf_query(self, query: Dict[str, Dict[str, Any]]) -> Optional[int]: ... + def _ceph_remove_mds_perf_query(self, query_id: int) -> None: ... + def _ceph_get_mds_perf_counters(self, query_id: int) -> Optional[Dict[str, List[PerfCounterT]]]: ... + def _ceph_unregister_client(self, addrs: str) -> None: ... + def _ceph_register_client(self, addrs: str) -> None: ... + def _ceph_is_authorized(self, arguments: Dict[str, str]) -> bool: ... diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index adabdd6e879b9..be93431617c6f 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -792,6 +792,13 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule, MgrModuleLoggingMixin): HealthChecksT = Mapping[str, Mapping[str, Union[int, str, Sequence[str]]]] +# {"type": service_type, "id": service_id} +ServiceInfoT = Dict[str, str] +# {"hostname": hostname, +# "ceph_version": version, +# "services": [service_info, ..]} +ServerInfoT = Dict[str, Union[str, List[ServiceInfoT]]] +PerfCounterT = Dict[str, Any] class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): @@ -1115,8 +1122,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): return ret - def get_server(self, hostname: str) -> Union[Dict[str, str], - List[Dict[str, str]]]: + def get_server(self, hostname) -> ServerInfoT: """ Called by the plugin to fetch metadata about a particular hostname from ceph-mgr. @@ -1126,7 +1132,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): :param hostname: a hostname """ - return self._ceph_get_server(hostname) + return cast(ServerInfoT, self._ceph_get_server(hostname)) def get_perf_schema(self, svc_type: str, @@ -1179,7 +1185,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): """ return self._ceph_get_latest_counter(svc_type, svc_name, path) - def list_servers(self) -> List[Dict[str, List[Dict[str, str]]]]: + def list_servers(self) -> List[ServerInfoT]: """ Like ``get_server``, but gives information about all servers (i.e. all unique hostnames that have been mentioned in daemon metadata) @@ -1187,7 +1193,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): :return: a list of information about all servers :rtype: list """ - return self._ceph_get_server(None) + return cast(List[ServerInfoT], self._ceph_get_server(None)) def get_metadata(self, svc_type: str, @@ -1514,7 +1520,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): OSDMap. :return: OSDMap """ - return self._ceph_get_osdmap() + return cast(OSDMap, self._ceph_get_osdmap()) def get_latest(self, daemon_type: str, daemon_name: str, counter: str) -> int: data = self.get_latest_counter( @@ -1554,7 +1560,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): result = defaultdict(dict) # type: Dict[str, dict] for server in self.list_servers(): - for service in server['services']: + for service in cast(List[ServiceInfoT], server['services']): if service['type'] not in services: continue @@ -1734,7 +1740,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): """ return self._ceph_remove_osd_perf_query(query_id) - def get_osd_perf_counters(self, query_id: int) -> Optional[Dict[str, Any]]: + def get_osd_perf_counters(self, query_id: int) -> Optional[Dict[str, List[PerfCounterT]]]: """ Get stats collected for an OSD perf query. @@ -1779,7 +1785,7 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): """ return self._ceph_remove_mds_perf_query(query_id) - def get_mds_perf_counters(self, query_id: int) -> Optional[Dict[str, Any]]: + def get_mds_perf_counters(self, query_id: int) -> Optional[Dict[str, List[PerfCounterT]]]: """ Get stats collected for an MDS perf query. -- 2.39.5