1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

fix: variant table deadlocks (#4309)

This commit is contained in:
Mateusz Kwasniewski 2023-07-21 11:34:32 +02:00 committed by GitHub
parent 512b8a41ed
commit eaf002ff7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -179,11 +179,21 @@ export class ClientMetricsStoreV2 implements IClientMetricsStoreV2 {
await this.db.raw(query);
const variantRows = spreadVariants(metrics).map(toVariantRow);
if (variantRows.length > 0) {
// Sort the rows to avoid deadlocks
const sortedVariantRows = variantRows.sort(
(a, b) =>
a.feature_name.localeCompare(b.feature_name) ||
a.app_name.localeCompare(b.app_name) ||
a.environment.localeCompare(b.environment) ||
a.variant.localeCompare(b.variant),
);
if (sortedVariantRows.length > 0) {
const insertVariants = this.db<ClientMetricsEnvVariantTable>(
TABLE_VARIANTS,
)
.insert(variantRows)
.insert(sortedVariantRows)
.toQuery();
const variantsQuery = `${insertVariants.toString()} ON CONFLICT (feature_name, app_name, environment, timestamp, variant) DO UPDATE SET "count" = "client_metrics_env_variants"."count" + EXCLUDED.count`;
await this.db.raw(variantsQuery);