jenkins-bot has submitted this change and it was merged.

Change subject: Use DB_SLAVE in TranslatablePage for GET request
......................................................................


Use DB_SLAVE in TranslatablePage for GET request

* Moved some internal callers to a new getSafeReadDB() function
  that tries to avoid DB_MASTER usage if it should not be needed.

Bug: T95753
Bug: T92357
Change-Id: I03204b43158784cb4162567cf84a6a6a9ec7b8ea
---
M tag/TranslatablePage.php
1 file changed, 25 insertions(+), 10 deletions(-)

Approvals:
  Krinkle: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tag/TranslatablePage.php b/tag/TranslatablePage.php
index 1e50818..4863d85 100644
--- a/tag/TranslatablePage.php
+++ b/tag/TranslatablePage.php
@@ -576,6 +576,7 @@
 
                $aid = $this->getTitle()->getArticleID();
 
+               // @TODO: cache ignores DB_SLAVE/DB_MASTER distinction
                if ( isset( self::$tagCache[$aid][$tag] ) ) {
                        return self::$tagCache[$aid][$tag];
                }
@@ -617,8 +618,7 @@
        }
 
        public function getMarkedRevs() {
-               // Avoid replication lag issues
-               $db = wfGetDB( DB_MASTER );
+               $db = self::getSafeReadDB();
 
                $fields = array( 'rt_revision', 'rt_value' );
                $conds = array(
@@ -635,8 +635,8 @@
         * @return Title[]
         */
        public function getTranslationPages() {
-               // Avoid replication lag issues
-               $dbr = wfGetDB( DB_MASTER );
+               $dbr = self::getSafeReadDB();
+
                $prefix = $this->getTitle()->getDBkey() . '/';
                $likePattern = $dbr->buildLike( $prefix, $dbr->anyString() );
                $res = $dbr->select(
@@ -673,9 +673,10 @@
         * @since 2012-08-06
         */
        protected function getSections() {
-               $dbw = wfGetDB( DB_MASTER );
+               $dbr = self::getSafeReadDB();
+
                $conds = array( 'trs_page' => $this->getTitle()->getArticleID() 
);
-               $res = $dbw->select( 'translate_sections', 'trs_key', $conds, 
__METHOD__ );
+               $res = $dbr->select( 'translate_sections', 'trs_key', $conds, 
__METHOD__ );
 
                $sections = array();
                foreach ( $res as $row ) {
@@ -694,6 +695,7 @@
         */
        public function getTranslationUnitPages( $set = 'active', $code = false 
) {
                $dbw = wfGetDB( DB_MASTER );
+
                $base = $this->getTitle()->getPrefixedDBKey();
                // Including the / used as separator
                $baseLength = strlen( $base ) + 1;
@@ -780,8 +782,7 @@
        public function getTransRev( $suffix ) {
                $title = Title::makeTitle( NS_TRANSLATIONS, $suffix );
 
-               // Avoid replication lag issues
-               $db = wfGetDB( DB_MASTER );
+               $db = self::getSafeReadDB();
                $fields = 'rt_value';
                $conds = array(
                        'rt_page' => $title->getArticleID(),
@@ -857,8 +858,7 @@
         * Get a list of page ids where the latest revision is either tagged or 
marked
         */
        public static function getTranslatablePages() {
-               // Avoid replication lag issues
-               $dbr = wfGetDB( DB_MASTER );
+               $dbr = self::getSafeReadDB();
 
                $tables = array( 'revtag', 'page' );
                $fields = 'rt_page';
@@ -877,4 +877,19 @@
 
                return $results;
        }
+
+       /**
+        * Get a DB handle suitable for read and read-for-write cases
+        *
+        * @return DatabaseBase Master for HTTP POST, CLI, DB already changed; 
slave otherwise
+        */
+       protected static function getSafeReadDB() {
+               $index = (
+                       PHP_SAPI === 'cli' ||
+                       RequestContext::getMain()->getRequest()->wasPosted() ||
+                       wfGetLB()->hasOrMadeRecentMasterChanges()
+               ) ? DB_MASTER : DB_SLAVE;
+
+               return wfGetDB( $index );
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I03204b43158784cb4162567cf84a6a6a9ec7b8ea
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to