mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-01 07:56:47 +00:00
Use AccountLockManager#withSingleAccountLock when appropriate
This commit is contained in:
1 parent
6610c37fea
commit
9870accbae
2 files changed
+13
-9
No files matched your search
@@ -710,11 +710,10 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
}
|
||||
|
||||
// Always fetch a fresh, non-cached copy of the account before making modifications
|
||||
final UUID phoneNumberIdentifier = accounts.getByAccountIdentifier(accountIdentifier)
|
||||
.map(account -> account.getIdentifier(IdentityType.PNI))
|
||||
final Account account = accounts.getByAccountIdentifier(accountIdentifier)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Account not found: " + accountIdentifier));
|
||||
|
||||
return accountLockManager.withLock(Set.of(phoneNumberIdentifier),
|
||||
return accountLockManager.withSingleAccountLock(account,
|
||||
() -> removeDevice(accountIdentifier, deviceId, MAX_UPDATE_ATTEMPTS),
|
||||
accountLockExecutor);
|
||||
}
|
||||
@@ -732,7 +731,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
|
||||
final List<TransactWriteItem> additionalWriteItems = new ArrayList<>(
|
||||
keysManager.buildWriteItemsForRemovedDevice(
|
||||
account.getIdentifier(IdentityType.ACI),
|
||||
account.getAccountIdentifier(),
|
||||
account.getPhoneNumberIdentifierOptional(),
|
||||
deviceId));
|
||||
try {
|
||||
@@ -1058,9 +1057,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
final Account account = accounts.getByAccountIdentifier(accountIdentifier)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Account not found: " + accountIdentifier));
|
||||
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()), () -> {
|
||||
return accountLockManager.withSingleAccountLock(account, () -> {
|
||||
final Account maybeUpdatedAccount = update(accountIdentifier, a -> {
|
||||
if (!a.getCurrentProfileVersion().orElse(new byte[0]).equals(expectedCurrentVersion)) {
|
||||
if (!Arrays.equals(a.getCurrentProfileVersion().orElse(new byte[0]), expectedCurrentVersion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1071,7 +1070,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> v.equals(newVersion)).orElse(false)) {
|
||||
if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> Arrays.equals(v, newVersion)).orElse(false)) {
|
||||
throw new WriteConflictException();
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -198,6 +198,11 @@ class AccountsManagerTest {
|
||||
return task.get();
|
||||
}).when(accountLockManager).withLock(anySet(), any(), any());
|
||||
|
||||
doAnswer(invocation -> {
|
||||
final ThrowingSupplier<?, ?> task = invocation.getArgument(1);
|
||||
return task.get();
|
||||
}).when(accountLockManager).withSingleAccountLock(any(Account.class), any(), any());
|
||||
|
||||
final PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager =
|
||||
mock(PhoneNumberRecoveryPasswordsManager.class);
|
||||
|
||||
@@ -678,7 +683,7 @@ class AccountsManagerTest {
|
||||
|
||||
assertTrue(account.getDevice(linkedDevice.getId()).isPresent());
|
||||
|
||||
account = accountsManager.removeDevice(account.getIdentifier(IdentityType.ACI), linkedDevice.getId());
|
||||
account = accountsManager.removeDevice(account.getAccountIdentifier(), linkedDevice.getId());
|
||||
|
||||
assertFalse(account.getDevice(linkedDevice.getId()).isPresent());
|
||||
verify(messagesManager, times(2)).clear(account.getAccountIdentifier(), linkedDevice.getId());
|
||||
@@ -698,7 +703,7 @@ class AccountsManagerTest {
|
||||
when(messagesManager.clear(any(), anyByte())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> accountsManager.removeDevice(account.getIdentifier(IdentityType.ACI), Device.PRIMARY_ID));
|
||||
() -> accountsManager.removeDevice(account.getAccountIdentifier(), Device.PRIMARY_ID));
|
||||
|
||||
assertDoesNotThrow(account::getPrimaryDevice);
|
||||
verify(messagesManager, never()).clear(any(), anyByte());
|
||||
|
||||
Reference in new issue
Block a user