配置
初始化
在FE启动时:
- Config类ConfField注解标记的静态属性反射出Field存储到内存confFields,作为一个可读取和修改的属性列表(真正的值存储在Config类的静态属性中,反射出Field并存储到confFields只是一个读取和修改指针而已)
- 读取配置文件,根据配置文件内容,设置Config类的静态属性
public static Map<String, Field> confFields;
public void init(String configFile) throws Exception {
confClass = this.getClass();
confFile = configFile;
confFields = Maps.newHashMap();
for (Field field : confClass.getFields()) {
ConfField confField = field.getAnnotation(ConfField.class);
if (confField == null) {
continue;
}
confFields.put(confField.value().equals("") ? field.getName() : confField.value(), field);
}
initConf(confFile);
}
private static void setFields(Properties props, boolean isLdapConfig) throws Exception {
Class<? extends ConfigBase> theClass = isLdapConfig ? ldapConfClass : confClass;
Field[] fields = theClass.getFields();
for (Field f : fields) {
// ensure that field has "@ConfField" annotation
ConfField anno = f.getAnnotation(ConfField.class);
if (anno == null) {
continue;
}
// ensure that field has property string
String confKey = anno.value().equals("") ? f.getName() : anno.value();
String confVal = props.getProperty(confKey);
if (Strings.isNullOrEmpty(confVal)) {
continue;
}
setConfigField(f, confVal);
// to be compatible with old version
if (confKey.equalsIgnoreCase("async_load_task_pool_size")) {
Config.async_loading_load_task_pool_size = Config.async_load_task_pool_size;
}
}
}
设置
从confFields中根据要修改的key获取具体的Field,然后反射修改实际Config类中的值
查看
遍历confFields中获取值并展示,根据反射字段Field从Config类中获取值
public static synchronized List<List<String>> getConfigInfo(PatternMatcher matcher) {
Map<String, Field> allConfFields = Maps.newHashMap();
allConfFields.putAll(confFields);
allConfFields.putAll(ldapConfFields);
return allConfFields.entrySet().stream().sorted(Map.Entry.comparingByKey()).flatMap(e -> {
String confKey = e.getKey();
Field f = e.getValue();
ConfField anno = f.getAnnotation(ConfField.class);
if (matcher == null || matcher.match(confKey)) {
List<String> config = Lists.newArrayList();
config.add(confKey);
config.add(getConfValue(f));
config.add(f.getType().getSimpleName());
config.add(String.valueOf(anno.mutable()));
config.add(String.valueOf(anno.masterOnly()));
config.add(anno.comment());
return Stream.of(config);
} else {
return Stream.empty();
}
}).collect(Collectors.toList());
}
使用方法
# 展示
ADMIN SHOW FRONTEND CONFIG LIKE "max_broker_concurrency";
# 设置
ADMIN SET FRONTEND CONFIG ("max_broker_concurrency" = "11");
注意临时的设置完后无法持久化,重启后又恢复原值
VARIABLE
通常表示在连接/Session范围内生效的变量。有几个关键对象:
- ConnectContext - 连接上下文,其中包含sessionVariable : SessionVariable属性
- SessionVariable - Session级别的变量(只是它在使用上也会作为SessionVariable的创建模板,故也有点全局设置的意思)
- GlobalVariable - Global级别设置的变量
- VariableMgr - 管理全局/Session级别的变量
初始化
VariableMgr静态初始化时:
- 实例化静态变量defaultSessionVariable = new SessionVariable(),作为SessionVariable创建的模板
- getStringVarContextBuilder初始化ctxByVarName列表
- 获取SessionVariable中以VarAttr注解标记的属性加入
- 获取GlobalVariable中以VarAttr注解标记的属性加入
Session建立
在Session建立连接时,克隆defaultSessionVariable到ConnectContext.sessionVariable中
设置
对于每个SetStmt中要设置的值:
- 获取ConnectContext中SessionVariable
- 调用VariableMgr.setVar
- 如果GLOBAL级别,将设置GlobalVariable中的静态变量值或者设置静态变量defaultSessionVariable的属性值,并同时设置ConnectContext中SessionVariable的属性值
- 如果SESSION级别,将设置ConnectContext中SessionVariable的属性值
SET exec_mem_limit = 137438953472;
SET GLOBAL exec_mem_limit = 137438953472;
整体流程:
- 上来初始化defaultSessionVariable : SessionVariable作为SessionVariable模板
- ConnectContext建立时,克隆defaultSessionVariable作为SessionVariable
- 如果设置Session级别的variable,直接设置SessionVariable
- 如果设置Global级别的variable,则设置GlobalVariable或者defaultSessionVariable : SessionVariable模板
- 下次ConnectContext建立时,克隆修改过的defaultSessionVariable作为SessionVariable
查看
- 查看SESSION级别变量 - 查看ConnectContext中SessionVariable的属性值
- 查看GLOBAL级别变量 - 查看GlobalVariable或者defaultSessionVariable : SessionVariable模板的属性值
SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern' | WHERE expr]