Skip to content

Commit

Permalink
Make MessagesManager#clear asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal committed Aug 15, 2023
1 parent 2c835b5 commit ccd8602
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public void removeDevice(@Auth AuthenticatedAccount auth, @PathParam("device_id"

final CompletableFuture<Void> deleteKeysFuture = keys.delete(account.getUuid(), deviceId);

messages.clear(account.getUuid(), deviceId);
messages.clear(account.getUuid(), deviceId).join();
account = accounts.update(account, a -> a.removeDevice(deviceId));
// ensure any messages that came in after the first clear() are also removed
messages.clear(account.getUuid(), deviceId);
messages.clear(account.getUuid(), deviceId).join();

deleteKeysFuture.join();
}
Expand Down Expand Up @@ -419,7 +419,7 @@ private Pair<Account, Device> createDevice(final String password,
keys.delete(a.getUuid(), device.getId()),
keys.delete(a.getPhoneNumberIdentifier(), device.getId()));

messages.clear(a.getUuid(), device.getId());
messages.clear(a.getUuid(), device.getId()).join();

deleteKeysFuture.join();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public Account create(final String number,
keysManager.delete(actualUuid),
keysManager.delete(account.getPhoneNumberIdentifier()));

messagesManager.clear(actualUuid);
messagesManager.clear(actualUuid).join();
profilesManager.deleteAll(actualUuid);

deleteKeysFuture.join();
Expand Down Expand Up @@ -877,12 +877,15 @@ private void delete(final Account account) {
keysManager.delete(account.getUuid()),
keysManager.delete(account.getPhoneNumberIdentifier()));

final CompletableFuture<Void> deleteMessagesFuture = CompletableFuture.allOf(
messagesManager.clear(account.getUuid()),
messagesManager.clear(account.getPhoneNumberIdentifier()));

profilesManager.deleteAll(account.getUuid());
messagesManager.clear(account.getUuid());
messagesManager.clear(account.getPhoneNumberIdentifier());
registrationRecoveryPasswordsManager.removeForNumber(account.getNumber());

deleteKeysFuture.join();
deleteMessagesFuture.join();
deleteStorageServiceDataFuture.join();
deleteBackupServiceDataFuture.join();
deleteSecureValueRecoveryServiceDataFuture.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ private Publisher<Envelope> getMessagesForDevice(UUID destinationUuid, long dest
.tap(Micrometer.metrics(Metrics.globalRegistry));
}

public void clear(UUID destinationUuid) {
messagesCache.clear(destinationUuid).join();
messagesDynamoDb.deleteAllMessagesForAccount(destinationUuid).join();
public CompletableFuture<Void> clear(UUID destinationUuid) {
return CompletableFuture.allOf(
messagesCache.clear(destinationUuid),
messagesDynamoDb.deleteAllMessagesForAccount(destinationUuid));
}

public void clear(UUID destinationUuid, long deviceId) {
messagesCache.clear(destinationUuid, deviceId).join();
messagesDynamoDb.deleteAllMessagesForDevice(destinationUuid, deviceId).join();
public CompletableFuture<Void> clear(UUID destinationUuid, long deviceId) {
return CompletableFuture.allOf(
messagesCache.clear(destinationUuid, deviceId),
messagesDynamoDb.deleteAllMessagesForDevice(destinationUuid, deviceId));
}

public CompletableFuture<Optional<Envelope>> delete(UUID destinationUuid, long destinationDeviceId, UUID guid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void run(final Environment environment, final Namespace namespace,
deps.keysManager().delete(account.getUuid(), deviceId).join();

System.out.format("Clearing additional messages for %s::%d\n", aci, deviceId);
deps.messagesManager().clear(account.getUuid(), deviceId);
deps.messagesManager().clear(account.getUuid(), deviceId).join();

System.out.format("Clearing presence state for %s::%d\n", aci, deviceId);
deps.clientPresenceManager().disconnectPresence(aci, deviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ void setup() {

when(keysManager.storePqLastResort(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
when(keysManager.delete(any(), anyLong())).thenReturn(CompletableFuture.completedFuture(null));

when(messagesManager.clear(any(), anyLong())).thenReturn(CompletableFuture.completedFuture(null));
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ void setup() throws InterruptedException {
final KeysManager keysManager = mock(KeysManager.class);
when(keysManager.delete(any())).thenReturn(CompletableFuture.completedFuture(null));

final MessagesManager messagesManager = mock(MessagesManager.class);
when(messagesManager.clear(any())).thenReturn(CompletableFuture.completedFuture(null));

accountsManager = new AccountsManager(
accounts,
phoneNumberIdentifiers,
CACHE_CLUSTER_EXTENSION.getRedisCluster(),
accountLockManager,
deletedAccounts,
keysManager,
mock(MessagesManager.class),
messagesManager,
mock(ProfilesManager.class),
secureStorageClient,
secureBackupClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ void setup() throws InterruptedException {
}).when(accountLockManager).withLock(any(), any());

when(keysManager.delete(any())).thenReturn(CompletableFuture.completedFuture(null));
when(messagesManager.clear(any())).thenReturn(CompletableFuture.completedFuture(null));

accountsManager = new AccountsManager(
accounts,
Expand Down

0 comments on commit ccd8602

Please sign in to comment.