The quasi-duplication and interaction between isPrimaryRunningReadOnly() and isPrimaryConnectionReadOnly() is convoluted. They should be merged into one method, using the same cache scheme.
Ideally, @@GLOBAL.read_only would only be queried 0 to 1 times per LoadBalancer per request, where any refresh queries triggered by getServerConnection() only happen for new connections rather than reused connections. This would avoid some occasional errors when the connection was lost in the meantime and silent re-connection does not apply due to things like Database::getScopedLock(). An example error is:
exception.previous.message A connection error occurred during a query. Query: SELECT @@GLOBAL.read_only AS Value Function: Wikimedia\Rdbms\DatabaseMysqlBase::serverIsReadOnly Error: 2006 MySQL server has gone away exception.previous.trace from /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(1524) #0 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(1145): Wikimedia\Rdbms\Database->getQueryException(string, integer, string, string) #1 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(991): Wikimedia\Rdbms\Database->attemptQuery(array, array, string, string, boolean, boolean) #2 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(845): Wikimedia\Rdbms\Database->executeQuery(string, string, integer, string) #3 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DatabaseMysqlBase.php(419): Wikimedia\Rdbms\Database->query(string, string, integer) #4 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/loadbalancer/LoadBalancer.php(1906): Wikimedia\Rdbms\DatabaseMysqlBase->serverIsReadOnly() #5 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/objectcache/BagOStuff.php(216): Wikimedia\Rdbms\LoadBalancer::Wikimedia\Rdbms\{closure}(integer) #6 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/loadbalancer/LoadBalancer.php(1912): BagOStuff->getWithSetCallback(string, integer, Closure) #7 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/loadbalancer/LoadBalancer.php(906): Wikimedia\Rdbms\LoadBalancer->isPrimaryConnectionReadOnly(Wikimedia\Rdbms\DatabaseMysqli, integer) #8 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/loadbalancer/LoadBalancer.php(861): Wikimedia\Rdbms\LoadBalancer->getServerConnection(integer, string, integer) #9 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DBConnRef.php(103): Wikimedia\Rdbms\LoadBalancer->getConnectionInternal(integer, array, string, integer) #10 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DBConnRef.php(117): Wikimedia\Rdbms\DBConnRef->ensureConnection() #11 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DBConnRef.php(331): Wikimedia\Rdbms\DBConnRef->__call(string, array) #12 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/querybuilder/SelectQueryBuilder.php(669): Wikimedia\Rdbms\DBConnRef->selectField(array, string, array, string, array, array) #13 /srv/mediawiki/php-1.41.0-wmf.4/extensions/DiscussionTools/includes/ThreadItemStore.php(376): Wikimedia\Rdbms\SelectQueryBuilder->fetchField() #14 /srv/mediawiki/php-1.41.0-wmf.4/extensions/DiscussionTools/includes/Hooks/DataUpdatesHooks.php(48): MediaWiki\Extension\DiscussionTools\ThreadItemStore->insertThreadItems(MediaWiki\Revision\RevisionStoreRecord, MediaWiki\Extension\DiscussionTools\ContentThreadItemSet) #15 /srv/mediawiki/php-1.41.0-wmf.4/includes/deferred/MWCallableUpdate.php(38): MediaWiki\Extension\DiscussionTools\Hooks\DataUpdatesHooks->MediaWiki\Extension\DiscussionTools\Hooks\{closure}() #16 /srv/mediawiki/php-1.41.0-wmf.4/includes/deferred/DeferredUpdates.php(473): MWCallableUpdate->doUpdate() #17 /srv/mediawiki/php-1.41.0-wmf.4/includes/deferred/RefreshSecondaryDataUpdate.php(103): DeferredUpdates::attemptUpdate(MWCallableUpdate, Wikimedia\Rdbms\LBFactoryMulti) #18 /srv/mediawiki/php-1.41.0-wmf.4/includes/deferred/DeferredUpdates.php(473): RefreshSecondaryDataUpdate->doUpdate() #19 /srv/mediawiki/php-1.41.0-wmf.4/includes/Storage/DerivedPageDataUpdater.php(1836): DeferredUpdates::attemptUpdate(RefreshSecondaryDataUpdate, Wikimedia\Rdbms\LBFactoryMulti) #20 /srv/mediawiki/php-1.41.0-wmf.4/includes/page/WikiPage.php(2145): MediaWiki\Storage\DerivedPageDataUpdater->doSecondaryDataUpdates(array) #21 /srv/mediawiki/php-1.41.0-wmf.4/includes/jobqueue/jobs/RefreshLinksJob.php(244): WikiPage->doSecondaryDataUpdates(array) #22 /srv/mediawiki/php-1.41.0-wmf.4/includes/jobqueue/jobs/RefreshLinksJob.php(162): RefreshLinksJob->runForTitle(MediaWiki\Title\Title) #23 /srv/mediawiki/php-1.41.0-wmf.4/extensions/EventBus/includes/JobExecutor.php(79): RefreshLinksJob->run() #24 /srv/mediawiki/rpc/RunSingleJob.php(77): MediaWiki\Extension\EventBus\JobExecutor->execute(array) #25 {main} exception.trace from /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/TransactionManager.php(225) #0 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(1293): Wikimedia\Rdbms\TransactionManager->assertSessionStatus(Wikimedia\Rdbms\DatabaseMysqli, string) #1 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(841): Wikimedia\Rdbms\Database->assertQueryIsCurrentlyAllowed(string, string) #2 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(1631): Wikimedia\Rdbms\Database->query(string, string, integer) #3 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/Database.php(1640): Wikimedia\Rdbms\Database->select(array, array, array, string, array, array) #4 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DBConnRef.php(119): Wikimedia\Rdbms\Database->selectRow(array, array, array, string, array, array) #5 /srv/mediawiki/php-1.41.0-wmf.4/includes/libs/rdbms/database/DBConnRef.php(362): Wikimedia\Rdbms\DBConnRef->__call(string, array) #6 /srv/mediawiki/php-1.41.0-wmf.4/includes/Revision/RevisionStore.php(2403): Wikimedia\Rdbms\DBConnRef->selectRow(array, array, array, string, array, array) #7 /srv/mediawiki/php-1.41.0-wmf.4/includes/Revision/RevisionStore.php(2347): MediaWiki\Revision\RevisionStore->fetchRevisionRowFromConds(Wikimedia\Rdbms\DBConnRef, array, integer, array) #8 /srv/mediawiki/php-1.41.0-wmf.4/includes/Revision/RevisionStore.php(1291): MediaWiki\Revision\RevisionStore->loadRevisionFromConds(Wikimedia\Rdbms\DBConnRef, array, integer, WikiPage) #9 /srv/mediawiki/php-1.41.0-wmf.4/includes/actions/InfoAction.php(193): MediaWiki\Revision\RevisionStore->getRevisionByTitle(WikiPage, integer, integer) #10 /srv/mediawiki/php-1.41.0-wmf.4/includes/jobqueue/jobs/RefreshLinksJob.php(245): InfoAction::invalidateCache(WikiPage) #11 /srv/mediawiki/php-1.41.0-wmf.4/includes/jobqueue/jobs/RefreshLinksJob.php(162): RefreshLinksJob->runForTitle(MediaWiki\Title\Title) #12 /srv/mediawiki/php-1.41.0-wmf.4/extensions/EventBus/includes/JobExecutor.php(79): RefreshLinksJob->run() #13 /srv/mediawiki/rpc/RunSingleJob.php(77): MediaWiki\Extension\EventBus\JobExecutor->execute(array) #14 {main}