Skip to content

Commit

Permalink
Merge branch 'MDL-76129-master' of https://github.com/sammarshallou/m…
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed Dec 28, 2022
2 parents 407f060 + 0b2c2a1 commit 1e4612d
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 11 deletions.
80 changes: 80 additions & 0 deletions cache/classes/allow_temporary_caches.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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 core_cache;

/**
* Create and keep an instance of this class to allow temporary caches when caches are disabled.
*
* This class works together with code in {@see cache_factory_disabled}.
*
* The intention is that temporary cache should be short-lived (not for the entire install process),
* which avoids two problems: first, that we might run out of memory for the caches, and second,
* that some code e.g. install.php/upgrade.php files, is entitled to assume that caching is not
* used and make direct database changes.
*
* @package core_cache
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class allow_temporary_caches {
/** @var int Number of references of this class; if more than 0, temporary caches are allowed */
protected static $references = 0;

/**
* Constructs an instance of this class.
*
* Temporary caches will be allowed until this instance goes out of scope. Store this token
* in a local variable, so that the caches have a limited life; do not save it outside your
* function.
*
* If cache is not disabled then normal (non-temporary) caches will be used, and this class
* does nothing.
*
* If an object of this class already exists then creating (or destroying) another one will
* have no effect.
*/
public function __construct() {
self::$references++;
}

/**
* Destroys an instance of this class.
*
* You do not need to call this manually; PHP will call it automatically when your variable
* goes out of scope. If you do need to remove your token at other times, use unset($token);
*
* If there are no other instances of this object, then all temporary caches will be discarded.
*/
public function __destruct() {
global $CFG;
require_once($CFG->dirroot . '/cache/disabledlib.php');

self::$references--;
if (self::$references === 0) {
\cache_factory_disabled::clear_temporary_caches();
}
}

/**
* Checks if temp caches are currently allowed.
*
* @return bool True if allowed
*/
public static function is_allowed(): bool {
return self::$references > 0;
}
}
27 changes: 27 additions & 0 deletions cache/disabledlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public function release_lock(string $key) : bool {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cache_factory_disabled extends cache_factory {
/** @var array Array of temporary caches in use. */
protected static $tempcaches = [];

/**
* Returns an instance of the cache_factor method.
Expand Down Expand Up @@ -283,6 +285,22 @@ public function create_cache(cache_definition $definition) {
* @return cache_application|cache_session|cache_request
*/
public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
// Temporary in-memory caches are sometimes allowed when caching is disabled.
if (\core_cache\allow_temporary_caches::is_allowed() && !$identifiers) {
$key = $component . '/' . $area;
if (array_key_exists($key, self::$tempcaches)) {
$cache = self::$tempcaches[$key];
} else {
$definition = $this->create_definition($component, $area);
// The cachestore_static class returns true to all three 'SUPPORTS_' checks so it
// can be used with all definitions.
$cache = new cachestore_static('TEMP:' . $component . '/' . $area);
$cache->initialise($definition);
self::$tempcaches[$key] = $cache;
}
return $cache;
}

// Regular cache definitions are cached inside create_definition(). This is not the case for disabledlib.php
// definitions as they use load_adhoc(). They are built as a new object on each call.
// We do not need to clone the definition because we know it's new.
Expand All @@ -292,6 +310,15 @@ public function create_cache_from_definition($component, $area, array $identifie
return $cache;
}

/**
* Removes all temporary caches.
*
* Don't call this directly - used by {@see \core_cache\allow_temporary_caches}.
*/
public static function clear_temporary_caches(): void {
self::$tempcaches = [];
}

/**
* Creates an ad-hoc cache from the given param.
*
Expand Down
65 changes: 65 additions & 0 deletions cache/tests/allow_temporary_caches_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 core_cache;

/**
* Unit tests for {@see allow_temporary_caches}.
*
* @package core_cache
* @category test
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_cache\allow_temporary_caches
*/
class allow_temporary_caches_test extends \advanced_testcase {

/**
* Tests whether temporary caches are allowed.
*/
public function test_is_allowed(): void {
// Not allowed by default.
$this->assertFalse(allow_temporary_caches::is_allowed());

// Allowed if we have an instance.
$frog = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());

// Or two instances.
$toad = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());

// Get rid of the instances.
unset($frog);
$this->assertTrue(allow_temporary_caches::is_allowed());

// Not allowed when we get back to no instances.
unset($toad);
$this->assertFalse(allow_temporary_caches::is_allowed());

// Check it works to automatically free up the instance when variable goes out of scope.
$this->inner_is_allowed();
$this->assertFalse(allow_temporary_caches::is_allowed());
}

/**
* Function call to demonstrate that you don't need to manually unset the variable.
*/
protected function inner_is_allowed(): void {
$gecko = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());
}
}
61 changes: 51 additions & 10 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@
define('CONTEXT_CACHE_MAX_SIZE', 2500);
}

/** Performance hint for assign_capability: the contextid is known to exist */
define('ACCESSLIB_HINT_CONTEXT_EXISTS', 'contextexists');
/** Performance hint for assign_capability: there is no existing entry in role_capabilities */
define('ACCESSLIB_HINT_NO_EXISTING', 'notexists');

/**
* Although this looks like a global variable, it isn't really.
*
Expand Down Expand Up @@ -1368,14 +1373,21 @@ function delete_role($roleid) {
/**
* Function to write context specific overrides, or default capabilities.
*
* The $performancehints array can currently contain two values intended to make this faster when
* this function is being called in a loop, if you have already checked certain details:
* 'contextexists' - if we already know the contextid exists in context table
* ASSIGN_HINT_NO_EXISTING - if we already know there is no entry in role_capabilities matching
* contextid, roleid, and capability
*
* @param string $capability string name
* @param int $permission CAP_ constants
* @param int $roleid role id
* @param int|context $contextid context id
* @param bool $overwrite
* @param string[] $performancehints Performance hints - leave blank unless needed
* @return bool always true or exception
*/
function assign_capability($capability, $permission, $roleid, $contextid, $overwrite = false) {
function assign_capability($capability, $permission, $roleid, $contextid, $overwrite = false, array $performancehints = []) {
global $USER, $DB;

if ($contextid instanceof context) {
Expand All @@ -1394,7 +1406,12 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw
return true;
}

$existing = $DB->get_record('role_capabilities', array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability));
if (in_array(ACCESSLIB_HINT_NO_EXISTING, $performancehints)) {
$existing = false;
} else {
$existing = $DB->get_record('role_capabilities',
['contextid' => $context->id, 'roleid' => $roleid, 'capability' => $capability]);
}

if ($existing and !$overwrite) { // We want to keep whatever is there already
return true;
Expand All @@ -1412,7 +1429,8 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw
$cap->id = $existing->id;
$DB->update_record('role_capabilities', $cap);
} else {
if ($DB->record_exists('context', array('id'=>$context->id))) {
if (in_array(ACCESSLIB_HINT_CONTEXT_EXISTS, $performancehints) ||
$DB->record_exists('context', ['id' => $context->id])) {
$DB->insert_record('role_capabilities', $cap);
}
}
Expand Down Expand Up @@ -2250,6 +2268,9 @@ function reset_role_capabilities($roleid) {
function update_capabilities($component = 'moodle') {
global $DB, $OUTPUT;

// Allow temporary caches to be used during install, dramatically boosting performance.
$token = new \core_cache\allow_temporary_caches();

$storedcaps = array();

$filecaps = load_capability_def($component);
Expand Down Expand Up @@ -2315,25 +2336,45 @@ function update_capabilities($component = 'moodle') {
}
// Add new capabilities to the stored definition.
$existingcaps = $DB->get_records_menu('capabilities', array(), 'id', 'id, name');
$capabilityobjects = [];
foreach ($newcaps as $capname => $capdef) {
$capability = new stdClass();
$capability->name = $capname;
$capability->captype = $capdef['captype'];
$capability->contextlevel = $capdef['contextlevel'];
$capability->component = $component;
$capability->riskbitmask = $capdef['riskbitmask'];
$capabilityobjects[] = $capability;
}
$DB->insert_records('capabilities', $capabilityobjects);

$DB->insert_record('capabilities', $capability, false);

// Flush the cached, as we have changed DB.
cache::make('core', 'capabilities')->delete('core_capabilities');
// Flush the cache, as we have changed DB.
cache::make('core', 'capabilities')->delete('core_capabilities');

foreach ($newcaps as $capname => $capdef) {
if (isset($capdef['clonepermissionsfrom']) && in_array($capdef['clonepermissionsfrom'], $existingcaps)){
if ($rolecapabilities = $DB->get_records('role_capabilities', array('capability'=>$capdef['clonepermissionsfrom']))){
foreach ($rolecapabilities as $rolecapability){
if ($rolecapabilities = $DB->get_records_sql('
SELECT rc.*,
CASE WHEN EXISTS(SELECT 1
FROM {role_capabilities} rc2
WHERE rc2.capability = ?
AND rc2.contextid = rc.contextid
AND rc2.roleid = rc.roleid) THEN 1 ELSE 0 END AS entryexists,
' . context_helper::get_preload_record_columns_sql('x') .'
FROM {role_capabilities} rc
JOIN {context} x ON x.id = rc.contextid
WHERE rc.capability = ?',
[$capname, $capdef['clonepermissionsfrom']])) {
foreach ($rolecapabilities as $rolecapability) {
// Preload the context and add performance hints based on the SQL query above.
context_helper::preload_from_record($rolecapability);
$performancehints = [ACCESSLIB_HINT_CONTEXT_EXISTS];
if (!$rolecapability->entryexists) {
$performancehints[] = ACCESSLIB_HINT_NO_EXISTING;
}
//assign_capability will update rather than insert if capability exists
if (!assign_capability($capname, $rolecapability->permission,
$rolecapability->roleid, $rolecapability->contextid, true)){
$rolecapability->roleid, $rolecapability->contextid, true, $performancehints)) {
echo $OUTPUT->notification('Could not clone capabilities for '.$capname);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8852,6 +8852,10 @@ function admin_get_root($reload=false, $requirefulltree=true) {
function admin_apply_default_settings($node=null, $unconditional=true, $admindefaultsettings=array(), $settingsoutput=array()) {
$counter = 0;

// This function relies heavily on config cache, so we need to enable in-memory caches if it
// is used during install when normal caching is disabled.
$token = new \core_cache\allow_temporary_caches();

if (is_null($node)) {
core_plugin_manager::reset_caches();
$node = admin_get_root(true, true);
Expand Down
Loading

0 comments on commit 1e4612d

Please sign in to comment.