From 6c60777703b32e9601dc9b20ac330c8123911677 Mon Sep 17 00:00:00 2001 From: JivusAyrus Date: Wed, 6 Nov 2024 18:47:16 +0530 Subject: [PATCH 1/3] fix: max call stack reached while inserting operation usage of checks --- .../src/core/repositories/SchemaCheckRepository.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/controlplane/src/core/repositories/SchemaCheckRepository.ts b/controlplane/src/core/repositories/SchemaCheckRepository.ts index 9f4595e763..312f3d050c 100644 --- a/controlplane/src/core/repositories/SchemaCheckRepository.ts +++ b/controlplane/src/core/repositories/SchemaCheckRepository.ts @@ -92,7 +92,7 @@ export class SchemaCheckRepository { .returning(); } - public async createOperationUsage( + public createOperationUsage( schemaCheckActionOperations: Map, federatedGraphId: string, ) { @@ -120,7 +120,17 @@ export class SchemaCheckRepository { return; } - await this.db.insert(schemaCheckChangeActionOperationUsage).values(values).execute(); + const arrayOfValues: NewSchemaChangeOperationUsage[][] = []; + for (let i = 0; i < values.length; i += 20) { + arrayOfValues.push(values.slice(i, i + 20)); + } + + return this.db.transaction(async (tx) => { + const promises = arrayOfValues.map((values) => + tx.insert(schemaCheckChangeActionOperationUsage).values(values).execute(), + ); + await Promise.all(promises); + }); } private mapChangesFromDriverValue = (val: any) => { From 7800a1f0cc9b09f19aba6f6ce8d3976a682eae70 Mon Sep 17 00:00:00 2001 From: JivusAyrus Date: Thu, 7 Nov 2024 18:36:13 +0530 Subject: [PATCH 2/3] fix: pr suggestion --- .../core/repositories/SchemaCheckRepository.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/controlplane/src/core/repositories/SchemaCheckRepository.ts b/controlplane/src/core/repositories/SchemaCheckRepository.ts index 312f3d050c..967114952c 100644 --- a/controlplane/src/core/repositories/SchemaCheckRepository.ts +++ b/controlplane/src/core/repositories/SchemaCheckRepository.ts @@ -92,7 +92,7 @@ export class SchemaCheckRepository { .returning(); } - public createOperationUsage( + public async createOperationUsage( schemaCheckActionOperations: Map, federatedGraphId: string, ) { @@ -121,16 +121,13 @@ export class SchemaCheckRepository { } const arrayOfValues: NewSchemaChangeOperationUsage[][] = []; - for (let i = 0; i < values.length; i += 20) { - arrayOfValues.push(values.slice(i, i + 20)); + for (let i = 0; i < values.length; i += 500) { + arrayOfValues.push(values.slice(i, i + 500)); } - return this.db.transaction(async (tx) => { - const promises = arrayOfValues.map((values) => - tx.insert(schemaCheckChangeActionOperationUsage).values(values).execute(), - ); - await Promise.all(promises); - }); + for (const values of arrayOfValues) { + await this.db.insert(schemaCheckChangeActionOperationUsage).values(values).execute(); + } } private mapChangesFromDriverValue = (val: any) => { From 12f58d819459d30e18c0df4144a14960a529d3a1 Mon Sep 17 00:00:00 2001 From: JivusAyrus Date: Thu, 7 Nov 2024 20:28:10 +0530 Subject: [PATCH 3/3] fix: pr suggestion --- .../src/core/repositories/SchemaCheckRepository.ts | 6 ++---- controlplane/src/core/util.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/controlplane/src/core/repositories/SchemaCheckRepository.ts b/controlplane/src/core/repositories/SchemaCheckRepository.ts index 967114952c..8f882c5dd0 100644 --- a/controlplane/src/core/repositories/SchemaCheckRepository.ts +++ b/controlplane/src/core/repositories/SchemaCheckRepository.ts @@ -13,6 +13,7 @@ import { import { ComposedFederatedGraph } from '../composition/composer.js'; import { SchemaDiff } from '../composition/schemaCheck.js'; import { InspectorOperationResult } from '../services/SchemaUsageTrafficInspector.js'; +import { createBatches } from '../util.js'; import { FederatedGraphConfig } from './FederatedGraphRepository.js'; export class SchemaCheckRepository { @@ -120,10 +121,7 @@ export class SchemaCheckRepository { return; } - const arrayOfValues: NewSchemaChangeOperationUsage[][] = []; - for (let i = 0; i < values.length; i += 500) { - arrayOfValues.push(values.slice(i, i + 500)); - } + const arrayOfValues: NewSchemaChangeOperationUsage[][] = createBatches(values, 500); for (const values of arrayOfValues) { await this.db.insert(schemaCheckChangeActionOperationUsage).values(values).execute(); diff --git a/controlplane/src/core/util.ts b/controlplane/src/core/util.ts index 85c9858434..05c43f7aec 100644 --- a/controlplane/src/core/util.ts +++ b/controlplane/src/core/util.ts @@ -427,3 +427,14 @@ export function mergeUrls(baseUrl: string, relativeUrl: string) { return new URL(relativeUrl, baseUrl).toString(); } + +export function createBatches(array: T[], batchSize: number): T[][] { + const batches: T[][] = []; + + for (let i = 0; i < array.length; i += batchSize) { + const batch = array.slice(i, i + batchSize); + batches.push(batch); + } + + return batches; +}