rule.xml和schema.xml的加載概況
rule.xml和schema.xml的加載入口雖然都在com.actiontech.dble.config.loader.xml.XMLSchemaLoader(null, null, lowerCaseNames),但是,實際上解析加載rule.xml的類是com.actiontech.dble.config.loader.xml.XMLRuleLoader,所以XMLSchemaLoader這個類依賴XMLRuleLoader類:
XMLSchemaLoader在構(gòu)造函數(shù)中實例化了一個即用即棄的XMLRuleLoader對象,用來加載rule.xml,在調(diào)用XMLRuleLoader.getTableRules()繼承到了它的成果后,就不再使用這個對象,轉(zhuǎn)向XMLSchemaLoader.load()來加載schema.xml。
rule.xml的加載過程
使用ConfigUtil.getDocument()將rule.xml加載入內(nèi)存后,先用loadFunctions()對用戶定義的算法(<function>的具體內(nèi)容),再用loadTableRules()讀取算法與表名的關(guān)聯(lián)關(guān)系(<tableRule>的具體內(nèi)容)。
在描述分片算法是如何跟邏輯表掛鉤這個事情上,DBLE繼承了MyCat的做法。兩者都使用了遵從OOP理念的逐層抽象方法(我認(rèn)為在這里反而是過于復(fù)雜化的):
- 最具體的概念是具體的邏輯分片函數(shù)類,例如內(nèi)置哈希分片函數(shù)
com.actiontech.dble.route.function.PartitionByLong,它在<function class="class_name">中指定; - 這個類的一個實例化對象,必然會有它自己特殊的對象屬性值(在
<function>里的<property>中指定),這就構(gòu)成了第一層的抽象概念function或algorithm(這層抽象的稱呼不太穩(wěn)定,我覺得應(yīng)該是項目管理不善導(dǎo)致的) - 在function或algorithm的基礎(chǔ)上,加上用作分片索引的邏輯表的列名,就進(jìn)一步抽象成了tableRule,可以供邏輯表使用了。
第一次抽象,生成function(algorithm)--loadFunctions()
工作過程與讀取server.xml的時候一樣,大致是:
從rule.xml中逐個找出
<function>標(biāo)簽。根據(jù)
<function>標(biāo)簽的class屬性,實例化指定分片函數(shù)的對象根據(jù)
<function>標(biāo)簽的name屬性,設(shè)置分片函數(shù)對象的名稱執(zhí)行分片函數(shù)對象自身的初始化方法,并登記到
XMLRuleLoader中(加入到內(nèi)部Map集合中)
private void loadFunctions(Element root) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
// 提取rule.xml中所有的<function>標(biāo)簽
NodeList list = root.getElementsByTagName("function");
// 逐個<function>標(biāo)簽進(jìn)行處理
for (int i = 0, n = list.getLength(); i < n; ++i) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
// 根據(jù)<function>標(biāo)簽的name屬性,確定該function的名稱
String name = e.getAttribute("name");
// 基于name屬性檢查是否已經(jīng)加載過該分片函數(shù)
if (functions.containsKey(name)) {
throw new ConfigException("rule function " + name + " duplicated!");
}
// 根據(jù)<function>標(biāo)簽的class屬性,通過java反射創(chuàng)建分片函數(shù)類的對象
String clazz = e.getAttribute("class");
//reflection
AbstractPartitionAlgorithm function = createFunction(name, clazz);
function.setName(name);
// 通過java反射設(shè)置分片函數(shù)對象的屬性
ParameterMapping.mapping(function, ConfigUtil.loadElements(e));
// 執(zhí)行分片函數(shù)對象的初始化方法
function.init();
// 注冊分片函數(shù)對象到XMLRuleLoader的內(nèi)部集合中
functions.put(name, function);
}
}
}
其中,需要特別說明的是createFunction()這個方法。在這個方法中,DBLE保留了MyCat的傳統(tǒng),讓用戶通過完全限定類名指定分片函數(shù);此外,還對內(nèi)置的6種分片算法提供了簡化名來簡化<function>標(biāo)簽的配置。例如,內(nèi)置哈希函數(shù)可以通過<function class="hash">來指定,而不用大費周章地寫<function class="com.actiontech.dble.route.function.PartitionByLong">了。
| 簡化名 | 完全限定類名 |
|---|---|
| hash | com.actiontech.dble.route.function.PartitionByLong |
| stringhash | com.actiontech.dble.route.function.PartitionByString |
| enum | com.actiontech.dble.route.function.PartitionByFileMap |
| numberrange | com.actiontech.dble.route.function.AutoPartitionByLong |
| patternrange | com.actiontech.dble.route.function.PartitionByPattern |
| date | com.actiontech.dble.route.function.PartitionByDate |
private AbstractPartitionAlgorithm createFunction(String name, String clazz)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException, InvocationTargetException {
String lowerClass = clazz.toLowerCase();
switch (lowerClass) {
// 等效于<function class="com.actiontech.dble.route.function.PartitionByLong">
case "hash":
return new PartitionByLong();
// 等效于<function class="com.actiontech.dble.route.function.PartitionByString">
case "stringhash":
return new PartitionByString();
// 等效于<function class="com.actiontech.dble.route.function.PartitionByFileMap">
case "enum":
return new PartitionByFileMap();
// 等效于<function class="com.actiontech.dble.route.function.AutoPartitionByLong">
case "numberrange":
return new AutoPartitionByLong();
// 等效于<function class="com.actiontech.dble.route.function.PartitionByPattern">
case "patternrange":
return new PartitionByPattern();
// 等效于<function class="com.actiontech.dble.route.function.PartitionByDate">
case "date":
return new PartitionByDate();
default:
Class<?> clz = Class.forName(clazz);
//all function must be extend from AbstractPartitionAlgorithm
if (!AbstractPartitionAlgorithm.class.isAssignableFrom(clz)) {
throw new IllegalArgumentException("rule function must implements " +
AbstractPartitionAlgorithm.class.getName() + ", name=" + name);
}
return (AbstractPartitionAlgorithm) clz.newInstance();
}
}
第二次抽象,生成tableRule--loadTableRules()
在第二次抽象后,生成的是com.actiontech.dble.config.model.rule.RuleConfig對象的Map集合,并存儲在XMLRuleLoader中,供XMLSchemaLoader在后面加載schema.xml時使用。
工作過程也非常單純,大致如下:
從rule.xml中逐個找出
<tableRule>標(biāo)簽。調(diào)用
loadRule(),根據(jù)<tableRule>標(biāo)簽的<rule>的<columns>和<algorithm>,創(chuàng)建一個RuleConfig對象根據(jù)
<tableRule>標(biāo)簽的name屬性,以此名稱為key登記剛才新創(chuàng)建的RuleConfig對象到XMLRuleLoader中(加入到內(nèi)部Map集合中)
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
// 提取rule.xml中所有的<tableRule>標(biāo)簽
NodeList list = root.getElementsByTagName("tableRule");
// 逐個<tableRule>標(biāo)簽進(jìn)行處理
for (int i = 0, n = list.getLength(); i < n; ++i) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
// 根據(jù)<tableRule>標(biāo)簽的name屬性,確定該tableRule的外部命名,
// RuleConfig本身是沒有name或者id之類的稱呼屬性
String name = e.getAttribute("name");
// <tableRule>標(biāo)簽的name屬性不能為空,也不能互相重復(fù)
if (StringUtil.isEmpty(name)) {
throw new ConfigException("name is null or empty");
}
if (tableRules.containsKey(name)) {
throw new ConfigException("table rule " + name + " duplicated!");
}
// 提取<tableRule>標(biāo)簽有且唯一的<rule>標(biāo)簽
// 目前一個<tableRule>只允許有一個<rule>,但從源代碼和注釋看來,
// MyCat希望日后能支持多個<rule>,而DBLE則繼承了這部分設(shè)計
NodeList ruleNodes = e.getElementsByTagName("rule");
int length = ruleNodes.getLength();
if (length > 1) {
throw new ConfigException("only one rule can defined :" + name);
}
// 創(chuàng)建對應(yīng)的RuleConfig對象,詳解在本代碼段內(nèi)
RuleConfig rule = loadRule((Element) ruleNodes.item(0));
// 注冊RuleConfig對象到XMLRuleLoader的內(nèi)部集合中
tableRules.put(name, new TableRuleConfig(name, rule));
}
}
}
// ...
private RuleConfig loadRule(Element element) throws SQLSyntaxErrorException {
// 獲取<tableRules>內(nèi)的<rule>的<columns>標(biāo)簽
Element columnsEle = ConfigUtil.loadElement(element, "columns");
String column = columnsEle.getTextContent();
// <rule>必須有<columns>標(biāo)簽
if (StringUtil.isEmpty(column)) {
throw new ConfigException("no rule column is found");
}
// <columns>標(biāo)簽?zāi)壳皟H支持1個列作為分片索引
// 但源代碼支持逗號分隔語法,MyCat應(yīng)該是希望日后能支持
// 邏輯表的多個列構(gòu)成一個分片索引(復(fù)合分片索引),
// 而DBLE則繼承了這部分設(shè)計
String[] columns = SplitUtil.split(column, ',', true);
if (columns.length > 1) {
throw new ConfigException("table rule coulmns has multi values:" +
columnsEle.getTextContent());
}
// <rule>必須要有<algorithm>標(biāo)簽
Element algorithmEle = ConfigUtil.loadElement(element, "algorithm");
String algorithmName = algorithmEle.getTextContent();
if (StringUtil.isEmpty(algorithmName)) {
throw new ConfigException("algorithm is null or empty");
}
// 根據(jù)<algorithm>中的內(nèi)容去找第一次抽象時生成的function
AbstractPartitionAlgorithm algorithm = functions.get(algorithmName);
// 如果能找到對應(yīng)的function,則以此生成新的RuleConfig對象
if (algorithm == null) {
throw new ConfigException("can't find function of name :" + algorithmName);
}
return new RuleConfig(column.toUpperCase(), algorithmName, algorithm);
}