core: fix problem solution vote race condition (#861)

This commit is contained in:
undefined authored and GitHub committed 2024-08-16 01:18:14 +08:00
1 parent 598972b1c9
commit 8903486c6c
6 files changed
+54 -51

No files matched your search

+2 -2
View File
@@ -5,8 +5,8 @@
"main": "react.yaml", "main": "react.yaml",
"dependencies": { "dependencies": {
"@stylistic/eslint-plugin": "^2.6.2", "@stylistic/eslint-plugin": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^8.0.1", "@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.0.1", "@typescript-eslint/parser": "^8.1.0",
"eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb": "^19.0.4",
"eslint-plugin-github": "^5.0.1", "eslint-plugin-github": "^5.0.1",
"eslint-plugin-import": "2.29.1", "eslint-plugin-import": "2.29.1",
+1 -1
View File
@@ -86,7 +86,7 @@ class DataReportHandler extends Handler {
this.ctx.emit('center/report', this, installId, old, payload); this.ctx.emit('center/report', this, installId, old, payload);
// TODO deliver messages // TODO deliver messages
this.response.body = { code: 0 }; this.response.body = { code: 0 };
if (old.notification) this.response.body.notification = old?.notification; if (old?.notification) this.response.body.notification = old?.notification;
} }
} }
+5 -5
View File
@@ -12,11 +12,11 @@
}, },
"preferUnplugged": true, "preferUnplugged": true,
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "3.627.0", "@aws-sdk/client-s3": "3.631.0",
"@aws-sdk/lib-storage": "3.627.0", "@aws-sdk/lib-storage": "3.631.0",
"@aws-sdk/s3-presigned-post": "3.627.0", "@aws-sdk/s3-presigned-post": "3.631.0",
"@aws-sdk/s3-request-presigner": "3.627.0", "@aws-sdk/s3-request-presigner": "3.631.0",
"@graphql-tools/schema": "^10.0.4", "@graphql-tools/schema": "^10.0.5",
"@hydrooj/framework": "workspace:^", "@hydrooj/framework": "workspace:^",
"@hydrooj/utils": "workspace:^", "@hydrooj/utils": "workspace:^",
"@simplewebauthn/server": "9.0.3", "@simplewebauthn/server": "9.0.3",
+30 -33
View File
@@ -302,12 +302,18 @@ export function getMultiStatusWithoutDomain<K extends keyof DocStatusType>(
} }
export async function setStatus<K extends keyof DocStatusType>( export async function setStatus<K extends keyof DocStatusType>(
domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, args: UpdateFilter<DocStatusType[K]>['$set'], domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number,
args: UpdateFilter<DocStatusType[K]>['$set'], returnDocument: 'before' | 'after' = 'after',
): Promise<DocStatusType[K]> { ): Promise<DocStatusType[K]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid }, { domainId, docType, docId, uid },
{ $set: args }, { $set: args, $inc: { counter: 1 } },
{ upsert: true, returnDocument: 'after' }, {
upsert: true,
returnDocument,
// if fetching document before update we want to ensure data was read from primary
...(returnDocument === 'before' ? { readConcern: { level: 'majority' }, readPreference: 'primary' } : {}),
},
); );
return res.value; return res.value;
} }
@@ -321,41 +327,32 @@ export async function setMultiStatus<K extends keyof DocStatusType>(
); );
} }
export async function setStatusIfNotCondition<T extends keyof DocStatusType>(
domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
filter: Filter<DocStatusType[T]>, args: Partial<DocStatusType[T]> = {},
returnDocument: 'before' | 'after' = 'after',
): Promise<DocStatusType[T]> {
try {
const res = await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid, $nor: [filter] },
{ $set: args },
{ upsert: true, returnDocument },
);
return res.value;
} catch (e) {
return false;
}
}
export async function setIfNotStatus<T extends keyof DocStatusType, K extends keyof DocStatusType[T]>( export async function setIfNotStatus<T extends keyof DocStatusType, K extends keyof DocStatusType[T]>(
domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: K, value: DocStatusType[T][K], ifNot: DocStatusType[T][K], args: Partial<DocStatusType[T]>, key: K, value: DocStatusType[T][K], ifNot: DocStatusType[T][K], args: Partial<DocStatusType[T]>,
returnDocument: 'before' | 'after' = 'after',
): Promise<DocStatusType[T]> { ): Promise<DocStatusType[T]> {
const current: Partial<DocStatusType[T]> = await collStatus.findOne({ domainId, docType, docId, uid }) || {}; return await setStatusIfNotCondition(
if (typeof key === 'string' && key.includes('.')) { domainId, docType, docId, uid, { [key]: ifNot } as any,
const acc = key.split('.'); { [key]: value, ...args }, returnDocument,
let c = current;
for (const i of acc) {
if (!(i in c)) break;
c = c[i];
if (c === null || c === undefined) break;
}
if (c === ifNot) return null;
} else if (current[key] === ifNot) return null;
const res = await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid },
{ $set: { [key]: value, ...args } },
{ upsert: true, returnDocument: 'after' },
); );
return res.value;
}
export async function setStatusIfNotCondition<T extends keyof DocStatusType>(
domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
filter: Filter<DocStatusType[T]>, args: Partial<DocStatusType[T]>,
): Promise<DocStatusType[T]> {
const match = await collStatus.findOne({ domainId, docType, docId, uid, ...filter });
if (match) return null;
const res = await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid },
{ $set: args },
{ upsert: true, returnDocument: 'after' },
);
return res.value;
} }
export async function cappedIncStatus<T extends keyof DocStatusType>( export async function cappedIncStatus<T extends keyof DocStatusType>(
+12 -6
View File
@@ -70,12 +70,18 @@ class SolutionModel {
} }
static async vote(domainId: string, psid: ObjectId, uid: number, value: number) { static async vote(domainId: string, psid: ObjectId, uid: number, value: number) {
let pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid); const before = await document.setStatus(
await document.setStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid, { vote: value }); domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid,
if (pssdoc) value += -pssdoc.vote; { vote: value }, 'before',
const psdoc = await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', value); );
pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid); let inc = value;
return [psdoc, pssdoc]; if (before?.vote) inc -= before.vote;
return [
inc
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
: await document.get(domainId, document.TYPE_PROBLEM_SOLUTION, psid),
await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid),
];
} }
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) { static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
+4 -4
View File
@@ -25,8 +25,8 @@
"@fontsource/source-code-pro": "^5.0.18", "@fontsource/source-code-pro": "^5.0.18",
"@fontsource/ubuntu-mono": "^5.0.20", "@fontsource/ubuntu-mono": "^5.0.20",
"@hydrooj/utils": "workspace:^", "@hydrooj/utils": "workspace:^",
"@sentry/browser": "^8.25.0", "@sentry/browser": "^8.26.0",
"@sentry/webpack-plugin": "^2.22.0", "@sentry/webpack-plugin": "^2.22.2",
"@simplewebauthn/browser": "9.0.1", "@simplewebauthn/browser": "9.0.1",
"@svgr/webpack": "^8.1.0", "@svgr/webpack": "^8.1.0",
"@types/gulp-if": "^0.0.34", "@types/gulp-if": "^0.0.34",
@@ -76,7 +76,7 @@
"jquery.easing": "^1.4.1", "jquery.easing": "^1.4.1",
"jquery.transit": "^0.9.12", "jquery.transit": "^0.9.12",
"matchmedia-polyfill": "^0.3.2", "matchmedia-polyfill": "^0.3.2",
"md-editor-rt": "^4.18.0", "md-editor-rt": "^4.18.1",
"mini-css-extract-plugin": "^2.9.0", "mini-css-extract-plugin": "^2.9.0",
"moment": "^2.30.1", "moment": "^2.30.1",
"monaco-editor": "0.50.0", "monaco-editor": "0.50.0",
@@ -141,7 +141,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"markdown-it": "^14.1.0", "markdown-it": "^14.1.0",
"markdown-it-anchor": "^9.0.1", "markdown-it-anchor": "^9.0.1",
"markdown-it-attrs": "^4.1.6", "markdown-it-attrs": "^4.2.0",
"markdown-it-footnote": "^4.0.0", "markdown-it-footnote": "^4.0.0",
"markdown-it-imsize": "^2.0.1", "markdown-it-imsize": "^2.0.1",
"markdown-it-mark": "^4.0.0", "markdown-it-mark": "^4.0.0",