Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discard pending data when dynamic ds is created
- Loading branch information
Showing
3 changed files
with
103 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020-2022 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getHeapStatistics } from 'v8'; | ||
import { getYargsOption } from '../yargs'; | ||
import { getLogger } from './logger'; | ||
|
||
const HIGH_THRESHOLD = 0.85; | ||
const LOW_THRESHOLD = 0.6; | ||
|
||
const { argv } = getYargsOption(); | ||
const logger = getLogger('memory'); | ||
|
||
export function checkMemoryUsage(batchSizeScale: number): number { | ||
const memoryData = getHeapStatistics(); | ||
const ratio = memoryData.used_heap_size / memoryData.heap_size_limit; | ||
if (argv.profiler) { | ||
logger.info(`Heap Statistics: ${JSON.stringify(memoryData)}`); | ||
logger.info(`Heap Usage: ${ratio}`); | ||
} | ||
let scale = batchSizeScale; | ||
|
||
if (ratio > HIGH_THRESHOLD) { | ||
if (scale > 0) { | ||
scale = Math.max(scale - 0.1, 0); | ||
logger.debug(`Heap usage: ${ratio}, decreasing batch size by 10%`); | ||
} | ||
} | ||
|
||
if (ratio < LOW_THRESHOLD) { | ||
if (scale < 1) { | ||
scale = Math.min(scale + 0.1, 1); | ||
logger.debug(`Heap usage: ${ratio} increasing batch size by 10%`); | ||
} | ||
} | ||
return scale; | ||
} |