jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/394644 )
Change subject: Improve re-use of connections between getDB()/openDB()
......................................................................
Improve re-use of connections between getDB()/openDB()
Current debug output:
> +0s: Create connection to s1
> +0.11s: Query all wikis for matching revisions
> +0s: Create connection to s3.labsdb
> +0.05s: Create connection to s2.labsdb
> +0.06s: Create connection to s1.labsdb
's1' and 's1.labsdb' did not result in re-use because the
normalisation happened inside openDB, but caching in getDB
on the non-canonical value.
Change-Id: Iecda557e8dfd45b4a76b7e32213ce024a28fc06a
---
M src/App.php
1 file changed, 20 insertions(+), 23 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
jenkins-bot: Verified
diff --git a/src/App.php b/src/App.php
index 48acec9..1cfdeec 100644
--- a/src/App.php
+++ b/src/App.php
@@ -20,54 +20,51 @@
/**
* Open a connection to the database.
*
- * @param string $database
- * @param int|string $cluster
+ * @param string $dbname
+ * @param string $host
* @return PDO
*/
- private function openDB($database = 'wikidatawiki', $cluster = null) {
- $dbname = "{$database}_p";
-
- if (is_string($cluster)) {
- if (strpos($cluster, '.') === false) {
- // Default suffix
- $host = "{$cluster}.labsdb";
- } else {
- $host = $cluster;
- }
- } else {
- $host = "{$database}.labsdb";
- }
-
- $this->aTP('Create connection to ' . $cluster);
+ private function openDB($dbname, $host) {
+ $this->aTP('Create connection to ' . $host);
try {
- // Establish connection
+ // Establish a connection
$pdo = new PDO('mysql:host='.$host.';dbname='.$dbname.';',
Settings::getSetting('user'), Settings::getSetting('password'));
} catch (PDOException $e) {
- throw new Exception('Database error: Unable to connect to '.
$dbname);
+ throw new Exception('Database error: Unable to connect to '.
$host);
}
+
return $pdo;
}
/**
- * Gibt die Verbindung zu einem Wiki zurück (cache)
+ * Get a connection to a database (cached)
*
* @param string $database
* @param string $cluster
* @return PDO
*/
public function getDB($database = 'meta', $cluster = 's1') {
- if (!$cluster) {
+ if (!is_string($cluster)) {
throw new Exception('Invalid DB cluster specification');
}
+ if (strpos($cluster, '.') === false) {
+ // Default suffix
+ $host = "{$cluster}.labsdb";
+ } else {
+ $host = $cluster;
+ }
+
+ $dbname = "{$database}_p";
+
// Reuse existing connection if possible
if (!isset($this->clusters[$cluster])) {
- $this->clusters[$cluster] = $this->openDB($database, $cluster);
+ $this->clusters[$cluster] = $this->openDB($dbname, $cluster);
}
$pdo = $this->clusters[$cluster];
// Select the right database on this cluster server
- $m = $pdo->prepare('USE `'.$database.'_p`;');
+ $m = $pdo->prepare('USE `' . $dbname . '`;');
$m->execute();
return $pdo;
--
To view, visit https://gerrit.wikimedia.org/r/394644
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iecda557e8dfd45b4a76b7e32213ce024a28fc06a
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/guc
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits