jenkins-bot has submitted this change and it was merged.
Change subject: Lazy initialize db connection in SqlIdGenerator
......................................................................
Lazy initialize db connection in SqlIdGenerator
SqlIdGenerator object is created as part of the
constructor arguments in many of the special pages,
so even when viewing special pages.
Since we use SqlIdGenerator like that, we need to
avoid opening db connection (esp. master conn)
when constructing the object.
Bug: T94030
Change-Id: I0ca44f70a916f2089e1a5f2128b420ca42b5cab7
---
M repo/includes/store/sql/SqlIdGenerator.php
M repo/includes/store/sql/SqlStore.php
M repo/tests/phpunit/includes/store/sql/SqlIdGeneratorTest.php
M repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
4 files changed, 30 insertions(+), 33 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/repo/includes/store/sql/SqlIdGenerator.php
b/repo/includes/store/sql/SqlIdGenerator.php
index 40b7f48..c91da45 100644
--- a/repo/includes/store/sql/SqlIdGenerator.php
+++ b/repo/includes/store/sql/SqlIdGenerator.php
@@ -3,6 +3,7 @@
namespace Wikibase;
use DatabaseBase;
+use LoadBalancer;
use MWException;
/**
@@ -17,14 +18,9 @@
class SqlIdGenerator implements IdGenerator {
/**
- * @var string
+ * @var LoadBalancer
*/
- private $table;
-
- /**
- * @var DatabaseBase
- */
- private $db;
+ private $loadBalancer;
/**
* @var int[]
@@ -32,13 +28,11 @@
private $idBlacklist;
/**
- * @param string $tableName
- * @param DatabaseBase $database
+ * @param LoadBalancer $loadBalancer
* @param int[] $idBlacklist
*/
- public function __construct( $tableName, DatabaseBase $database, array
$idBlacklist ) {
- $this->table = $tableName;
- $this->db = $database;
+ public function __construct( LoadBalancer $loadBalancer, array
$idBlacklist ) {
+ $this->loadBalancer = $loadBalancer;
$this->idBlacklist = $idBlacklist;
}
@@ -50,47 +44,50 @@
* @return int
*/
public function getNewId( $type ) {
- return $this->generateNewId( $type );
+ $database = $this->loadBalancer->getConnection( DB_MASTER );
+ $id = $this->generateNewId( $database, $type );
+ $this->loadBalancer->reuseConnection( $database );
+
+ return $id;
}
/**
* Generates and returns a new ID.
*
- * @since 0,1
+ * @since 0.1
*
+ * @param DatabaseBase $database
* @param string $type
* @param bool $retry Retry once in case of e.g. race conditions.
Defaults to true.
*
* @throws MWException
* @return int
*/
- private function generateNewId( $type, $retry = true ) {
- $trx = $this->db->trxLevel();
+ private function generateNewId( DatabaseBase $database, $type, $retry =
true ) {
+ $trx = $database->trxLevel();
if ( $trx == 0 ) {
- $this->db->begin( __METHOD__ );
+ $database->begin( __METHOD__ );
}
- $currentId = $this->db->selectRow(
- $this->table,
+ $currentId = $database->selectRow(
+ 'wb_id_counters',
'id_value',
array( 'id_type' => $type )
);
if ( is_object( $currentId ) ) {
$id = $currentId->id_value + 1;
-
- $success = $this->db->update(
- $this->table,
+ $success = $database->update(
+ 'wb_id_counters',
array( 'id_value' => $id ),
array( 'id_type' => $type )
);
- }
- else {
+ } else {
$id = 1;
- $success = $this->db->insert(
- $this->table,
+ $success = $database->insert(
+ 'wb_id_counters',
array(
'id_value' => $id,
'id_type' => $type,
@@ -101,13 +98,13 @@
// Race condition is possible due to occurrence of
phantom reads is possible
// at non serializable transaction isolation level.
if ( !$success && $retry ) {
- $id = $this->getNewId( $type, false );
+ $id = $this->generateNewId( $database, $type,
false );
$success = true;
}
}
if ( $trx == 0 ) {
- $this->db->commit( __METHOD__ );
+ $database->commit( __METHOD__ );
}
if ( !$success ) {
@@ -115,7 +112,7 @@
}
if ( in_array( $id, $this->idBlacklist ) ) {
- $id = $this->generateNewId( $type );
+ $id = $this->generateNewId( $database, $type );
}
return $id;
diff --git a/repo/includes/store/sql/SqlStore.php
b/repo/includes/store/sql/SqlStore.php
index 395cbf0..c18871c 100644
--- a/repo/includes/store/sql/SqlStore.php
+++ b/repo/includes/store/sql/SqlStore.php
@@ -466,7 +466,7 @@
* @return IdGenerator
*/
public function newIdGenerator() {
- return new SqlIdGenerator( 'wb_id_counters', wfGetDB( DB_MASTER
), $this->idBlacklist );
+ return new SqlIdGenerator( wfGetLB(), $this->idBlacklist );
}
/**
diff --git a/repo/tests/phpunit/includes/store/sql/SqlIdGeneratorTest.php
b/repo/tests/phpunit/includes/store/sql/SqlIdGeneratorTest.php
index f9bf697..bd3c429 100644
--- a/repo/tests/phpunit/includes/store/sql/SqlIdGeneratorTest.php
+++ b/repo/tests/phpunit/includes/store/sql/SqlIdGeneratorTest.php
@@ -20,14 +20,14 @@
class SqlIdGeneratorTest extends \MediaWikiTestCase {
public function testGetNewId() {
- $generator = new SqlIdGenerator( 'wb_id_counters', wfGetDB(
DB_MASTER ), array() );
+ $generator = new SqlIdGenerator( wfGetLB(), array() );
$id = $generator->getNewId( 'wikibase-kittens' );
$this->assertSame( 1, $id );
}
public function testIdBlacklisting() {
- $generator = new SqlIdGenerator( 'wb_id_counters', wfGetDB(
DB_MASTER ), array( 1, 2 ) );
+ $generator = new SqlIdGenerator( wfGetLB(), array( 1, 2 ) );
$id = $generator->getNewId( 'wikibase-blacklist' );
$this->assertSame( 3, $id );
diff --git a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
index ec48536..b87d14e 100644
--- a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
+++ b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
@@ -70,7 +70,7 @@
$store = new WikiPageEntityStore(
new EntityContentFactory( $typeMap ),
- new SqlIdGenerator( 'wb_id_counters', wfGetDB(
DB_MASTER ), array() )
+ new SqlIdGenerator( wfGetLB(), array() )
);
return array( $store, $lookup );
--
To view, visit https://gerrit.wikimedia.org/r/203353
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0ca44f70a916f2089e1a5f2128b420ca42b5cab7
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits