mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:36:53 +00:00
Remove AccountLockManager#withLockAsync
This commit is contained in:
1 parent
ec2cb8581c
commit
d519a80f90
7 files changed
+47
-146
No files matched your search
+6
-46
@@ -10,12 +10,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import org.whispersystems.textsecuregcm.util.ThrowingSupplier;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
||||
|
||||
public class AccountLockManager {
|
||||
@@ -54,11 +53,11 @@ public class AccountLockManager {
|
||||
*
|
||||
* @return the value returned by the given {@code task}
|
||||
*
|
||||
* @throws Exception if an exception is thrown by the given {@code task}
|
||||
* @throws E if an exception is thrown by the given {@code task}
|
||||
*/
|
||||
public <V> V withLock(final Set<UUID> phoneNumberIdentifiers,
|
||||
final Callable<V> task,
|
||||
final Executor lockAcquisitionExecutor) throws Exception {
|
||||
public <V, E extends Exception> V withLock(final Set<UUID> phoneNumberIdentifiers,
|
||||
final ThrowingSupplier<V, E> task,
|
||||
final Executor lockAcquisitionExecutor) throws E {
|
||||
|
||||
if (phoneNumberIdentifiers.isEmpty()) {
|
||||
throw new IllegalArgumentException("List of PNIs to lock must not be empty");
|
||||
@@ -82,7 +81,7 @@ public class AccountLockManager {
|
||||
}
|
||||
}, lockAcquisitionExecutor).join();
|
||||
|
||||
return task.call();
|
||||
return task.get();
|
||||
} finally {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
for (final LockItem lockItem : lockItems) {
|
||||
@@ -93,43 +92,4 @@ public class AccountLockManager {
|
||||
}, lockAcquisitionExecutor).join();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Acquires a distributed, pessimistic lock for the accounts identified by the given phone number identifiers. By
|
||||
* design, the accounts need not actually exist in order to acquire a lock; this allows lock acquisition for
|
||||
* operations that span account lifecycle changes (like deleting an account or changing a phone number). The given
|
||||
* task runs once locks for all given identifiers have been acquired, and the locks are released as soon as the task
|
||||
* completes by any means.
|
||||
*
|
||||
* @param phoneNumberIdentifiers the phone number identifiers for which to acquire a distributed, pessimistic lock
|
||||
* @param taskSupplier a supplier for the task to execute once locks have been acquired
|
||||
* @param executor the executor on which to acquire and release locks
|
||||
* @return a future that completes normally when the given task has executed successfully and all locks have been
|
||||
* released; the returned future may fail with an {@link InterruptedException} if interrupted while acquiring a lock
|
||||
*/
|
||||
public <T> CompletableFuture<T> withLockAsync(final Set<UUID> phoneNumberIdentifiers,
|
||||
final Supplier<CompletableFuture<T>> taskSupplier, final Executor executor) {
|
||||
|
||||
if (phoneNumberIdentifiers.isEmpty()) {
|
||||
throw new IllegalArgumentException("List of PNIs to lock must not be empty");
|
||||
}
|
||||
|
||||
final List<LockItem> lockItems = new ArrayList<>(phoneNumberIdentifiers.size());
|
||||
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
for (final UUID pni : phoneNumberIdentifiers) {
|
||||
try {
|
||||
lockItems.add(lockClient.acquireLock(AcquireLockOptions.builder(pni.toString())
|
||||
.withAcquireReleasedLocksConsistently(true)
|
||||
.build()));
|
||||
} catch (final InterruptedException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
}
|
||||
}, executor)
|
||||
.thenCompose(ignored -> taskSupplier.get())
|
||||
.whenCompleteAsync((ignored, throwable) -> lockItems.forEach(lockItem -> lockClient.releaseLock(ReleaseLockOptions.builder(lockItem)
|
||||
.withBestEffort(true)
|
||||
.build())), executor);
|
||||
}
|
||||
}
|
||||
+12
-37
@@ -298,13 +298,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
try {
|
||||
return accountLockManager.withLock(Set.of(pni),
|
||||
() -> create(number, pni, accountAttributes, accountBadges, aciIdentityKey, pniIdentityKey, primaryDeviceSpec, userAgent), accountLockExecutor);
|
||||
} catch (final Exception e) {
|
||||
if (e instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
|
||||
} catch (final RuntimeException e) {
|
||||
logger.error("Unexpected exception while creating account", e);
|
||||
throw new RuntimeException(e);
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -442,15 +438,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
public Pair<Account, Device> addDevice(final Account account, final DeviceSpec deviceSpec, final String linkDeviceToken)
|
||||
throws LinkDeviceTokenAlreadyUsedException {
|
||||
|
||||
try {
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()),
|
||||
() -> addDevice(account.getIdentifier(IdentityType.ACI), deviceSpec, linkDeviceToken, MAX_UPDATE_ATTEMPTS),
|
||||
accountLockExecutor);
|
||||
} catch (final LinkDeviceTokenAlreadyUsedException | RuntimeException e) {
|
||||
throw e;
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()),
|
||||
() -> addDevice(account.getIdentifier(IdentityType.ACI), deviceSpec, linkDeviceToken, MAX_UPDATE_ATTEMPTS),
|
||||
accountLockExecutor);
|
||||
}
|
||||
|
||||
private Pair<Account, Device> addDevice(final UUID accountIdentifier, final DeviceSpec deviceSpec, final String linkDeviceToken, final int retries)
|
||||
@@ -639,13 +629,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
throw new IllegalArgumentException("Cannot remove primary device");
|
||||
}
|
||||
|
||||
try {
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()),
|
||||
() -> removeDevice(account.getIdentifier(IdentityType.ACI), deviceId, MAX_UPDATE_ATTEMPTS),
|
||||
accountLockExecutor);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()),
|
||||
() -> removeDevice(account.getIdentifier(IdentityType.ACI), deviceId, MAX_UPDATE_ATTEMPTS),
|
||||
accountLockExecutor);
|
||||
}
|
||||
|
||||
private Account removeDevice(final UUID accountIdentifier, final byte deviceId, final int retries) {
|
||||
@@ -700,15 +686,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
try {
|
||||
return accountLockManager.withLock(new HashSet<>(List.of(account.getPhoneNumberIdentifier(), targetPhoneNumberIdentifier)),
|
||||
() -> changeNumber(account, targetNumber, targetPhoneNumberIdentifier, pniIdentityKey, pniSignedPreKeys, pniPqLastResortPreKeys, pniRegistrationIds), accountLockExecutor);
|
||||
} catch (final Exception e) {
|
||||
if (e instanceof MismatchedDevicesException mismatchedDevicesException) {
|
||||
throw mismatchedDevicesException;
|
||||
} if (e instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
|
||||
} catch (final RuntimeException e) {
|
||||
logger.error("Unexpected exception when changing phone number", e);
|
||||
throw new RuntimeException(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1146,14 +1126,9 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
COUNTRY_CODE_TAG_NAME, Util.getCountryCode(account.getNumber()),
|
||||
DELETION_REASON_TAG_NAME, deletionReason.tagValue)
|
||||
.increment();
|
||||
} catch (final Exception e) {
|
||||
} catch (final RuntimeException e) {
|
||||
logger.warn("Failed to delete account", e);
|
||||
|
||||
if (e instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
|
||||
throw new RuntimeException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
sample.stop(deleteTimer);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
/// Represents a supplier of results that may throw an exception.
|
||||
///
|
||||
/// There is no requirement that a new or distinct result be returned each time the supplier is invoked.
|
||||
@FunctionalInterface
|
||||
public interface ThrowingSupplier<T, E extends Exception> {
|
||||
|
||||
/// Gets a result, potentially throwing an exception.
|
||||
///
|
||||
/// @return a result
|
||||
///
|
||||
/// @throws E at the discretion of the implementation
|
||||
T get() throws E;
|
||||
}
|
||||
Reference in new issue
Block a user