Skip to content

Commit 5c0adcb

Browse files
authored
Merge pull request #1467 from simplex-chat/postgres
SMP server PostgreSQL storage - feature branch (merge, dont squash)
2 parents 6e505f5 + b2f16ee commit 5c0adcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2904
-1565
lines changed

.github/workflows/build.yml

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ jobs:
1515
env:
1616
apps: "smp-server xftp-server ntf-server xftp"
1717
runs-on: ubuntu-${{ matrix.os }}
18+
services:
19+
postgres:
20+
image: postgres:15
21+
env:
22+
POSTGRES_HOST_AUTH_METHOD: trust # Allows passwordless access
23+
options: >-
24+
--health-cmd pg_isready
25+
--health-interval 10s
26+
--health-timeout 5s
27+
--health-retries 5
28+
ports:
29+
# Maps tcp port 5432 on service container to the host
30+
- 5432:5432
1831
strategy:
1932
fail-fast: false
2033
matrix:
@@ -43,9 +56,24 @@ jobs:
4356
shell: bash
4457
run: docker run -t -d --name builder local
4558

46-
- name: Build binaries
59+
- name: Build smp-server (postgresql) and tests
4760
shell: bash
48-
run: docker exec -t -e apps="$apps" builder sh -c 'cabal build --enable-tests && mkdir /out && for i in $apps; do bin=$(find /project/dist-newstyle -name "$i" -type f -executable); strip "$bin"; chmod +x "$bin"; mv "$bin" /out/; done'
61+
run: docker exec -t builder sh -c 'cabal build --enable-tests -fserver_postgres && mkdir -p /out && for i in smp-server simplexmq-test; do bin=$(find /project/dist-newstyle -name "$i" -type f -executable); chmod +x "$bin"; mv "$bin" /out/; done; strip /out/smp-server'
62+
63+
- name: Copy simplexmq-test from container
64+
shell: bash
65+
run: |
66+
docker cp builder:/out/simplexmq-test .
67+
68+
- name: Copy smp-server (postgresql) from container and prepare it
69+
if: startsWith(github.ref, 'refs/tags/v')
70+
shell: bash
71+
run: |
72+
docker cp builder:/out/smp-server ./smp-server-postgres-ubuntu-${{ matrix.platform_name }}
73+
74+
- name: Build everything else (standard)
75+
shell: bash
76+
run: docker exec -t -e apps="$apps" builder sh -c 'cabal build && mkdir -p /out && for i in $apps; do bin=$(find /project/dist-newstyle -name "$i" -type f -executable); strip "$bin"; chmod +x "$bin"; mv "$bin" /out/; done'
4977

5078
- name: Copy binaries from container and prepare them
5179
if: startsWith(github.ref, 'refs/tags/v')
@@ -79,6 +107,7 @@ jobs:
79107
files: |
80108
LICENSE
81109
smp-server-ubuntu-${{ matrix.platform_name }}
110+
smp-server-postgres-ubuntu-${{ matrix.platform_name }}
82111
ntf-server-ubuntu-${{ matrix.platform_name }}
83112
xftp-server-ubuntu-${{ matrix.platform_name }}
84113
xftp-ubuntu-${{ matrix.platform_name }}
@@ -88,7 +117,7 @@ jobs:
88117

89118
- name: Test
90119
shell: bash
120+
env:
121+
PGHOST: localhost
91122
run: |
92-
docker exec -t builder sh -c 'mv $(find /project/dist-newstyle -name "simplexmq-test" -type f -executable) /out/'
93-
docker cp builder:/out/simplexmq-test .
94123
./simplexmq-test

Dockerfile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ARG GHC=9.6.3
88
ARG CABAL=3.14.1.1
99

1010
# Install curl, git and and simplexmq dependencies
11-
RUN apt-get update && apt-get install -y curl git sqlite3 libsqlite3-dev build-essential libgmp3-dev zlib1g-dev llvm llvm-dev libnuma-dev libssl-dev
11+
RUN apt-get update && apt-get install -y curl libpq-dev git sqlite3 libsqlite3-dev build-essential libgmp3-dev zlib1g-dev llvm llvm-dev libnuma-dev libssl-dev
1212

1313
# Specify bootstrap Haskell versions
1414
ENV BOOTSTRAP_HASKELL_GHC_VERSION=${GHC}

simplexmq.cabal

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ flag client_postgres
7272
manual: True
7373
default: False
7474

75+
flag server_postgres
76+
description: Build server with support of PostgreSQL.
77+
manual: True
78+
default: False
79+
7580
library
7681
exposed-modules:
7782
Simplex.FileTransfer.Agent
@@ -101,6 +106,7 @@ library
101106
Simplex.Messaging.Agent.Store.Interface
102107
Simplex.Messaging.Agent.Store.Migrations
103108
Simplex.Messaging.Agent.Store.Migrations.App
109+
Simplex.Messaging.Agent.Store.Postgres.Options
104110
Simplex.Messaging.Agent.Store.Shared
105111
Simplex.Messaging.Agent.TRcvQueues
106112
Simplex.Messaging.Client
@@ -124,6 +130,7 @@ library
124130
Simplex.Messaging.Parsers
125131
Simplex.Messaging.Protocol
126132
Simplex.Messaging.Server.Expiration
133+
Simplex.Messaging.Server.QueueStore.Postgres.Config
127134
Simplex.Messaging.Server.QueueStore.QueueInfo
128135
Simplex.Messaging.ServiceScheme
129136
Simplex.Messaging.Session
@@ -148,16 +155,9 @@ library
148155
Simplex.RemoteControl.Types
149156
if flag(client_postgres)
150157
exposed-modules:
151-
Simplex.Messaging.Agent.Store.Postgres
152-
Simplex.Messaging.Agent.Store.Postgres.Common
153-
Simplex.Messaging.Agent.Store.Postgres.DB
154-
Simplex.Messaging.Agent.Store.Postgres.Migrations
155158
Simplex.Messaging.Agent.Store.Postgres.Migrations.App
156159
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20241210_initial
157160
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20250203_msg_bodies
158-
if !flag(client_library)
159-
exposed-modules:
160-
Simplex.Messaging.Agent.Store.Postgres.Util
161161
else
162162
exposed-modules:
163163
Simplex.Messaging.Agent.Store.SQLite
@@ -228,18 +228,34 @@ library
228228
Simplex.Messaging.Server.Env.STM
229229
Simplex.Messaging.Server.Information
230230
Simplex.Messaging.Server.Main
231+
Simplex.Messaging.Server.Main.Init
231232
Simplex.Messaging.Server.MsgStore
232233
Simplex.Messaging.Server.MsgStore.Journal
234+
Simplex.Messaging.Server.MsgStore.Journal.SharedLock
233235
Simplex.Messaging.Server.MsgStore.STM
234236
Simplex.Messaging.Server.MsgStore.Types
235237
Simplex.Messaging.Server.NtfStore
236238
Simplex.Messaging.Server.Prometheus
237239
Simplex.Messaging.Server.QueueStore
238240
Simplex.Messaging.Server.QueueStore.STM
241+
Simplex.Messaging.Server.QueueStore.Types
239242
Simplex.Messaging.Server.Stats
240243
Simplex.Messaging.Server.StoreLog
244+
Simplex.Messaging.Server.StoreLog.ReadWrite
241245
Simplex.Messaging.Server.StoreLog.Types
242246
Simplex.Messaging.Transport.WebSockets
247+
if flag(client_postgres) || flag(server_postgres)
248+
exposed-modules:
249+
Simplex.Messaging.Agent.Store.Postgres
250+
Simplex.Messaging.Agent.Store.Postgres.Common
251+
Simplex.Messaging.Agent.Store.Postgres.DB
252+
Simplex.Messaging.Agent.Store.Postgres.Migrations
253+
Simplex.Messaging.Agent.Store.Postgres.Util
254+
255+
if flag(server_postgres)
256+
exposed-modules:
257+
Simplex.Messaging.Server.QueueStore.Postgres
258+
Simplex.Messaging.Server.QueueStore.Postgres.Migrations
243259
other-modules:
244260
Paths_simplexmq
245261
hs-source-dirs:
@@ -308,16 +324,19 @@ library
308324
, process ==1.6.*
309325
, temporary ==1.3.*
310326
, websockets ==0.12.*
311-
if flag(client_postgres)
327+
if flag(client_postgres) || flag(server_postgres)
312328
build-depends:
313329
postgresql-libpq >=0.10.0.0
314330
, postgresql-simple ==0.7.*
315331
, raw-strings-qq ==1.1.*
332+
if flag(client_postgres)
316333
cpp-options: -DdbPostgres
317334
else
318335
build-depends:
319336
direct-sqlcipher ==2.3.*
320337
, sqlcipher-simple ==0.4.*
338+
if flag(server_postgres)
339+
cpp-options: -DdbServerPostgres
321340
if impl(ghc >= 9.6.2)
322341
build-depends:
323342
bytestring ==0.11.*
@@ -435,7 +454,6 @@ test-suite simplexmq-test
435454
CoreTests.UtilTests
436455
CoreTests.VersionRangeTests
437456
FileDescriptionTests
438-
Fixtures
439457
NtfClient
440458
NtfServerTests
441459
RemoteControl
@@ -451,7 +469,10 @@ test-suite simplexmq-test
451469
Static
452470
Static.Embedded
453471
Paths_simplexmq
454-
if !flag(client_postgres)
472+
if flag(client_postgres)
473+
other-modules:
474+
Fixtures
475+
else
455476
other-modules:
456477
AgentTests.SchemaDump
457478
AgentTests.SQLiteTests
@@ -473,7 +494,6 @@ test-suite simplexmq-test
473494
, crypton-x509
474495
, crypton-x509-store
475496
, crypton-x509-validation
476-
, deepseq ==1.4.*
477497
, directory
478498
, file-embed
479499
, filepath
@@ -487,10 +507,8 @@ test-suite simplexmq-test
487507
, ini
488508
, iso8601-time
489509
, main-tester ==0.2.*
490-
, memory
491510
, mtl
492511
, network
493-
, process
494512
, QuickCheck ==2.14.*
495513
, random
496514
, silently ==1.2.*
@@ -511,11 +529,15 @@ test-suite simplexmq-test
511529
, yaml
512530
default-language: Haskell2010
513531
if flag(client_postgres)
514-
build-depends:
515-
postgresql-libpq >=0.10.0.0
516-
, postgresql-simple ==0.7.*
517-
, raw-strings-qq ==1.1.*
518532
cpp-options: -DdbPostgres
519533
else
520534
build-depends:
521-
sqlcipher-simple
535+
deepseq ==1.4.*
536+
, memory
537+
, process
538+
, sqlcipher-simple
539+
if flag(client_postgres) || flag(server_postgres)
540+
build-depends:
541+
postgresql-simple ==0.7.*
542+
if flag(server_postgres)
543+
cpp-options: -DdbServerPostgres

src/Simplex/FileTransfer/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Simplex.Messaging.Encoding.String
2222
import Simplex.Messaging.Parsers
2323
import Simplex.Messaging.Protocol (XFTPServer)
2424
import System.FilePath ((</>))
25-
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..))
25+
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..), fromTextField_)
2626

2727
type RcvFileId = ByteString -- Agent entity ID
2828

src/Simplex/Messaging/Agent/Lock.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Simplex.Messaging.Agent.Lock
66
withLock',
77
withGetLock,
88
withGetLocks,
9+
getPutLock,
910
)
1011
where
1112

src/Simplex/Messaging/Agent/Protocol.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ import Data.Time.Clock.System (SystemTime)
168168
import Data.Type.Equality
169169
import Data.Typeable ()
170170
import Data.Word (Word16, Word32)
171-
import Simplex.Messaging.Agent.Store.DB (Binary (..), FromField (..), ToField (..))
171+
import Simplex.Messaging.Agent.Store.DB (Binary (..), FromField (..), ToField (..), blobFieldDecoder, fromTextField_)
172172
import Simplex.FileTransfer.Description
173173
import Simplex.FileTransfer.Protocol (FileParty (..))
174174
import Simplex.FileTransfer.Transport (XFTPErrorType)
@@ -1016,7 +1016,7 @@ instance Encoding AMessage where
10161016

10171017
instance ToField AMessage where toField = toField . Binary . smpEncode
10181018

1019-
instance FromField AMessage where fromField = blobFieldParser smpP
1019+
instance FromField AMessage where fromField = blobFieldDecoder smpDecode
10201020

10211021
instance Encoding AMessageReceipt where
10221022
smpEncode AMessageReceipt {agentMsgId, msgHash, rcptInfo} =

src/Simplex/Messaging/Agent/Stats.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import Data.Int (Int64)
1111
import Data.Map.Strict (Map)
1212
import qualified Data.Map.Strict as M
1313
import Simplex.Messaging.Agent.Protocol (UserId)
14-
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..))
15-
import Simplex.Messaging.Parsers (defaultJSON, fromTextField_)
14+
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..), fromTextField_)
15+
import Simplex.Messaging.Parsers (defaultJSON)
1616
import Simplex.Messaging.Protocol (NtfServer, SMPServer, XFTPServer)
1717
import Simplex.Messaging.Util (decodeJSON, encodeJSON)
1818
import UnliftIO.STM

src/Simplex/Messaging/Agent/Store.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Data.Type.Equality
3030
import Simplex.Messaging.Agent.Protocol
3131
import Simplex.Messaging.Agent.RetryInterval (RI2State)
3232
import Simplex.Messaging.Agent.Store.Common
33-
import Simplex.Messaging.Agent.Store.Interface (DBOpts, createDBStore)
33+
import Simplex.Messaging.Agent.Store.Interface (createDBStore)
3434
import Simplex.Messaging.Agent.Store.Migrations.App (appMigrations)
3535
import Simplex.Messaging.Agent.Store.Shared (MigrationConfirmation (..), MigrationError (..))
3636
import qualified Simplex.Messaging.Crypto as C

0 commit comments

Comments
 (0)