From 312db6182d34bc947ab6e12f0d3a039278ce02ca Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 10 Dec 2021 15:10:45 +0000 Subject: [PATCH 1/3] Reducing the load on DB caused by CouchDB all_db when working with a large number of couch databases. --- packages/auth/src/db/utils.js | 45 +++++++++++++++++++++++++++++------ packages/auth/src/helpers.js | 9 +++++++ 2 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 packages/auth/src/helpers.js diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index d23d407dab..7ced405c82 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -3,10 +3,15 @@ const Replication = require("./Replication") const { DEFAULT_TENANT_ID, Configs } = require("../constants") const env = require("../environment") const { StaticDatabases, SEPARATOR, DocumentTypes } = require("./constants") -const { getTenantId, getTenantIDFromAppID } = require("../tenancy") +const { + getTenantId, + getTenantIDFromAppID, + getGlobalDBName, +} = require("../tenancy") const fetch = require("node-fetch") const { getCouch } = require("./index") const { getAppMetadata } = require("../cache/appMetadata") +const { checkSlashesInUrl } = require("../helpers") const NO_APP_ERROR = "No app provided" @@ -194,6 +199,10 @@ exports.getCouchUrl = () => { return `${protocol}://${env.COUCH_DB_USERNAME}:${env.COUCH_DB_PASSWORD}@${rest}` } +exports.getStartEndKeyURL = (base, baseKey, tenantId = "") => { + return `${base}?startkey="${baseKey}${SEPARATOR}${tenantId}"&endkey="${baseKey}${SEPARATOR}${tenantId}${UNICODE_MAX}"` +} + /** * if in production this will use the CouchDB _all_dbs call to retrieve a list of databases. If testing * when using Pouch it will use the pouchdb-all-dbs package. @@ -203,12 +212,34 @@ exports.getAllDbs = async () => { if (env.isTest()) { return getCouch().allDbs() } - const response = await fetch(`${exports.getCouchUrl()}/_all_dbs`) - if (response.status === 200) { - return response.json() - } else { - throw "Cannot connect to CouchDB instance" + let dbs = [] + async function addDbs(url) { + const response = await fetch(checkSlashesInUrl(encodeURI(url))) + if (response.status === 200) { + let json = await response.json() + dbs = dbs.concat(json) + } else { + throw "Cannot connect to CouchDB instance" + } } + let couchUrl = `${exports.getCouchUrl()}_all_dbs` + if (env.MULTI_TENANCY) { + let tenantId = getTenantId() + // get prod apps + await addDbs( + exports.getStartEndKeyURL(couchUrl, DocumentTypes.APP, tenantId) + ) + // get dev apps + await addDbs( + exports.getStartEndKeyURL(couchUrl, DocumentTypes.APP_DEV, tenantId) + ) + // add global db name + dbs.push(getGlobalDBName(tenantId)) + } else { + // just get all DBs in self host + await addDbs(couchUrl) + } + return dbs } /** @@ -389,7 +420,7 @@ const getScopedFullConfig = async function (db, { type, user, workspace }) { } const getPlatformUrl = async settings => { - let platformUrl = env.PLATFORM_URL + let platformUrl = env.PLATFORM_URL || "http://localhost:10000" if (!env.SELF_HOSTED && env.MULTI_TENANCY) { // cloud and multi tenant - add the tenant to the default platform url diff --git a/packages/auth/src/helpers.js b/packages/auth/src/helpers.js new file mode 100644 index 0000000000..b402a82cf3 --- /dev/null +++ b/packages/auth/src/helpers.js @@ -0,0 +1,9 @@ +/** + * Makes sure that a URL has the correct number of slashes, while maintaining the + * http(s):// double slashes. + * @param {string} url The URL to test and remove any extra double slashes. + * @return {string} The updated url. + */ +exports.checkSlashesInUrl = url => { + return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2") +} From ce3d7df39de7f224aa3ef25b9dee01979f5478c1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 10 Dec 2021 15:17:18 +0000 Subject: [PATCH 2/3] Allowing start end key syntax to be used outside of tenancy if we need to. --- packages/auth/src/db/utils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 7ced405c82..a091896342 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -199,8 +199,9 @@ exports.getCouchUrl = () => { return `${protocol}://${env.COUCH_DB_USERNAME}:${env.COUCH_DB_PASSWORD}@${rest}` } -exports.getStartEndKeyURL = (base, baseKey, tenantId = "") => { - return `${base}?startkey="${baseKey}${SEPARATOR}${tenantId}"&endkey="${baseKey}${SEPARATOR}${tenantId}${UNICODE_MAX}"` +exports.getStartEndKeyURL = (base, baseKey, tenantId = null) => { + const tenancy = tenantId ? `${SEPARATOR}${tenantId}` : "" + return `${base}?startkey="${baseKey}${tenancy}"&endkey="${baseKey}${tenancy}${UNICODE_MAX}"` } /** From 7567c778c8b46b3a4630be6d37567a32d56d682c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 10 Dec 2021 15:18:57 +0000 Subject: [PATCH 3/3] Removing repeated line. --- packages/auth/src/db/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index a091896342..03c6e8e145 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -436,7 +436,7 @@ const getPlatformUrl = async settings => { } } - return platformUrl ? platformUrl : "http://localhost:10000" + return platformUrl } async function getScopedConfig(db, params) {