Skip to content

Commit 850d2fa

Browse files
authored
ntf server: PostgreSQL database storage (#1519)
* ntf server: PostgreSQL database storage * ntf server: import/export stubs * ntf server postgres db functions * some notifications tests pass * notifications tests pass * import/export notification store logs * fix ntf server CLI * log in parralel * update subscription statuses using executeMany * fix import/export * refactor * fix queries * prohibit token_id and subscription_id updates, dont update tokens and subscriptions on conflict, improve server insertion, remove duplicate tokens for import, remove subscriptions without tokens for import * comment * remame * increase test delay
1 parent ec5a604 commit 850d2fa

File tree

29 files changed

+1809
-738
lines changed

29 files changed

+1809
-738
lines changed

simplexmq.cabal

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,6 @@ library
216216
Simplex.FileTransfer.Server.Stats
217217
Simplex.FileTransfer.Server.Store
218218
Simplex.FileTransfer.Server.StoreLog
219-
Simplex.Messaging.Notifications.Server
220-
Simplex.Messaging.Notifications.Server.Control
221-
Simplex.Messaging.Notifications.Server.Env
222-
Simplex.Messaging.Notifications.Server.Main
223-
Simplex.Messaging.Notifications.Server.Push.APNS
224-
Simplex.Messaging.Notifications.Server.Push.APNS.Internal
225-
Simplex.Messaging.Notifications.Server.Stats
226-
Simplex.Messaging.Notifications.Server.Store
227-
Simplex.Messaging.Notifications.Server.StoreLog
228219
Simplex.Messaging.Server
229220
Simplex.Messaging.Server.CLI
230221
Simplex.Messaging.Server.Control
@@ -257,6 +248,18 @@ library
257248

258249
if flag(server_postgres)
259250
exposed-modules:
251+
Simplex.Messaging.Notifications.Server
252+
Simplex.Messaging.Notifications.Server.Control
253+
Simplex.Messaging.Notifications.Server.Env
254+
Simplex.Messaging.Notifications.Server.Main
255+
Simplex.Messaging.Notifications.Server.Push.APNS
256+
Simplex.Messaging.Notifications.Server.Push.APNS.Internal
257+
Simplex.Messaging.Notifications.Server.Stats
258+
Simplex.Messaging.Notifications.Server.Store
259+
Simplex.Messaging.Notifications.Server.Store.Migrations
260+
Simplex.Messaging.Notifications.Server.Store.Postgres
261+
Simplex.Messaging.Notifications.Server.Store.Types
262+
Simplex.Messaging.Notifications.Server.StoreLog
260263
Simplex.Messaging.Server.QueueStore.Postgres
261264
Simplex.Messaging.Server.QueueStore.Postgres.Migrations
262265
other-modules:
@@ -340,6 +343,8 @@ library
340343
, sqlcipher-simple ==0.4.*
341344
if flag(server_postgres)
342345
cpp-options: -DdbServerPostgres
346+
build-depends:
347+
hex-text ==0.1.*
343348
if impl(ghc >= 9.6.2)
344349
build-depends:
345350
bytestring ==0.11.*
@@ -352,6 +357,10 @@ library
352357
executable ntf-server
353358
if flag(client_library)
354359
buildable: False
360+
if flag(server_postgres)
361+
cpp-options: -DdbServerPostgres
362+
else
363+
buildable: False
355364
main-is: Main.hs
356365
other-modules:
357366
Paths_simplexmq
@@ -444,7 +453,6 @@ test-suite simplexmq-test
444453
AgentTests.EqInstances
445454
AgentTests.FunctionalAPITests
446455
AgentTests.MigrationTests
447-
AgentTests.NotificationTests
448456
AgentTests.ServerChoice
449457
AgentTests.ShortLinkTests
450458
CLITests
@@ -460,8 +468,6 @@ test-suite simplexmq-test
460468
CoreTests.UtilTests
461469
CoreTests.VersionRangeTests
462470
FileDescriptionTests
463-
NtfClient
464-
NtfServerTests
465471
RemoteControl
466472
ServerTests
467473
SMPAgentClient
@@ -484,6 +490,9 @@ test-suite simplexmq-test
484490
AgentTests.SQLiteTests
485491
if flag(server_postgres)
486492
other-modules:
493+
AgentTests.NotificationTests
494+
NtfClient
495+
NtfServerTests
487496
ServerTests.SchemaDump
488497
hs-source-dirs:
489498
tests
@@ -537,6 +546,8 @@ test-suite simplexmq-test
537546
, warp-tls
538547
, yaml
539548
default-language: Haskell2010
549+
if flag(server_postgres)
550+
cpp-options: -DdbServerPostgres
540551
if flag(client_postgres)
541552
cpp-options: -DdbPostgres
542553
else
@@ -550,5 +561,3 @@ test-suite simplexmq-test
550561
if flag(client_postgres) || flag(server_postgres)
551562
build-depends:
552563
postgresql-simple ==0.7.*
553-
if flag(server_postgres)
554-
cpp-options: -DdbServerPostgres

src/Simplex/Messaging/Client/Agent.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,22 @@ removeSubscription :: SMPClientAgent -> SMPServer -> SMPSub -> STM ()
412412
removeSubscription = removeSub_ . srvSubs
413413
{-# INLINE removeSubscription #-}
414414

415+
removePendingSub :: SMPClientAgent -> SMPServer -> SMPSub -> STM ()
416+
removePendingSub = removeSub_ . pendingSrvSubs
417+
{-# INLINE removePendingSub #-}
418+
415419
removeSub_ :: TMap SMPServer (TMap SMPSub s) -> SMPServer -> SMPSub -> STM ()
416420
removeSub_ subs srv s = TM.lookup srv subs >>= mapM_ (TM.delete s)
417421

422+
removeSubscriptions :: SMPClientAgent -> SMPServer -> SMPSubParty -> [QueueId] -> STM ()
423+
removeSubscriptions = removeSubs_ . srvSubs
424+
{-# INLINE removeSubscriptions #-}
425+
418426
removePendingSubs :: SMPClientAgent -> SMPServer -> SMPSubParty -> [QueueId] -> STM ()
419427
removePendingSubs = removeSubs_ . pendingSrvSubs
420428
{-# INLINE removePendingSubs #-}
421429

422-
removeSubs_ :: TMap SMPServer (TMap SMPSub C.APrivateAuthKey) -> SMPServer -> SMPSubParty -> [QueueId] -> STM ()
430+
removeSubs_ :: TMap SMPServer (TMap SMPSub s) -> SMPServer -> SMPSubParty -> [QueueId] -> STM ()
423431
removeSubs_ subs srv party qs = TM.lookup srv subs >>= mapM_ (`modifyTVar'` (`M.withoutKeys` ss))
424432
where
425433
ss = S.fromList $ map (party,) qs

src/Simplex/Messaging/Encoding.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ instance Encoding Large where
143143
instance Encoding SystemTime where
144144
smpEncode = smpEncode . systemSeconds
145145
{-# INLINE smpEncode #-}
146-
smpP = MkSystemTime <$> smpP <*> pure 0
146+
smpP = (`MkSystemTime` 0) <$> smpP
147147
{-# INLINE smpP #-}
148148

149149
_smpP :: Encoding a => Parser a

src/Simplex/Messaging/Encoding/String.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ instance StrEncoding Int64 where
140140

141141
instance StrEncoding SystemTime where
142142
strEncode = strEncode . systemSeconds
143-
strP = MkSystemTime <$> strP <*> pure 0
143+
strP = (`MkSystemTime` 0) <$> strP
144144

145145
instance StrEncoding UTCTime where
146146
strEncode = B.pack . iso8601Show

src/Simplex/Messaging/Notifications/Protocol.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,11 @@ instance Encoding NtfSubStatus where
517517

518518
instance StrEncoding NtfSubStatus where
519519
strEncode = smpEncode
520+
{-# INLINE strEncode #-}
520521
strP = smpP
522+
{-# INLINE strP #-}
521523

524+
-- TODO [ntfdb] check what happens in agent when token in not yet registered
522525
data NtfTknStatus
523526
= -- | Token created in DB
524527
NTNew
@@ -534,6 +537,17 @@ data NtfTknStatus
534537
NTExpired
535538
deriving (Eq, Show)
536539

540+
allowNtfSubCommands :: NtfTknStatus -> Bool
541+
allowNtfSubCommands = \case
542+
NTNew -> False
543+
NTRegistered -> False
544+
-- TODO [ntfdb] we could have separate statuses to show whether it became invalid
545+
-- after verification (allow commands) or before (do not allow)
546+
NTInvalid _ -> True
547+
NTConfirmed -> False
548+
NTActive -> True
549+
NTExpired -> True
550+
537551
instance Encoding NtfTknStatus where
538552
smpEncode = \case
539553
NTNew -> "NEW"

0 commit comments

Comments
 (0)