core: handle sync delay in database cluster

This commit is contained in:
undefined committed 2025-10-03 18:36:16 +08:00
1 parent fb4ff1c196
commit fe437740d5
4 files changed
+16 -12

No files matched your search

+4 -4
View File
@@ -925,14 +925,14 @@ export class ProblemSolutionHandler extends ProblemDetailHandler {
@param('psid', Types.ObjectId)
async postUpvote(domainId: string, psid: ObjectId) {
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, 1);
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
const psdoc = await solution.vote(domainId, psid, this.user._id, 1);
this.back({ vote: psdoc.vote, user_vote: 1 });
}
@param('psid', Types.ObjectId)
async postDownvote(domainId: string, psid: ObjectId) {
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, -1);
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
const psdoc = await solution.vote(domainId, psid, this.user._id, -1);
this.back({ vote: psdoc.vote, user_vote: -1 });
}
}
+1 -1
View File
@@ -299,7 +299,7 @@ export async function setStatus<K extends keyof DocStatusType>(
): Promise<DocStatusType[K]> {
return await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid },
{ $set: args, $inc: { counter: 1 } },
{ $set: args },
{
upsert: true,
returnDocument,
+3 -6
View File
@@ -78,12 +78,9 @@ class SolutionModel {
);
let inc = value;
if (before?.vote) inc -= before.vote;
return [
inc
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
: doc,
await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid),
];
return inc
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
: doc;
}
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
+8 -1
View File
@@ -12,7 +12,7 @@ import pwhash from '../lib/hash.hydro';
import bus from '../service/bus';
import db from '../service/db';
import { Value } from '../typeutils';
import { ArgMethod, buildProjection, randomstring } from '../utils';
import { ArgMethod, buildProjection, randomstring, sleep } from '../utils';
import { PERM, PRIV } from './builtin';
import domain from './domain';
import * as setting from './setting';
@@ -346,6 +346,13 @@ class UserModel {
{ $set: { join: true } },
{ upsert: true },
);
// make sure user is immediately available after creation
// give some time for database to sync in replicas
for (let i = 1; i <= 10; i++) {
const udoc = await UserModel.getById('system', uid); // eslint-disable-line no-await-in-loop
if (udoc) break;
await sleep(500); // eslint-disable-line no-await-in-loop
}
return uid;
} catch (e) {
if (e?.code === 11000) {