From 9114c8cb8e7d0bb0f686190d47c60591ac211f35 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 8 Apr 2023 15:53:36 +0900 Subject: [PATCH] feat(backend): support replication of postgresql Resolve #10205 --- .config/docker_example.yml | 17 +++++++++++++++++ .config/example.yml | 17 +++++++++++++++++ .devcontainer/devcontainer.yml | 17 +++++++++++++++++ CHANGELOG.md | 2 ++ chart/files/default.yml | 17 +++++++++++++++++ packages/backend/src/config.ts | 10 ++++++++++ packages/backend/src/postgres.ts | 16 ++++++++++++++++ 7 files changed, 96 insertions(+) diff --git a/.config/docker_example.yml b/.config/docker_example.yml index d93cc8b70..af0a90dc9 100644 --- a/.config/docker_example.yml +++ b/.config/docker_example.yml @@ -51,6 +51,23 @@ db: #extra: # ssl: true +dbReplications: false + +# You can configure any number of replicas here +#dbSlaves: +# - +# host: +# port: +# db: +# user: +# pass: +# - +# host: +# port: +# db: +# user: +# pass: + # ┌─────────────────────┐ #───┘ Redis configuration └───────────────────────────────────── diff --git a/.config/example.yml b/.config/example.yml index b61ed1480..57e2b56b7 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -51,6 +51,23 @@ db: #extra: # ssl: true +dbReplications: false + +# You can configure any number of replicas here +#dbSlaves: +# - +# host: +# port: +# db: +# user: +# pass: +# - +# host: +# port: +# db: +# user: +# pass: + # ┌─────────────────────┐ #───┘ Redis configuration └───────────────────────────────────── diff --git a/.devcontainer/devcontainer.yml b/.devcontainer/devcontainer.yml index 1350e7015..2af306e3d 100644 --- a/.devcontainer/devcontainer.yml +++ b/.devcontainer/devcontainer.yml @@ -51,6 +51,23 @@ db: #extra: # ssl: true +dbReplications: false + +# You can configure any number of replicas here +#dbSlaves: +# - +# host: +# port: +# db: +# user: +# pass: +# - +# host: +# port: +# db: +# user: +# pass: + # ┌─────────────────────┐ #───┘ Redis configuration └───────────────────────────────────── diff --git a/CHANGELOG.md b/CHANGELOG.md index 615a3aad3..2486a7e54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ - Add Minimizing ("folding") of windows ### Server +- PostgreSQLのレプリケーション対応 + - 設定ファイルの `dbReplications` および `dbSlaves` にて設定できます - イベント用Redisを別サーバーに分離できるように - ジョブキュー用Redisを別サーバーに分離できるように - サーバーの全体的なパフォーマンスを向上 diff --git a/chart/files/default.yml b/chart/files/default.yml index 1d8e5b490..188866924 100644 --- a/chart/files/default.yml +++ b/chart/files/default.yml @@ -72,6 +72,23 @@ db: #extra: # ssl: true +dbReplications: false + +# You can configure any number of replicas here +#dbSlaves: +# - +# host: +# port: +# db: +# user: +# pass: +# - +# host: +# port: +# db: +# user: +# pass: + # ┌─────────────────────┐ #───┘ Redis configuration └───────────────────────────────────── diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index fd2b83cf2..1443e6338 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -25,6 +25,16 @@ export type Source = { disableCache?: boolean; extra?: { [x: string]: string }; }; + dbReplications?: boolean; + dbSlaves?: { + host: string; + port: number; + db: string; + user: string; + pass: string; + disableCache?: boolean; + extra?: { [x: string]: string }; + }[]; redis: { host: string; port: number; diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index efeca46b4..bb21ed827 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -200,6 +200,22 @@ export function createPostgresDataSource(config: Config) { statement_timeout: 1000 * 10, ...config.db.extra, }, + replication: config.dbReplications ? { + master: { + host: config.db.host, + port: config.db.port, + username: config.db.user, + password: config.db.pass, + database: config.db.db, + }, + slaves: config.dbSlaves!.map(rep => ({ + host: rep.host, + port: rep.port, + username: rep.user, + password: rep.pass, + database: rep.db, + })), + } : undefined, synchronize: process.env.NODE_ENV === 'test', dropSchema: process.env.NODE_ENV === 'test', cache: !config.db.disableCache && process.env.NODE_ENV !== 'test' ? { // dbをcloseしても何故かredisのコネクションが内部的に残り続けるようで、テストの際に支障が出るため無効にする(キャッシュも含めてテストしたいため本当は有効にしたいが...)