UBER-266: Fix mongo exceptions (#3267)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2023-05-27 14:13:24 +07:00 committed by GitHub
parent 4fad67b6a4
commit b95066d71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -223,10 +223,8 @@ export async function extractIndexedValues (
continue
}
// Check if attribute is indexable
let keyAttr: AnyAttribute
try {
keyAttr = hierarchy.getAttribute(_class, attr)
} catch (err: any) {
const keyAttr: AnyAttribute | undefined = hierarchy.findAttribute(_class, attr)
if (keyAttr === undefined) {
// Skip if there is no attribute.
continue
}

View File

@ -617,14 +617,23 @@ abstract class MongoAdapterBase implements DbAdapter {
async update (domain: Domain, operations: Map<Ref<Doc>, DocumentUpdate<Doc>>): Promise<void> {
const coll = this.db.collection(domain)
try {
// remove old and insert new ones
const ops = Array.from(operations.entries())
if (ops.length > 0) {
const part = ops.splice(0, 500)
// remove old and insert new ones
const ops = Array.from(operations.entries())
let skip = 500
while (ops.length > 0) {
const part = ops.splice(0, skip)
try {
await coll.bulkWrite(
part.map((it) => {
const { $unset, ...set } = it[1] as any
if ($unset !== undefined) {
for (const k of Object.keys(set)) {
if ($unset[k] === '') {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete $unset[k]
}
}
}
return {
updateOne: {
filter: { _id: it[0] },
@ -636,9 +645,13 @@ abstract class MongoAdapterBase implements DbAdapter {
}
})
)
} catch (err: any) {
if (skip !== 1) {
ops.push(...part)
skip = 1 // Let's update one by one, to loose only one failed variant.
}
console.error(err)
}
} catch (err: any) {
console.error(err)
}
}