rdbms: avoid transaction status errors from ping() in rollback()
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 25 Oct 2018 15:34:39 +0000 (08:34 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 25 Oct 2018 15:34:39 +0000 (08:34 -0700)
Change-Id: I38658cbf90518d9818824674da371637db15e156

includes/libs/rdbms/database/Database.php

index c436b64..16e8d8b 100644 (file)
@@ -719,19 +719,27 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
                switch ( $type ) {
                        case self::ESTIMATE_DB_APPLY:
 
                switch ( $type ) {
                        case self::ESTIMATE_DB_APPLY:
-                               $this->ping( $rtt );
-                               $rttAdjTotal = $this->trxWriteAdjQueryCount * $rtt;
-                               $applyTime = max( $this->trxWriteAdjDuration - $rttAdjTotal, 0 );
-                               // For omitted queries, make them count as something at least
-                               $omitted = $this->trxWriteQueryCount - $this->trxWriteAdjQueryCount;
-                               $applyTime += self::TINY_WRITE_SEC * $omitted;
-
-                               return $applyTime;
+                               return $this->pingAndCalculateLastTrxApplyTime();
                        default: // everything
                                return $this->trxWriteDuration;
                }
        }
 
                        default: // everything
                                return $this->trxWriteDuration;
                }
        }
 
+       /**
+        * @return float Time to apply writes to replicas based on trxWrite* fields
+        */
+       private function pingAndCalculateLastTrxApplyTime() {
+               $this->ping( $rtt );
+
+               $rttAdjTotal = $this->trxWriteAdjQueryCount * $rtt;
+               $applyTime = max( $this->trxWriteAdjDuration - $rttAdjTotal, 0 );
+               // For omitted queries, make them count as something at least
+               $omitted = $this->trxWriteQueryCount - $this->trxWriteAdjQueryCount;
+               $applyTime += self::TINY_WRITE_SEC * $omitted;
+
+               return $applyTime;
+       }
+
        public function pendingWriteCallers() {
                return $this->trxLevel ? $this->trxWriteCallers : [];
        }
        public function pendingWriteCallers() {
                return $this->trxLevel ? $this->trxWriteCallers : [];
        }
@@ -3972,10 +3980,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        // Avoid fatals if close() was called
                        $this->assertOpen();
 
                        // Avoid fatals if close() was called
                        $this->assertOpen();
 
-                       $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
                        $this->doRollback( $fname );
                        $this->trxStatus = self::STATUS_TRX_NONE;
                        $this->trxAtomicLevels = [];
                        $this->doRollback( $fname );
                        $this->trxStatus = self::STATUS_TRX_NONE;
                        $this->trxAtomicLevels = [];
+                       // Estimate the RTT via a query now that trxStatus is OK
+                       $writeTime = $this->pingAndCalculateLastTrxApplyTime();
 
                        if ( $this->trxDoneWrites ) {
                                $this->trxProfiler->transactionWritingOut(
 
                        if ( $this->trxDoneWrites ) {
                                $this->trxProfiler->transactionWritingOut(