From 04bb873191840d07f1c1000597f34ef5d33dc53c Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Mon, 15 Apr 2024 16:11:53 +0700 Subject: [PATCH] UBERF-6557: Clean old domains during clone of workspace to new place (#5361) Signed-off-by: Andrey Sobolev --- server/backup/src/index.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/server/backup/src/index.ts b/server/backup/src/index.ts index 28c03c5c2e..5cba896145 100644 --- a/server/backup/src/index.ts +++ b/server/backup/src/index.ts @@ -287,6 +287,9 @@ export async function cloneWorkspace ( console.log('Retrieve chunk:', needRetrieve.length) let docs: Doc[] = [] try { + // We need to clean target connection before copying something. + await cleanDomain(targetConnection, c) + docs = await sourceConnection.loadDocs(c, needRetrieve) if (clearTime) { docs = docs.map((p) => { @@ -337,6 +340,32 @@ export async function cloneWorkspace ( } } +async function cleanDomain (connection: CoreClient & BackupClient, domain: Domain): Promise { + // Load all digest from collection. + let idx: number | undefined + const ids: Ref[] = [] + while (true) { + try { + const it = await connection.loadChunk(domain, idx) + idx = it.idx + + ids.push(...it.docs.map((it) => it.id as Ref)) + if (it.finished) { + break + } + } catch (err: any) { + console.error(err) + if (idx !== undefined) { + await connection.closeChunk(idx) + } + } + } + while (ids.length > 0) { + const part = ids.splice(0, 5000) + await connection.clean(domain, part) + } +} + /** * @public */