Skip to content

Commit

Permalink
Merge pull request #154 from catalyst/135-cache-consistency-check
Browse files Browse the repository at this point in the history
[#135] Add cache consistency check
  • Loading branch information
matthewhilton authored Oct 26, 2023
2 parents c5a9bf5 + 92e30fe commit af01778
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 15 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# A heartbeat test page for Moodle

- [A heartbeat test page for Moodle](#a-heartbeat-test-page-for-moodle)
- [Branches](#branches)
- [What is this?](#what-is-this)
- [Front end health](#front-end-health)
- [Application health](#application-health)
- [Failed login detection](#failed-login-detection)
- [Branches](#branches)
- [Installation](#installation)
- [Configuration](#configuration)
- [Testing](#testing)
Expand All @@ -20,6 +20,18 @@ NOTE: In an ideal world this plugin should be redundant and most of it's functio

https://tracker.moodle.org/browse/MDL-47271

# Branches

| Branch | Moodle version | PHP Version |
| ------------------ | ----------------- | ----------- |
| master | Moodle 2.7 - 4.1 | Php 5.4.4+ |
| MOODLE_39_STABLE | Moodle 3.9 + | Php 7.2+ |

The master branch retains very deep support for old Totara's and Moodle's back to Moodle 2.7.

For any site using Moodle 3.9 or later, it is recommended to use the MOODLE_39_STABLE branch.

The MOODLE_39_STABLE branch uses the [Check API](https://moodledev.io/docs/apis/subsystems/check) exclusively, which simplifies the code massively.

## Front end health

Expand Down Expand Up @@ -109,18 +121,6 @@ The various thresholds can be configured with query params or cli args see this
php loginchecker.php -h
```

# Branches

| Branch | Version |
| ----------- | ----------- |
| master | Moodle 2.7 + |
| MOODLE_39_STABLE | Moodle 3.9 + |

The master branch is always stable and should retain very deep support for old Totara's and Moodle's back to Moodle 2.7

For this reason we will continue to support php5 for some time.

The MOODLE_39_STABLE branch uses the [Check API](https://moodledev.io/docs/apis/subsystems/check) exclusively.

# Installation

Expand Down
134 changes: 134 additions & 0 deletions classes/check/cachecheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_heartbeat\check;
use core\check\check;
use core\check\result;

/**
* Cache check class
*
* This detects some split brain cache setups
*
* @package tool_heartbeat
* @author Brendan Heywood <[email protected]>
* @copyright Catalyst IT 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachecheck extends check {

/**
* Get Result.
*
* @return result
*/
public function get_result() : result {
$results = $this->check('web');
$results += $this->check('cron');

list($status, $summary) = $this->build_result($results);

$details = '';

if ($status != result::OK) {
$details .= get_string('checkcachedetails', 'tool_heartbeat');
}

$details .= '<table class="table table-sm w-auto table-bordered">';

foreach ($results as $key => $value) {
$details .= \html_writer::start_tag('tr');
$details .= \html_writer::tag('td', $key);
$details .= \html_writer::tag('td', $value);

// Use DATE_RSS to show seconds, as well as timezone.
$details .= \html_writer::tag('td', date(DATE_RSS, $value));
$details .= \html_writer::end_tag('tr');
}
$details .= '</table>';
return new result($status, $summary, $details);
}

/**
* Reads the results and buils a check API result.
* @param array $results from check() function.
* @return array of [result status, summary string]
*/
private function build_result(array $results): array {
// Nothing set for web API.
if (empty($results['webapi'])) {
return [result::CRITICAL, get_string('checkcachewebmissing', 'tool_heartbeat')];
}

// Nothing set for cron API.
if (empty($results['cronapi'])) {
return [result::CRITICAL, get_string('checkcachecronmissing', 'tool_heartbeat')];
}

// Check for split cron cache/db, web cache/db, and all of them together.
$cronsplit = $results['cronapi'] != $results['crondb'];
$websplit = $results['webapi'] != $results['webdb'];

if ($cronsplit || $websplit) {
$splits = [
'cron' => $cronsplit,
'web' => $websplit,
];
$splits = implode(",", array_keys(array_filter($splits)));

return [result::CRITICAL, get_string('checkcacheerrorsplit', 'tool_heartbeat', $splits)];
}

// Else OK.
return [result::OK, get_string('checkcachenotsplit', 'tool_heartbeat')];
}

/**
* Get the ping values from the cache and db to compare
* @param string $type type of check (e.g. web, cron)
*/
public function check($type) {
global $DB;

$return = [];
$key = "checkcache{$type}ping";

// Read from cache (e.g. get_config uses cache).
$return[$type . 'api'] = get_config('tool_heartbeat', $key);

// Read directly from database.
$return[$type . 'db'] = $DB->get_field('config_plugins', 'value', [
'plugin' => 'tool_heartbeat',
'name' => $key,
]);
return $return;
}

/**
* Sets a timestamp in config from web or cron
* @param string $type type of check (e.g. web, cron)
*/
public static function ping($type) {
$key = "checkcache{$type}ping";
$current = get_config('tool_heartbeat', $key);

// Only update if the currently cached time is very old.
if ($current < (time() - DAYSECS)) {
mtrace("\nHEARTBEAT doing {$type} ping {$current}\n", DEBUG_DEVELOPER);
set_config($key, time(), 'tool_heartbeat');
}
}
}
48 changes: 48 additions & 0 deletions classes/task/cachecheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


namespace tool_heartbeat\task;

/**
* Scheduled task to ping the cache from CRON.
*
* @package tool_heartbeat
* @author Brendan Heywood <[email protected]>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachecheck extends \core\task\scheduled_task {

/**
* Get task name
*/
public function get_name() {
return get_string('checkcachecheck', 'tool_heartbeat');
}

/**
* Execute task
*/
public function execute() {
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('cron');
}
}

}


35 changes: 35 additions & 0 deletions db/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cache split check.
*
* @package tool_heartbeat
* @author Brendan Heywood <[email protected]>
* @copyright Catalyst IT 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Install
*/
function xmldb_tool_heartbeat_install() {
// If there are issues with split caches they need to be exposed
// after some time for them to diverge.
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('web');
\tool_heartbeat\check\cachecheck::ping('cron');
}
}
36 changes: 36 additions & 0 deletions db/tasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tool heartbeat
*
* @author Brendan Heywood <[email protected]>
* @copyright Catalyst IT 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$tasks = [
[
'classname' => 'tool_heartbeat\task\cachecheck',
'minute' => '*',
'hour' => '*/8',
'day' => '*',
'dayofweek' => '*',
'month' => '*',
],
];

41 changes: 41 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* DB upgrade script.
*
* @package tool_heartbeat
* @author Matthew Hilton <[email protected]>
* @copyright Catalyst IT 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Upgrade
* @param int $oldversion
*/
function xmldb_tool_heartbeat_upgrade($oldversion) {
if ($oldversion < 2023102400) {
// If there are issues with split caches they need to be exposed
// after some time for them to diverge.
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('web');
\tool_heartbeat\check\cachecheck::ping('cron');
}
upgrade_main_savepoint(true, 2023102400);
}

return true;
}
6 changes: 6 additions & 0 deletions lang/en/tool_heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
$string['configuredauths'] = 'Check auth methods';
$string['configuredauthsdesc'] = 'Auth methods to check are enabled in the Check API. A warning will be emitted if they are not enabled.';
$string['checkauthcheck'] = 'Authentication methods';
$string['checkcachecheck'] = 'Cache consistency check';
$string['checkcachecronmissing'] = 'The cron cache check has not succeeded yet or is missing';
$string['checkcachedetails'] = 'A split brain cache was detected. The value stored in the database table config_plugins was not the same as the cached value returned from get_config. If you purge the cache and this check passes and then fails again after a few hours then that strongly suggests a cache misconfiguration.';
$string['checkcacheerrorsplit'] = 'The caches are not consistent: {$a}';
$string['checkcachenotsplit'] = 'Caches appear consistent between web and cron';
$string['checkcachewebmissing'] = 'The web cache check has not succeeded yet';
$string['checkdnscheck'] = 'DNS check';
$string['checkrangerequestcheck'] = 'Range requests check';
$string['checkrangerequestok'] = 'Range requests are working, 206 response with only 10 bytes of data';
Expand Down
10 changes: 10 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Runs before HTTP headers. Used to ping the cachecheck.
*/
function tool_heartbeat_before_http_headers() {
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('web');
}
}

/**
* Status checks.
*
Expand All @@ -29,6 +38,7 @@
function tool_heartbeat_status_checks() {
return [
new \tool_heartbeat\check\authcheck(),
new \tool_heartbeat\check\cachecheck(),
new \tool_heartbeat\check\logstorecheck(),
new \tool_heartbeat\check\tasklatencycheck(),
];
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023101100;
$plugin->release = 2023101100; // Match release exactly to version.
$plugin->version = 2023102400;
$plugin->release = 2023102400; // Match release exactly to version.
$plugin->requires = 2012120311; // Deep support going back to 2.4.
$plugin->supported = [24, 401];
$plugin->component = 'tool_heartbeat';
Expand Down

0 comments on commit af01778

Please sign in to comment.