Skip to content

Commit

Permalink
Merge pull request #31192 from nextcloud/backport/31106/stable23
Browse files Browse the repository at this point in the history
[stable23] Improve user status revert performance
  • Loading branch information
nickvergessen committed Feb 23, 2022
2 parents 5d82e62 + 9292834 commit 28c526b
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 77 deletions.
10 changes: 2 additions & 8 deletions apps/user_status/lib/Connector/UserStatusProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ public function getUserStatuses(array $userIds): array {
return $userStatuses;
}

public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void {
if ($createBackup) {
if ($this->service->backupCurrentStatus($userId) === false) {
return; // Already a status set automatically => abort.
}
}
$this->service->setStatus($userId, $status, null, true);
$this->service->setPredefinedMessage($userId, $messageId, null);
public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup): void {
$this->service->setUserStatus($userId, $status, $messageId, $createBackup);
}

public function revertUserStatus(string $userId, string $messageId, string $status): void {
Expand Down
39 changes: 37 additions & 2 deletions apps/user_status/lib/Db/UserStatusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public function findByUserId(string $userId, bool $isBackup = false):UserStatus
$qb
->select('*')
->from($this->tableName)
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter($isBackup, IQueryBuilder::PARAM_BOOL)));
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)));

return $this->findEntity($qb);
}
Expand Down Expand Up @@ -166,4 +165,40 @@ public function clearMessagesOlderThan(int $timestamp): void {

$qb->execute();
}


/**
* Deletes a user status so we can restore the backup
*
* @param string $userId
* @param string $messageId
* @param string $status
* @return bool True if an entry was deleted
*/
public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId, string $status): bool {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->tableName)
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('message_id', $qb->createNamedParameter($messageId)))
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter($status)))
->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)));
return $qb->executeStatement() > 0;
}

/**
* @param string $userId
* @return bool
* @throws \OCP\DB\Exception
*/
public function createBackupStatus(string $userId): bool {
// Prefix user account with an underscore because user_id is marked as unique
// in the table. Starting a username with an underscore is not allowed so this
// shouldn't create any trouble.
$qb = $this->db->getQueryBuilder();
$qb->update($this->tableName)
->set('is_backup', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
->set('user_id', $qb->createNamedParameter('_' . $userId))
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return $qb->executeStatement() > 0;
}
}
97 changes: 69 additions & 28 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\UserStatus\Exception\StatusMessageTooLongException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\UserStatus\IUserStatus;

Expand Down Expand Up @@ -229,6 +230,7 @@ public function setPredefinedMessage(string $userId,
$userStatus->setStatus(IUserStatus::OFFLINE);
$userStatus->setStatusTimestamp(0);
$userStatus->setIsUserDefined(false);
$userStatus->setIsBackup(false);
}

if (!$this->predefinedStatusService->isValidId($messageId)) {
Expand All @@ -252,6 +254,60 @@ public function setPredefinedMessage(string $userId,
return $this->mapper->update($userStatus);
}

/**
* @param string $userId
* @param string $status
* @param string $messageId
* @param bool $createBackup
* @throws InvalidStatusTypeException
* @throws InvalidMessageIdException
*/
public function setUserStatus(string $userId,
string $status,
string $messageId,
bool $createBackup): void {
// Check if status-type is valid
if (!\in_array($status, self::PRIORITY_ORDERED_STATUSES, true)) {
throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
}

if (!$this->predefinedStatusService->isValidId($messageId)) {
throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
}

if ($createBackup) {
if ($this->backupCurrentStatus($userId) === false) {
return; // Already a status set automatically => abort.
}

// If we just created the backup
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
} else {
try {
$userStatus = $this->mapper->findByUserId($userId);
} catch (DoesNotExistException $ex) {
$userStatus = new UserStatus();
$userStatus->setUserId($userId);
}
}

$userStatus->setStatus($status);
$userStatus->setStatusTimestamp($this->timeFactory->getTime());
$userStatus->setIsUserDefined(false);
$userStatus->setIsBackup(false);
$userStatus->setMessageId($messageId);
$userStatus->setCustomIcon(null);
$userStatus->setCustomMessage(null);
$userStatus->setClearAt(null);

if ($userStatus->getId() !== null) {
$this->mapper->update($userStatus);
return;
}
$this->mapper->insert($userStatus);
}

/**
* @param string $userId
* @param string|null $statusIcon
Expand Down Expand Up @@ -434,30 +490,18 @@ private function addDefaultMessage(UserStatus $status): void {
}

/**
* @return bool false iff there is already a backup. In this case abort the procedure.
* @return bool false if there is already a backup. In this case abort the procedure.
*/
public function backupCurrentStatus(string $userId): bool {
try {
$this->mapper->findByUserId($userId, true);
return false;
} catch (DoesNotExistException $ex) {
// No backup already existing => Good
}

try {
$userStatus = $this->mapper->findByUserId($userId);
} catch (DoesNotExistException $ex) {
// if there is no status to backup, just return
$this->mapper->createBackupStatus($userId);
return true;
} catch (Exception $ex) {
if ($ex->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
return false;
}
throw $ex;
}

$userStatus->setIsBackup(true);
// Prefix user account with an underscore because user_id is marked as unique
// in the table. Starting an username with an underscore is not allowed so this
// shouldn't create any trouble.
$userStatus->setUserId('_' . $userStatus->getUserId());
$this->mapper->update($userStatus);
return true;
}

public function revertUserStatus(string $userId, ?string $messageId, string $status): void {
Expand All @@ -468,16 +512,13 @@ public function revertUserStatus(string $userId, ?string $messageId, string $sta
// No user status to revert, do nothing
return;
}
try {
$userStatus = $this->mapper->findByUserId($userId);
if ($userStatus->getMessageId() !== $messageId || $userStatus->getStatus() !== $status) {
// Another status is set automatically, do nothing
return;
}
$this->removeUserStatus($userId);
} catch (DoesNotExistException $ex) {
// No current status => nothing to delete

$deleted = $this->mapper->deleteCurrentStatusToRestoreBackup($userId, $messageId ?? '', $status);
if (!$deleted) {
// Another status is set automatically or no status, do nothing
return;
}

$backupUserStatus->setIsBackup(false);
// Remove the underscore prefix added when creating the backup
$backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1));
Expand Down
61 changes: 61 additions & 0 deletions apps/user_status/tests/Unit/Db/UserStatusMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,65 @@ private function insertSampleStatuses(): void {
$this->mapper->insert($userStatus2);
$this->mapper->insert($userStatus3);
}

public function dataCreateBackupStatus(): array {
return [
[false, false, false],
[true, false, true],
[false, true, false],
[true, true, false],
];
}

/**
* @dataProvider dataCreateBackupStatus
* @param bool $hasStatus
* @param bool $hasBackup
* @param bool $backupCreated
*/
public function testCreateBackupStatus(bool $hasStatus, bool $hasBackup, bool $backupCreated): void {
if ($hasStatus) {
$userStatus1 = new UserStatus();
$userStatus1->setUserId('user1');
$userStatus1->setStatus('online');
$userStatus1->setStatusTimestamp(5000);
$userStatus1->setIsUserDefined(true);
$userStatus1->setIsBackup(false);
$userStatus1->setCustomIcon('🚀');
$userStatus1->setCustomMessage('Current');
$userStatus1->setClearAt(50000);
$this->mapper->insert($userStatus1);
}

if ($hasBackup) {
$userStatus1 = new UserStatus();
$userStatus1->setUserId('_user1');
$userStatus1->setStatus('online');
$userStatus1->setStatusTimestamp(5000);
$userStatus1->setIsUserDefined(true);
$userStatus1->setIsBackup(true);
$userStatus1->setCustomIcon('🚀');
$userStatus1->setCustomMessage('Backup');
$userStatus1->setClearAt(50000);
$this->mapper->insert($userStatus1);
}

if ($hasStatus && $hasBackup) {
$this->expectException(Exception::class);
}

self::assertSame($backupCreated, $this->mapper->createBackupStatus('user1'));

if ($backupCreated) {
$user1Status = $this->mapper->findByUserId('user1', true);
$this->assertEquals('_user1', $user1Status->getUserId());
$this->assertEquals(true, $user1Status->getIsBackup());
$this->assertEquals('Current', $user1Status->getCustomMessage());
} else if ($hasBackup) {
$user1Status = $this->mapper->findByUserId('user1', true);
$this->assertEquals('_user1', $user1Status->getUserId());
$this->assertEquals(true, $user1Status->getIsBackup());
$this->assertEquals('Backup', $user1Status->getCustomMessage());
}
}
}
64 changes: 25 additions & 39 deletions apps/user_status/tests/Unit/Service/StatusServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
namespace OCA\UserStatus\Tests\Service;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\DB\Exceptions\DbalException;
use OCA\UserStatus\Db\UserStatus;
use OCA\UserStatus\Db\UserStatusMapper;
use OCA\UserStatus\Exception\InvalidClearAtException;
Expand All @@ -38,6 +40,7 @@
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\UserStatus\IUserStatus;
use Test\TestCase;
Expand Down Expand Up @@ -723,52 +726,35 @@ public function testCleanStatusCleanedAlready(): void {
parent::invokePrivate($this->service, 'cleanStatus', [$status]);
}

public function testBackupWorkingHasBackupAlready() {
$status = new UserStatus();
$status->setStatus(IUserStatus::ONLINE);
$status->setStatusTimestamp(1337);
$status->setIsUserDefined(true);
$status->setMessageId('meeting');
$status->setUserId('john');
$status->setIsBackup(true);

public function testBackupWorkingHasBackupAlready(): void {
$p = $this->createMock(UniqueConstraintViolationException::class);
$e = DbalException::wrap($p);
$this->mapper->expects($this->once())
->method('findByUserId')
->with('john', true)
->willReturn($status);
->method('createBackupStatus')
->with('john')
->willThrowException($e);

$this->service->backupCurrentStatus('john');
$this->assertFalse($this->service->backupCurrentStatus('john'));
}

public function testBackup() {
$currentStatus = new UserStatus();
$currentStatus->setStatus(IUserStatus::ONLINE);
$currentStatus->setStatusTimestamp(1337);
$currentStatus->setIsUserDefined(true);
$currentStatus->setMessageId('meeting');
$currentStatus->setUserId('john');

$this->mapper->expects($this->at(0))
->method('findByUserId')
->with('john', true)
->willThrowException(new DoesNotExistException(''));
$this->mapper->expects($this->at(1))
->method('findByUserId')
->with('john', false)
->willReturn($currentStatus);
public function testBackupThrowsOther(): void {
$e = new Exception('', Exception::REASON_CONNECTION_LOST);
$this->mapper->expects($this->once())
->method('createBackupStatus')
->with('john')
->willThrowException($e);

$newBackupStatus = new UserStatus();
$newBackupStatus->setStatus(IUserStatus::ONLINE);
$newBackupStatus->setStatusTimestamp(1337);
$newBackupStatus->setIsUserDefined(true);
$newBackupStatus->setMessageId('meeting');
$newBackupStatus->setUserId('_john');
$newBackupStatus->setIsBackup(true);
$this->expectException(Exception::class);
$this->service->backupCurrentStatus('john');
}

public function testBackup(): void {
$e = new Exception('', Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION);
$this->mapper->expects($this->once())
->method('update')
->with($newBackupStatus);
->method('createBackupStatus')
->with('john')
->willReturn(true);

$this->service->backupCurrentStatus('john');
$this->assertTrue($this->service->backupCurrentStatus('john'));
}
}

0 comments on commit 28c526b

Please sign in to comment.