From 2691ac930e8785a1a903023d62363234ccd2b65f Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Tue, 22 Feb 2022 16:09:43 +0700 Subject: [PATCH] Fix elastic restore (#1038) Signed-off-by: Andrey Sobolev --- dev/tool/src/elastic.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dev/tool/src/elastic.ts b/dev/tool/src/elastic.ts index a1a54caa77..a0d9e5a9c7 100644 --- a/dev/tool/src/elastic.ts +++ b/dev/tool/src/elastic.ts @@ -195,6 +195,7 @@ async function restoreElastic (mongoUrl: string, dbName: string, minio: Client, const isCollectionCreateTx = (tx: Tx): boolean => tx._class === core.class.TxCollectionCUD && (tx as TxCollectionCUD).tx._class === core.class.TxCreateDoc const tdata = data.filter(tx => isCreateTx(tx) || isMixinTx(tx) || isCollectionCreateTx(tx)) + const removedDocument = new Set>() for (const tx of tdata) { pos++ if (pos % 5000 === 0) { @@ -219,12 +220,27 @@ async function restoreElastic (mongoUrl: string, dbName: string, minio: Client, } catch (err: any) { console.error('failed to replay tx', tx, err.message) } + } else { + removedDocument.add(createTx.objectId) } } // We need process mixins. if (isMixinTx(tx)) { - await tool.storage.tx(metricsCtx, tx) + try { + let deleted = false + if (tx._class === core.class.TxMixin) { + deleted = removedDocument.has((tx as TxMixin).objectId) + } + if (tx._class === core.class.TxCollectionCUD && (tx as TxCollectionCUD).tx._class === core.class.TxMixin) { + deleted = removedDocument.has((tx as TxCollectionCUD).tx.objectId) + } + if (!deleted) { + await tool.storage.tx(metricsCtx, tx) + } + } catch (err: any) { + console.error('failed to replay tx', tx, err.message) + } } // We need process collection creations. @@ -273,6 +289,7 @@ async function restoreElastic (mongoUrl: string, dbName: string, minio: Client, console.log('replay elastic transactions done') console.log(metricsToString(m)) } finally { + console.log('Elastic restore done') await done() } }