Skip to content

Commit fd190d1

Browse files
committed
parameterize journal store for queue storage
1 parent 79e9447 commit fd190d1

File tree

10 files changed

+246
-165
lines changed

10 files changed

+246
-165
lines changed

src/Simplex/Messaging/Server.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import Simplex.Messaging.Server.MsgStore.Types
100100
import Simplex.Messaging.Server.NtfStore
101101
import Simplex.Messaging.Server.QueueStore
102102
import Simplex.Messaging.Server.QueueStore.QueueInfo
103-
import Simplex.Messaging.Server.QueueStore.STM
104103
import Simplex.Messaging.Server.Stats
105104
import Simplex.Messaging.TMap (TMap)
106105
import qualified Simplex.Messaging.TMap as TM
@@ -841,7 +840,7 @@ runClientTransport h@THandle {params = thParams@THandleParams {thVersion, sessio
841840
c <- liftIO $ newClient msType clientId q thVersion sessionId ts
842841
runClientThreads msType ms active c clientId `finally` clientDisconnected c
843842
where
844-
runClientThreads :: STMQueueStore (MsgStore s) => SMSType s -> MsgStore s -> TVar (IM.IntMap (Maybe AClient)) -> Client (MsgStore s) -> IS.Key -> M ()
843+
runClientThreads :: MsgStoreClass (MsgStore s) => SMSType s -> MsgStore s -> TVar (IM.IntMap (Maybe AClient)) -> Client (MsgStore s) -> IS.Key -> M ()
845844
runClientThreads msType ms active c clientId = do
846845
atomically $ modifyTVar' active $ IM.insert clientId $ Just (AClient msType c)
847846
s <- asks server
@@ -897,7 +896,7 @@ cancelSub s = case subThread s of
897896
_ -> pure ()
898897
ProhibitSub -> pure ()
899898

900-
receive :: forall c s. (Transport c, STMQueueStore s) => THandleSMP c 'TServer -> s -> Client s -> M ()
899+
receive :: forall c s. (Transport c, MsgStoreClass s) => THandleSMP c 'TServer -> s -> Client s -> M ()
901900
receive h@THandle {params = THandleParams {thAuth}} ms Client {rcvQ, sndQ, rcvActiveAt, sessionId} = do
902901
labelMyThread . B.unpack $ "client $" <> encode sessionId <> " receive"
903902
sa <- asks serverActive
@@ -997,7 +996,7 @@ data VerificationResult s = VRVerified (Maybe (StoreQueue s, QueueRec)) | VRFail
997996
-- - the queue or party key do not exist.
998997
-- In all cases, the time of the verification should depend only on the provided authorization type,
999998
-- a dummy key is used to run verification in the last two cases, and failure is returned irrespective of the result.
1000-
verifyTransmission :: forall s. STMQueueStore s => s -> Maybe (THandleAuth 'TServer, C.CbNonce) -> Maybe TransmissionAuth -> ByteString -> QueueId -> Cmd -> M (VerificationResult s)
999+
verifyTransmission :: forall s. MsgStoreClass s => s -> Maybe (THandleAuth 'TServer, C.CbNonce) -> Maybe TransmissionAuth -> ByteString -> QueueId -> Cmd -> M (VerificationResult s)
10011000
verifyTransmission ms auth_ tAuth authorized queueId cmd =
10021001
case cmd of
10031002
Cmd SRecipient (NEW k _ _ _ _) -> pure $ Nothing `verifiedWith` k
@@ -1074,7 +1073,7 @@ forkClient Client {endThreads, endThreadSeq} label action = do
10741073
action `finally` atomically (modifyTVar' endThreads $ IM.delete tId)
10751074
mkWeakThreadId t >>= atomically . modifyTVar' endThreads . IM.insert tId
10761075

1077-
client :: forall s. STMQueueStore s => THandleParams SMPVersion 'TServer -> Server -> s -> Client s -> M ()
1076+
client :: forall s. MsgStoreClass s => THandleParams SMPVersion 'TServer -> Server -> s -> Client s -> M ()
10781077
client
10791078
thParams'
10801079
Server {subscribedQ, ntfSubscribedQ, subscribers}
@@ -1768,7 +1767,7 @@ processServerMessages = do
17681767
stored'' <- getQueueSize ms rId q
17691768
liftIO $ closeMsgQueue q
17701769
pure (stored'', expired'')
1771-
processValidateQueue :: RecipientId -> JournalQueue -> IO MessageStats
1770+
processValidateQueue :: RecipientId -> JournalQueue 'MSMemory -> IO MessageStats
17721771
processValidateQueue rId q =
17731772
runExceptT (getQueueSize ms rId q) >>= \case
17741773
Right storedMsgsCount -> pure newMessageStats {storedMsgsCount, storedQueues = 1}
@@ -1777,7 +1776,7 @@ processServerMessages = do
17771776
exitFailure
17781777

17791778
-- TODO this function should be called after importing queues from store log
1780-
importMessages :: forall s. STMQueueStore s => Bool -> s -> FilePath -> Maybe Int64 -> IO MessageStats
1779+
importMessages :: forall s. MsgStoreClass s => Bool -> s -> FilePath -> Maybe Int64 -> IO MessageStats
17811780
importMessages tty ms f old_ = do
17821781
logInfo $ "restoring messages from file " <> T.pack f
17831782
LB.readFile f >>= runExceptT . foldM restoreMsg (0, Nothing, (0, 0, M.empty)) . LB.lines >>= \case

src/Simplex/Messaging/Server/Env/STM.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ data Env = Env
184184

185185
type family MsgStore s where
186186
MsgStore 'MSMemory = STMMsgStore
187-
MsgStore 'MSJournal = JournalMsgStore
187+
MsgStore 'MSJournal = JournalMsgStore 'MSMemory
188188

189-
data AMsgStore = forall s. (STMQueueStore (MsgStore s), MsgStoreClass (MsgStore s)) => AMS (SMSType s) (MsgStore s)
189+
data AMsgStore = forall s. (StoreClass (MsgStore s), MsgStoreClass (MsgStore s)) => AMS (SMSType s) (MsgStore s)
190190

191191
data AStoreQueue = forall s. MsgStoreClass (MsgStore s) => ASQ (SMSType s) (StoreQueue (MsgStore s))
192192

@@ -295,7 +295,7 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, storeLogFile, msgSt
295295
AMSType SMSMemory -> AMS SMSMemory <$> newMsgStore STMStoreConfig {storePath = storeMsgsFile, quota = msgQueueQuota}
296296
AMSType SMSJournal -> case storeMsgsFile of
297297
Just storePath ->
298-
let cfg = JournalStoreConfig {storePath, quota = msgQueueQuota, pathParts = journalMsgStoreDepth, maxMsgCount = maxJournalMsgCount, maxStateLines = maxJournalStateLines, stateTailSize = defaultStateTailSize, idleInterval = idleQueueInterval}
298+
let cfg = JournalStoreConfig {storePath, quota = msgQueueQuota, pathParts = journalMsgStoreDepth, queueStoreType = SMSMemory, maxMsgCount = maxJournalMsgCount, maxStateLines = maxJournalStateLines, stateTailSize = defaultStateTailSize, idleInterval = idleQueueInterval}
299299
in AMS SMSJournal <$> newMsgStore cfg
300300
Nothing -> putStrLn "Error: journal msg store require path in [STORE_LOG], restore_messages" >> exitFailure
301301
ntfStore <- NtfStore <$> TM.emptyIO
@@ -359,5 +359,5 @@ newSMPProxyAgent smpAgentCfg random = do
359359
smpAgent <- newSMPClientAgent smpAgentCfg random
360360
pure ProxyAgent {smpAgent}
361361

362-
readWriteQueueStore :: STMQueueStore s => FilePath -> s -> IO (StoreLog 'WriteMode)
362+
readWriteQueueStore :: MsgStoreClass s => FilePath -> s -> IO (StoreLog 'WriteMode)
363363
readWriteQueueStore = readWriteStoreLog readQueueStore writeQueueStore

src/Simplex/Messaging/Server/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
148148
doesFileExist iniFile >>= \case
149149
True -> readIniFile iniFile >>= either exitError a
150150
_ -> exitError $ "Error: server is not initialized (" <> iniFile <> " does not exist).\nRun `" <> executableName <> " init`."
151-
newJournalMsgStore = newMsgStore JournalStoreConfig {storePath = storeMsgsJournalDir, pathParts = journalMsgStoreDepth, quota = defaultMsgQueueQuota, maxMsgCount = defaultMaxJournalMsgCount, maxStateLines = defaultMaxJournalStateLines, stateTailSize = defaultStateTailSize, idleInterval = checkInterval defaultMessageExpiration}
151+
newJournalMsgStore = newMsgStore JournalStoreConfig {storePath = storeMsgsJournalDir, pathParts = journalMsgStoreDepth, queueStoreType = SMSMemory, quota = defaultMsgQueueQuota, maxMsgCount = defaultMaxJournalMsgCount, maxStateLines = defaultMaxJournalStateLines, stateTailSize = defaultStateTailSize, idleInterval = checkInterval defaultMessageExpiration}
152152
iniFile = combine cfgPath "smp-server.ini"
153153
serverVersion = "SMP server v" <> simplexMQVersion
154154
defaultServerPorts = "5223,443"

0 commit comments

Comments
 (0)