Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBER-1198: Upgrade to mongo 7 #4472

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,458 changes: 257 additions & 1,201 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
mongodb:
image: mongo
image: 'mongo:7-jammy'
container_name: mongodb
environment:
- PUID=1000
Expand Down
2 changes: 1 addition & 1 deletion dev/tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"got": "^11.8.3",
"libphonenumber-js": "^1.9.46",
"mime-types": "~2.1.34",
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"ws": "^8.10.0",
"xml2js": "~0.4.23"
}
Expand Down
12 changes: 6 additions & 6 deletions dev/tool/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ export async function fixSkills (
tag: t._id
})) as TagReference[]
const ids = references.map((r) => r._id)
await db.collection(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection(DOMAIN_TAGS).deleteOne({ _id: t._id })
await db.collection<Doc>(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection<Doc>(DOMAIN_TAGS).deleteOne({ _id: t._id })
}
}
await fixCount()
Expand All @@ -588,8 +588,8 @@ export async function fixSkills (
tag: t._id
})) as TagReference[]
const ids = references.map((r) => r._id)
await db.collection(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection(DOMAIN_TAGS).deleteOne({ _id: t._id })
await db.collection<Doc>(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection<Doc>(DOMAIN_TAGS).deleteOne({ _id: t._id })
}
console.log('DONE 6 STEP')
}
Expand All @@ -610,8 +610,8 @@ export async function fixSkills (
tag: t._id
})) as TagReference[]
const ids = references.map((r) => r._id)
await db.collection(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection(DOMAIN_TAGS).deleteOne({ _id: t._id })
await db.collection<Doc>(DOMAIN_TAGS).deleteMany({ _id: { $in: ids } })
await db.collection<Doc>(DOMAIN_TAGS).deleteOne({ _id: t._id })
}
}
await fixCount()
Expand Down
2 changes: 1 addition & 1 deletion plugins/activity-resources/src/activityMessagesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import activity, {
// Use 5 minutes to combine similar messages
const combineThresholdMs = 5 * 60 * 1000
// Use 10 seconds to combine update messages after creation.
const createCombineThreshold = 10 * 1000
const createCombineThreshold = parseInt(localStorage.getItem('platform.activity.threshold') ?? '10 * 1000')

const valueTypes: ReadonlyArray<Ref<Class<Doc>>> = [
core.class.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion pods/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@hcengineering/account": "^0.6.0",
"@hcengineering/platform": "^0.6.9",
"@hcengineering/core": "^0.6.28",
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"koa": "^2.13.1",
"koa-router": "^12.0.1",
"koa-bodyparser": "^4.3.0",
Expand Down
2 changes: 1 addition & 1 deletion pods/backup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"@hcengineering/platform": "^0.6.9",
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"@hcengineering/server-tool": "^0.6.0",
"@hcengineering/server-token": "^0.6.7",
"@hcengineering/client": "^0.6.14",
Expand Down
2 changes: 1 addition & 1 deletion server/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prettier-plugin-svelte": "^3.1.0"
},
"dependencies": {
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"@hcengineering/platform": "^0.6.9",
"@hcengineering/core": "^0.6.28",
"@hcengineering/contact": "^0.6.20",
Expand Down
4 changes: 2 additions & 2 deletions server/account/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function withProductId (productId: string, query: Filter<Workspace>): Filter<Wor
* @returns
*/
export async function getWorkspace (db: Db, productId: string, workspace: string): Promise<Workspace | null> {
return await db.collection(WORKSPACE_COLLECTION).findOne<Workspace>(withProductId(productId, { workspace }))
return await db.collection<Workspace>(WORKSPACE_COLLECTION).findOne(withProductId(productId, { workspace }))
}

function toAccountInfo (account: Account): AccountInfo {
Expand All @@ -196,7 +196,7 @@ async function getAccountInfo (db: Db, email: string, password: string): Promise
if (account === null) {
throw new PlatformError(new Status(Severity.ERROR, platform.status.AccountNotFound, { account: email }))
}
if (!verifyPassword(password, account.hash.buffer, account.salt.buffer)) {
if (!verifyPassword(password, Buffer.from(account.hash.buffer), Buffer.from(account.salt.buffer))) {
throw new PlatformError(new Status(Severity.ERROR, platform.status.InvalidPassword, { account: email }))
}
return toAccountInfo(account)
Expand Down
2 changes: 1 addition & 1 deletion server/collaborator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@hocuspocus/transformer": "^2.9.0",
"@tiptap/core": "^2.1.12",
"@tiptap/html": "^2.1.12",
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"yjs": "^13.5.52",
"y-prosemirror": "^1.2.1",
"express": "^4.17.1",
Expand Down
8 changes: 5 additions & 3 deletions server/collaborator/src/storage/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

import { MeasureContext, toWorkspaceString } from '@hcengineering/core'
import { Doc, MeasureContext, Ref, toWorkspaceString } from '@hcengineering/core'
import { Transformer } from '@hocuspocus/transformer'
import { MongoClient } from 'mongodb'
import { Doc as YDoc } from 'yjs'
Expand Down Expand Up @@ -66,10 +66,12 @@ export class MongodbStorageAdapter implements StorageAdapter {
return await this.ctx.with('load-document', {}, async (ctx) => {
const doc = await ctx.with('query', {}, async () => {
const db = this.mongodb.db(toWorkspaceString(context.workspaceId))
return await db.collection(objectDomain).findOne({ _id: objectId }, { projection: { [objectAttr]: 1 } })
return await db
.collection<Doc>(objectDomain)
.findOne({ _id: objectId as Ref<Doc> }, { projection: { [objectAttr]: 1 } })
})

const content = doc !== null && objectAttr in doc ? (doc[objectAttr] as string) : ''
const content = doc !== null && objectAttr in doc ? ((doc as any)[objectAttr] as string) : ''

return await ctx.with('transform', {}, () => {
return this.transformer.toYdoc(content, objectAttr)
Expand Down
2 changes: 1 addition & 1 deletion server/mongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"@hcengineering/core": "^0.6.28",
"@hcengineering/platform": "^0.6.9",
"@hcengineering/server-core": "^0.6.1",
"mongodb": "^4.11.0"
"mongodb": "^6.3.0"
}
}
54 changes: 28 additions & 26 deletions server/mongo/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import core, {
DOMAIN_TX,
SortingOrder,
TxProcessor,
cutObjectArray,
escapeLikeForRegexp,
getTypeOf,
isOperator,
Expand Down Expand Up @@ -57,8 +58,8 @@ import core, {
type WorkspaceId
} from '@hcengineering/core'
import type { DbAdapter, TxAdapter } from '@hcengineering/server-core'
import serverCore from '@hcengineering/server-core'
import {
type AbstractCursor,
type AnyBulkWriteOperation,
type Collection,
type Db,
Expand All @@ -70,8 +71,6 @@ import {
} from 'mongodb'
import { createHash } from 'node:crypto'
import { getMongoClient, getWorkspaceDB } from './utils'
import { cutObjectArray } from '@hcengineering/core'
import { getMetadata } from '@hcengineering/platform'

function translateDoc (doc: Doc): Document {
return { ...doc, '%hash%': null }
Expand Down Expand Up @@ -111,12 +110,21 @@ abstract class MongoAdapterBase implements DbAdapter {

async init (): Promise<void> {}

async toArray<T>(cursor: AbstractCursor<T>): Promise<T[]> {
const data: T[] = []
for await (const r of cursor.stream()) {
data.push(r)
}
await cursor.close()
return data
}

async createIndexes (domain: Domain, config: Pick<IndexingConfiguration<Doc>, 'indexes'>): Promise<void> {
for (const vv of config.indexes) {
try {
await this.db.collection(domain).createIndex(vv)
} catch (err: any) {
console.error(err)
console.error('failed to create index', domain, vv, err)
}
}
}
Expand Down Expand Up @@ -451,16 +459,18 @@ abstract class MongoAdapterBase implements DbAdapter {
checkKeys: false,
enableUtf8Validation: false
})
cursor.maxTimeMS(parseInt(getMetadata(serverCore.metadata.CursorMaxTimeMS) ?? '30000'))
let res: Document = []
const result: WithLookup<T>[] = []
let total = options?.total === true ? 0 : -1
try {
res = (await cursor.toArray())[0]
const rres = await this.toArray(cursor)
for (const r of rres) {
result.push(...r.results)
total = options?.total === true ? r.totalCount?.shift()?.count ?? 0 : -1
}
} catch (e) {
console.error('error during executing cursor in findWithPipeline', clazz, cutObjectArray(query), options, e)
throw e
}
const result = res.results as WithLookup<T>[]
const total = options?.total === true ? res.totalCount?.shift()?.count ?? 0 : -1
for (const row of result) {
await this.fillLookupValue(clazz, options?.lookup, row)
this.clearExtraLookups(row)
Expand Down Expand Up @@ -596,11 +606,8 @@ abstract class MongoAdapterBase implements DbAdapter {
}

// Error in case of timeout
cursor.maxTimeMS(parseInt(getMetadata(serverCore.metadata.CursorMaxTimeMS) ?? '30000'))
cursor.maxAwaitTimeMS(30000)
let res: T[] = []
try {
res = await cursor.toArray()
const res: T[] = await this.toArray(cursor)
if (options?.total === true && options?.limit === undefined) {
total = res.length
}
Expand Down Expand Up @@ -712,14 +719,9 @@ abstract class MongoAdapterBase implements DbAdapter {
}

async load (domain: Domain, docs: Ref<Doc>[]): Promise<Doc[]> {
return this.stripHash(
this.stripHash(
await this.db
.collection(domain)
.find<Doc>({ _id: { $in: docs } })
.toArray()
)
)
const cursor = this.db.collection<Doc>(domain).find<Doc>({ _id: { $in: docs } })
const result = await this.toArray(cursor)
return this.stripHash(this.stripHash(result))
}

async upload (domain: Domain, docs: Doc[]): Promise<void> {
Expand Down Expand Up @@ -783,7 +785,7 @@ abstract class MongoAdapterBase implements DbAdapter {
}

async clean (domain: Domain, docs: Ref<Doc>[]): Promise<void> {
await this.db.collection(domain).deleteMany({ _id: { $in: docs } })
await this.db.collection<Doc>(domain).deleteMany({ _id: { $in: docs } })
}
}

Expand Down Expand Up @@ -1087,7 +1089,7 @@ class MongoAdapter extends MongoAdapterBase {
'%hash%': null
}
} as unknown as UpdateFilter<Document>,
{ returnDocument: 'after' }
{ returnDocument: 'after', includeResultMetadata: true }
)
return { object: result.value }
}
Expand Down Expand Up @@ -1128,7 +1130,7 @@ class MongoAdapter extends MongoAdapterBase {
? async (): Promise<TxResult> => {
const result = await this.db
.collection(domain)
.findOneAndUpdate(filter, update, { returnDocument: 'after' })
.findOneAndUpdate(filter, update, { returnDocument: 'after', includeResultMetadata: true })
return { object: result.value }
}
: async () => await this.db.collection(domain).updateOne(filter, update)
Expand Down Expand Up @@ -1163,11 +1165,11 @@ class MongoTxAdapter extends MongoAdapterBase implements TxAdapter {
}

async getModel (): Promise<Tx[]> {
const model = await this.db
const cursor = this.db
.collection(DOMAIN_TX)
.find<Tx>({ objectSpace: core.space.Model })
.sort({ _id: 1, modifiedOn: 1 })
.toArray()
const model = await this.toArray(cursor)
// We need to put all core.account.System transactions first
const systemTx: Tx[] = []
const userTx: Tx[] = []
Expand Down
2 changes: 1 addition & 1 deletion server/tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"prettier-plugin-svelte": "^3.1.0"
},
"dependencies": {
"mongodb": "^4.11.0",
"mongodb": "^6.3.0",
"@hcengineering/platform": "^0.6.9",
"@hcengineering/core": "^0.6.28",
"@hcengineering/contact": "^0.6.20",
Expand Down
6 changes: 5 additions & 1 deletion server/tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ async function createUpdateIndexes (connection: CoreClient, db: Db, logger: Mode
}

for (const [d, v] of domains.entries()) {
const collInfo = await db.listCollections({ name: d }).next()
if (collInfo === null) {
await db.createCollection(d)
}
const collection = db.collection(d)
const bb: (string | FieldIndex<Doc>)[] = []
for (const vv of v.values()) {
Expand All @@ -286,7 +290,7 @@ async function createUpdateIndexes (connection: CoreClient, db: Db, logger: Mode
await collection.createIndex(vv)
}
} catch (err: any) {
logger.log('error', JSON.stringify(err))
logger.log('error: failed to create index', d, vv, JSON.stringify(err))
}
bb.push(vv)
}
Expand Down
2 changes: 1 addition & 1 deletion server/tool/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,6 @@ export class MigrateClientImpl implements MigrationClient {
}

async deleteMany<T extends Doc>(domain: Domain, query: DocumentQuery<T>): Promise<void> {
await this.db.collection(domain).deleteMany(query)
await this.db.collection<Doc>(domain).deleteMany(query as any)
}
}
2 changes: 1 addition & 1 deletion tests/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
mongodb:
image: mongo
image: 'mongo:7-jammy'
command: mongod --port 27018
environment:
- PUID=1000
Expand Down
1 change: 1 addition & 0 deletions tests/sanity/storage-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"name": "#platform.notification.timeout",
"value": "0"
},

{
"name": "#platform.testing.enabled",
"value": "true"
Expand Down
13 changes: 9 additions & 4 deletions tests/sanity/storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
"name": "login:metadata:LoginEndpoint",
"value": "ws://localhost:3334"
},
{
"name": "#platform.notification.logging",
"value": "false"
},
{
"name": "#platform.notification.timeout",
"value": "0"
},

{
"name": "#platform.testing.enabled",
"value": "true"
},
{
"name": "#platform.notification.logging",
"value": "false"
},
{
"name": "#platform.lazy.loading",
"value": "false"
Expand Down
5 changes: 1 addition & 4 deletions tests/sanity/storageSecond-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"name": "#platform.notification.timeout",
"value": "0"
},
{
"name": "#platform.notification.timeout",
"value": "0"
},

{
"name": "#platform.testing.enabled",
"value": "true"
Expand Down
9 changes: 9 additions & 0 deletions tests/sanity/storageSecond.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
"name": "login:metadata:LoginEndpoint",
"value": "ws://localhost:3334"
},
{
"name": "#platform.notification.timeout",
"value": "0"
},

{
"name": "#platform.testing.enabled",
"value": "true"
},
{
"name": "#platform.notification.logging",
"value": "false"
Expand Down
Loading
Loading