Aaron Schulz has uploaded a new change for review.
https://gerrit.wikimedia.org/r/305952
Change subject: Add LBFactory::beginMasterChanges() for doing DBO_TRX rounds
......................................................................
Add LBFactory::beginMasterChanges() for doing DBO_TRX rounds
This is in intended to replace the DataUpdate transaction round logic.
Change-Id: If21c2ba5e8bac48c250b96137279e7edaa8289f7
---
M includes/db/Database.php
M includes/db/IDatabase.php
M includes/db/loadbalancer/LBFactory.php
M includes/db/loadbalancer/LoadBalancer.php
4 files changed, 82 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/52/305952/1
diff --git a/includes/db/Database.php b/includes/db/Database.php
index 186a87b..3634dad 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -177,6 +177,9 @@
*/
protected $allViews = null;
+ /** @var int[] Prior mFlags values */
+ private $priorFlags = [];
+
/** @var TransactionProfiler */
protected $trxProfiler;
@@ -410,13 +413,28 @@
}
public function setFlag( $flag ) {
+ array_push( $this->priorFlags, $this->mFlags );
$this->mFlags |= $flag;
}
public function clearFlag( $flag ) {
+ array_push( $this->priorFlags, $this->mFlags );
$this->mFlags &= ~$flag;
}
+ public function restoreFlags( $state = self::RESTORE_PRIOR ) {
+ if ( !$this->priorFlags ) {
+ return;
+ }
+
+ if ( $state === self::RESTORE_INITIAL ) {
+ $this->mFlags = array_slice( $this->priorFlags, 0, 1 );
+ $this->priorFlags = [];
+ } else {
+ $this->mFlags = array_pop( $this->priorFlags );
+ }
+ }
+
public function getFlag( $flag ) {
return !!( $this->mFlags & $flag );
}
diff --git a/includes/db/IDatabase.php b/includes/db/IDatabase.php
index 25100db..0eb1c98 100644
--- a/includes/db/IDatabase.php
+++ b/includes/db/IDatabase.php
@@ -50,6 +50,11 @@
/** @var string Transaction operation comes from the database class
internally */
const FLUSHING_INTERNAL = 'flush';
+ /** @var string Restore to the prior flag state */
+ const RESTORE_PRIOR = 'prior';
+ /** @var string Restore to the initial flag state */
+ const RESTORE_INITIAL = 'initial';
+
/**
* A string describing the current software version, and possibly
* other details in a user-friendly way. Will be listed on
Special:Version, etc.
@@ -247,6 +252,14 @@
public function clearFlag( $flag );
/**
+ * Restore the flags to their prior state before the last
setFlag/clearFlag call
+ *
+ * @param string $state IDatabase::RESTORE_* constant. [default:
RESTORE_PRIOR]
+ * @since 1.28
+ */
+ public function restoreFlags( $state = self::RESTORE_PRIOR );
+
+ /**
* Returns a boolean whether the flag $flag is set for this connection
*
* @param int $flag DBO_* constants from Defines.php:
diff --git a/includes/db/loadbalancer/LBFactory.php
b/includes/db/loadbalancer/LBFactory.php
index b320544..f560cf1 100644
--- a/includes/db/loadbalancer/LBFactory.php
+++ b/includes/db/loadbalancer/LBFactory.php
@@ -217,6 +217,22 @@
}
/**
+ * Flush any master transaction snapshots and set DBO_TRX (if
DBO_DEFAULT is set)
+ *
+ * The DBO_TRX setting will be reverted to the default in each of these
methods:
+ * - commitMasterChanges()
+ * - rollbackMasterChanges()
+ * - commitAll()
+ * This allows for custom transaction rounds from any outer transaction
scope.
+ *
+ * @param string $fname
+ * @since 1.28
+ */
+ public function beginMasterChanges( $fname = __METHOD__ ) {
+ $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname ] );
+ }
+
+ /**
* Commit on all connections. Done for two reasons:
* 1. To commit changes to the masters.
* 2. To release the snapshot on all connections, master and slave.
diff --git a/includes/db/loadbalancer/LoadBalancer.php
b/includes/db/loadbalancer/LoadBalancer.php
index f045acf..e282c16 100644
--- a/includes/db/loadbalancer/LoadBalancer.php
+++ b/includes/db/loadbalancer/LoadBalancer.php
@@ -1062,6 +1062,7 @@
*/
public function commitAll( $fname = __METHOD__ ) {
$this->forEachOpenConnection( function ( DatabaseBase $conn )
use ( $fname ) {
+ $conn->restoreFlags( IDatabase::RESTORE_INITIAL );
$conn->commit( $fname, IDatabase::FLUSHING_ALL_PEERS );
} );
}
@@ -1112,12 +1113,45 @@
}
/**
+ * Flush any master transaction snapshots and set DBO_TRX (if
DBO_DEFAULT is set)
+ *
+ * The DBO_TRX setting will be reverted to the default in each of these
methods:
+ * - commitMasterChanges()
+ * - rollbackMasterChanges()
+ * - commitAll()
+ * This allows for custom transaction rounds from any outer transaction
scope.
+ *
+ * @param string $fname
+ * @since 1.28
+ */
+ public function beginMasterChanges( $fname = __METHOD__ ) {
+ $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $fname ) {
+ if ( $conn->writesOrCallbacksPending() ) {
+ throw new DBTransactionError(
+ $conn,
+ "Cannot clear snapshot; transaction
with pending writes still active."
+ );
+ } elseif ( $conn->trxLevel() ) {
+ $conn->commit( $fname,
IDatabase::FLUSHING_ALL_PEERS );
+ }
+ if ( $conn->getFlag( DBO_DEFAULT ) ) {
+ // DBO_TRX is controlled entirely by CLI mode
implicitly with DBO_DEFAULT.
+ // Force DBO_TRX even in CLI mode since a
commit round is expected soon.
+ $conn->setFlag( DBO_TRX );
+ } else {
+ // Config has explicitly requested DBO_TRX be
either on or off; respect that.
+ }
+ } );
+ }
+
+ /**
* Issue COMMIT on all master connections where writes where done
* @param string $fname Caller name
*/
public function commitMasterChanges( $fname = __METHOD__ ) {
$this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $fname ) {
if ( $conn->writesOrCallbacksPending() ) {
+ $conn->restoreFlags( IDatabase::RESTORE_INITIAL
);
$conn->commit( $fname,
IDatabase::FLUSHING_ALL_PEERS );
}
} );
@@ -1160,6 +1194,7 @@
foreach ( $conns2[$masterIndex] as $conn ) {
if ( $conn->trxLevel() &&
$conn->writesOrCallbacksPending() ) {
try {
+ $conn->restoreFlags(
IDatabase::RESTORE_INITIAL );
$conn->rollback( $fname,
IDatabase::FLUSHING_ALL_PEERS );
} catch ( DBError $e ) {
MWExceptionHandler::logException( $e );
--
To view, visit https://gerrit.wikimedia.org/r/305952
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If21c2ba5e8bac48c250b96137279e7edaa8289f7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits