Skip to content

Commit

Permalink
Clear account records from the account cache after username operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal committed Feb 20, 2024
1 parent 1bebceb commit 4aa4246
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ public CompletableFuture<UsernameReservation> reserveUsernameHash(final Account
() -> accounts.getByAccountIdentifierAsync(account.getUuid()).thenApply(Optional::orElseThrow),
AccountChangeValidator.USERNAME_CHANGE_VALIDATOR,
MAX_UPDATE_ATTEMPTS))
.whenComplete((updatedAccount, throwable) -> {
if (throwable == null) {
// Make a best effort to clear any stale data that may have been cached while this operation was in progress
redisDeleteAsync(updatedAccount);
}
})
.thenApply(updatedAccount -> new UsernameReservation(updatedAccount, reservedUsernameHash.get()));
}

Expand Down Expand Up @@ -623,7 +629,13 @@ public CompletableFuture<Account> confirmReservedUsernameHash(final Account acco
() -> accounts.getByAccountIdentifierAsync(account.getUuid()).thenApply(Optional::orElseThrow),
AccountChangeValidator.USERNAME_CHANGE_VALIDATOR,
MAX_UPDATE_ATTEMPTS
));
))
.whenComplete((updatedAccount, throwable) -> {
if (throwable == null) {
// Make a best effort to clear any stale data that may have been cached while this operation was in progress
redisDeleteAsync(updatedAccount);
}
});
}

public CompletableFuture<Account> clearUsernameHash(final Account account) {
Expand All @@ -634,7 +646,13 @@ public CompletableFuture<Account> clearUsernameHash(final Account account) {
accounts::clearUsernameHash,
() -> accounts.getByAccountIdentifierAsync(account.getUuid()).thenApply(Optional::orElseThrow),
AccountChangeValidator.USERNAME_CHANGE_VALIDATOR,
MAX_UPDATE_ATTEMPTS));
MAX_UPDATE_ATTEMPTS))
.whenComplete((updatedAccount, throwable) -> {
if (throwable == null) {
// Make a best effort to clear any stale data that may have been cached while this operation was in progress
redisDeleteAsync(updatedAccount);
}
});
}

public Account update(Account account, Consumer<Account> updater) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ void testPniPqUpdate_incompleteKeys() {
}

@Test
void testReserveUsernameHash() throws UsernameHashNotAvailableException {
void testReserveUsernameHash() {
final Account account = AccountsHelper.generateTestAccount("+18005551234", UUID.randomUUID(), UUID.randomUUID(), new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
when(accounts.getByAccountIdentifierAsync(account.getUuid())).thenReturn(CompletableFuture.completedFuture(Optional.of(account)));

Expand All @@ -1359,7 +1359,7 @@ void testReserveUsernameHash() throws UsernameHashNotAvailableException {
}

@Test
void testReserveOwnUsernameHash() throws UsernameHashNotAvailableException {
void testReserveOwnUsernameHash() {
final byte[] oldUsernameHash = TestRandomUtil.nextBytes(32);
final Account account = AccountsHelper.generateTestAccount("+18005551234", UUID.randomUUID(), UUID.randomUUID(), new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
account.setUsernameHash(oldUsernameHash);
Expand All @@ -1373,7 +1373,7 @@ void testReserveOwnUsernameHash() throws UsernameHashNotAvailableException {
}

@Test
void testReserveUsernameOptimisticLockingFailure() throws UsernameHashNotAvailableException {
void testReserveUsernameOptimisticLockingFailure() {
final Account account = AccountsHelper.generateTestAccount("+18005551234", UUID.randomUUID(), UUID.randomUUID(), new ArrayList<>(), new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
when(accounts.getByAccountIdentifierAsync(account.getUuid())).thenReturn(CompletableFuture.completedFuture(Optional.of(account)));

Expand Down

0 comments on commit 4aa4246

Please sign in to comment.