Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare in the document that ReadWriteSplitting Config is parsed by GroovyShell by default and adds corresponding unit tests #29034

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}