-
Notifications
You must be signed in to change notification settings - Fork 96
Fix problems of softdog watchdog driver #2155
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
Open
liangxin1300
wants to merge
4
commits into
ClusterLabs:master
Choose a base branch
from
liangxin1300:20260708_watchdog_issue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a654859
Fix: watchdog: Load watchdog driver correctly
liangxin1300 6f25e6f
Dev: watchdog: Resolve <unknown> from configured driver
liangxin1300 1760cba
Dev: watchdog: Warn when SBD uses softdog
liangxin1300 18e8d20
Dev: watchdog: Prefer hardware watchdog by default
liangxin1300 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,24 @@ | ||
| import logging | ||
| import re | ||
|
|
||
| from . import utils | ||
| from .sh import ShellUtils | ||
| from . import sh | ||
| from . import sbd | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Watchdog(object): | ||
| """ | ||
| Class to find valid watchdog device name | ||
| """ | ||
| WATCHDOG_CFG = "/etc/modules-load.d/watchdog.conf" | ||
| QUERY_CMD = "sudo sbd query-watchdog" | ||
| DEVICE_FIND_REGREX = "\\[[0-9]+\\] (/dev/.*)\n.*\nDriver: (.*)" | ||
| # output format might like: | ||
| # [1] /dev/watchdog\nIdentity: Software Watchdog\nDriver: softdog\n | ||
| DEVICE_FIND_REGREX = r"[ \t]*\[[0-9]+\] (/dev/[^\n]+)\n[ \t]*Identity: ([^\n]+)\n[ \t]*Driver: ([^\n]+)" | ||
|
|
||
| def __init__(self, _input=None, remote_user=None, peer_host=None): | ||
| """ | ||
|
|
@@ -28,25 +35,23 @@ def watchdog_device_name(self): | |
| return self._watchdog_device_name | ||
|
|
||
| @staticmethod | ||
| def verify_watchdog_device(dev, ignore_error=False): | ||
| def verify_watchdog_device(dev): | ||
| """ | ||
| Use wdctl to verify watchdog device | ||
| """ | ||
| rc, _, err = ShellUtils().get_stdout_stderr("wdctl {}".format(dev)) | ||
| rc, _, err = ShellUtils().get_stdout_stderr(f"wdctl {dev}") | ||
| if rc != 0: | ||
| if ignore_error: | ||
| return False | ||
| else: | ||
| utils.fatal("Invalid watchdog device {}: {}".format(dev, err)) | ||
| utils.fatal(f"Invalid watchdog device {dev}: {err}") | ||
| return True | ||
|
|
||
| @staticmethod | ||
| def _load_watchdog_driver(driver): | ||
| def _load_watchdog_driver(driver, join=False): | ||
| """ | ||
| Load specific watchdog driver | ||
| """ | ||
| ShellUtils().get_stdout_stderr(f"echo {driver} > {Watchdog.WATCHDOG_CFG}") | ||
| ShellUtils().get_stdout_stderr("systemctl restart systemd-modules-load") | ||
| cmd = f"echo {driver} > {Watchdog.WATCHDOG_CFG} && systemctl restart systemd-modules-load" | ||
| node_list = utils.this_node() if join else None | ||
| utils.cluster_run_cmd(cmd, node_list) | ||
|
|
||
| @staticmethod | ||
| def get_watchdog_device_from_sbd_config(): | ||
|
|
@@ -64,16 +69,57 @@ def _driver_is_loaded(driver): | |
| _, out, _ = ShellUtils().get_stdout_stderr("lsmod") | ||
| return re.search("\n{}\\s+".format(driver), out) | ||
|
|
||
| @staticmethod | ||
| def _get_configured_watchdog_driver(): | ||
| """ | ||
| Get watchdog driver name from modules-load config. | ||
| """ | ||
| try: | ||
| with open(Watchdog.WATCHDOG_CFG) as f: | ||
| return f.readline().strip() | ||
| except OSError: | ||
| return None | ||
|
|
||
| @classmethod | ||
| def get_watchdog_info(cls, out, sbd_only=False): | ||
| """ | ||
| Parse sbd query-watchdog output into {device_name: driver_name}. | ||
| """ | ||
| if not out: | ||
| return {} | ||
|
|
||
| watchdog_info = {} | ||
| for device, identity, driver in re.findall(cls.DEVICE_FIND_REGREX, out): | ||
| if sbd_only and not re.search(r"Busy: .*sbd", identity): | ||
| continue | ||
| if driver == "<unknown>": | ||
| configured_driver = cls._get_configured_watchdog_driver() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect as Probably we should fix |
||
| if configured_driver and cls._driver_is_loaded(configured_driver): | ||
| driver = configured_driver | ||
| watchdog_info[device] = driver | ||
| return watchdog_info | ||
|
|
||
| @staticmethod | ||
| def warn_if_using_softdog(): | ||
| """ | ||
| Warn if SBD is using softdog as watchdog driver. | ||
| """ | ||
| rc, out, err = ShellUtils().get_stdout_stderr(Watchdog.QUERY_CMD) | ||
| if rc != 0 or not out: | ||
| logger.debug("Failed to run %s: %s", Watchdog.QUERY_CMD, err) | ||
| return | ||
|
|
||
| if "softdog" in Watchdog.get_watchdog_info(out, sbd_only=True).values(): | ||
| logger.warning("It's not recommended to use softdog as watchdog driver in production environment") | ||
|
|
||
| def _set_watchdog_info(self): | ||
| """ | ||
| Set watchdog info through sbd query-watchdog command | ||
| Content in self._watchdog_info_dict: {device_name: driver_name} | ||
| """ | ||
| rc, out, err = ShellUtils().get_stdout_stderr(self.QUERY_CMD) | ||
| if rc == 0 and out: | ||
| # output format might like: | ||
| # [1] /dev/watchdog\nIdentity: Software Watchdog\nDriver: softdog\n | ||
| self._watchdog_info_dict = dict(re.findall(self.DEVICE_FIND_REGREX, out)) | ||
| self._watchdog_info_dict = self.get_watchdog_info(out) | ||
| else: | ||
| utils.fatal("Failed to run {}: {}".format(self.QUERY_CMD, err)) | ||
|
|
||
|
|
@@ -92,39 +138,24 @@ def _get_driver_through_device_remotely(self, dev_name): | |
| """ | ||
| rc, out, err = sh.cluster_shell().get_rc_stdout_stderr_without_input(self._peer_host, self.QUERY_CMD) | ||
| if rc == 0 and out: | ||
| # output format might like: | ||
| # [1] /dev/watchdog\nIdentity: Software Watchdog\nDriver: softdog\n | ||
| device_driver_dict = dict(re.findall(self.DEVICE_FIND_REGREX, out)) | ||
| if device_driver_dict and dev_name in device_driver_dict: | ||
| device_driver_dict = self.get_watchdog_info(out) | ||
| if dev_name in device_driver_dict: | ||
| return device_driver_dict[dev_name] | ||
| else: | ||
| return None | ||
| else: | ||
| utils.fatal("Failed to run {} remotely: {}".format(self.QUERY_CMD, err)) | ||
|
|
||
| def _get_first_unused_device(self): | ||
| """ | ||
| Get first unused watchdog device name | ||
| """ | ||
| for dev in self._watchdog_info_dict: | ||
| if self.verify_watchdog_device(dev, ignore_error=True): | ||
| return dev | ||
| return None | ||
|
|
||
| def _set_input(self): | ||
| """ | ||
| If self._input was not provided by option: | ||
| 1. Try to get it from sbd config file | ||
| 2. Try to get the first valid device from result of sbd query-watchdog | ||
| 3. Set the self._input as softdog | ||
| """ | ||
| if not self._input: | ||
| dev = self.get_watchdog_device_from_sbd_config() | ||
| if dev and self.verify_watchdog_device(dev, ignore_error=True): | ||
| if self._input: | ||
| return | ||
|
|
||
| for dev, driver in self._watchdog_info_dict.items(): | ||
| if driver != "softdog": | ||
| self._input = dev | ||
| return | ||
| first_unused = self._get_first_unused_device() | ||
| self._input = first_unused if first_unused else "softdog" | ||
|
|
||
| self._input = "softdog" | ||
|
|
||
| def _valid_device(self, dev): | ||
| """ | ||
|
|
@@ -136,7 +167,7 @@ def _valid_device(self, dev): | |
|
|
||
| def join_watchdog(self): | ||
| """ | ||
| In join proces, get watchdog device from config | ||
| In join process, get watchdog device from config | ||
| If that device not exist, get driver name from init node, and load that driver | ||
| """ | ||
| self._set_watchdog_info() | ||
|
|
@@ -148,7 +179,7 @@ def join_watchdog(self): | |
|
|
||
| if not self._valid_device(self._input): | ||
| driver = self._get_driver_through_device_remotely(self._input) | ||
| self._load_watchdog_driver(driver) | ||
| self._load_watchdog_driver(driver, join=True) | ||
|
|
||
| def init_watchdog(self): | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
joinhere couples a low level driver loading util with the cluster-level concept (init vs join). A better approach is to passnode_listhere and let the caller decide.