Skip to content

Commit f4b55bf

Browse files
authored
smp server: CLI option to compact store log with PostgreSQL database (#1484)
* smp server: CLI option to compact store log with PostgreSQL database * version * fix test
1 parent fe64d42 commit f4b55bf

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

simplexmq.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cabal-version: 1.12
22

33
name: simplexmq
4-
version: 6.3.0.8
4+
version: 6.3.0.805
55
synopsis: SimpleXMQ message broker
66
description: This package includes <./docs/Simplex-Messaging-Server.html server>,
77
<./docs/Simplex-Messaging-Client.html client> and

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ data ServerConfig = ServerConfig
135135

136136
data StartOptions = StartOptions
137137
{ maintenance :: Bool,
138+
compactLog :: Bool,
138139
skipWarnings :: Bool,
139140
confirmMigrations :: MigrationConfirmation
140141
}
@@ -331,7 +332,7 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
331332
ASSCfg qt mt (SSCMemory storePaths_) -> do
332333
let storePath = storeMsgsFile =<< storePaths_
333334
ms <- newMsgStore STMStoreConfig {storePath, quota = msgQueueQuota}
334-
forM_ storePaths_ $ \StorePaths {storeLogFile = f} -> loadStoreLog (mkQueue ms) f $ queueStore ms
335+
forM_ storePaths_ $ \StorePaths {storeLogFile = f} -> loadStoreLog (mkQueue ms) f $ queueStore ms
335336
pure $ AMS qt mt ms
336337
ASSCfg qt mt SSCMemoryJournal {storeLogFile, storeMsgsPath} -> do
337338
let qsCfg = MQStoreCfg
@@ -341,9 +342,10 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
341342
pure $ AMS qt mt ms
342343
#if defined(dbServerPostgres)
343344
ASSCfg qt mt SSCDatabaseJournal {storeCfg, storeMsgsPath'} -> do
344-
let StartOptions {confirmMigrations} = startOptions config
345+
let StartOptions {compactLog, confirmMigrations} = startOptions config
345346
qsCfg = PQStoreCfg (storeCfg {confirmMigrations} :: PostgresStoreCfg)
346347
cfg = mkJournalStoreConfig qsCfg storeMsgsPath' msgQueueQuota maxJournalMsgCount maxJournalStateLines idleQueueInterval
348+
when compactLog $ compactDbStoreLog $ dbStoreLogPath storeCfg
347349
ms <- newMsgStore cfg
348350
pure $ AMS qt mt ms
349351
#else
@@ -368,6 +370,16 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
368370
logInfo $ "restoring queues from file " <> T.pack f
369371
sl <- readWriteQueueStore False mkQ f st
370372
setStoreLog st sl
373+
compactDbStoreLog = \case
374+
Just f -> do
375+
logInfo $ "compacting queues in file " <> T.pack f
376+
st <- newMsgStore STMStoreConfig {storePath = Nothing, quota = msgQueueQuota}
377+
sl <- readWriteQueueStore False (mkQueue st) f (queueStore st)
378+
setStoreLog (queueStore st) sl
379+
closeMsgStore st
380+
Nothing -> do
381+
logError "Error: `--compact-log` used without `db_store_log` INI option"
382+
exitFailure
371383
getCredentials protocol creds = do
372384
files <- missingCreds
373385
unless (null files) $ do

src/Simplex/Messaging/Server/Main.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,14 @@ cliCommandP cfgPath logPath iniFile =
795795
maintenance <-
796796
switch
797797
( long "maintenance"
798+
<> short 'm'
798799
<> help "Do not start the server, only perform start and stop tasks"
799800
)
801+
compactLog <-
802+
switch
803+
( long "compact-log"
804+
<> help "Compact store log (always enabled with `memory` storage for queues)"
805+
)
800806
skipWarnings <-
801807
switch
802808
( long "skip-warnings"
@@ -810,7 +816,7 @@ cliCommandP cfgPath logPath iniFile =
810816
<> help "Confirm PostgreSQL database migration: up, down (default is manual confirmation)"
811817
<> value MCConsole
812818
)
813-
pure StartOptions {maintenance, skipWarnings, confirmMigrations}
819+
pure StartOptions {maintenance, compactLog, skipWarnings, confirmMigrations}
814820
journalCmdP = storeCmdP "message log file" "journal storage"
815821
databaseCmdP = storeCmdP "queue store log file" "PostgreSQL database schema"
816822
storeCmdP src dest =

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ instance StoreQueueClass q => QueueStoreClass q (STMQueueStore q) where
6363
pure STMQueueStore {queues, senders, notifiers, storeLog}
6464

6565
closeQueueStore :: STMQueueStore q -> IO ()
66-
closeQueueStore st = readTVarIO (storeLog st) >>= mapM_ closeStoreLog
66+
closeQueueStore STMQueueStore {queues, senders, notifiers, storeLog} = do
67+
readTVarIO storeLog >>= mapM_ closeStoreLog
68+
atomically $ TM.clear queues
69+
atomically $ TM.clear senders
70+
atomically $ TM.clear notifiers
6771

6872
loadedQueues = queues
6973
{-# INLINE loadedQueues #-}

tests/CoreTests/MsgStoreTests.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ testReadFileMissing ms = do
472472

473473
mq <- fromJust <$> readTVarIO (msgQueue q)
474474
MsgQueueState {readState = rs} <- readTVarIO $ state mq
475-
closeMsgStore ms
475+
closeMsgQueue q
476476
let path = journalFilePath (queueDirectory $ queue mq) $ journalId rs
477477
removeFile path
478478

@@ -491,7 +491,7 @@ testReadFileMissingSwitch ms = do
491491

492492
mq <- fromJust <$> readTVarIO (msgQueue q)
493493
MsgQueueState {readState = rs} <- readTVarIO $ state mq
494-
closeMsgStore ms
494+
closeMsgQueue q
495495
let path = journalFilePath (queueDirectory $ queue mq) $ journalId rs
496496
removeFile path
497497

@@ -509,7 +509,7 @@ testWriteFileMissing ms = do
509509

510510
mq <- fromJust <$> readTVarIO (msgQueue q)
511511
MsgQueueState {writeState = ws} <- readTVarIO $ state mq
512-
closeMsgStore ms
512+
closeMsgQueue q
513513
let path = journalFilePath (queueDirectory $ queue mq) $ journalId ws
514514
print path
515515
removeFile path
@@ -532,7 +532,7 @@ testReadAndWriteFilesMissing ms = do
532532

533533
mq <- fromJust <$> readTVarIO (msgQueue q)
534534
MsgQueueState {readState = rs, writeState = ws} <- readTVarIO $ state mq
535-
closeMsgStore ms
535+
closeMsgQueue q
536536
removeFile $ journalFilePath (queueDirectory $ queue mq) $ journalId rs
537537
removeFile $ journalFilePath (queueDirectory $ queue mq) $ journalId ws
538538

tests/SMPClient.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ cfgMS msType =
212212
allowSMPProxy = False,
213213
serverClientConcurrency = 2,
214214
information = Nothing,
215-
startOptions = StartOptions {maintenance = False, skipWarnings = False, confirmMigrations = MCYesUp}
215+
startOptions = StartOptions {maintenance = False, compactLog = False, skipWarnings = False, confirmMigrations = MCYesUp}
216216
}
217217

218218
serverStoreConfig :: AStoreType -> AServerStoreCfg

0 commit comments

Comments
 (0)