Skip to content

Commit

Permalink
MDL-78212 dml: Added MYSQLI_CLIENT_COMPRESS option for mysqli
Browse files Browse the repository at this point in the history
Config dboptions key 'clientcompress'.
Decreases traffic between the application server and the database host.
  • Loading branch information
srdjan-catalyst committed Jun 21, 2023
1 parent 1b1a15a commit 9fcba75
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config-dist.php
Expand Up @@ -91,6 +91,10 @@
// set to zero if you are using pg_bouncer in
// 'transaction' mode (it is fine in 'session'
// mode).
// 'clientcompress' => true // Use compression protocol to communicate with the database server.
// Decreases traffic from the database server.
// Not needed if the databse is on the same host.
// Currently supported only with mysqli driver.
/*
'connecttimeout' => null, // Set connect timeout in seconds. Not all drivers support it.
'readonly' => [ // Set to read-only slave details, to get safe reads
Expand Down
7 changes: 6 additions & 1 deletion lib/dml/mysqli_native_moodle_database.php
Expand Up @@ -573,11 +573,16 @@ public function raw_connect(string $dbhost, string $dbuser, string $dbpass, stri
$this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, $this->dboptions['connecttimeout']);
}

$flags = 0;
if ($this->dboptions['clientcompress'] ?? false) {
$flags |= MYSQLI_CLIENT_COMPRESS;
}

$conn = null;
$dberr = null;
try {
// real_connect() is doing things we don't expext.
$conn = @$this->mysqli->real_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
$conn = @$this->mysqli->real_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket, $flags);
} catch (\Exception $e) {
$dberr = "$e";
}
Expand Down
109 changes: 109 additions & 0 deletions lib/dml/tests/mysqli_native_moodle_database_test.php
@@ -0,0 +1,109 @@
<?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;

use ReflectionClass;
use mysqli;
use moodle_database, mysqli_native_moodle_database;
use moodle_exception;

/**
* Test specific features of the MySql dml.
*
* @package core
* @category test
* @copyright 2023 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \mysqli_native_moodle_database
*/
class mysqli_native_moodle_database_test extends \advanced_testcase {

/**
* Set up.
*/
public function setUp(): void {
global $DB;
parent::setUp();
// Skip tests if not using Postgres.
if (!($DB instanceof mysqli_native_moodle_database)) {
$this->markTestSkipped('MySql-only test');
}
}

/**
* SSL connection helper.
*
* @param bool|null $compress
* @return mysqli
* @throws moodle_exception
*/
public function new_connection(?bool $compress = false): mysqli {
global $DB;

// Open new connection.
$cfg = $DB->export_dbconfig();
if (!isset($cfg->dboptions)) {
$cfg->dboptions = [];
}

$cfg->dboptions['clientcompress'] = $compress;

// Get a separate disposable db connection handle with guaranteed 'readonly' config.
$db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
$db2->raw_connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);

$reflector = new ReflectionClass($db2);
$rp = $reflector->getProperty('mysqli');
$rp->setAccessible(true);
return $rp->getValue($db2);
}

/**
* Test client compression helper.
*
* @param mysqli $mysqli
* @return array
*/
public function connection_status($mysqli): array {
$mysqli->query("SELECT * FROM INFORMATION_SCHEMA.TABLES");

$stats = [];
foreach ($mysqli->query('SHOW SESSION STATUS')->fetch_all(MYSQLI_ASSOC) as $r) {
$stats[$r['Variable_name']] = $r['Value'];
}
return $stats;
}

/**
* Test client compression.
*
* @return void
*/
public function test_client_compression(): void {
$mysqli = $this->new_connection();
$stats = $this->connection_status($mysqli);
$this->assertEquals('OFF', $stats['Compression']);
$sent = $stats['Bytes_sent'];

$mysqlic = $this->new_connection(true);
$stats = $this->connection_status($mysqlic);
$this->assertEquals('ON', $stats['Compression']);
$sentc = $stats['Bytes_sent'];

$this->assertLessThan($sent, $sentc);
}
}

0 comments on commit 9fcba75

Please sign in to comment.