From 828ae0048918814a027f226c28be27033324bbc7 Mon Sep 17 00:00:00 2001 From: MarioDu Date: Wed, 24 Jan 2024 14:12:34 +0800 Subject: [PATCH] feat: worker default ema config (#103) FEAT: AONE#54578464 --- src/config/default.ts | 14 ++++++++++++++ src/config/index.ts | 24 ++++++++++++++++++++++++ src/lib/function_profile.ts | 14 ++++++++++++++ src/lib/json/function_profile.ts | 6 +++--- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/config/default.ts b/src/config/default.ts index ae9a05b..09d3988 100644 --- a/src/config/default.ts +++ b/src/config/default.ts @@ -1,3 +1,4 @@ +import { ConcurrencyStatsMode } from '#self/lib/json/function_profile'; import path from 'path'; const projectRoot = path.resolve(__dirname, '../../'); @@ -42,6 +43,19 @@ export default { maxActivateRequests: 10, defaultInitializerTimeout: 10_000, replicaCountLimit: 10, + + // ema 相关默认值 + concurrencySlidingWindowSize: 60_000, + concurrencySlidingBucketCount: 6, + emaConcurrencyAlpha: 0.5, + concurrencyExpandThreshold: 0.7, + concurrencyShrinkThreshold: 0.3, + expandCooldown: 1000, + shrinkCooldown: 60_000, + scaleFactor: 0.5, + precisionZeroThreshold: 0.01, + concurrencyStatsMode: ConcurrencyStatsMode.INSTANT, + shrinkCooldownOnStartup: true, }, starter: { aworker: { diff --git a/src/config/index.ts b/src/config/index.ts index b18cd34..3bf23df 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,6 +1,7 @@ import type { LoggerLevel } from '@midwayjs/logger'; import type { ChannelOptions } from '@grpc/grpc-js'; import { resolveConfig } from './loader'; +import { ConcurrencyStatsMode } from '#self/lib/json/function_profile'; export const config = resolveConfig(); export { dumpConfig } from './loader'; @@ -204,6 +205,29 @@ export interface WorkerDefaultConfig { * 默认为 10 */ replicaCountLimit: number; + + // 并发度滑动时间窗口大小,单位 ms,默认 60s + concurrencySlidingWindowSize?: number; + // 并发度滑动时间窗口分桶数,默认 6 + concurrencySlidingBucketCount?: number; + // 指数移动平均平滑系数 (0,1],默认 0.5 + emaConcurrencyAlpha?: number; + // 并发度扩容水位阈值,默认 0.7 + concurrencyExpandThreshold?: number; + // 并发度缩容水位阈值,默认 0.3 + concurrencyShrinkThreshold?: number; + // 扩容冷却时间,单位 ms,默认 1s + expandCooldown?: number; + // 缩容冷却时间,单位 ms,默认 60s + shrinkCooldown?: number; + // 扩缩容后并发度水位,影响扩缩容操作数量 + scaleFactor?: number; + // ema concurrency 小于该值则视为 0 + precisionZeroThreshold?: number; + // worker 并发度统计算法,默认为 ConcurrencyStatsMode.INSTANT + concurrencyStatsMode?: ConcurrencyStatsMode; + // 启动后是否进入缩容冷却期,默认为 true + shrinkCooldownOnStartup?: boolean; } export interface StarterConfig { diff --git a/src/lib/function_profile.ts b/src/lib/function_profile.ts index 5c1f03b..71ff053 100644 --- a/src/lib/function_profile.ts +++ b/src/lib/function_profile.ts @@ -52,6 +52,20 @@ function buildProfile( disableRequestQueue: false, v8Options: [], execArgv: [], + + concurrencySlidingWindowSize: config.worker.concurrencySlidingWindowSize, + concurrencySlidingBucketCount: + config.worker.concurrencySlidingBucketCount, + emaConcurrencyAlpha: config.worker.emaConcurrencyAlpha, + concurrencyExpandThreshold: config.worker.concurrencyExpandThreshold, + concurrencyShrinkThreshold: config.worker.concurrencyShrinkThreshold, + expandCooldown: config.worker.expandCooldown, + shrinkCooldown: config.worker.shrinkCooldown, + scaleFactor: config.worker.scaleFactor, + precisionZeroThreshold: config.worker.precisionZeroThreshold, + concurrencyStatsMode: config.worker.concurrencyStatsMode, + shrinkCooldownOnStartup: config.worker.shrinkCooldownOnStartup, + ...(json.worker ?? {}), }, environments: json.environments ?? [], diff --git a/src/lib/json/function_profile.ts b/src/lib/json/function_profile.ts index 4f8c062..f9deb76 100644 --- a/src/lib/json/function_profile.ts +++ b/src/lib/json/function_profile.ts @@ -45,11 +45,11 @@ export interface ProcessFunctionProfile { expandCooldown?: number; // 缩容冷却时间,单位 ms,默认 60s shrinkCooldown?: number; - // 扩缩容后并发度水位,影响扩缩容操作数量 + // 扩缩容后并发度水位,影响扩缩容操作数量,默认为 0.5 scaleFactor?: number; - // ema concurrency 小于该值则视为 0 + // ema concurrency 小于该值则视为 0,默认为 0.01 precisionZeroThreshold?: number; - // worker 并发度统计算法 + // worker 并发度统计算法,默认为 ConcurrencyStatsMode.INSTANT concurrencyStatsMode?: ConcurrencyStatsMode; // 启动后是否进入缩容冷却期,默认为 true shrinkCooldownOnStartup?: boolean;