Skip to content

Commit 8af20e0

Browse files
committed
for #98
1 parent baf78e6 commit 8af20e0

File tree

10 files changed

+193
-31
lines changed

10 files changed

+193
-31
lines changed

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/MasterSlaveDataSourceFactory.java

+43-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.dangdang.ddframe.rdb.sharding.api;
1919

20+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.MasterSlaveLoadBalanceStrategy;
21+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.MasterSlaveLoadBalanceStrategyType;
2022
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.MasterSlaveDataSource;
2123
import lombok.AccessLevel;
2224
import lombok.NoArgsConstructor;
@@ -54,7 +56,7 @@ public static DataSource createDataSource(final String name, final DataSource ma
5456
for (DataSource each : otherSlaveDataSources) {
5557
slaveDataSourceMap.put(each.toString(), each);
5658
}
57-
return new MasterSlaveDataSource(name, masterDataSource.toString(), masterDataSource, slaveDataSourceMap);
59+
return new MasterSlaveDataSource(name, masterDataSource.toString(), masterDataSource, slaveDataSourceMap, MasterSlaveLoadBalanceStrategyType.getDefaultStrategy());
5860
}
5961

6062
/**
@@ -69,8 +71,45 @@ public static DataSource createDataSource(final String name, final DataSource ma
6971
* @return master-slave data source
7072
* @throws SQLException SQL exception
7173
*/
72-
public static DataSource createDataSource(final String name,
73-
final String masterDataSourceName, final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap) throws SQLException {
74-
return new MasterSlaveDataSource(name, masterDataSourceName, masterDataSource, slaveDataSourceMap);
74+
public static DataSource createDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
75+
final Map<String, DataSource> slaveDataSourceMap) throws SQLException {
76+
return new MasterSlaveDataSource(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, MasterSlaveLoadBalanceStrategyType.getDefaultStrategy());
77+
}
78+
79+
/**
80+
* Create master-slave data source.
81+
*
82+
* <p>One master data source can configure multiple slave data source.</p>
83+
*
84+
* @param name data source name
85+
* @param masterDataSourceName name of data source for master
86+
* @param masterDataSource data source for master
87+
* @param slaveDataSourceMap map of data source name and data source for slave
88+
* @param strategyType master-slave database load-balance strategy type
89+
* @return master-slave data source
90+
* @throws SQLException SQL exception
91+
*/
92+
public static DataSource createDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
93+
final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceStrategyType strategyType) throws SQLException {
94+
return new MasterSlaveDataSource(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategyType);
95+
}
96+
97+
98+
/**
99+
* Create master-slave data source.
100+
*
101+
* <p>One master data source can configure multiple slave data source.</p>
102+
*
103+
* @param name data source name
104+
* @param masterDataSourceName name of data source for master
105+
* @param masterDataSource data source for master
106+
* @param slaveDataSourceMap map of data source name and data source for slave
107+
* @param strategy master-slave database load-balance strategy
108+
* @return master-slave data source
109+
* @throws SQLException SQL exception
110+
*/
111+
public static DataSource createDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
112+
final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceStrategy strategy) throws SQLException {
113+
return new MasterSlaveDataSource(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategy);
75114
}
76115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 1999-2015 dangdang.com.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* </p>
16+
*/
17+
18+
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
23+
/**
24+
* Master-slave database load-balance strategy type.
25+
*
26+
* @author zhangliang
27+
*/
28+
@RequiredArgsConstructor
29+
@Getter
30+
public enum MasterSlaveLoadBalanceStrategyType {
31+
32+
RoundRobin(new RoundRobinMasterSlaveLoadBalanceStrategy()),
33+
Random(new RandomMasterSlaveLoadBalanceStrategy());
34+
35+
private final MasterSlaveLoadBalanceStrategy strategy;
36+
37+
/**
38+
* Get default master-slave database load-balance strategy.
39+
*
40+
* @return default master-slave database load-balance strategy
41+
*/
42+
public static MasterSlaveLoadBalanceStrategy getDefaultStrategy() {
43+
return RoundRobin.strategy;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 1999-2015 dangdang.com.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* </p>
16+
*/
17+
18+
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave;
19+
20+
import java.util.List;
21+
import java.util.Random;
22+
23+
/**
24+
* Random slave database load-balance strategy.
25+
*
26+
* @author zhangliang
27+
*/
28+
public final class RandomMasterSlaveLoadBalanceStrategy implements MasterSlaveLoadBalanceStrategy {
29+
30+
@Override
31+
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
32+
return slaveDataSourceNames.get(new Random().nextInt(slaveDataSourceNames.size()));
33+
}
34+
}

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/datasource/MasterSlaveDataSource.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource;
1919

20-
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinMasterSlaveLoadBalanceStrategy;
2120
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.MasterSlaveLoadBalanceStrategy;
21+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.MasterSlaveLoadBalanceStrategyType;
2222
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
2323
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
2424
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
@@ -60,14 +60,27 @@ protected Boolean initialValue() {
6060
@Getter
6161
private final Map<String, DataSource> slaveDataSources;
6262

63-
private final MasterSlaveLoadBalanceStrategy masterSlaveLoadBalanceStrategy = new RoundRobinMasterSlaveLoadBalanceStrategy();
63+
private final MasterSlaveLoadBalanceStrategy masterSlaveLoadBalanceStrategy;
6464

65-
public MasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource, final Map<String, DataSource> slaveDataSources) throws SQLException {
65+
@Deprecated // TODO for spring namespace only
66+
public MasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
67+
final Map<String, DataSource> slaveDataSources) throws SQLException {
68+
this(name, masterDataSourceName, masterDataSource, slaveDataSources, MasterSlaveLoadBalanceStrategyType.getDefaultStrategy());
69+
}
70+
71+
public MasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
72+
final Map<String, DataSource> slaveDataSources, final MasterSlaveLoadBalanceStrategyType strategyType) throws SQLException {
73+
this(name, masterDataSourceName, masterDataSource, slaveDataSources, strategyType.getStrategy());
74+
}
75+
76+
public MasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
77+
final Map<String, DataSource> slaveDataSources, final MasterSlaveLoadBalanceStrategy masterSlaveLoadBalanceStrategy) throws SQLException {
6678
super(getAllDataSources(masterDataSource, slaveDataSources.values()));
6779
this.name = name;
6880
this.masterDataSourceName = masterDataSourceName;
6981
this.masterDataSource = masterDataSource;
7082
this.slaveDataSources = slaveDataSources;
83+
this.masterSlaveLoadBalanceStrategy = masterSlaveLoadBalanceStrategy;
7184
}
7285

7386
private static Collection<DataSource> getAllDataSources(final DataSource masterDataSource, final Collection<DataSource> slaveDataSources) {

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/type/ShardingMasterSlaveTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
2828
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
2929
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
30+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinMasterSlaveLoadBalanceStrategy;
3031
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
3132
import com.dangdang.ddframe.rdb.sharding.constant.DatabaseType;
3233
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
@@ -151,7 +152,7 @@ private MasterSlaveDataSource getMasterSlaveDataSource(final Map<String, DataSou
151152
final String name, final String masterDataSourceName, final String slaveDataSourceName) throws SQLException {
152153
Map<String, DataSource> slaveDs0 = new HashMap<>(1, 1);
153154
slaveDs0.put(slaveDataSourceName, masterSlaveDataSourceMap.get(slaveDataSourceName));
154-
return new MasterSlaveDataSource(name, masterDataSourceName, masterSlaveDataSourceMap.get(masterDataSourceName), slaveDs0);
155+
return new MasterSlaveDataSource(name, masterDataSourceName, masterSlaveDataSourceMap.get(masterDataSourceName), slaveDs0, new RoundRobinMasterSlaveLoadBalanceStrategy());
155156
}
156157

157158
@After

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/AllApiTests.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.dangdang.ddframe.rdb.sharding.api.strategy.common.ShardingStrategyTest;
2727
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategyTest;
2828
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithmTest;
29+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RandomMasterSlaveLoadBalanceStrategyTest;
2930
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinMasterSlaveLoadBalanceStrategyTest;
3031
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithmTest;
3132
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategyTest;
@@ -36,24 +37,25 @@
3637

3738
@RunWith(Suite.class)
3839
@Suite.SuiteClasses({
39-
ShardingDataSourceFactoryTest.class,
40-
ShardingPropertiesTest.class,
41-
ShardingPropertiesConstantTest.class,
42-
ShardingValueTest.class,
43-
DataSourceRuleTest.class,
44-
ShardingRuleTest.class,
45-
TableRuleTest.class,
46-
DataNodeTest.class,
47-
DynamicDataNodeTest.class,
48-
BindingTableRuleTest.class,
49-
ShardingStrategyTest.class,
50-
DatabaseShardingStrategyTest.class,
51-
NoneDatabaseShardingAlgorithmTest.class,
52-
TableShardingStrategyTest.class,
53-
NoneTableShardingAlgorithmTest.class,
54-
HintManagerTest.class,
55-
MasterSlaveDataSourceFactoryTest.class,
56-
RoundRobinMasterSlaveLoadBalanceStrategyTest.class
40+
ShardingDataSourceFactoryTest.class,
41+
ShardingPropertiesTest.class,
42+
ShardingPropertiesConstantTest.class,
43+
ShardingValueTest.class,
44+
DataSourceRuleTest.class,
45+
ShardingRuleTest.class,
46+
TableRuleTest.class,
47+
DataNodeTest.class,
48+
DynamicDataNodeTest.class,
49+
BindingTableRuleTest.class,
50+
ShardingStrategyTest.class,
51+
DatabaseShardingStrategyTest.class,
52+
NoneDatabaseShardingAlgorithmTest.class,
53+
TableShardingStrategyTest.class,
54+
NoneTableShardingAlgorithmTest.class,
55+
HintManagerTest.class,
56+
MasterSlaveDataSourceFactoryTest.class,
57+
RoundRobinMasterSlaveLoadBalanceStrategyTest.class,
58+
RandomMasterSlaveLoadBalanceStrategyTest.class
5759
})
5860
public class AllApiTests {
5961
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertTrue;
9+
10+
public final class RandomMasterSlaveLoadBalanceStrategyTest {
11+
12+
private final RandomMasterSlaveLoadBalanceStrategy randomMasterSlaveLoadBalanceStrategy = new RandomMasterSlaveLoadBalanceStrategy();
13+
14+
@Test
15+
public void assertGetDataSource() {
16+
String masterDataSourceName = "test_ds_master";
17+
String slaveDataSourceName1 = "test_ds_slave_1";
18+
String slaveDataSourceName2 = "test_ds_slave_2";
19+
List<String> slaveDataSourceNames = Arrays.asList(slaveDataSourceName1, slaveDataSourceName2);
20+
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames)));
21+
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames)));
22+
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames)));
23+
}
24+
}

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/strategy/slave/RoundRobinMasterSlaveLoadBalanceStrategyTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.Test;
2121

2222
import java.util.Arrays;
23+
import java.util.List;
2324

2425
import static org.hamcrest.core.Is.is;
2526
import static org.junit.Assert.assertThat;
@@ -33,8 +34,9 @@ public void assertGetDataSource() {
3334
String masterDataSourceName = "test_ds_master";
3435
String slaveDataSourceName1 = "test_ds_slave_1";
3536
String slaveDataSourceName2 = "test_ds_slave_2";
36-
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, Arrays.asList(slaveDataSourceName1, slaveDataSourceName2)), is(slaveDataSourceName1));
37-
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, Arrays.asList(slaveDataSourceName1, slaveDataSourceName2)), is(slaveDataSourceName2));
38-
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, Arrays.asList(slaveDataSourceName1, slaveDataSourceName2)), is(slaveDataSourceName1));
37+
List<String> slaveDataSourceNames = Arrays.asList(slaveDataSourceName1, slaveDataSourceName2);
38+
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames), is(slaveDataSourceName1));
39+
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames), is(slaveDataSourceName2));
40+
assertThat(roundRobinSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames), is(slaveDataSourceName1));
3941
}
4042
}

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/connection/ShardingConnectionTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
2121
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
2222
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
23+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinMasterSlaveLoadBalanceStrategy;
2324
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
2425
import com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource;
2526
import com.dangdang.ddframe.rdb.sharding.jdbc.core.ShardingContext;
@@ -53,7 +54,7 @@ public static void init() throws SQLException {
5354
DataSource slaveDataSource = new TestDataSource("test_ds_slave");
5455
Map<String, DataSource> slaveDataSourceMap = new HashMap<>(1, 1);
5556
slaveDataSourceMap.put("test_ds_slave", slaveDataSource);
56-
masterSlaveDataSource = new MasterSlaveDataSource("test_ds", "test_ds_master", masterDataSource, slaveDataSourceMap);
57+
masterSlaveDataSource = new MasterSlaveDataSource("test_ds", "test_ds_master", masterDataSource, slaveDataSourceMap, new RoundRobinMasterSlaveLoadBalanceStrategy());
5758
((TestDataSource) slaveDataSource).setThrowExceptionWhenClosing(true);
5859
}
5960

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/datasource/MasterSlaveDataSourceTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.dangdang.ddframe.rdb.sharding.api.HintManager;
2121
import com.dangdang.ddframe.rdb.sharding.api.MasterSlaveDataSourceFactory;
22+
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinMasterSlaveLoadBalanceStrategy;
2223
import com.dangdang.ddframe.rdb.sharding.constant.DatabaseType;
2324
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
2425
import com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource;
@@ -55,7 +56,7 @@ public MasterSlaveDataSourceTest() throws SQLException {
5556
slaveDataSource = new TestDataSource("test_ds_slave");
5657
Map<String, DataSource> slaveDataSourceMap = new HashMap<>(1, 1);
5758
slaveDataSourceMap.put("test_ds_slave", slaveDataSource);
58-
masterSlaveDataSource = new MasterSlaveDataSource("test_ds", "test_ds_master", masterDataSource, slaveDataSourceMap);
59+
masterSlaveDataSource = new MasterSlaveDataSource("test_ds", "test_ds_master", masterDataSource, slaveDataSourceMap, new RoundRobinMasterSlaveLoadBalanceStrategy());
5960
}
6061

6162
@Before

0 commit comments

Comments
 (0)