From 442fcace43859e20555999919a9b567aa21deeca Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Wed, 5 Apr 2023 15:47:10 +0700 Subject: [PATCH] Mongo timeouts and long request logging (#2891) Signed-off-by: Andrey Sobolev --- models/all/src/version.json | 2 +- models/bitrix/src/index.ts | 10 +++++++--- models/core/src/core.ts | 3 +++ models/tracker/src/index.ts | 1 + pods/server/Dockerfile | 2 +- pods/server/package.json | 3 ++- server/mongo/src/storage.ts | 6 ++++++ server/ws/src/server.ts | 33 ++++++++++++++++++++++++++++++--- 8 files changed, 51 insertions(+), 9 deletions(-) diff --git a/models/all/src/version.json b/models/all/src/version.json index f72b01b9e0..81b6e25fa9 100644 --- a/models/all/src/version.json +++ b/models/all/src/version.json @@ -1 +1 @@ -{ "major": 0, "minor": 6, "patch": 73 } +{ "major": 0, "minor": 6, "patch": 78 } diff --git a/models/bitrix/src/index.ts b/models/bitrix/src/index.ts index 346cbf4298..d3c1dc73b3 100644 --- a/models/bitrix/src/index.ts +++ b/models/bitrix/src/index.ts @@ -14,23 +14,27 @@ // limitations under the License. // -import { ArrOf, Builder, Collection, Mixin, Model, Prop, TypeRef, TypeString } from '@hcengineering/model' +import { ArrOf, Builder, Collection, Mixin, Model, Prop, ReadOnly, TypeRef, TypeString, UX } from '@hcengineering/model' import core, { TAttachedDoc, TDoc } from '@hcengineering/model-core' import bitrix from './plugin' import { BitrixEntityMapping, BitrixFieldMapping, BitrixSyncDoc, Fields } from '@hcengineering/bitrix' -import { AnyAttribute, Class, Doc, Domain, Mixin as CoreMixin, Ref } from '@hcengineering/core' +import { AnyAttribute, Class, Mixin as CoreMixin, Doc, Domain, Ref } from '@hcengineering/core' import view, { createAction } from '@hcengineering/model-view' +import { getEmbeddedLabel } from '@hcengineering/platform' import setting from '@hcengineering/setting' const DOMAIN_BITRIX = 'bitrix' as Domain @Mixin(bitrix.mixin.BitrixSyncDoc, core.class.Doc) +@UX(getEmbeddedLabel('Bitrix')) export class TBitrixSyncDoc extends TDoc implements BitrixSyncDoc { type!: string - bitrixId!: string + @Prop(TypeString(), getEmbeddedLabel('BitrixId')) + @ReadOnly() + bitrixId!: string } @Model(bitrix.class.EntityMapping, core.class.Doc, DOMAIN_BITRIX) diff --git a/models/core/src/core.ts b/models/core/src/core.ts index 7748405e34..3e3e048050 100644 --- a/models/core/src/core.ts +++ b/models/core/src/core.ts @@ -90,12 +90,15 @@ export class TDoc extends TObj implements Doc { space!: Ref @Prop(TypeTimestamp(), core.string.Modified) + @Index(IndexKind.Indexed) modifiedOn!: Timestamp @Prop(TypeRef(core.class.Account), core.string.ModifiedBy) + @Index(IndexKind.Indexed) modifiedBy!: Ref @Prop(TypeRef(core.class.Account), core.string.CreatedBy) + @Index(IndexKind.Indexed) createdBy!: Ref } diff --git a/models/tracker/src/index.ts b/models/tracker/src/index.ts index 1c07b52d05..0a112d7135 100644 --- a/models/tracker/src/index.ts +++ b/models/tracker/src/index.ts @@ -207,6 +207,7 @@ export class TIssue extends TAttachedDoc implements Issue { blockedBy!: RelatedDocument[] @Prop(ArrOf(TypeRef(core.class.TypeRelatedDocument)), tracker.string.RelatedTo) + @Index(IndexKind.Indexed) relations!: RelatedDocument[] parents!: IssueParentInfo[] diff --git a/pods/server/Dockerfile b/pods/server/Dockerfile index 295785e977..f4224c7908 100644 --- a/pods/server/Dockerfile +++ b/pods/server/Dockerfile @@ -5,4 +5,4 @@ WORKDIR /usr/src/app COPY bundle.js ./ EXPOSE 8080 -CMD [ "node", "--enable-source-maps", "bundle.js" ] +CMD [ "node", "--enable-source-maps", "--inspect", "bundle.js" ] diff --git a/pods/server/package.json b/pods/server/package.json index f1888a46f7..4194d51ed8 100644 --- a/pods/server/package.json +++ b/pods/server/package.json @@ -14,7 +14,8 @@ "docker:push": "../../common/scripts/docker_tag.sh hardcoreeng/transactor", "build:watch": "tsc", "lint": "eslint src", - "format": "prettier --write src && eslint --fix src" + "format": "prettier --write src && eslint --fix src", + "docker:tbuild": "docker build -t hardcoreeng/transactor . --platform=linux/amd64 && ../../common/scripts/docker_tag_push.sh hardcoreeng/transactor" }, "devDependencies": { "cross-env": "~7.0.3", diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 23e89d86fa..13178a73ef 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -408,6 +408,7 @@ abstract class MongoAdapterBase implements DbAdapter { }) const domain = this.hierarchy.getDomain(clazz) const cursor = this.db.collection(domain).aggregate(pipeline) + cursor.maxTimeMS(30000) const res = (await cursor.toArray())[0] const result = res.results as WithLookup[] const total = res.totalCount?.shift()?.count @@ -529,6 +530,11 @@ abstract class MongoAdapterBase implements DbAdapter { cursor = cursor.limit(options.limit) } } + + // Error in case of timeout + cursor.maxTimeMS(30000) + cursor.maxAwaitTimeMS(30000) + const res = await cursor.toArray() return toFindResult(res, total) } diff --git a/server/ws/src/server.ts b/server/ws/src/server.ts index 9d09c60e75..06a027d5e2 100644 --- a/server/ws/src/server.ts +++ b/server/ws/src/server.ts @@ -319,7 +319,8 @@ async function handleRequest ( ctx: MeasureContext, service: S, ws: WebSocket, - msg: string + msg: string, + workspace: string ): Promise { const request = readRequest(msg) if (request.id === -1 && request.method === 'hello') { @@ -336,10 +337,36 @@ async function handleRequest ( const f = (service as any)[request.method] try { const params = [userCtx, ...request.params] + + const st = Date.now() + const timeout = setTimeout(() => { + console.log('long request found', workspace, service.getUser(), request, params) + }, 4000) + + const hangTimeout = setTimeout(() => { + console.log('request hang found, 30sec', workspace, service.getUser(), request, params) + }, 30000) + const result = await f.apply(service, params) + clearTimeout(timeout) + clearTimeout(hangTimeout) const resp: Response = { id: request.id, result } + + const diff = Date.now() - st + if (diff > 5000) { + console.log( + 'very long request found', + workspace, + service.getUser(), + request, + params, + Array.isArray(result) ? result.length : '0', + diff + ) + } ws.send(serialize(resp)) } catch (err: any) { + console.error(err) const resp: Response = { id: request.id, error: unknownError(err) @@ -398,7 +425,7 @@ export function start ( } else if (Array.isArray(msg)) { msgStr = Buffer.concat(msg).toString() } - await handleRequest(ctx, session, ws, msgStr) + await handleRequest(ctx, session, ws, msgStr, token.workspace.name) }) // eslint-disable-next-line @typescript-eslint/no-misused-promises ws.on('close', (code: number, reason: Buffer) => { @@ -420,7 +447,7 @@ export function start ( const b = buffer buffer = undefined for (const msg of b) { - await handleRequest(ctx, session, ws, msg) + await handleRequest(ctx, session, ws, msg, token.workspace.name) } })