mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:36:53 +00:00
Remove asynchronous account update plumbing
This commit is contained in:
1 parent
de458c3e6a
commit
31de1fc1a3
8 files changed
+37
-191
No files matched your search
@@ -4,13 +4,12 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Throwables;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import java.io.IOException;
|
||||
@@ -30,7 +29,6 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -70,6 +68,7 @@ import software.amazon.awssdk.services.dynamodb.model.TransactionCanceledExcepti
|
||||
import software.amazon.awssdk.services.dynamodb.model.TransactionConflictException;
|
||||
import software.amazon.awssdk.services.dynamodb.model.Update;
|
||||
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
|
||||
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;
|
||||
import software.amazon.awssdk.services.dynamodb.paginators.ScanPublisher;
|
||||
import software.amazon.awssdk.utils.CompletableFutureUtils;
|
||||
|
||||
@@ -990,53 +989,28 @@ public class Accounts {
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CompletionStage<Void> updateAsync(final Account account) {
|
||||
return AsyncTimerUtil.record(UPDATE_TIMER, () -> {
|
||||
final UpdateItemRequest updateItemRequest = UpdateAccountSpec
|
||||
.forAccount(accountsTableName, account)
|
||||
.updateItemRequest();
|
||||
|
||||
return dynamoDbAsyncClient.updateItem(updateItemRequest)
|
||||
.thenApply(response -> {
|
||||
account.setVersion(AttributeValues.getInt(response.attributes(), "V", account.getVersion() + 1));
|
||||
return (Void) null;
|
||||
})
|
||||
.exceptionallyCompose(throwable -> {
|
||||
final Throwable unwrapped = ExceptionUtils.unwrap(throwable);
|
||||
if (unwrapped instanceof TransactionConflictException) {
|
||||
throw new ContestedOptimisticLockException();
|
||||
} else if (unwrapped instanceof ConditionalCheckFailedException e) {
|
||||
// the exception doesn't give details about which condition failed,
|
||||
// but we can infer it was an optimistic locking failure if the UUID is known
|
||||
return getByAccountIdentifierAsync(account.getUuid())
|
||||
.thenAccept(refreshedAccount -> {
|
||||
throw refreshedAccount.isPresent() ? new ContestedOptimisticLockException() : e;
|
||||
});
|
||||
} else {
|
||||
// rethrow
|
||||
throw CompletableFutureUtils.errorAsCompletionException(throwable);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static void joinAndUnwrapUpdateFuture(CompletionStage<Void> future) {
|
||||
try {
|
||||
future.toCompletableFuture().join();
|
||||
} catch (final CompletionException e) {
|
||||
// unwrap CompletionExceptions, throw as long is it's unchecked
|
||||
Throwables.throwIfUnchecked(ExceptionUtils.unwrap(e));
|
||||
|
||||
// if we otherwise somehow got a wrapped checked exception,
|
||||
// rethrow the checked exception wrapped by the original CompletionException
|
||||
log.error("Unexpected checked exception thrown from dynamo update", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void update(final Account account) throws ContestedOptimisticLockException {
|
||||
joinAndUnwrapUpdateFuture(updateAsync(account));
|
||||
final Timer.Sample sample = Timer.start();
|
||||
|
||||
try {
|
||||
final UpdateItemResponse response = dynamoDbClient.updateItem(UpdateAccountSpec
|
||||
.forAccount(accountsTableName, account)
|
||||
.updateItemRequest());
|
||||
|
||||
account.setVersion(AttributeValues.getInt(response.attributes(), "V", account.getVersion() + 1));
|
||||
} catch (final TransactionConflictException _) {
|
||||
throw new ContestedOptimisticLockException();
|
||||
} catch (final ConditionalCheckFailedException e) {
|
||||
// the exception doesn't give details about which condition failed,
|
||||
// but we can infer it was an optimistic locking failure if the UUID is known
|
||||
if (getByAccountIdentifier(account.getUuid()).isPresent()) {
|
||||
throw new ContestedOptimisticLockException();
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
sample.stop(UPDATE_TIMER);
|
||||
}
|
||||
}
|
||||
|
||||
public CompletionStage<Void> updateTransactionallyAsync(final Account account,
|
||||
|
||||
@@ -45,7 +45,6 @@ import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -947,14 +946,6 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Account> updateAsync(Account account, Consumer<Account> updater) {
|
||||
return updateAsync(account, a -> {
|
||||
updater.accept(a);
|
||||
// assume that all updaters passed to the public method actually modify the account
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialized version of {@link #updateDevice(Account, byte, Consumer)} that minimizes potentially contentious and
|
||||
* redundant updates of {@code device.lastSeen}
|
||||
@@ -1006,25 +997,6 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
});
|
||||
}
|
||||
|
||||
private CompletableFuture<Account> updateAsync(final Account account, final Function<Account, Boolean> updater) {
|
||||
|
||||
final Timer.Sample timerSample = Timer.start();
|
||||
|
||||
return redisDeleteAsync(account)
|
||||
.thenCompose(ignored -> {
|
||||
final UUID uuid = account.getUuid();
|
||||
|
||||
return updateWithRetriesAsync(account,
|
||||
updater,
|
||||
a -> accounts.updateAsync(a).toCompletableFuture(),
|
||||
() -> accounts.getByAccountIdentifierAsync(uuid).thenApply(Optional::orElseThrow),
|
||||
AccountChangeValidator.GENERAL_CHANGE_VALIDATOR,
|
||||
MAX_UPDATE_ATTEMPTS);
|
||||
})
|
||||
.thenCompose(updatedAccount -> redisSetAsync(updatedAccount).thenApply(ignored -> updatedAccount))
|
||||
.whenComplete((_, _) -> timerSample.stop(updateTimer));
|
||||
}
|
||||
|
||||
private <E extends Exception> Account updateWithRetries(Account account,
|
||||
final Function<Account, Boolean> updater,
|
||||
final ThrowingConsumer<Account, E> persister,
|
||||
@@ -1066,42 +1038,6 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
throw new OptimisticLockRetryLimitExceededException();
|
||||
}
|
||||
|
||||
private CompletionStage<Account> updateWithRetriesAsync(Account account,
|
||||
final Function<Account, Boolean> updater,
|
||||
final Function<Account, CompletionStage<Void>> persister,
|
||||
final Supplier<CompletionStage<Account>> retriever,
|
||||
final AccountChangeValidator changeValidator,
|
||||
final int remainingTries) {
|
||||
|
||||
final Account originalAccount = AccountUtil.cloneAccountAsNotStale(account);
|
||||
|
||||
if (!updater.apply(account)) {
|
||||
return CompletableFuture.completedFuture(account);
|
||||
}
|
||||
|
||||
if (remainingTries > 0) {
|
||||
return persister.apply(account)
|
||||
.thenApply(ignored -> {
|
||||
final Account updatedAccount = AccountUtil.cloneAccountAsNotStale(account);
|
||||
account.markStale();
|
||||
|
||||
changeValidator.validateChange(originalAccount, updatedAccount);
|
||||
|
||||
return updatedAccount;
|
||||
})
|
||||
.exceptionallyCompose(throwable -> {
|
||||
if (ExceptionUtils.unwrap(throwable) instanceof ContestedOptimisticLockException) {
|
||||
return retriever.get().thenCompose(refreshedAccount ->
|
||||
updateWithRetriesAsync(refreshedAccount, updater, persister, retriever, changeValidator, remainingTries - 1));
|
||||
} else {
|
||||
throw ExceptionUtils.wrap(throwable);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return CompletableFuture.failedFuture(new OptimisticLockRetryLimitExceededException());
|
||||
}
|
||||
|
||||
public Account updateDevice(Account account, byte deviceId, Consumer<Device> deviceUpdater) {
|
||||
return update(account, a -> {
|
||||
a.getDevice(deviceId).ifPresent(deviceUpdater);
|
||||
|
||||
+4
-3
@@ -22,9 +22,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import reactor.util.retry.Retry;
|
||||
|
||||
public class RemoveExpiredUsernameHoldsCommand extends AbstractSinglePassCrawlAccountsCommand {
|
||||
@@ -85,8 +85,9 @@ public class RemoveExpiredUsernameHoldsCommand extends AbstractSinglePassCrawlAc
|
||||
final int holdsToRemove = removeExpired(holds);
|
||||
final Mono<Void> purgeMono = isDryRun || holdsToRemove == 0
|
||||
? Mono.empty()
|
||||
: Mono.fromFuture(() ->
|
||||
accountManager.updateAsync(account, a -> a.setUsernameHolds(holds)).thenRun(Util.NOOP));
|
||||
: Mono.fromRunnable(() -> accountManager.update(account, a -> a.setUsernameHolds(holds)))
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.then();
|
||||
Metrics.counter(INSPECTED_ACCOUNTS_COUNTER_NAME,
|
||||
"dryRun", String.valueOf(isDryRun),
|
||||
"expiredHolds", String.valueOf(holdsToRemove > 0))
|
||||
|
||||
Reference in new issue
Block a user