Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ingestion): optionally serialize the calls to loadPlugin to limit memory usage #17391

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf(ingestion): limit concurrency when setting-up plugins
  • Loading branch information
xvello committed Sep 12, 2023
commit 22e3b4d9c15c3e54161322da7517c98a69420250
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"posthog-js": "1.78.2",
"posthog-js-lite": "2.0.0-alpha5",
"prettier": "^2.8.8",
"prom-utils": "^0.5.0",
"prop-types": "^15.7.2",
"query-selector-shadow-dom": "^1.0.0",
"rc-field-form": "~1.21.0",
Expand Down
1 change: 1 addition & 0 deletions plugin-server/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export function getDefaultConfig(): PluginsServerConfig {
OBJECT_STORAGE_SECRET_ACCESS_KEY: 'object_storage_root_password',
OBJECT_STORAGE_BUCKET: 'posthog',
PLUGIN_SERVER_MODE: null,
PLUGIN_LOAD_CONCURRENCY: 50,
KAFKAJS_LOG_LEVEL: 'WARN',
HISTORICAL_EXPORTS_ENABLED: true,
HISTORICAL_EXPORTS_MAX_RETRY_COUNT: 15,
Expand Down
1 change: 1 addition & 0 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export interface PluginsServerConfig {
OBJECT_STORAGE_SECRET_ACCESS_KEY: string
OBJECT_STORAGE_BUCKET: string // the object storage bucket name
PLUGIN_SERVER_MODE: PluginServerMode | null
PLUGIN_LOAD_CONCURRENCY: number // concurrency of the queue executing loadPlugin calls, to reduce heap usage spikes
KAFKAJS_LOG_LEVEL: 'NOTHING' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
HISTORICAL_EXPORTS_ENABLED: boolean // enables historical exports for export apps
HISTORICAL_EXPORTS_MAX_RETRY_COUNT: number
Expand Down
8 changes: 5 additions & 3 deletions plugin-server/src/worker/plugins/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { rateLimit } from 'prom-utils'

import { Hub, StatelessVmMap } from '../../types'
import { LazyPluginVM } from '../vm/lazy'
import { loadPlugin } from './loadPlugin'
Expand All @@ -7,9 +9,9 @@ import { teardownPlugins } from './teardown'

export async function setupPlugins(hub: Hub): Promise<void> {
const { plugins, pluginConfigs, pluginConfigsPerTeam } = await loadPluginsFromDB(hub)
const pluginVMLoadPromises: Array<Promise<any>> = []
const statelessVms = {} as StatelessVmMap

const limiter = rateLimit(hub.PLUGIN_LOAD_CONCURRENCY)
const timer = new Date()

for (const [id, pluginConfig] of pluginConfigs) {
Expand All @@ -26,7 +28,7 @@ export async function setupPlugins(hub: Hub): Promise<void> {
pluginConfig.vm = statelessVms[plugin.id]
} else {
pluginConfig.vm = new LazyPluginVM(hub, pluginConfig)
pluginVMLoadPromises.push(loadPlugin(hub, pluginConfig))
await limiter.add(loadPlugin(hub, pluginConfig)) // Will block if PLUGIN_LOAD_CONCURRENCY is reached

if (prevConfig) {
void teardownPlugins(hub, prevConfig)
Expand All @@ -38,7 +40,7 @@ export async function setupPlugins(hub: Hub): Promise<void> {
}
}

await Promise.all(pluginVMLoadPromises)
await limiter.finish()
hub.statsd?.timing('setup_plugins.success', timer)

hub.plugins = plugins
Expand Down
23 changes: 19 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.