Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/54008


Change subject: Make sure that no out-of-date data is read from a slave
......................................................................

Make sure that no out-of-date data is read from a slave

Change-Id: I4e633bf530100a6163f069ff0258ce36b7c68461
---
M ArticleFeedbackv5.activity.php
M ArticleFeedbackv5.log.php
M ArticleFeedbackv5.utils.php
M api/ApiAddFlagNoteArticleFeedbackv5.php
M data/DataModelBackend.LBFactory.php
5 files changed, 74 insertions(+), 9 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ArticleFeedbackv5 
refs/changes/08/54008/1

diff --git a/ArticleFeedbackv5.activity.php b/ArticleFeedbackv5.activity.php
index 61d6fd1..7102aa4 100644
--- a/ArticleFeedbackv5.activity.php
+++ b/ArticleFeedbackv5.activity.php
@@ -216,7 +216,7 @@
                $where['log_namespace'] = NS_SPECIAL;
                $where = self::applyContinue( $continue, $where );
 
-               $activity = wfGetDB( DB_SLAVE )->select(
+               $activity = ArticleFeedbackv5Utils::getDB( DB_SLAVE )->select(
                        array( 'logging' ),
                        array(
                                'log_id',
@@ -354,7 +354,7 @@
                $where['log_title'] = $title;
                $where['log_namespace'] = NS_SPECIAL;
 
-               return (int) wfGetDB( DB_SLAVE )->selectField(
+               return (int) ArticleFeedbackv5Utils::getDB( DB_SLAVE 
)->selectField(
                        'logging',
                        'COUNT(log_id)',
                        $where,
@@ -372,7 +372,7 @@
         */
        public static function getLastEditorActivity( array $entries ) {
                global $wgMemc;
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = ArticleFeedbackv5Utils::getDB( DB_SLAVE );
 
                $activity = array();
                $where = array();
@@ -430,7 +430,7 @@
                         * a subquery (the below $ids query), which will then 
be folded into the
                         * main query that will get all of those last actions' 
details.
                         */
-                       $ids = wfGetDB( DB_SLAVE )->selectSQLText(
+                       $ids = ArticleFeedbackv5Utils::getDB( DB_SLAVE 
)->selectSQLText(
                                array( 'logging' ),
                                array( 'last_id' => 'MAX(log_id)' ),
                                $where,
@@ -446,7 +446,7 @@
                                )
                        );
 
-                       $rows = wfGetDB( DB_SLAVE )->select(
+                       $rows = ArticleFeedbackv5Utils::getDB( DB_SLAVE 
)->select(
                                array(
                                        'logging',
                                        'ids' => "($ids)" // the subquery that 
will provide the most recent log_id's
@@ -501,7 +501,7 @@
                        throw new MWException( 'Invalid continue param. You 
should pass the original value returned by the previous query', 'badcontinue' );
                }
 
-               $db = wfGetDB( DB_SLAVE );
+               $db = ArticleFeedbackv5Utils::getDB( DB_SLAVE );
                $ts = $db->addQuotes( $db->timestamp( $values[0] ) );
                $id = intval( $values[1] );
                $where[] = '(log_id = ' . $id . ' AND log_timestamp <= ' . $ts 
. ') OR log_timestamp < ' . $ts;
@@ -541,7 +541,7 @@
        protected static function buildWhereActions( $permissions = array(), 
$actions = array() ) {
                global $wgLogActionsHandlers;
 
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = ArticleFeedbackv5Utils::getDB( DB_SLAVE );
 
                $where = array();
                foreach ( self::$actions as $action => $options ) {
diff --git a/ArticleFeedbackv5.log.php b/ArticleFeedbackv5.log.php
index 45b027e..1ebc7c4 100644
--- a/ArticleFeedbackv5.log.php
+++ b/ArticleFeedbackv5.log.php
@@ -68,6 +68,13 @@
                $logId = $logEntry->insert();
                $logEntry->publish( $logId );
 
+               /**
+                * ManualLogEntry will have written to database. To make sure 
that subsequent
+                * reads are up-to-date, I'll set a flag to know that we've 
written data, so
+                * DB_MASTER will be queried.
+                */
+               ArticleFeedbackv5Utils::$written = true;
+
                wfProfileOut( __METHOD__ );
 
                return $logId;
diff --git a/ArticleFeedbackv5.utils.php b/ArticleFeedbackv5.utils.php
index 5930c11..e5ad3dd 100644
--- a/ArticleFeedbackv5.utils.php
+++ b/ArticleFeedbackv5.utils.php
@@ -22,6 +22,64 @@
  */
 class ArticleFeedbackv5Utils {
        /**
+        * @var LoadBalancer
+        */
+       protected static $lb;
+
+       /**
+        * @var bool
+        */
+       public static $written = false;
+
+       /**
+        * @return LoadBalancer
+        */
+       public static function getLB( $wiki ) {
+               if ( static::$lb === null ) {
+                       static::$lb = wfGetLB( $wiki );
+               }
+
+               return static::$lb;
+       }
+
+       /**
+        * Wrapper function for wfGetDB.
+        *
+        * @param $db Integer: index of the connection to get. May be DB_MASTER 
for the
+        *            master (for write queries), DB_SLAVE for potentially 
lagged read
+        *            queries, or an integer >= 0 for a particular server.
+        * @param $groups Mixed: query groups. An array of group names that 
this query
+        *                belongs to. May contain a single string if the query 
is only
+        *                in one group.
+        * @param $wiki String: the wiki ID, or false for the current wiki
+        */
+       public static function getDB( $db, $groups = array(), $wiki = false ) {
+               $lb = static::getLB( $wiki );
+
+               if ( $db === DB_MASTER ) {
+                       // mark that we're writing data
+                       static::$written = true;
+               } elseif ( static::$written ) {
+                       if ( $db === DB_SLAVE ) {
+                               /*
+                                * Let's keep querying master to make sure we 
have up-to-date
+                                * data (waiting for slaves to sync up might 
take some time)
+                                */
+                               $db = DB_MASTER;
+                       } else {
+                               /*
+                                * If another db is requested and we already 
requested master,
+                                * make sure this slave has caught up!
+                                */
+                               $lb->waitFor( $lb->getMasterPos() );
+                               static::$written = false;
+                       }
+               }
+
+               return $lb->getConnection( $db, $groups, $wiki );
+       }
+
+       /**
         * Get the full, prefixed, name that data is saved at in cookie.
         * The cookie name is prefixed by the extension name and a version 
number,
         * to avoid collisions with other extensions or code versions.
diff --git a/api/ApiAddFlagNoteArticleFeedbackv5.php 
b/api/ApiAddFlagNoteArticleFeedbackv5.php
index cc9b12a..d1f6ab6 100644
--- a/api/ApiAddFlagNoteArticleFeedbackv5.php
+++ b/api/ApiAddFlagNoteArticleFeedbackv5.php
@@ -39,7 +39,7 @@
                        $notes      = $params['note'];
 
                        // update log entry in database
-                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw = ArticleFeedbackv5Utils::getDB( DB_MASTER );
                        $affected = $dbw->update(
                                'logging',
                                array( 'log_comment' => $notes ),
diff --git a/data/DataModelBackend.LBFactory.php 
b/data/DataModelBackend.LBFactory.php
index 5bc3124..7e96150 100644
--- a/data/DataModelBackend.LBFactory.php
+++ b/data/DataModelBackend.LBFactory.php
@@ -42,7 +42,7 @@
         * @param $wiki String: the wiki ID, or false for the current wiki
         */
        public function getDB( $db, $groups = array(), $wiki = false ) {
-               $lb = $this->getLB( $wiki );
+               $lb = wfGetLB( $wiki );
 
                if ( $db === DB_MASTER ) {
                        // mark that we're writing data

-- 
To view, visit https://gerrit.wikimedia.org/r/54008
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4e633bf530100a6163f069ff0258ce36b7c68461
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ArticleFeedbackv5
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to