From 393e15815b59ef49cea21f0a44238ef430c225b5 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Fri, 12 Aug 2022 16:15:33 -0500 Subject: [PATCH] Rename secondary account key namespace for usernames --- .../textsecuregcm/storage/AccountsManager.java | 16 ++++++++++------ .../storage/AccountsManagerTest.java | 16 ++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index 94e173ae2..1faa0ff1e 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -607,6 +607,10 @@ public class AccountsManager { clientPresenceManager.disconnectPresence(account.getUuid(), device.getId()))); } + private String getUsernameAccountMapKey(String key) { + return "UAccountMap::" + key; + } + private String getAccountMapKey(String key) { return "AccountMap::" + key; } @@ -627,7 +631,7 @@ public class AccountsManager { commands.setex(getAccountEntityKey(account.getUuid()), CACHE_TTL_SECONDS, accountJson); 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) { throw new IllegalStateException(e); @@ -635,20 +639,20 @@ public class AccountsManager { } private Optional redisGetByPhoneNumberIdentifier(UUID uuid) { - return redisGetBySecondaryKey(uuid.toString(), redisPniGetTimer); + return redisGetBySecondaryKey(getAccountMapKey(uuid.toString()), redisPniGetTimer); } private Optional redisGetByE164(String e164) { - return redisGetBySecondaryKey(e164, redisNumberGetTimer); + return redisGetBySecondaryKey(getAccountMapKey(e164), redisNumberGetTimer); } private Optional redisGetByUsername(String username) { - return redisGetBySecondaryKey(username, redisUsernameGetTimer); + return redisGetBySecondaryKey(getUsernameAccountMapKey(username), redisUsernameGetTimer); } private Optional redisGetBySecondaryKey(String secondaryKey, Timer timer) { 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)); else return Optional.empty(); @@ -694,7 +698,7 @@ public class AccountsManager { getAccountMapKey(account.getPhoneNumberIdentifier().toString()), getAccountEntityKey(account.getUuid())); - account.getUsername().ifPresent(username -> connection.sync().del(getAccountMapKey(username))); + account.getUsername().ifPresent(username -> connection.sync().del(getUsernameAccountMapKey(username))); }); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java index ceea14f29..918d78183 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -225,7 +225,7 @@ class AccountsManagerTest { UUID uuid = UUID.randomUUID(); 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\"}"); Optional account = accountsManager.getByUsername(username); @@ -235,7 +235,7 @@ class AccountsManagerTest { assertEquals(UUID.fromString("de24dc73-fbd8-41be-a7d5-764c70d9da7e"), account.get().getPhoneNumberIdentifier()); 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)); verifyNoMoreInteractions(commands); @@ -323,7 +323,7 @@ class AccountsManagerTest { Account account = AccountsHelper.generateTestAccount("+14152222222", uuid, UUID.randomUUID(), new ArrayList<>(), new byte[16]); 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)); Optional retrieved = accountsManager.getByUsername(username); @@ -331,8 +331,8 @@ class AccountsManagerTest { assertTrue(retrieved.isPresent()); assertSame(retrieved.get(), account); - verify(commands).get(eq("AccountMap::" + username)); - verify(commands).setex(eq("AccountMap::" + username), anyLong(), eq(uuid.toString())); + verify(commands).get(eq("UAccountMap::" + username)); + 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::+14152222222"), anyLong(), eq(uuid.toString())); 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.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)); Optional retrieved = accountsManager.getByUsername(username); @@ -431,8 +431,8 @@ class AccountsManagerTest { assertTrue(retrieved.isPresent()); assertSame(retrieved.get(), account); - verify(commands).get(eq("AccountMap::" + username)); - verify(commands).setex(eq("AccountMap::" + username), anyLong(), eq(uuid.toString())); + verify(commands).get(eq("UAccountMap::" + username)); + 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::+14152222222"), anyLong(), eq(uuid.toString())); verify(commands).setex(eq("Account3::" + uuid), anyLong(), anyString());