From 5447404431a17caa6ceb0fe746a4bf92da183c1b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 17 Jul 2023 17:52:39 +0100 Subject: [PATCH 1/2] Upgrading to latest version of MongoDB to attempt to solve the memory leak, as well as some minor changes to websocket integration to get the development environment to work without the account portal when running in multi-tenancy mode. --- .../backend-core/src/context/mainContext.ts | 9 +++++++++ packages/server/package.json | 2 +- packages/server/src/startup.ts | 2 +- packages/server/src/websockets/builder.ts | 8 ++++++-- yarn.lock | 18 +++++++++--------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/backend-core/src/context/mainContext.ts b/packages/backend-core/src/context/mainContext.ts index 61d96bb4b0..ae198aafc7 100644 --- a/packages/backend-core/src/context/mainContext.ts +++ b/packages/backend-core/src/context/mainContext.ts @@ -203,6 +203,15 @@ export function getTenantId(): string { return tenantId } +export function hasTenantId(): boolean { + if (!isMultiTenant()) { + return true + } + const context = Context.get() + const tenantId = context?.tenantId + return tenantId != null +} + export function getAutomationId(): string | undefined { const context = Context.get() return context?.automationId diff --git a/packages/server/package.json b/packages/server/package.json index a35f78d36d..52f209a403 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -98,7 +98,7 @@ "koa2-ratelimit": "1.1.1", "lodash": "4.17.21", "memorystream": "0.3.1", - "mongodb": "5.6", + "mongodb": "5.7", "mssql": "9.1.1", "mysql2": "2.3.3", "node-fetch": "2.6.7", diff --git a/packages/server/src/startup.ts b/packages/server/src/startup.ts index 64a6c011c4..9da26ac2aa 100644 --- a/packages/server/src/startup.ts +++ b/packages/server/src/startup.ts @@ -5,7 +5,7 @@ import { generateApiKey, getChecklist, } from "./utilities/workerRequests" -import { installation, tenancy, logging, events } from "@budibase/backend-core" +import { events, installation, logging, tenancy } from "@budibase/backend-core" import fs from "fs" import { watch } from "./watch" import * as automations from "./automations" diff --git a/packages/server/src/websockets/builder.ts b/packages/server/src/websockets/builder.ts index 2524d9608b..91d14269ed 100644 --- a/packages/server/src/websockets/builder.ts +++ b/packages/server/src/websockets/builder.ts @@ -1,6 +1,6 @@ import authorized from "../middleware/authorized" import { BaseSocket } from "./websocket" -import { permissions, events } from "@budibase/backend-core" +import { permissions, events, context } from "@budibase/backend-core" import http from "http" import Koa from "koa" import { Datasource, Table, SocketSession, ContextUser } from "@budibase/types" @@ -8,6 +8,7 @@ import { gridSocket } from "./index" import { clearLock, updateLock } from "../utilities/redis" import { Socket } from "socket.io" import { BuilderSocketEvent } from "@budibase/shared-core" +import { hasTenantId } from "@budibase/backend-core/src/context" export default class BuilderSocket extends BaseSocket { constructor(app: Koa, server: http.Server) { @@ -27,7 +28,10 @@ export default class BuilderSocket extends BaseSocket { userIdMap[session._id] = true } }) - await events.user.dataCollaboration(Object.keys(userIdMap).length) + + if (context.hasTenantId()) { + await events.user.dataCollaboration(Object.keys(userIdMap).length) + } // Reply with all current sessions callback({ users: sessions }) diff --git a/yarn.lock b/yarn.lock index 3acf765870..47f8bafb80 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7956,10 +7956,10 @@ bson@*: resolved "https://registry.yarnpkg.com/bson/-/bson-5.0.1.tgz#4cd3eeeabf6652ef0d6ab600f9a18212d39baac3" integrity sha512-y09gBGusgHtinMon/GVbv1J6FrXhnr/+6hqLlSmEFzkz6PodqF6TxjyvfvY3AfO+oG1mgUtbC86xSbOlwvM62Q== -bson@^5.3.0: - version "5.3.0" - resolved "https://registry.yarnpkg.com/bson/-/bson-5.3.0.tgz#37b006df4cd91ed125cb686467c1dd6d4606b514" - integrity sha512-ukmCZMneMlaC5ebPHXIkP8YJzNl5DC41N5MAIvKDqLggdao342t4McltoJBQfQya/nHBWAcSsYRqlXPoQkTJag== +bson@^5.4.0: + version "5.4.0" + resolved "https://registry.yarnpkg.com/bson/-/bson-5.4.0.tgz#0eea77276d490953ad8616b483298dbff07384c6" + integrity sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA== buffer-alloc-unsafe@^1.1.0: version "1.1.0" @@ -18085,12 +18085,12 @@ mongodb-connection-string-url@^2.6.0: "@types/whatwg-url" "^8.2.1" whatwg-url "^11.0.0" -mongodb@5.6: - version "5.6.0" - resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-5.6.0.tgz#caff5278341bfc0f1ef6f394bb403d207de03d1e" - integrity sha512-z8qVs9NfobHJm6uzK56XBZF8XwM9H294iRnB7wNjF0SnY93si5HPziIJn+qqvUR5QOff/4L0gCD6SShdR/GtVQ== +mongodb@5.7: + version "5.7.0" + resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-5.7.0.tgz#e16d2fcdfd9f8503ec2d88288392dc3235bb3ecc" + integrity sha512-zm82Bq33QbqtxDf58fLWBwTjARK3NSvKYjyz997KSy6hpat0prjeX/kxjbPVyZY60XYPDNETaHkHJI2UCzSLuw== dependencies: - bson "^5.3.0" + bson "^5.4.0" mongodb-connection-string-url "^2.6.0" socks "^2.7.1" optionalDependencies: From cddd0d80f2b79f8835f41cab074ec68dee388eff Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 17 Jul 2023 18:03:09 +0100 Subject: [PATCH 2/2] Making sure tenant ID is always available to the builder websocket. --- packages/backend-core/src/context/mainContext.ts | 9 --------- packages/server/src/websockets/builder.ts | 8 +++++--- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/backend-core/src/context/mainContext.ts b/packages/backend-core/src/context/mainContext.ts index ae198aafc7..61d96bb4b0 100644 --- a/packages/backend-core/src/context/mainContext.ts +++ b/packages/backend-core/src/context/mainContext.ts @@ -203,15 +203,6 @@ export function getTenantId(): string { return tenantId } -export function hasTenantId(): boolean { - if (!isMultiTenant()) { - return true - } - const context = Context.get() - const tenantId = context?.tenantId - return tenantId != null -} - export function getAutomationId(): string | undefined { const context = Context.get() return context?.automationId diff --git a/packages/server/src/websockets/builder.ts b/packages/server/src/websockets/builder.ts index 91d14269ed..66ec7bdef9 100644 --- a/packages/server/src/websockets/builder.ts +++ b/packages/server/src/websockets/builder.ts @@ -8,7 +8,6 @@ import { gridSocket } from "./index" import { clearLock, updateLock } from "../utilities/redis" import { Socket } from "socket.io" import { BuilderSocketEvent } from "@budibase/shared-core" -import { hasTenantId } from "@budibase/backend-core/src/context" export default class BuilderSocket extends BaseSocket { constructor(app: Koa, server: http.Server) { @@ -29,8 +28,11 @@ export default class BuilderSocket extends BaseSocket { } }) - if (context.hasTenantId()) { - await events.user.dataCollaboration(Object.keys(userIdMap).length) + const tenantId = context.getTenantIDFromAppID(appId) + if (tenantId) { + await context.doInTenant(tenantId, async () => { + await events.user.dataCollaboration(Object.keys(userIdMap).length) + }) } // Reply with all current sessions