From 7984fcd01c83995fa65422d7178b506131454243 Mon Sep 17 00:00:00 2001 From: fisx Date: Wed, 26 May 2021 17:14:32 +0200 Subject: [PATCH] Fix slow cassandra query. (#1530) --- services/galley/src/Galley/Data.hs | 5 +++-- services/galley/src/Galley/Data/Queries.hs | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/services/galley/src/Galley/Data.hs b/services/galley/src/Galley/Data.hs index 126456b6bd5..5e446966696 100644 --- a/services/galley/src/Galley/Data.hs +++ b/services/galley/src/Galley/Data.hs @@ -148,6 +148,7 @@ import Imports hiding (Set, max) import System.Logger.Class (MonadLogger) import qualified System.Logger.Class as Log import UnliftIO (async, mapConcurrently, wait) +import UnliftIO.Async (pooledMapConcurrentlyN) import Wire.API.Team.Member -- We use this newtype to highlight the fact that the 'Page' wrapped in here @@ -310,9 +311,9 @@ userTeams u = map runIdentity <$> retry x1 (query Cql.selectUserTeams (params Quorum (Identity u))) -usersTeams :: MonadClient m => [UserId] -> m (Map UserId TeamId) +usersTeams :: (MonadUnliftIO m, MonadClient m) => [UserId] -> m (Map UserId TeamId) usersTeams uids = do - pairs <- retry x1 (query Cql.selectUsersTeams (params Quorum (Identity uids))) + pairs :: [(UserId, TeamId)] <- catMaybes <$> pooledMapConcurrentlyN 8 (\uid -> (uid,) <$$> oneUserTeam uid) uids pure $ foldl' (\m (k, v) -> Map.insert k v m) Map.empty pairs oneUserTeam :: MonadClient m => UserId -> m (Maybe TeamId) diff --git a/services/galley/src/Galley/Data/Queries.hs b/services/galley/src/Galley/Data/Queries.hs index bfe1b284b9d..00916aa20fe 100644 --- a/services/galley/src/Galley/Data/Queries.hs +++ b/services/galley/src/Galley/Data/Queries.hs @@ -135,9 +135,6 @@ selectUserTeamsIn = "select team from user_team where user = ? and team in ? ord selectUserTeamsFrom :: PrepQuery R (UserId, TeamId) (Identity TeamId) selectUserTeamsFrom = "select team from user_team where user = ? and team > ? order by team" -selectUsersTeams :: PrepQuery R (Identity [UserId]) (UserId, TeamId) -selectUsersTeams = "select user, team from user_team where user in ?" - insertTeam :: PrepQuery W (TeamId, UserId, Text, Text, Maybe Text, TeamStatus, TeamBinding) () insertTeam = "insert into team (team, creator, name, icon, icon_key, deleted, status, binding) values (?, ?, ?, ?, ?, false, ?, ?)"