MDL-63329 core: Remove memcache session support
authorAndrew Nicols <andrew@nicols.co.uk>
Mon, 10 Sep 2018 01:50:08 +0000 (09:50 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 12 Sep 2018 00:15:35 +0000 (08:15 +0800)
config-dist.php
lib/classes/session/memcache.php [deleted file]
lib/classes/session/memcached.php
lib/classes/session/util.php [deleted file]
lib/upgrade.txt

index fa43382..0b9c4e8 100644 (file)
@@ -284,16 +284,8 @@ $CFG->admin = 'admin';
 //      igbinary support to make the setting to work. Also, if you change the serializer you have to flush the database!
 //      $CFG->session_redis_serializer_use_igbinary = false; // Optional, default is PHP builtin serializer.
 //
-//   Memcache session handler (requires memcached server and memcache extension):
-//      $CFG->session_handler_class = '\core\session\memcache';
-//      $CFG->session_memcache_save_path = '127.0.0.1:11211';
-//      $CFG->session_memcache_acquire_lock_timeout = 120;
-//      ** NOTE: Memcache extension has less features than memcached and may be
-//         less reliable. Use memcached where possible or if you encounter
-//         session problems. **
-//
-// Please be aware that when selecting either Memcached or Memcache for sessions that it is advised to use a dedicated
-// memcache server. The memcache and memcached extensions do not provide isolated environments for individual uses.
+// Please be aware that when selecting Memcached for sessions that it is advised to use a dedicated
+// memcache server. The memcached extension does not provide isolated environments for individual uses.
 // Using the same server for other purposes (MUC for example) can lead to sessions being prematurely removed should
 // the other uses of the server purge the cache.
 //
diff --git a/lib/classes/session/memcache.php b/lib/classes/session/memcache.php
deleted file mode 100644 (file)
index 62e88bf..0000000
+++ /dev/null
@@ -1,205 +0,0 @@
-<?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/>.
-
-/**
- * Memcache based session handler.
- *
- * This is based on the memcached code. It lacks some features, such as
- * locking options, but appears to work in practice.
- *
- * Note: You may need to manually configure redundancy and fail-over
- * if you specify multiple servers.
- *
- * @package core
- * @copyright 2014 The Open University
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-namespace core\session;
-
-defined('MOODLE_INTERNAL') || die();
-
-/**
- * Memcache based session handler.
- *
- * @package core
- * @copyright 2014 The Open University
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class memcache extends handler {
-    /** @var string $savepath save_path string  */
-    protected $savepath;
-    /** @var array $servers list of servers parsed from save_path */
-    protected $servers;
-    /** @var int $acquiretimeout how long to wait for session lock */
-    protected $acquiretimeout = 120;
-
-    /**
-     * Creates new instance of handler.
-     */
-    public function __construct() {
-        global $CFG;
-
-        if (empty($CFG->session_memcache_save_path)) {
-            $this->savepath = '';
-        } else {
-            $this->savepath = $CFG->session_memcache_save_path;
-        }
-
-        if (empty($this->savepath)) {
-            $this->servers = array();
-        } else {
-            $this->servers = util::connection_string_to_memcache_servers($this->savepath);
-        }
-
-        if (!empty($CFG->session_memcache_acquire_lock_timeout)) {
-            $this->acquiretimeout = (int)$CFG->session_memcache_acquire_lock_timeout;
-        }
-    }
-
-    /**
-     * Starts the session.
-     *
-     * @return bool success
-     */
-    public function start() {
-        $default = ini_get('max_execution_time');
-        set_time_limit($this->acquiretimeout);
-
-        $result = parent::start();
-
-        set_time_limit($default);
-        return $result;
-    }
-
-    /**
-     * Inits session handler.
-     */
-    public function init() {
-        if (!extension_loaded('memcache')) {
-            throw new exception('sessionhandlerproblem', 'error', '', null,
-                    'memcache extension is not loaded');
-        }
-        $version = phpversion('memcache');
-        if (!$version or version_compare($version, '2.2') < 0) {
-            throw new exception('sessionhandlerproblem', 'error', '', null,
-                    'memcache extension version must be at least 2.2');
-        }
-        if (empty($this->savepath)) {
-            throw new exception('sessionhandlerproblem', 'error', '', null,
-                    '$CFG->session_memcache_save_path must be specified in config.php');
-        }
-        // Check in case anybody mistakenly includes tcp://, which you
-        // would do in the raw PHP config. We require the same format as
-        // for memcached (without tcp://). Otherwse the code that splits it into
-        // individual servers won't have worked properly.
-        if (strpos($this->savepath, 'tcp://') !== false) {
-            throw new exception('sessionhandlerproblem', 'error', '', null,
-                    '$CFG->session_memcache_save_path should not contain tcp://');
-        }
-
-        ini_set('session.save_handler', 'memcache');
-
-        // The format of save_path is different for memcache (compared to memcached).
-        // We are using the same format in config.php to avoid confusion.
-        // It has to have tcp:// at the start of each entry.
-        $memcacheformat = preg_replace('~(^|,\s*)~','$1tcp://', $this->savepath);
-        ini_set('session.save_path', $memcacheformat);
-    }
-
-    /**
-     * Check the backend contains data for this session id.
-     *
-     * Note: this is intended to be called from manager::session_exists() only.
-     *
-     * @param string $sid PHP session ID
-     * @return bool true if session found.
-     */
-    public function session_exists($sid) {
-        $result = false;
-
-        foreach ($this->get_memcaches() as $memcache) {
-            if ($result === false) {
-                $value = $memcache->get($sid);
-                if ($value !== false) {
-                    $result = true;
-                }
-            }
-            $memcache->close();
-        }
-
-        return $result;
-    }
-
-    /**
-     * Gets the Memcache objects, one for each server.
-     * The connects must be closed manually after use.
-     *
-     * Note: the servers are not automatically synchronised
-     *       when accessed via Memcache class, it needs to be
-     *       done manually by accessing all configured servers.
-     *
-     * @return \Memcache[] Array of initialised memcache objects
-     */
-    protected function get_memcaches() {
-        $result = array();
-        foreach ($this->servers as $server) {
-            $memcache = new \Memcache();
-            $memcache->addServer($server[0], $server[1]);
-            $result[] = $memcache;
-        }
-        return $result;
-    }
-
-    /**
-     * Kills all active sessions, the core sessions table is purged afterwards.
-     */
-    public function kill_all_sessions() {
-        global $DB;
-        if (!$this->servers) {
-            return;
-        }
-
-        $memcaches = $this->get_memcaches();
-
-        // Note: this can be significantly improved by fetching keys from memcache,
-        // but we need to make sure we are not deleting somebody else's sessions.
-
-        $rs = $DB->get_recordset('sessions', array(), 'id DESC', 'id, sid');
-        foreach ($rs as $record) {
-            foreach ($memcaches as $memcache) {
-                $memcache->delete($record->sid);
-            }
-        }
-        $rs->close();
-
-        foreach ($memcaches as $memcache) {
-            $memcache->close();
-        }
-    }
-
-    /**
-     * Kills one session, the session record is removed afterwards.
-     *
-     * @param string $sid PHP session ID
-     */
-    public function kill_session($sid) {
-        foreach ($this->get_memcaches() as $memcache) {
-            $memcache->delete($sid);
-            $memcache->close();
-        }
-    }
-}
index 5854f45..643e10e 100644 (file)
@@ -74,7 +74,7 @@ class memcached extends handler {
         if (empty($this->savepath)) {
             $this->servers = array();
         } else {
-            $this->servers = util::connection_string_to_memcache_servers($this->savepath);
+            $this->servers = self::connection_string_to_memcache_servers($this->savepath);
         }
 
         if (empty($CFG->session_memcached_prefix)) {
@@ -268,4 +268,33 @@ class memcached extends handler {
         }
     }
 
+    /**
+     * Convert a connection string to an array of servers.
+     *
+     * "abc:123, xyz:789" to
+     *  [
+     *      ['abc', '123'],
+     *      ['xyz', '789'],
+     *  ]
+     *
+     * @param   string  $str save_path value containing memcached connection string
+     * @return  array[]
+     */
+    protected static function connection_string_to_memcache_servers(string $str) : array {
+        $servers = [];
+        $parts   = explode(',', $str);
+        foreach ($parts as $part) {
+            $part = trim($part);
+            $pos  = strrpos($part, ':');
+            if ($pos !== false) {
+                $host = substr($part, 0, $pos);
+                $port = substr($part, ($pos + 1));
+            } else {
+                $host = $part;
+                $port = 11211;
+            }
+            $servers[] = [$host, $port];
+        }
+        return $servers;
+    }
 }
diff --git a/lib/classes/session/util.php b/lib/classes/session/util.php
deleted file mode 100644 (file)
index 72c476a..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-<?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/>.
-
-/**
- * Shared utility functions for session handlers.
- *
- * This contains functions that are shared between two or more handlers.
- *
- * @package core
- * @copyright 2014 The Open University
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-namespace core\session;
-
-defined('MOODLE_INTERNAL') || die();
-
-/**
- * Shared utility functions for session handlers.
- *
- * This contains functions that are shared between two or more handlers.
- *
- * @package core
- * @copyright 2014 The Open University
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-abstract class util {
-    /**
-     * Convert a connection string to an array of servers
-     *
-     * EG: Converts: "abc:123, xyz:789" to
-     *
-     *  array(
-     *      array('abc', '123'),
-     *      array('xyz', '789'),
-     *  )
-     *
-     * @copyright  2013 Moodlerooms Inc. (http://www.moodlerooms.com)
-     * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-     * @author     Mark Nielsen
-     *
-     * @param string $str save_path value containing memcached connection string
-     * @return array
-     */
-    public static function connection_string_to_memcache_servers($str) {
-        $servers = array();
-        $parts   = explode(',', $str);
-        foreach ($parts as $part) {
-            $part = trim($part);
-            $pos  = strrpos($part, ':');
-            if ($pos !== false) {
-                $host = substr($part, 0, $pos);
-                $port = substr($part, ($pos + 1));
-            } else {
-                $host = $part;
-                $port = 11211;
-            }
-            $servers[] = array($host, $port);
-        }
-        return $servers;
-    }
-}
index ba7071a..70b6a59 100644 (file)
@@ -71,6 +71,8 @@ information provided here is intended especially for developers.
     - I navigate to "PATH > ITEM" in site administration
     - I navigate to course participants
     - I navigate to "TAB1 > TAB2" in the course gradebook
+* The core\session\util class has been removed. This contained one function only used by the memcached class which has
+  been moved there instead (connection_string_to_memcache_servers).
 
   If some items are not available without Navigation block at all, one can use combination of:
     - I add the "Navigation" block if not present