From d7845da3f6829bd4a82a4fbd4a272252471896a7 Mon Sep 17 00:00:00 2001 From: Stephane Bisson Date: Mon, 12 Sep 2016 12:05:33 -0400 Subject: [PATCH] resetNotificationCount() from replica with no lag When notifications are being moderated, the unread count for affected users has to be recalculated. It was initially done using DB_SLAVE but it was leading to inconsistent data since that database did not know about recently deleted notifications. It was changed to DB_MASTER but it potentially led to too many queries on master. This patch tries to wait for the replica to have caught up with those changes and use it to update the unread count for the affected users. Bug: T93673 Change-Id: Ib4a845e82f686dd7ed807ab21a28490014b56604 --- includes/controller/ModerationController.php | 22 +++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/includes/controller/ModerationController.php b/includes/controller/ModerationController.php index c849c6270..0c45357ed 100644 --- a/includes/controller/ModerationController.php +++ b/includes/controller/ModerationController.php @@ -1,4 +1,5 @@ fetchUsersWithNotificationsForEvents( $eventIds ); $eventMapper->toggleDeleted( $eventIds, $moderate ); - /** - * Recompute the notification count for the - * users whose notifications have been moderated. - */ - foreach ( $affectedUserIds as $userId ) { - $user = User::newFromId( $userId ); - MWEchoNotifUser::newFromUser( $user )->resetNotificationCount( DB_MASTER ); - } + DeferredUpdates::addCallableUpdate( function () { + // This udate runs after than main transaction round commits. + // Wait for the events deletions to be propagated to replica DBs + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory->waitForReplication( [ 'timeout' => 5 ] ); + $lbFactory->flushReplicaSnapshots( __METHOD__ ); + // Recompute the notification count for the + // users whose notifications have been moderated. + foreach ( $affectedUserIds as $userId ) { + $user = User::newFromId( $userId ); + MWEchoNotifUser::newFromUser( $user )->resetNotificationCount( DB_REPLICA ); + } + } ); } }