Rename secondary account key namespace for usernames

This commit is contained in:
Ravi Khadiwala 2022-08-12 16:15:33 -05:00 committed by ravi-signal
parent a7f1cd25b9
commit 393e15815b
2 changed files with 18 additions and 14 deletions

View File

@ -607,6 +607,10 @@ public class AccountsManager {
clientPresenceManager.disconnectPresence(account.getUuid(), device.getId()))); clientPresenceManager.disconnectPresence(account.getUuid(), device.getId())));
} }
private String getUsernameAccountMapKey(String key) {
return "UAccountMap::" + key;
}
private String getAccountMapKey(String key) { private String getAccountMapKey(String key) {
return "AccountMap::" + key; return "AccountMap::" + key;
} }
@ -627,7 +631,7 @@ public class AccountsManager {
commands.setex(getAccountEntityKey(account.getUuid()), CACHE_TTL_SECONDS, accountJson); commands.setex(getAccountEntityKey(account.getUuid()), CACHE_TTL_SECONDS, accountJson);
account.getUsername().ifPresent(username -> account.getUsername().ifPresent(username ->
commands.setex(getAccountMapKey(username), CACHE_TTL_SECONDS, account.getUuid().toString())); commands.setex(getUsernameAccountMapKey(username), CACHE_TTL_SECONDS, account.getUuid().toString()));
}); });
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -635,20 +639,20 @@ public class AccountsManager {
} }
private Optional<Account> redisGetByPhoneNumberIdentifier(UUID uuid) { private Optional<Account> redisGetByPhoneNumberIdentifier(UUID uuid) {
return redisGetBySecondaryKey(uuid.toString(), redisPniGetTimer); return redisGetBySecondaryKey(getAccountMapKey(uuid.toString()), redisPniGetTimer);
} }
private Optional<Account> redisGetByE164(String e164) { private Optional<Account> redisGetByE164(String e164) {
return redisGetBySecondaryKey(e164, redisNumberGetTimer); return redisGetBySecondaryKey(getAccountMapKey(e164), redisNumberGetTimer);
} }
private Optional<Account> redisGetByUsername(String username) { private Optional<Account> redisGetByUsername(String username) {
return redisGetBySecondaryKey(username, redisUsernameGetTimer); return redisGetBySecondaryKey(getUsernameAccountMapKey(username), redisUsernameGetTimer);
} }
private Optional<Account> redisGetBySecondaryKey(String secondaryKey, Timer timer) { private Optional<Account> redisGetBySecondaryKey(String secondaryKey, Timer timer) {
try (Timer.Context ignored = timer.time()) { try (Timer.Context ignored = timer.time()) {
final String uuid = cacheCluster.withCluster(connection -> connection.sync().get(getAccountMapKey(secondaryKey))); final String uuid = cacheCluster.withCluster(connection -> connection.sync().get(secondaryKey));
if (uuid != null) return redisGetByAccountIdentifier(UUID.fromString(uuid)); if (uuid != null) return redisGetByAccountIdentifier(UUID.fromString(uuid));
else return Optional.empty(); else return Optional.empty();
@ -694,7 +698,7 @@ public class AccountsManager {
getAccountMapKey(account.getPhoneNumberIdentifier().toString()), getAccountMapKey(account.getPhoneNumberIdentifier().toString()),
getAccountEntityKey(account.getUuid())); getAccountEntityKey(account.getUuid()));
account.getUsername().ifPresent(username -> connection.sync().del(getAccountMapKey(username))); account.getUsername().ifPresent(username -> connection.sync().del(getUsernameAccountMapKey(username)));
}); });
} }
} }

View File

@ -225,7 +225,7 @@ class AccountsManagerTest {
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
String username = "test"; String username = "test";
when(commands.get(eq("AccountMap::" + username))).thenReturn(uuid.toString()); when(commands.get(eq("UAccountMap::" + username))).thenReturn(uuid.toString());
when(commands.get(eq("Account3::" + uuid))).thenReturn("{\"number\": \"+14152222222\", \"pni\": \"de24dc73-fbd8-41be-a7d5-764c70d9da7e\", \"username\": \"test\"}"); when(commands.get(eq("Account3::" + uuid))).thenReturn("{\"number\": \"+14152222222\", \"pni\": \"de24dc73-fbd8-41be-a7d5-764c70d9da7e\", \"username\": \"test\"}");
Optional<Account> account = accountsManager.getByUsername(username); Optional<Account> account = accountsManager.getByUsername(username);
@ -235,7 +235,7 @@ class AccountsManagerTest {
assertEquals(UUID.fromString("de24dc73-fbd8-41be-a7d5-764c70d9da7e"), account.get().getPhoneNumberIdentifier()); assertEquals(UUID.fromString("de24dc73-fbd8-41be-a7d5-764c70d9da7e"), account.get().getPhoneNumberIdentifier());
assertEquals(Optional.of(username), account.get().getUsername()); assertEquals(Optional.of(username), account.get().getUsername());
verify(commands).get(eq("AccountMap::" + username)); verify(commands).get(eq("UAccountMap::" + username));
verify(commands).get(eq("Account3::" + uuid)); verify(commands).get(eq("Account3::" + uuid));
verifyNoMoreInteractions(commands); verifyNoMoreInteractions(commands);
@ -323,7 +323,7 @@ class AccountsManagerTest {
Account account = AccountsHelper.generateTestAccount("+14152222222", uuid, UUID.randomUUID(), new ArrayList<>(), new byte[16]); Account account = AccountsHelper.generateTestAccount("+14152222222", uuid, UUID.randomUUID(), new ArrayList<>(), new byte[16]);
account.setUsername(username); account.setUsername(username);
when(commands.get(eq("AccountMap::" + username))).thenReturn(null); when(commands.get(eq("UAccountMap::" + username))).thenReturn(null);
when(accounts.getByUsername(username)).thenReturn(Optional.of(account)); when(accounts.getByUsername(username)).thenReturn(Optional.of(account));
Optional<Account> retrieved = accountsManager.getByUsername(username); Optional<Account> retrieved = accountsManager.getByUsername(username);
@ -331,8 +331,8 @@ class AccountsManagerTest {
assertTrue(retrieved.isPresent()); assertTrue(retrieved.isPresent());
assertSame(retrieved.get(), account); assertSame(retrieved.get(), account);
verify(commands).get(eq("AccountMap::" + username)); verify(commands).get(eq("UAccountMap::" + username));
verify(commands).setex(eq("AccountMap::" + username), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("UAccountMap::" + username), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("AccountMap::" + account.getPhoneNumberIdentifier()), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("AccountMap::" + account.getPhoneNumberIdentifier()), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("AccountMap::+14152222222"), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("AccountMap::+14152222222"), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("Account3::" + uuid), anyLong(), anyString()); verify(commands).setex(eq("Account3::" + uuid), anyLong(), anyString());
@ -423,7 +423,7 @@ class AccountsManagerTest {
Account account = AccountsHelper.generateTestAccount("+14152222222", uuid, UUID.randomUUID(), new ArrayList<>(), new byte[16]); Account account = AccountsHelper.generateTestAccount("+14152222222", uuid, UUID.randomUUID(), new ArrayList<>(), new byte[16]);
account.setUsername(username); account.setUsername(username);
when(commands.get(eq("AccountMap::" + username))).thenThrow(new RedisException("OH NO")); when(commands.get(eq("UAccountMap::" + username))).thenThrow(new RedisException("OH NO"));
when(accounts.getByUsername(username)).thenReturn(Optional.of(account)); when(accounts.getByUsername(username)).thenReturn(Optional.of(account));
Optional<Account> retrieved = accountsManager.getByUsername(username); Optional<Account> retrieved = accountsManager.getByUsername(username);
@ -431,8 +431,8 @@ class AccountsManagerTest {
assertTrue(retrieved.isPresent()); assertTrue(retrieved.isPresent());
assertSame(retrieved.get(), account); assertSame(retrieved.get(), account);
verify(commands).get(eq("AccountMap::" + username)); verify(commands).get(eq("UAccountMap::" + username));
verify(commands).setex(eq("AccountMap::" + username), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("UAccountMap::" + username), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("AccountMap::" + account.getPhoneNumberIdentifier()), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("AccountMap::" + account.getPhoneNumberIdentifier()), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("AccountMap::+14152222222"), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("AccountMap::+14152222222"), anyLong(), eq(uuid.toString()));
verify(commands).setex(eq("Account3::" + uuid), anyLong(), anyString()); verify(commands).setex(eq("Account3::" + uuid), anyLong(), anyString());