From 1d9a67189a2b30981df1129fe7427165f94fd369 Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Fri, 3 Nov 2023 14:06:13 +0100 Subject: [PATCH] fix: last seen deadlocks (#5264) Sort the items before inserting them into the database in order to reduce the chance of deadlocks happening when multiple pods are inserting at the same time. --- .../last-seen/last-seen-store.ts | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/services/client-metrics/last-seen/last-seen-store.ts b/src/lib/services/client-metrics/last-seen/last-seen-store.ts index 9dbd4059cc..cc1deb6930 100644 --- a/src/lib/services/client-metrics/last-seen/last-seen-store.ts +++ b/src/lib/services/client-metrics/last-seen/last-seen-store.ts @@ -14,6 +14,26 @@ export interface FeaturesTable { environment: string; } +const prepareLastSeenInput = (data: LastSeenInput[]) => { + const now = new Date(); + + const sortedData = data.sort( + (a, b) => + a.featureName.localeCompare(b.featureName) || + a.environment.localeCompare(b.environment), + ); + + const inserts = sortedData.map((item) => { + return { + feature_name: item.featureName, + environment: item.environment, + last_seen_at: now, + }; + }); + + return inserts; +}; + export default class LastSeenStore implements ILastSeenStore { private db: Db; @@ -32,18 +52,10 @@ export default class LastSeenStore implements ILastSeenStore { } async setLastSeen(data: LastSeenInput[]): Promise { - const now = new Date(); - try { - const inserts = data.map((item) => { - return { - feature_name: item.featureName, - environment: item.environment, - last_seen_at: now, - }; - }); + const inserts = prepareLastSeenInput(data); - const batchSize = 1000; + const batchSize = 500; for (let i = 0; i < inserts.length; i += batchSize) { const batch = inserts.slice(i, i + batchSize);