Skip to content

Commit

Permalink
fix(condo): INFRA-424 notify about tooManyReturned exponentially (#5088)
Browse files Browse the repository at this point in the history
* fix(condo): INFRA-424 notify about tooManyReturned exponentially

* fix(condo): INFRA-424 add visible log limits
  • Loading branch information
pahaz authored Aug 12, 2024
1 parent 3365362 commit 4b7b8f1
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions apps/condo/domains/common/utils/serverSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { getLogger } = require('@open-condo/keystone/logging')
const { getSchemaCtx } = require('@open-condo/keystone/schema')

const GLOBAL_QUERY_LIMIT = 1000
const TOO_MANY_RETURNED_LOG_LIMITS = Object.freeze([1100, 4900, 9000, 14900, 49000, 149000])
const logger = getLogger('common/utils/serverSchema.js')

// When we load models with Apollo graphql - every relation on a field for every object makes sql request
Expand All @@ -21,6 +22,24 @@ const logger = getLogger('common/utils/serverSchema.js')
// Tested on tickets export for 24755 tickets: without knex 71220.902ms, with knex: 16094.841ms )
// TODO(zuch): find out how to make 1 request for 1. and 2.

function logTooManyReturnedIfRequired (tooManyReturnedLimitCounters, allObjects, { functionName, schemaName, data }) {
if (!Array.isArray(tooManyReturnedLimitCounters)) throw new Error('logTooManyReturned: wrong argument type')
if (tooManyReturnedLimitCounters.length <= 0) return // trying to notify only if have any counter

const realLimit = tooManyReturnedLimitCounters[0]

if (allObjects && Array.isArray(allObjects) && allObjects.length > realLimit) {
logger.warn({
msg: 'tooManyReturned',
tooManyLimit: realLimit,
functionName,
schemaName,
data,
})
tooManyReturnedLimitCounters.shift() // remove counter and mark as already notified
}
}

class GqlWithKnexLoadList {

constructor ({ listKey, fields, singleRelations = [], multipleRelations = [], where = {}, sortBy = [] }) {
Expand All @@ -40,26 +59,21 @@ class GqlWithKnexLoadList {
let skip = 0
let newchunk = []
let all = []
let haveNotifiedAboutTooManyObjs = false
let tooManyReturnedLimitCounters = [...TOO_MANY_RETURNED_LOG_LIMITS]

let maxiterationsCount = 100 // we need some limits - 100K records is more then enough
do {
newchunk = await this.loadChunk(skip)
all = all.concat(newchunk)
skip += newchunk.length

if (!haveNotifiedAboutTooManyObjs && all && Array.isArray(all) && all.length > 1000) {
logger.warn({
msg: 'tooManyReturned',
functionName: 'GqlWithKnexLoadList.load',
schemaName: this.listKey,
data: {
limit: 1000,
GqlWithKnexLoadListArgs: { singleRelations: this.singleRelations, multipleRelations: this.multipleRelations, where: this.where, fields: this.fields },
},
})
haveNotifiedAboutTooManyObjs = true
}
logTooManyReturnedIfRequired(tooManyReturnedLimitCounters, all, {
functionName: 'GqlWithKnexLoadList.load',
schemaName: this.listKey,
data: {
singleRelations: this.singleRelations, multipleRelations: this.multipleRelations, where: this.where, fields: this.fields,
},
})

if (newchunk.length < GLOBAL_QUERY_LIMIT) {
break
Expand Down

0 comments on commit 4b7b8f1

Please sign in to comment.