已开启
RFC: memcache 增加一种设置config的方法 #9
shilinlee创建于  3月20日
shilinlee
shilinlee成员
3月20日 创建


from memcache_hybrid import LocalConfig, DistributedObjectStore

# 创建配置对象
config = LocalConfig()
config.meta_service_url = "tcp://192.168.1.1:5000"
config.dram_size = "2GB"
config.tls_enable = True
# ... 其他配置

# 使用配置
store = DistributedObjectStore()
store.setup(config)  # 设置配置
store.init(device_id=0, init_bm=True)  # 初始化



from memcache_hybrid import DistributedObjectStore, ClientConfig                                                                                                        
                                                                                                                                                                            
    # 创建配置对象                                                                                                                                                          
    config = ClientConfig()                                                                                                                                                 
    config.discoveryURL = "tcp://127.0.0.1:5124"                                                                                                                            
    config.rankId = 0                                                                                                                                                       
    config.dataOpType = "host_rdma"                                                                                                                                         
                                                                                                                                                                            
    # 创建存储对象并设置配置                                                                                                                                                
    store = DistributedObjectStore()                                                                                                                                        
    store.setup(config)                                                                                                                                                     
    store.init(device_id=0)



likedislike
shilinleeshilinlee成员
3月20日 修改了issue 的描述
shilinleeshilinlee成员
3月20日 修改了issue 的描述
shilinlee
shilinlee成员
3月21日 评论:

memcache 增加一种设置config的方法

Motivation

目前memcache只能通过环境变量(MMC_LOCAL_CONFIG_PATH和MMC_META_CONFIG_PATH)指定配置文件路径来加载配置,这种方式在Python环境中使用不够灵活,不便于动态配置和程序内配置管理。为了提高Python API的易用性和灵活性,需要新增一种通过Python代码直接设置配置的方法。

Proposed Change

AS IS

通过设置环境变量MMC_LOCAL_CONFIG_PATH和MMC_META_CONFIG_PATH,指定local和meta的配置文件路径:

import os
from memcache_hybrid import DistributedObjectStore

# 设置环境变量
os.environ["MMC_LOCAL_CONFIG_PATH"] = "/path/to/local_config.conf"
os.environ["MMC_META_CONFIG_PATH"] = "/path/to/meta_config.conf"

# 创建并初始化客户端
store = DistributedObjectStore()
store.init(device_id=0)

TO BE

新增一种Python入口配置方法,同时保持原有环境变量方式的兼容性:

from memcache_hybrid import LocalConfig, DistributedObjectStore

# 创建配置对象
config = LocalConfig()
config.meta_service_url = "tcp://192.168.1.1:5000"
config.protocol = "device_sdma"
config.dram_size = "2GB"
# ... 其他配置

# 使用配置
store = DistributedObjectStore()
store.setup(config)  # 设置配置
store.init(device_id=0, init_bm=True)

实现细节

1. LocalConfig类

LocalConfig是一个Python类,用于封装所有的客户端配置参数,包括:

  • 元服务配置(meta_service_url等)
  • 网络配置(protocol、hcom_url等)
  • 内存配置(dram_size、hbm_size等)
  • 客户端行为配置(client_timeout_seconds、read_thread_pool_size等)
  • TLS配置(tls_enable、tls_ca_path等)

2. setup方法

DistributedObjectStore类新增setup方法,用于接收LocalConfig对象并设置客户端配置:

def setup(self, config: LocalConfig) -> int

该方法将LocalConfig对象中的配置参数传递给C++底层的ClientConfig::Setup方法。

3. 兼容性

原有通过环境变量指定配置文件的方式仍然有效,新的配置方法不会破坏现有代码。

使用示例

基本用法

from memcache_hybrid import LocalConfig, DistributedObjectStore

# 创建配置对象
config = LocalConfig()
config.meta_service_url = "tcp://192.168.1.1:5000"
config.protocol = "device_sdma"
config.dram_size = "2GB"
config.hbm_size = "8GB"

# 使用配置
store = DistributedObjectStore()
store.setup(config)
store.init(device_id=0)

# 执行数据操作
store.put("key1", b"value1")
data = store.get("key1")

# 关闭连接
store.close()

完整配置示例

from memcache_hybrid import LocalConfig, DistributedObjectStore

# 创建配置对象
config = LocalConfig()

# 元服务配置
config.meta_service_url = "tcp://192.168.1.1:5000"
config.config_store_url = "tcp://192.168.1.2:6000"

# 网络配置
config.protocol = "device_sdma"
config.hcom_url = "hcom://192.168.1.0/"

# 内存配置
config.dram_size = "2GB"
config.hbm_size = "8GB"
config.max_dram_size = "16GB"
config.max_hbm_size = "64GB"
config.memory_pool_mode = "expanded"

# 客户端行为配置
config.client_retry_milliseconds = 10000
config.client_timeout_seconds = 30
config.read_thread_pool_size = 8
config.write_thread_pool_size = 8
config.aggregate_io = True
config.aggregate_num = 1024

# UBS IO配置
config.ubs_io_enable = False

# TLS配置
config.tls_enable = False

# 使用配置
store = DistributedObjectStore()
store.setup(config)
store.init(device_id=0)

优势

  1. 灵活性:可以在Python代码中动态设置配置,无需依赖外部配置文件
  2. 类型安全:通过Python类的属性设置配置,避免了配置文件中的语法错误
  3. 易用性:提供了直观的API,便于开发者使用
  4. 兼容性:保持了与原有环境变量方式的兼容性

影响范围

  1. 新增:LocalConfig Python类
  2. 新增:DistributedObjectStore.setup方法
  3. 文档更新:memcache_python_api.md

测试

已经在test_mmc_configuration.cpp中添加了对clientConfig.Setup的测试用例,确保配置设置的正确性。

结论

新增的Python入口配置方法提高了memcache在Python环境中的易用性和灵活性,同时保持了与原有方式的兼容性,是一个值得实现的功能增强。

likedislike
shilinleeshilinlee成员
3月21日 修改标题为 “RFC: memcache 增加一种设置config的方法”,原标题为“RFC”