[backend] Refactor database transactions

This moves all code that isn't a direct call to transactionalEntityManager to outside of the transaction blocks, and removes all transaction blocks that were unnecessary
This commit is contained in:
Laura Hausmann 2023-10-25 15:23:57 +02:00
parent 7c56ee348b
commit 4dd8fdbd04
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
7 changed files with 248 additions and 262 deletions

View File

@ -1,6 +1,7 @@
import { db } from "@/db/postgre.js"; import { db } from "@/db/postgre.js";
import { Meta } from "@/models/entities/meta.js"; import { Meta } from "@/models/entities/meta.js";
import push from 'web-push'; import push from 'web-push';
import { Metas } from "@/models/index.js";
let cache: Meta; let cache: Meta;
@ -33,41 +34,31 @@ export function metaToPugArgs(meta: Meta): object {
export async function fetchMeta(noCache = false): Promise<Meta> { export async function fetchMeta(noCache = false): Promise<Meta> {
if (!noCache && cache) return cache; if (!noCache && cache) return cache;
return await db.transaction(async (transactionalEntityManager) => {
// New IDs are prioritized because multiple records may have been created due to past bugs. // New IDs are prioritized because multiple records may have been created due to past bugs.
const metas = await transactionalEntityManager.find(Meta, { const meta = await Metas.findOne({
where: {},
order: { order: {
id: "DESC", id: "DESC",
}, },
}); });
const meta = metas[0];
if (meta) { if (meta) {
cache = meta; cache = meta;
return meta; return meta;
} else { }
const { publicKey, privateKey } = push.generateVAPIDKeys();
// If fetchMeta is called at the same time when meta is empty, this part may be called at the same time, so use fail-safe upsert. const { publicKey, privateKey } = push.generateVAPIDKeys();
const saved = await transactionalEntityManager const data = {
.upsert(
Meta,
{
id: "x", id: "x",
swPublicKey: publicKey, swPublicKey: publicKey,
swPrivateKey: privateKey, swPrivateKey: privateKey,
}, };
["id"],
)
.then((x) =>
transactionalEntityManager.findOneByOrFail(Meta, x.identifiers[0]),
);
cache = saved; // If fetchMeta is called at the same time when meta is empty, this part may be called at the same time, so use fail-safe upsert.
return saved; await Metas.upsert(data, ["id"]);
}
}); cache = await Metas.findOneByOrFail({ id: data.id });
return cache;
} }
setInterval(() => { setInterval(() => {

View File

@ -294,13 +294,8 @@ export async function createPerson(
} }
} }
// Create user // Prepare objects
let user: IRemoteUser; let user = new User({
try {
// Start transaction
await db.transaction(async (transactionalEntityManager) => {
user = (await transactionalEntityManager.save(
new User({
id: genId(), id: genId(),
avatarId: null, avatarId: null,
bannerId: null, bannerId: null,
@ -342,11 +337,9 @@ export async function createPerson(
tags, tags,
isBot, isBot,
isCat: (person as any).isCat === true, isCat: (person as any).isCat === true,
}), }) as IRemoteUser;
)) as IRemoteUser;
await transactionalEntityManager.save( const profile = new UserProfile({
new UserProfile({
userId: user.id, userId: user.id,
description: person.summary description: person.summary
? await htmlToMfm(truncate(person.summary, summaryLength), person.tag) ? await htmlToMfm(truncate(person.summary, summaryLength), person.tag)
@ -356,18 +349,22 @@ export async function createPerson(
birthday: bday ? bday[0] : null, birthday: bday ? bday[0] : null,
location: person["vcard:Address"] || null, location: person["vcard:Address"] || null,
userHost: host, userHost: host,
}), });
);
if (person.publicKey) { const publicKey = person.publicKey
await transactionalEntityManager.save( ? new UserPublickey({
new UserPublickey({
userId: user.id, userId: user.id,
keyId: person.publicKey.id, keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem, keyPem: person.publicKey.publicKeyPem,
}), })
); : null;
}
try {
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.save(user);
await transactionalEntityManager.save(profile);
if (publicKey) await transactionalEntityManager.save(publicKey);
}); });
} catch (e) { } catch (e) {
// duplicate key error // duplicate key error
@ -754,21 +751,23 @@ export async function updateFeatured(userId: User["id"], resolver?: Resolver, li
.map((item) => limit(() => resolveNote(item, resolver, limiter))), .map((item) => limit(() => resolveNote(item, resolver, limiter))),
); );
await db.transaction(async (transactionalEntityManager) => { // Prepare the objects
await transactionalEntityManager.delete(UserNotePining, {
userId: user.id,
});
// For now, generate the id at a different time and maintain the order. // For now, generate the id at a different time and maintain the order.
const data: Partial<UserNotePining>[] = [];
let td = 0; let td = 0;
for (const note of featuredNotes.filter((note) => note != null)) { for (const note of featuredNotes.filter((note) => note != null)) {
td -= 1000; td -= 1000;
transactionalEntityManager.insert(UserNotePining, { data.push({
id: genId(new Date(Date.now() + td)), id: genId(new Date(Date.now() + td)),
createdAt: new Date(), createdAt: new Date(),
userId: user.id, userId: user.id,
noteId: note!.id, noteId: note!.id,
}); });
} }
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.delete(UserNotePining, { userId: user.id });
await transactionalEntityManager.insert(UserNotePining, data);
}); });
} }

View File

@ -84,19 +84,15 @@ export async function signup(opts: {
), ),
); );
let account!: User; const exist = await Users.findOneBy({
// Start transaction
await db.transaction(async (transactionalEntityManager) => {
const exist = await transactionalEntityManager.findOneBy(User, {
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
host: IsNull(), host: IsNull(),
}); });
if (exist) throw new Error(" the username is already used"); if (exist) throw new Error("The username is already in use");
account = await transactionalEntityManager.save( // Prepare objects
new User({ const user = new User({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
username: username, username: username,
@ -108,34 +104,35 @@ export async function signup(opts: {
host: IsNull(), host: IsNull(),
isAdmin: true, isAdmin: true,
})) === 0, })) === 0,
}),
);
await transactionalEntityManager.save(
new UserKeypair({
publicKey: keyPair[0],
privateKey: keyPair[1],
userId: account.id,
}),
);
await transactionalEntityManager.save(
new UserProfile({
userId: account.id,
autoAcceptFollowed: true,
password: hash,
}),
);
await transactionalEntityManager.save(
new UsedUsername({
createdAt: new Date(),
username: username.toLowerCase(),
}),
);
}); });
usersChart.update(account, true); const userKeypair = new UserKeypair({
publicKey: keyPair[0],
privateKey: keyPair[1],
userId: user.id,
});
const userProfile = new UserProfile({
userId: user.id,
autoAcceptFollowed: true,
password: hash,
});
const usedUsername = new UsedUsername({
createdAt: new Date(),
username: username.toLowerCase(),
});
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.save(user);
await transactionalEntityManager.save(userKeypair);
await transactionalEntityManager.save(userProfile);
await transactionalEntityManager.save(usedUsername);
});
const account = await Users.findOneByOrFail({ id: user.id });
usersChart.update(account, true);
return { account, secret }; return { account, secret };
} }

View File

@ -3,6 +3,7 @@ import { Meta } from "@/models/entities/meta.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { db } from "@/db/postgre.js"; import { db } from "@/db/postgre.js";
import define from "../../../define.js"; import define from "../../../define.js";
import { Metas } from "@/models/index.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -106,21 +107,19 @@ export default define(meta, paramDef, async (ps, me) => {
if (config.summalyProxyUrl !== undefined) { if (config.summalyProxyUrl !== undefined) {
set.summalyProxy = config.summalyProxyUrl; set.summalyProxy = config.summalyProxyUrl;
} }
await db.transaction(async (transactionalEntityManager) => {
const metas = await transactionalEntityManager.find(Meta, { const meta = await Metas.findOne({
where: {},
order: { order: {
id: "DESC", id: "DESC",
}, },
}); });
const meta = metas[0]; if (meta)
await Metas.update(meta.id, set);
else
await Metas.save(set);
if (meta) {
await transactionalEntityManager.update(Meta, meta.id, set);
} else {
await transactionalEntityManager.save(Meta, set);
}
});
insertModerationLog(me, "updateMeta"); insertModerationLog(me, "updateMeta");
} }
return hosted; return hosted;

View File

@ -2,6 +2,7 @@ import { Meta } from "@/models/entities/meta.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { db } from "@/db/postgre.js"; import { db } from "@/db/postgre.js";
import define from "../../define.js"; import define from "../../define.js";
import { Metas } from "@/models/index.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -546,21 +547,17 @@ export default define(meta, paramDef, async (ps, me) => {
} }
} }
await db.transaction(async (transactionalEntityManager) => { const meta = await Metas.findOne({
const metas = await transactionalEntityManager.find(Meta, { where: {},
order: { order: {
id: "DESC", id: "DESC",
}, },
}); });
const meta = metas[0]; if (meta)
await Metas.update(meta.id, set);
if (meta) { else
await transactionalEntityManager.update(Meta, meta.id, set); await Metas.save(set);
} else {
await transactionalEntityManager.save(Meta, set);
}
});
insertModerationLog(me, "updateMeta"); insertModerationLog(me, "updateMeta");
}); });

View File

@ -9,8 +9,9 @@ import { UserKeypair } from "@/models/entities/user-keypair.js";
import { UsedUsername } from "@/models/entities/used-username.js"; import { UsedUsername } from "@/models/entities/used-username.js";
import { db } from "@/db/postgre.js"; import { db } from "@/db/postgre.js";
import { hashPassword } from "@/misc/password.js"; import { hashPassword } from "@/misc/password.js";
import { Users } from "@/models/index.js";
export async function createSystemUser(username: string) { export async function createSystemUser(username: string): Promise<User> {
const password = uuid(); const password = uuid();
// Generate hash of password // Generate hash of password
@ -23,17 +24,15 @@ export async function createSystemUser(username: string) {
let account!: User; let account!: User;
// Start transaction const exist = await Users.findOneBy({
await db.transaction(async (transactionalEntityManager) => {
const exist = await transactionalEntityManager.findOneBy(User, {
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
host: IsNull(), host: IsNull(),
}); });
if (exist) throw new Error("the user is already exists"); if (exist) throw new Error("the user is already exists");
account = await transactionalEntityManager // Prepare objects
.insert(User, { const user = {
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
username: username, username: username,
@ -44,28 +43,32 @@ export async function createSystemUser(username: string) {
isLocked: true, isLocked: true,
isExplorable: false, isExplorable: false,
isBot: true, isBot: true,
}) };
.then((x) =>
transactionalEntityManager.findOneByOrFail(User, x.identifiers[0]),
);
await transactionalEntityManager.insert(UserKeypair, { const userKeypair = {
publicKey: keyPair.publicKey, publicKey: keyPair.publicKey,
privateKey: keyPair.privateKey, privateKey: keyPair.privateKey,
userId: account.id, userId: user.id,
}); };
await transactionalEntityManager.insert(UserProfile, { const userProfile = {
userId: account.id, userId: user.id,
autoAcceptFollowed: false, autoAcceptFollowed: false,
password: hash, password: hash,
}); };
await transactionalEntityManager.insert(UsedUsername, { const usedUsername = {
createdAt: new Date(), createdAt: new Date(),
username: username.toLowerCase(), username: username.toLowerCase(),
}); }
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.insert(User, user);
await transactionalEntityManager.insert(UserKeypair, userKeypair);
await transactionalEntityManager.insert(UserProfile, userProfile);
await transactionalEntityManager.insert(UsedUsername, usedUsername);
}); });
return account; return Users.findOneByOrFail({ id: user.id });
} }

View File

@ -756,12 +756,9 @@ async function insertNote(
// 投稿を作成 // 投稿を作成
try { try {
if (insert.hasPoll) { if (insert.hasPoll) {
// Start transaction // Prepare objects
await db.transaction(async (transactionalEntityManager) => {
if (!data.poll) throw new Error("Empty poll data"); if (!data.poll) throw new Error("Empty poll data");
await transactionalEntityManager.insert(Note, insert);
let expiresAt: Date | null; let expiresAt: Date | null;
if (!data.poll.expiresAt || isNaN(data.poll.expiresAt.getTime())) { if (!data.poll.expiresAt || isNaN(data.poll.expiresAt.getTime())) {
expiresAt = null; expiresAt = null;
@ -780,6 +777,9 @@ async function insertNote(
userHost: user.host, userHost: user.host,
}); });
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.insert(Note, insert);
await transactionalEntityManager.insert(Poll, poll); await transactionalEntityManager.insert(Poll, poll);
}); });
} else { } else {