Skip to content

Commit 96e8b4a

Browse files
authored
smp server: wrap all queries in transactions (#1603)
* smp server: wrap all queries in transactions * fix test * fix schema test
1 parent 2cedb66 commit 96e8b4a

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

src/Simplex/Messaging/Server/QueueStore/Postgres.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
142142

143143
getEntityCounts :: PostgresQueueStore q -> IO EntityCounts
144144
getEntityCounts st =
145-
withConnection (dbStore st) $ \db -> do
145+
withTransaction (dbStore st) $ \db -> do
146146
(queueCount, notifierCount, rcvServiceCount, ntfServiceCount, rcvServiceQueuesCount, ntfServiceQueuesCount) : _ <-
147147
DB.query
148148
db
@@ -496,7 +496,7 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
496496

497497
batchInsertServices :: [STMService] -> PostgresQueueStore q -> IO Int64
498498
batchInsertServices services' toStore =
499-
withConnection (dbStore toStore) $ \db ->
499+
withTransaction (dbStore toStore) $ \db ->
500500
DB.executeMany db insertServiceQuery $ map (serviceRecToRow . serviceRec) services'
501501

502502
batchInsertQueues :: StoreQueueClass q => Bool -> M.Map RecipientId q -> PostgresQueueStore q' -> IO Int64
@@ -505,7 +505,7 @@ batchInsertQueues tty queues toStore = do
505505
putStrLn $ "Importing " <> show (length qs) <> " queues..."
506506
let st = dbStore toStore
507507
count <-
508-
withConnection st $ \db -> do
508+
withTransaction st $ \db -> do
509509
DB.copy_
510510
db
511511
[sql|
@@ -514,7 +514,7 @@ batchInsertQueues tty queues toStore = do
514514
|]
515515
mapM_ (putQueue db) (zip [1..] qs)
516516
DB.putCopyEnd db
517-
Only qCnt : _ <- withConnection st (`DB.query_` "SELECT count(*) FROM msg_queues")
517+
Only qCnt : _ <- withTransaction st (`DB.query_` "SELECT count(*) FROM msg_queues")
518518
putStrLn $ progress count
519519
pure qCnt
520520
where
@@ -541,13 +541,13 @@ insertServiceQuery =
541541

542542
foldServiceRecs :: forall a q. Monoid a => PostgresQueueStore q -> (ServiceRec -> IO a) -> IO a
543543
foldServiceRecs st f =
544-
withConnection (dbStore st) $ \db ->
544+
withTransaction (dbStore st) $ \db ->
545545
DB.fold_ db "SELECT service_id, service_role, service_cert, service_cert_hash, created_at FROM services" mempty $
546546
\ !acc -> fmap (acc <>) . f . rowToServiceRec
547547

548548
foldQueueRecs :: forall a q. Monoid a => Bool -> Bool -> PostgresQueueStore q -> Maybe Int64 -> ((RecipientId, QueueRec) -> IO a) -> IO a
549549
foldQueueRecs tty withData st skipOld_ f = do
550-
(n, r) <- withConnection (dbStore st) $ \db ->
550+
(n, r) <- withTransaction (dbStore st) $ \db ->
551551
foldRecs db (0 :: Int, mempty) $ \(i, acc) qr -> do
552552
r <- f qr
553553
let !i' = i + 1
@@ -686,7 +686,7 @@ withDB' op st action = withDB op st $ fmap Right . action
686686

687687
withDB :: forall a q. Text -> PostgresQueueStore q -> (DB.Connection -> IO (Either ErrorType a)) -> ExceptT ErrorType IO a
688688
withDB op st action =
689-
ExceptT $ E.try (withConnection (dbStore st) action) >>= either logErr pure
689+
ExceptT $ E.try (withTransaction (dbStore st) action) >>= either logErr pure
690690
where
691691
logErr :: E.SomeException -> IO (Either ErrorType a)
692692
logErr e = logError ("STORE: " <> err) $> Left (STORE err)

tests/PostgresSchemaDump.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ postgresSchemaDumpTest migrations skipComparisonForDownMigrations testDBOpts@DBO
6565
void $ readCreateProcess (shell cmd) ""
6666
threadDelay 20000
6767
let sed = (if ci then "sed -i" else "sed -i ''")
68-
void $ readCreateProcess (shell $ sed <> " '/^--/d' " <> schemaPath) ""
68+
void $ readCreateProcess (shell $ sed <> " '/^--/d; /^\\\\restrict/d; /^\\\\unrestrict/d' " <> schemaPath) ""
6969
sch <- readFile schemaPath
7070
sch `deepseq` pure sch

tests/ServerTests.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ testTiming =
954954
forM_ timingTests $ \tst ->
955955
it (testName tst) $ \(ATransport t, msType) ->
956956
smpTest2Cfg (cfgMS msType) (mkVersionRange minServerSMPRelayVersion authCmdsSMPVersion) t $ \rh sh ->
957-
testSameTiming rh sh tst
957+
testSameTiming rh sh tst msType
958958
where
959959
testName :: (C.AuthAlg, C.AuthAlg, Int) -> String
960960
testName (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, _) = unwords ["queue key:", show goodKeyAlg, "/ used key:", show badKeyAlg]
@@ -971,11 +971,16 @@ testTiming =
971971
(C.AuthAlg C.SX25519, C.AuthAlg C.SX25519, 200) -- correct key type
972972
]
973973
timeRepeat n = fmap fst . timeItT . forM_ (replicate n ()) . const
974-
similarTime t1 t2
975-
| t1 <= t2 = abs (1 - t1 / t2) < 0.3 -- normally the difference between "no queue" and "wrong key" is less than 5%
976-
| otherwise = similarTime t2 t1
977-
testSameTiming :: forall c. Transport c => THandleSMP c 'TClient -> THandleSMP c 'TClient -> (C.AuthAlg, C.AuthAlg, Int) -> Expectation
978-
testSameTiming rh sh (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, n) = do
974+
similarTime t1 t2 msType
975+
| t1 <= t2 = abs (1 - t1 / t2) < diff
976+
| otherwise = similarTime t2 t1 msType
977+
where
978+
-- normally the difference between "no queue" and "wrong key" is less than 5%, but it's higher on PostgreSQL and on CI
979+
diff = case msType of
980+
ASType SQSPostgres _ -> 0.45
981+
_ -> 0.3
982+
testSameTiming :: forall c. Transport c => THandleSMP c 'TClient -> THandleSMP c 'TClient -> (C.AuthAlg, C.AuthAlg, Int) -> AStoreType -> Expectation
983+
testSameTiming rh sh (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, n) msType = do
979984
g <- C.newRandom
980985
(rPub, rKey) <- atomically $ C.generateAuthKeyPair goodKeyAlg g
981986
(dhPub, dhPriv :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g
@@ -1010,7 +1015,7 @@ testTiming =
10101015
timeNoQueue <- timeRepeat n $ do
10111016
Resp "dabc" _ (ERR AUTH) <- signSendRecv h badKey ("dabc", EntityId "1234", cmd)
10121017
return ()
1013-
let ok = similarTime timeNoQueue timeWrongKey
1018+
let ok = similarTime timeNoQueue timeWrongKey msType
10141019
unless ok . putStrLn . unwords $
10151020
[ show goodKeyAlg,
10161021
show badKeyAlg,

tests/Test.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ main = do
9595
describe "Agent core tests" agentCoreTests
9696
#if defined(dbServerPostgres)
9797
around_ (postgressBracket testServerDBConnectInfo) $
98-
describe "SMP server schema dump" $
98+
fdescribe "SMP server schema dump" $
9999
postgresSchemaDumpTest
100100
serverMigrations
101101
[ "20250320_short_links" -- snd_secure moves to the bottom on down migration
@@ -116,7 +116,7 @@ main = do
116116
-- before (pure (transport @WS, ASType SQSMemory SMSJournal)) serverTests
117117
#if defined(dbServerPostgres)
118118
around_ (postgressBracket ntfTestServerDBConnectInfo) $
119-
describe "Ntf server schema dump" $
119+
fdescribe "Ntf server schema dump" $
120120
postgresSchemaDumpTest
121121
ntfServerMigrations
122122
[] -- skipComparisonForDownMigrations

0 commit comments

Comments
 (0)