From f73af919a7b4f1693206ca1824ab244a156e7537 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Fri, 28 Apr 2023 19:09:22 +0100 Subject: [PATCH 1/5] attaching clustered mode to an env var --- packages/backend-core/src/environment.ts | 1 + packages/backend-core/src/redis/redis.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index 5718494fc4..409b6beff4 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -56,6 +56,7 @@ const environment = { SALT_ROUNDS: process.env.SALT_ROUNDS, REDIS_URL: process.env.REDIS_URL || "localhost:6379", REDIS_PASSWORD: process.env.REDIS_PASSWORD || "budibase", + REDIS_CLUSTERED: process.env.REDIS_CLUSTERED || false, MOCK_REDIS: process.env.MOCK_REDIS, MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, diff --git a/packages/backend-core/src/redis/redis.ts b/packages/backend-core/src/redis/redis.ts index 186865ccda..47d33bc32c 100644 --- a/packages/backend-core/src/redis/redis.ts +++ b/packages/backend-core/src/redis/redis.ts @@ -9,10 +9,11 @@ import { SelectableDatabase, } from "./utils" import * as timers from "../timers" +import environment from "../environment" const RETRY_PERIOD_MS = 2000 const STARTUP_TIMEOUT_MS = 5000 -const CLUSTERED = false +const CLUSTERED = environment.REDIS_CLUSTERED const DEFAULT_SELECT_DB = SelectableDatabase.DEFAULT // for testing just generate the client once From e75e91e248ee2e97b2813b359746988c56ebf0a4 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Fri, 28 Apr 2023 19:53:15 +0100 Subject: [PATCH 2/5] control all redis clustering through env var --- packages/backend-core/src/environment.ts | 2 +- packages/backend-core/src/redis/redis.ts | 2 +- packages/backend-core/src/redis/utils.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index 409b6beff4..d0f540a0b0 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -56,7 +56,7 @@ const environment = { SALT_ROUNDS: process.env.SALT_ROUNDS, REDIS_URL: process.env.REDIS_URL || "localhost:6379", REDIS_PASSWORD: process.env.REDIS_PASSWORD || "budibase", - REDIS_CLUSTERED: process.env.REDIS_CLUSTERED || false, + REDIS_CLUSTERED: process.env.REDIS_CLUSTERED, MOCK_REDIS: process.env.MOCK_REDIS, MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, diff --git a/packages/backend-core/src/redis/redis.ts b/packages/backend-core/src/redis/redis.ts index 47d33bc32c..ecc83167dd 100644 --- a/packages/backend-core/src/redis/redis.ts +++ b/packages/backend-core/src/redis/redis.ts @@ -82,7 +82,7 @@ function init(selectDb = DEFAULT_SELECT_DB) { if (client) { client.disconnect() } - const { redisProtocolUrl, opts, host, port } = getRedisOptions(CLUSTERED) + const { redisProtocolUrl, opts, host, port } = getRedisOptions() if (CLUSTERED) { client = new Redis.Cluster([{ host, port }], opts) diff --git a/packages/backend-core/src/redis/utils.ts b/packages/backend-core/src/redis/utils.ts index 7606c77b87..2c49ee4941 100644 --- a/packages/backend-core/src/redis/utils.ts +++ b/packages/backend-core/src/redis/utils.ts @@ -57,7 +57,7 @@ export enum SelectableDatabase { UNUSED_14 = 15, } -export function getRedisOptions(clustered = false) { +export function getRedisOptions() { let password = env.REDIS_PASSWORD let url: string[] | string = env.REDIS_URL.split("//") // get rid of the protocol @@ -83,7 +83,7 @@ export function getRedisOptions(clustered = false) { const opts: any = { connectTimeout: CONNECT_TIMEOUT_MS, } - if (clustered) { + if (env.REDIS_CLUSTERED) { opts.redisOptions = {} opts.redisOptions.tls = {} opts.redisOptions.password = password From 36397bb77dac6895886ea1bd647796d59fda19e8 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Fri, 28 Apr 2023 20:23:39 +0100 Subject: [PATCH 3/5] enabling cluster support for ratelimit cache --- packages/server/src/api/routes/public/index.ts | 8 ++++++-- packages/server/src/environment.ts | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/public/index.ts b/packages/server/src/api/routes/public/index.ts index e2bce1e730..5f93732a06 100644 --- a/packages/server/src/api/routes/public/index.ts +++ b/packages/server/src/api/routes/public/index.ts @@ -42,8 +42,12 @@ if (!env.isTest()) { host: REDIS_OPTS.host, port: REDIS_OPTS.port, }, - password: REDIS_OPTS.opts.password, - database: 1, + password: REDIS_OPTS.opts.password || REDIS_OPTS.opts.redisOptions.password, + } + + if (!env.REDIS_CLUSTERED) { + // Can't set direct redis db in clustered env + options.database = 1 } } rateLimitStore = new Stores.Redis(options) diff --git a/packages/server/src/environment.ts b/packages/server/src/environment.ts index 058e8bdff8..48eb78c41d 100644 --- a/packages/server/src/environment.ts +++ b/packages/server/src/environment.ts @@ -47,6 +47,7 @@ const environment = { MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, REDIS_URL: process.env.REDIS_URL, REDIS_PASSWORD: process.env.REDIS_PASSWORD, + REDIS_CLUSTERED: process.env.REDIS_CLUSTERED, HTTP_MIGRATIONS: process.env.HTTP_MIGRATIONS, API_REQ_LIMIT_PER_SEC: process.env.API_REQ_LIMIT_PER_SEC, GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, From 167dfd39d79aed57e8b2db896f40ed5b608555de Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 4 May 2023 13:32:17 +0100 Subject: [PATCH 4/5] fix ts build --- packages/backend-core/src/redis/redis.ts | 3 +-- packages/server/src/api/routes/public/index.ts | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend-core/src/redis/redis.ts b/packages/backend-core/src/redis/redis.ts index ecc83167dd..2d54b51a9f 100644 --- a/packages/backend-core/src/redis/redis.ts +++ b/packages/backend-core/src/redis/redis.ts @@ -9,11 +9,10 @@ import { SelectableDatabase, } from "./utils" import * as timers from "../timers" -import environment from "../environment" const RETRY_PERIOD_MS = 2000 const STARTUP_TIMEOUT_MS = 5000 -const CLUSTERED = environment.REDIS_CLUSTERED +const CLUSTERED = env.REDIS_CLUSTERED const DEFAULT_SELECT_DB = SelectableDatabase.DEFAULT // for testing just generate the client once diff --git a/packages/server/src/api/routes/public/index.ts b/packages/server/src/api/routes/public/index.ts index 5f93732a06..107287348c 100644 --- a/packages/server/src/api/routes/public/index.ts +++ b/packages/server/src/api/routes/public/index.ts @@ -47,6 +47,7 @@ if (!env.isTest()) { if (!env.REDIS_CLUSTERED) { // Can't set direct redis db in clustered env + // @ts-ignore options.database = 1 } } From 41c131050ce9f9e8b86f432ae9af60251cc9d100 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 4 May 2023 13:43:53 +0100 Subject: [PATCH 5/5] lint --- packages/server/src/api/routes/public/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/public/index.ts b/packages/server/src/api/routes/public/index.ts index 107287348c..bed798f75b 100644 --- a/packages/server/src/api/routes/public/index.ts +++ b/packages/server/src/api/routes/public/index.ts @@ -42,7 +42,8 @@ if (!env.isTest()) { host: REDIS_OPTS.host, port: REDIS_OPTS.port, }, - password: REDIS_OPTS.opts.password || REDIS_OPTS.opts.redisOptions.password, + password: + REDIS_OPTS.opts.password || REDIS_OPTS.opts.redisOptions.password, } if (!env.REDIS_CLUSTERED) {