Skip to content

Commit

Permalink
Declare in the document that ReadWriteSplitting Config is parsed by G…
Browse files Browse the repository at this point in the history
…roovyShell by default and adds corresponding unit tests
  • Loading branch information
linghengqian committed Nov 14, 2023
1 parent ec55e98 commit 60ac332
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ weight = 3
rules:
- !READWRITE_SPLITTING
dataSources:
<data_source_name> (+): # 读写分离逻辑数据源名称
write_data_source_name: # 写库数据源名称
read_data_source_names: # 读库数据源名称,多个从数据源用逗号分隔
<data_source_name> (+): # 读写分离逻辑数据源名称,默认使用 Groovy 的行表达式 SPI 实现来解析
write_data_source_name: # 写库数据源名称,默认使用 Groovy 的行表达式 SPI 实现来解析
read_data_source_names: # 读库数据源名称,多个从数据源用逗号分隔,默认使用 Groovy 的行表达式 SPI 实现来解析
transactionalReadQueryStrategy (?): # 事务内读请求的路由策略,可选值:PRIMARY(路由至主库)、FIXED(同一事务内路由至固定数据源)、DYNAMIC(同一事务内路由至非固定数据源)。默认值:DYNAMIC
loadBalancerName: # 负载均衡算法名称

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Read/write splitting YAML configuration is highly readable. The YAML format enab
rules:
- !READWRITE_SPLITTING
dataSources:
<data_source_name> (+): # Logic data source name of readwrite-splitting
write_data_source_name: # Write data source name
read_data_source_names: # Read data source names, multiple data source names separated with comma
<data_source_name> (+): # Logic data source name of readwrite-splitting, which uses Groovy's Row Value Expressions SPI implementation to parse by default
write_data_source_name: # Write data source name, which uses Groovy's Row Value Expressions SPI implementation to parse by default
read_data_source_names: # Read data source names, multiple data source names separated with comma, which uses Groovy's Row Value Expressions SPI implementation to parse by default
transactionalReadQueryStrategy (?): # Routing strategy for read query within a transaction, values include: PRIMARY (to primary), FIXED (to fixed data source), DYNAMIC (to any data source), default value: DYNAMIC
loadBalancerName: # Load balance algorithm name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,24 @@ void assertGetDataSourceMapper() {
Map<String, Collection<String>> expected = Collections.singletonMap("readwrite", Arrays.asList("write_ds", "read_ds_0", "read_ds_1"));
assertThat(actual, is(expected));
}

@Test
void assertCreateReadwriteSplittingRuleWithRowValueExpressionImpl() {
ReadwriteSplittingDataSourceRuleConfiguration config = new ReadwriteSplittingDataSourceRuleConfiguration(
"<GROOVY>${['readwrite']}_ds",
"<GROOVY>${['write']}_ds",
Arrays.asList("<GROOVY>read_ds_${['0']}", "read_ds_${['1']}", "read_ds_2", "<LITERAL>read_ds_3"),
"random");
ReadwriteSplittingRule readwriteSplittingRule = new ReadwriteSplittingRule(
"logic_db",
new ReadwriteSplittingRuleConfiguration(
Collections.singleton(config), Collections.singletonMap("random", new AlgorithmConfiguration("RANDOM", new Properties()))),
mock(InstanceContext.class));
Optional<ReadwriteSplittingDataSourceRule> actual = readwriteSplittingRule.findDataSourceRule("readwrite_ds");
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("readwrite_ds"));
assertThat(actual.get().getReadwriteSplittingGroup().getWriteDataSource(), is("write_ds"));
assertThat(actual.get().getReadwriteSplittingGroup().getReadDataSources(), is(Arrays.asList("read_ds_0", "read_ds_1", "read_ds_2", "read_ds_3")));
assertThat(actual.get().getLoadBalancer().getType(), is("RANDOM"));
}
}

0 comments on commit 60ac332

Please sign in to comment.