From f23b0b5bec892de6341fe9b1eece496042bc4cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 18 Aug 2023 11:59:42 -0400 Subject: [PATCH] fix: some more upgrade script fixes reduce parallel calls for psql --- src/upgrades/3.3.0/chat_message_mids.js | 5 +- src/upgrades/3.3.0/chat_room_online_zset.js | 2 +- src/upgrades/3.3.0/chat_room_refactor.js | 90 ++++++++++----------- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/upgrades/3.3.0/chat_message_mids.js b/src/upgrades/3.3.0/chat_message_mids.js index d7f349ae98..36c331f1a8 100644 --- a/src/upgrades/3.3.0/chat_message_mids.js +++ b/src/upgrades/3.3.0/chat_message_mids.js @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ 'use strict'; @@ -13,7 +14,7 @@ module.exports = { progress.total = await db.sortedSetCard(`chat:rooms`); await batch.processSortedSet(`chat:rooms`, async (roomIds) => { progress.incr(roomIds.length); - await Promise.all(roomIds.map(async (roomId) => { + for (const roomId of roomIds) { await batch.processSortedSet(`chat:room:${roomId}:mids`, async (mids) => { let messageData = await db.getObjects(mids.map(mid => `message:${mid}`)); messageData.forEach((m, idx) => { @@ -36,7 +37,7 @@ module.exports = { }, { batch: 500, }); - })); + } }, { batch: 500, }); diff --git a/src/upgrades/3.3.0/chat_room_online_zset.js b/src/upgrades/3.3.0/chat_room_online_zset.js index 0a57076fa0..409d8bc0ef 100644 --- a/src/upgrades/3.3.0/chat_room_online_zset.js +++ b/src/upgrades/3.3.0/chat_room_online_zset.js @@ -26,7 +26,7 @@ module.exports = { }); await db.sortedSetAddBulk(bulkAdd); }, { - batch: 500, + batch: 100, }); }, }; diff --git a/src/upgrades/3.3.0/chat_room_refactor.js b/src/upgrades/3.3.0/chat_room_refactor.js index 0d9242b10c..252484d18f 100644 --- a/src/upgrades/3.3.0/chat_room_refactor.js +++ b/src/upgrades/3.3.0/chat_room_refactor.js @@ -1,10 +1,10 @@ -'use strict'; +/* eslint-disable no-await-in-loop */ +'use strict'; const db = require('../../database'); const batch = require('../../batch'); - module.exports = { name: 'Update chat messages to add roomId field', timestamp: Date.UTC(2023, 6, 2), @@ -31,55 +31,51 @@ module.exports = { await batch.processArray(allRoomIds, async (roomIds) => { const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`)); - - await Promise.all(roomIds.map(async (roomId, index) => { - const roomData = arrayOfRoomData[index]; - if (!roomData) { - return; - } - - const midsSeen = Object.create(null); - await batch.processSortedSet(`chat:room:${roomId}:uids`, async (uids) => { - for (const uid of uids) { - // eslint-disable-next-line no-await-in-loop - await batch.processSortedSet(`uid:${uid}:chat:room:${roomId}:mids`, async (mids) => { - const uniqMids = mids.filter(mid => !midsSeen.hasOwnProperty(mid)); - if (!uniqMids.length) { - return; - } - - let messageData = await db.getObjects(uniqMids.map(mid => `message:${mid}`)); - messageData.forEach((m, idx) => { - if (m) { - m.mid = parseInt(uniqMids[idx], 10); + for (const roomData of arrayOfRoomData) { + if (roomData) { + const midsSeen = Object.create(null); + const { roomId } = roomData; + await batch.processSortedSet(`chat:room:${roomId}:uids`, async (uids) => { + for (const uid of uids) { + await batch.processSortedSet(`uid:${uid}:chat:room:${roomId}:mids`, async (mids) => { + const uniqMids = mids.filter(mid => !midsSeen.hasOwnProperty(mid)); + if (!uniqMids.length) { + return; } - }); - messageData = messageData.filter(Boolean); - const bulkSet = messageData.map( - msg => [`message:${msg.mid}`, { roomId: roomId }] - ); + let messageData = await db.getObjects(uniqMids.map(mid => `message:${mid}`)); + messageData.forEach((m, idx) => { + if (m) { + m.mid = parseInt(uniqMids[idx], 10); + } + }); + messageData = messageData.filter(Boolean); + + const bulkSet = messageData.map( + msg => [`message:${msg.mid}`, { roomId: roomId }] + ); - await db.setObjectBulk(bulkSet); - await db.sortedSetAdd( - `chat:room:${roomId}:mids`, - messageData.map(m => m.timestamp), - messageData.map(m => m.mid), - ); - uniqMids.forEach((mid) => { - midsSeen[mid] = 1; + await db.setObjectBulk(bulkSet); + await db.sortedSetAdd( + `chat:room:${roomId}:mids`, + messageData.map(m => m.timestamp), + messageData.map(m => m.mid), + ); + uniqMids.forEach((mid) => { + midsSeen[mid] = 1; + }); + }, { + batch: 500, }); - }, { - batch: 500, - }); - // eslint-disable-next-line no-await-in-loop - await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`); - } - progress.incr(uids.length); - }, { - batch: 500, - }); - })); + // eslint-disable-next-line no-await-in-loop + await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`); + } + progress.incr(uids.length); + }, { + batch: 500, + }); + } + } }, { batch: 500, });