运行时插件#

LMCache 运行时插件系统提供了通过在 LMCache 进程旁边运行自定义脚本来扩展功能的能力。目前,插件可以用 Python 和 Bash 编写,并由 RuntimePluginLauncher 类管理。

关键使用案例#

  • 启动度量报告器以进行集中监控

  • 实现日志报告器以用于日志收集系统

  • 将进程级别的指标报告给警报系统

  • 实现健康检查和服务发现

  • 自定义缓存管理操作

配置#

运行时插件通过环境变量和配置文件进行配置:

环境变量: - LMCACHE_RUNTIME_PLUGIN_ROLE:进程角色(例如,SCHEDULER,WORKER) - LMCACHE_RUNTIME_PLUGIN_CONFIG:包含插件配置的 JSON 字符串 - LMCACHE_RUNTIME_PLUGIN_WORKER_ID:当前工作线程 ID - LMCACHE_RUNTIME_PLUGIN_WORKER_COUNT:集群中的总工作线程数

配置文件 (lmcache.yaml):

runtime_plugin_locations:
  - "/path/to/plugins"

extra_config:
  custom_setting: value

运行时插件命名约定#

插件文件名决定执行目标:

角色特定插件: - 格式:<ROLE>[_<WORKER_ID>][_<DESCRIPTION>].<EXTENSION> - 示例:

  • scheduler_foo_plugin.py:仅在 SCHEDULER 上运行

  • worker_0_test.sh: 仅在工作节点 ID 0 上运行

  • all_plugin.sh: 在所有工作节点上运行

注意:- 角色名称不区分大小写 - 指定时工作节点 ID 必须为数字 - 要针对特定的工作节点 ID,文件名必须至少包含三个由下划线分隔的部分(例如,worker_<ID>_<DESCRIPTION>.ext)。名为 worker_<DESCRIPTION>.ext 的文件将在所有工作节点上运行。

执行模型#

  1. 解释器检测: - 使用 shebang 行(例如,#!/opt/venv/bin/python) - 备用解释器:

    • .py → python

    • .sh → bash

  2. 输出处理:- 持续捕获标准输出/标准错误 - 以插件名称前缀记录

  3. 进程管理: - 作为子进程启动 - 当父进程退出时终止

示例运行时插件#

Python 插件 (scheduler_foo_plugin.py):

 1#!/opt/venv/bin/python
 2# SPDX-License-Identifier: Apache-2.0
 3"""Example plugin for LMCache system
 4This plugin runs continuously and exits when parent process terminates"""
 5
 6# Standard
 7import json
 8import os
 9import signal
10import time
11
12# First Party
13from lmcache.inprocess.integration.vllm.utils import lmcache_get_or_create_config
14from lmcache.inprocess.v1.config import LMCacheEngineConfig
15
16
17# Graceful exit handler
18def handle_exit(signum, frame):
19    print("Received termination signal, exiting...")
20    exit(0)
21
22
23signal.signal(signal.SIGTERM, handle_exit)
24
25role = os.getenv("LMCACHE_RUNTIME_PLUGIN_ROLE")
26worker_id = os.getenv("LMCACHE_RUNTIME_PLUGIN_WORKER_ID")
27worker_count = os.getenv("LMCACHE_RUNTIME_PLUGIN_WORKER_COUNT")
28config_str = os.getenv("LMCACHE_RUNTIME_PLUGIN_CONFIG")
29try:
30    config = LMCacheEngineConfig.from_json(config_str)
31except json.JSONDecodeError as e:
32    print(f"Error parsing LMCACHE_RUNTIME_PLUGIN_CONFIG: {e}")
33    config = lmcache_get_or_create_config()
34
35print(
36    f"Python plugin running with role: {role}, worker_id: {worker_id}, "
37    f"worker_count: {worker_count}"
38)
39print(f"Config: {config}")
40
41# Main loop
42loop_count = 0
43while True:
44    print(f"Scheduler plugin is running... (loop_count: {loop_count})")
45    loop_count += 1
46    time.sleep(10)

Bash 插件 (all_plugin.sh):

 1#!/bin/bash
 2# Example plugin for LMCache system
 3# This plugin runs continuously and exits when parent process terminates
 4
 5# Handle termination signal
 6trap "echo 'Received termination signal, exiting...'; exit 0" SIGTERM
 7
 8role="$LMCACHE_RUNTIME_PLUGIN_ROLE"
 9worker_id="$LMCACHE_RUNTIME_PLUGIN_WORKER_ID"
10worker_count="$LMCACHE_RUNTIME_PLUGIN_WORKER_COUNT"
11config="$LMCACHE_RUNTIME_PLUGIN_CONFIG"
12
13echo "All plugin started for role: $role, worker ID: $worker_id, worker count: $worker_count"
14echo "All plugin accept LMCache Config: $config"
15
16loop_count=0
17while true; do
18    echo "All plugin is running for ${role} ${worker_id}...(loop_count: ${loop_count})"
19    loop_count=$((loop_count + 1))
20    sleep 10
21done

最佳实践#

  1. 保持运行时插件轻量和高效

  2. 使用描述性命名约定

  3. 实现优雅的错误处理

  4. 包含 shebang 以提高可移植性

  5. 验证配置输入

  6. 为长时间操作添加超时机制