Skip to content

Template Object

mytrygithub edited this page Jul 10, 2023 · 13 revisions

(一)背景

很多时候我们会定义多个 DB,多个 CF(ColumnFamily),而这些 DB/CF 的 Option 又大同小异,都在 json/yaml 中重复写一遍,会很啰嗦,并且很可能会因疏忽导致不一致的情况发生。

使用 RocksDB 时,这样的重复可以通过抽取共同代码来解决。

所以,在 SidePlugin 配置中,我们新加了一个功能,允许定义 template 参数,把共同的配置定义在 template 中,然后再分别定义不同的配置项。

(二)template 参数

目前支持 template 参数的对象有 Options, DBOptions, ColumnFamilyOptions, DcompactEtcd,举例如下:

DBOptions:
  dbo:
    create_if_missing: true
    create_missing_column_families: true
    max_background_compactions: 40
    max_subcompactions: 1
    max_level1_subcompactions: 7
    inplace_update_support: false
    WAL_size_limit_MB: 0
    statistics: "${stat}"
  dbo2:
    template: "$dbo"
    max_level1_subcompactions: 3 # override this param

(三)和预定义对象一起工作

Ingest Predefine Object 中,用户代码可以通过 SidePluginRepo::Put 注入自己用代码创建的 Option 等对象,但在这种情况下,整个对象需要全部由用户代码来配置,例如 CFOptions 中的 compaction_filter/merge_operator 等对象需要用户自己提前设置好。一旦注入,这个对象就不能再改变,这大大限制了灵活性。

现在,我们可以组合 template 和 put:先 put 一个 Option 作为模板,然后其它 Option 在该模板的基础上修改。

yaml 配置

DBOptions:
  dbo:
    create_if_missing: true
    create_missing_column_families: true
CFOptions:
  default:
    template: "$template_cfo" # has been ingested by put
    table_factory: dispatch
  cfo1:
    template: "$template_cfo" # has been ingested by put
    memtable_factory: "${cspp}"
    table_factory: dispatch
  cfo2:
    template: "$template_cfo" # has been ingested by put
    memtable_factory: "${cspp}"   
databases:
  db1:
    method: DB::Open
    params:
      db_options: "$dbo"
      column_families:
        default: "$default"
        cf1: "$cfo1"
      dyna_cf_opt: "$cfo2"

C++ 代码

  SidePluginRepo repo;
  repo.Put("template_cfo", CreateMyColumnFamilyOptions());
  repo.ImportAutoFile("config.yaml"); // omit error check
  // now CFOptions objects have been initialized with "template_cfo"
  // and modified by their "params"

Java 代码

  SidePluginRepo repo = new SidePluginRepo();
  repo.put("template_cfo", createMyColumnFamilyOptions());
  repo.importAutoFile("config.yaml"); // omit error check
  // now CFOptions objects have been initialized with "template_cfo"
  // and modified by their "params"

(四)相关文档