Skip to content

Commit ac45e99

Browse files
author
朱勇铭
committed
add mod stratrgy unit test.
1 parent 5e5bb94 commit ac45e99

File tree

3 files changed

+356
-20
lines changed

3 files changed

+356
-20
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.ctrip.framework.dal.cluster.client;
2+
3+
import com.ctrip.framework.dal.cluster.client.cluster.DefaultClusterTest;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Suite;
6+
7+
/**
8+
* Created by @author zhuYongMing on 2019/11/29.
9+
*/
10+
@RunWith(Suite.class)
11+
@Suite.SuiteClasses({
12+
DefaultClusterTest.class
13+
})
14+
public class AllTests {
15+
}
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
package com.ctrip.framework.dal.cluster.client.cluster;
2+
3+
import com.ctrip.framework.dal.cluster.client.Cluster;
4+
import com.ctrip.framework.dal.cluster.client.config.ClusterConfig;
5+
import com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider;
6+
import com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider;
7+
import com.ctrip.framework.dal.cluster.client.database.DatabaseCategory;
8+
import com.ctrip.framework.dal.cluster.client.sharding.context.DbShardContext;
9+
import com.ctrip.framework.dal.cluster.client.sharding.context.MappedShardData;
10+
import com.ctrip.framework.dal.cluster.client.sharding.context.ShardData;
11+
import com.ctrip.framework.dal.cluster.client.sharding.context.TableShardContext;
12+
import org.junit.Assert;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
import java.util.HashMap;
17+
import java.util.HashSet;
18+
import java.util.Map;
19+
import java.util.Set;
20+
21+
import static com.ctrip.framework.dal.cluster.client.database.DatabaseCategory.MYSQL;
22+
23+
/**
24+
* Created by @author zhuYongMing on 2019/11/14.
25+
*/
26+
public class DefaultClusterTest {
27+
28+
private static Cluster cluster;
29+
30+
@BeforeClass
31+
public static void init() {
32+
ClusterConfigProvider provider = new DefaultLocalConfigProvider("demo-cluster");
33+
ClusterConfig config = provider.getClusterConfig();
34+
cluster = config.generate();
35+
}
36+
37+
@Test
38+
public void getClusterNameTest() {
39+
final String clusterName = cluster.getClusterName();
40+
Assert.assertEquals("demo-cluster", clusterName);
41+
}
42+
43+
@Test
44+
public void getDatabaseCategoryTest() {
45+
final DatabaseCategory databaseCategory = cluster.getDatabaseCategory();
46+
Assert.assertEquals(MYSQL, databaseCategory);
47+
}
48+
49+
@Test
50+
public void getDbShardNullTest() {
51+
final Integer table1ShardIndex = cluster.getDbShard("table1", new DbShardContext("whatever"));
52+
Assert.assertNull(table1ShardIndex);
53+
}
54+
55+
@Test
56+
public void getDbShardTest() {
57+
final DbShardContext assignationShardIdContext = new DbShardContext("whatever").setShardId(0);
58+
final Integer AssignationShardIdShardIndex = cluster.getDbShard("table13", assignationShardIdContext);
59+
Assert.assertEquals(0, (int) AssignationShardIdShardIndex);
60+
61+
final DbShardContext assignationShardValueContext = new DbShardContext("whatever");
62+
assignationShardValueContext.setShardValue(10);
63+
final Integer assignationShardValueShardIndex = cluster.getDbShard("table13", assignationShardValueContext);
64+
Assert.assertEquals(0, (int) assignationShardValueShardIndex);
65+
66+
final DbShardContext assignationShardColValueContext = new DbShardContext("whatever");
67+
final Map<String, Object> data = new HashMap<>();
68+
data.put("other", 101); // ignore
69+
data.put("id", 100001); // ignore
70+
final ShardData shardData = new MappedShardData(data);
71+
assignationShardColValueContext.setShardColValues(shardData);
72+
final Integer assignationShardColValueShardIndex = cluster.getDbShard("table14", assignationShardColValueContext);
73+
Assert.assertEquals(1, (int) assignationShardColValueShardIndex);
74+
75+
final DbShardContext assignationShardDataCandidates = new DbShardContext("whatever");
76+
final Map<String, Object> data1 = new HashMap<>();
77+
data1.put("age", 100); // ignore
78+
data1.put("id", 100002); // ignore
79+
final ShardData shardData1 = new MappedShardData(data1);
80+
assignationShardDataCandidates.addShardData(shardData1);
81+
final Integer assignationShardDataCandidatesShardIndex = cluster.getDbShard("table14", assignationShardDataCandidates);
82+
Assert.assertEquals(0, (int) assignationShardDataCandidatesShardIndex);
83+
}
84+
85+
@Test
86+
public void getDbShardIndexShardOffsetTest() {
87+
final DbShardContext context = new DbShardContext("whatever");
88+
final Map<String, Object> data = new HashMap<>();
89+
data.put("id", 8);
90+
final ShardData shardData = new MappedShardData(data);
91+
context.setShardColValues(shardData);
92+
final Integer shardIndexHasOffset = cluster.getDbShard("table11", context);
93+
Assert.assertEquals(1, (int) shardIndexHasOffset);
94+
}
95+
96+
@Test
97+
public void getDbShardShardIdWorkTest() {
98+
final DbShardContext context = new DbShardContext("whatever");
99+
context.setShardId(100);
100+
context.setShardValue(100);
101+
102+
final Map<String, Object> data1 = new HashMap<>();
103+
data1.put("id", 7);
104+
final ShardData shardData1 = new MappedShardData(data1);
105+
context.setShardColValues(shardData1);
106+
107+
final Map<String, Object> data2 = new HashMap<>();
108+
data2.put("id", 8);
109+
final ShardData shardData2 = new MappedShardData(data2);
110+
context.addShardData(shardData2);
111+
112+
final Integer shardIdWordShardIndex = cluster.getDbShard("table12", context);
113+
Assert.assertEquals(100, (int) shardIdWordShardIndex);
114+
}
115+
116+
@Test
117+
public void getDbShardShardValuedWorkTest() {
118+
final DbShardContext context = new DbShardContext("whatever");
119+
context.setShardValue(1002);
120+
121+
final Map<String, Object> data1 = new HashMap<>();
122+
data1.put("id", 7);
123+
final ShardData shardData1 = new MappedShardData(data1);
124+
context.setShardColValues(shardData1);
125+
126+
final Map<String, Object> data2 = new HashMap<>();
127+
data2.put("id", 8);
128+
final ShardData shardData2 = new MappedShardData(data2);
129+
context.addShardData(shardData2);
130+
131+
final Integer shardValueWordShardIndex = cluster.getDbShard("table12", context);
132+
Assert.assertEquals(2, (int) shardValueWordShardIndex);
133+
}
134+
135+
@Test
136+
public void getDbShardShardColValuedWorkTest() {
137+
final DbShardContext context = new DbShardContext("whatever");
138+
final Map<String, Object> data1 = new HashMap<>();
139+
data1.put("id", 7);
140+
final ShardData shardData1 = new MappedShardData(data1);
141+
context.setShardColValues(shardData1);
142+
143+
final Map<String, Object> data2 = new HashMap<>();
144+
data2.put("id", 8);
145+
final ShardData shardData2 = new MappedShardData(data2);
146+
context.addShardData(shardData2);
147+
148+
final Integer shardColValueWordShardIndex = cluster.getDbShard("table12", context);
149+
Assert.assertEquals(3, (int) shardColValueWordShardIndex);
150+
}
151+
152+
@Test
153+
public void getDbShardMultiShardValueTypeTest() {
154+
155+
}
156+
157+
@Test
158+
public void getTableShardMultiShardValueTypeTest() {
159+
160+
}
161+
162+
@Test
163+
public void getUndefinedTableDbShardTest() {
164+
final DbShardContext context = new DbShardContext("whatever");
165+
final Map<String, Object> data1 = new HashMap<>();
166+
data1.put("id", 7);
167+
final ShardData shardData1 = new MappedShardData(data1);
168+
context.setShardColValues(shardData1);
169+
170+
final Integer undefinedTable1ShardIndex = cluster.getDbShard("table1", context);
171+
Assert.assertEquals(1, (int) undefinedTable1ShardIndex);
172+
173+
final boolean table1Enabled = cluster.tableShardingEnabled("table1");
174+
Assert.assertTrue(table1Enabled);
175+
176+
final String undefinedTable1Separator = cluster.getTableShardSeparator("table1");
177+
Assert.assertEquals("=", undefinedTable1Separator);
178+
}
179+
180+
181+
182+
/* table shard */
183+
@Test
184+
public void getTableShardNullTest() {
185+
final String tableShardIndex = cluster.getTableShard("table1", new TableShardContext("whatever"));
186+
Assert.assertNull(tableShardIndex);
187+
}
188+
189+
@Test
190+
public void getTableShardTest() {
191+
final TableShardContext assignationShardIdContext = new TableShardContext("whatever").setShardId("100");
192+
final String assignationShardIdShardIndex = cluster.getTableShard("table13", assignationShardIdContext);
193+
Assert.assertEquals("100", assignationShardIdShardIndex);
194+
195+
final TableShardContext assignationShardValueContext = new TableShardContext("whatever");
196+
assignationShardValueContext.setShardValue(10);
197+
final String assignationShardValueShardIndex = cluster.getTableShard("table13", assignationShardValueContext);
198+
Assert.assertEquals("2", assignationShardValueShardIndex);
199+
200+
final TableShardContext assignationShardColValueContext = new TableShardContext("whatever");
201+
final Map<String, Object> data = new HashMap<>();
202+
data.put("other", 101); // ignore
203+
data.put("id", 100001); // ignore
204+
data.put("age", 18);
205+
final ShardData shardData = new MappedShardData(data);
206+
assignationShardColValueContext.setShardColValues(shardData);
207+
final String assignationShardColValueShardIndex = cluster.getTableShard("table14", assignationShardColValueContext);
208+
Assert.assertEquals("2", assignationShardColValueShardIndex);
209+
210+
final TableShardContext assignationShardDataCandidates = new TableShardContext("whatever");
211+
final Map<String, Object> data1 = new HashMap<>();
212+
data1.put("age", 100);
213+
data1.put("id", 100002);
214+
final ShardData shardData1 = new MappedShardData(data1);
215+
assignationShardDataCandidates.addShardData(shardData1);
216+
final String assignationShardDataCandidatesShardIndex = cluster.getTableShard("table14", assignationShardDataCandidates);
217+
Assert.assertEquals("0", assignationShardDataCandidatesShardIndex);
218+
}
219+
220+
@Test
221+
public void getTableShardIndexShardOffsetTest() {
222+
final TableShardContext context = new TableShardContext("whatever");
223+
final Map<String, Object> data = new HashMap<>();
224+
data.put("id", 8); // ignore
225+
data.put("age", 18);
226+
final ShardData shardData = new MappedShardData(data);
227+
context.setShardColValues(shardData);
228+
final String shardIndexHasOffset = cluster.getTableShard("table11", context);
229+
Assert.assertEquals("1", shardIndexHasOffset);
230+
}
231+
232+
@Test
233+
public void getTableShardShardIdWorkTest() {
234+
final TableShardContext context = new TableShardContext("whatever");
235+
context.setShardId("100");
236+
context.setShardValue(100);
237+
238+
final Map<String, Object> data1 = new HashMap<>();
239+
data1.put("id", 7); // ignore
240+
data1.put("age", 17);
241+
final ShardData shardData1 = new MappedShardData(data1);
242+
context.setShardColValues(shardData1);
243+
244+
final Map<String, Object> data2 = new HashMap<>();
245+
data2.put("id", 8); // ignore
246+
data2.put("age", 18);
247+
final ShardData shardData2 = new MappedShardData(data2);
248+
context.addShardData(shardData2);
249+
250+
final String shardIdWordShardIndex = cluster.getTableShard("table12", context);
251+
Assert.assertEquals("100", shardIdWordShardIndex);
252+
}
253+
254+
@Test
255+
public void getTableShardShardValueWorkTest() {
256+
final TableShardContext context = new TableShardContext("whatever");
257+
context.setShardValue(100);
258+
259+
final Map<String, Object> data1 = new HashMap<>();
260+
data1.put("id", 7); // ignore
261+
data1.put("age", 17);
262+
final ShardData shardData1 = new MappedShardData(data1);
263+
context.setShardColValues(shardData1);
264+
265+
final Map<String, Object> data2 = new HashMap<>();
266+
data2.put("id", 8); // ignore
267+
data2.put("age", 18);
268+
final ShardData shardData2 = new MappedShardData(data2);
269+
context.addShardData(shardData2);
270+
271+
final String shardValueWordShardIndex = cluster.getTableShard("table12", context);
272+
Assert.assertEquals("1", shardValueWordShardIndex);
273+
}
274+
275+
@Test
276+
public void getTableShardShardColValuedWorkTest() {
277+
final TableShardContext context = new TableShardContext("whatever");
278+
final Map<String, Object> data1 = new HashMap<>();
279+
data1.put("id", 7); // ignore
280+
data1.put("age", 17);
281+
final ShardData shardData1 = new MappedShardData(data1);
282+
context.setShardColValues(shardData1);
283+
284+
final Map<String, Object> data2 = new HashMap<>();
285+
data2.put("id", 8); // ignore
286+
data2.put("age", 18);
287+
final ShardData shardData2 = new MappedShardData(data2);
288+
context.addShardData(shardData2);
289+
290+
final String shardColValueWordShardIndex = cluster.getTableShard("table12", context);
291+
Assert.assertEquals("2", shardColValueWordShardIndex);
292+
}
293+
294+
@Test
295+
public void getTableShardingEnabledTest() {
296+
final boolean table14Enabled = cluster.tableShardingEnabled("table14");
297+
Assert.assertTrue(table14Enabled);
298+
299+
final boolean table15Enabled = cluster.tableShardingEnabled("table15");
300+
Assert.assertFalse(table15Enabled);
301+
}
302+
303+
@Test
304+
public void getAllTableShardTest() {
305+
final Set<String> expected = new HashSet<>();
306+
expected.add("0");
307+
expected.add("1");
308+
expected.add("2");
309+
final Set<String> table15 = cluster.getAllTableShards("table15");
310+
Assert.assertEquals(expected, table15);
311+
}
312+
313+
@Test
314+
public void getTableShardSeparatorTest() {
315+
final String table15Separator = cluster.getTableShardSeparator("table15");
316+
Assert.assertEquals("=", table15Separator);
317+
}
318+
}

dal-cluster-client/src/test/resources/demo-cluster.xml

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,40 @@
2020
</DatabaseShards>
2121
<ShardStrategies>
2222
<ModStrategy default="true">
23-
<Property name="shardByDb" value="true"/>
24-
<Property name="shardByTable" value="true"/>
25-
<Property name="dbShardKey" value="id"/>
23+
<Property name="tableSharding" value="true"/>
24+
<Property name="dbShardColumn" value="id"/>
2625
<Property name="dbShardMod" value="3"/>
27-
<Property name="tableShardKey" value="age"/>
26+
<!--<Property name="dbShardOffset" value="1"/>-->
27+
<Property name="tableShardColumn" value="age"/>
2828
<Property name="tableShardMod" value="7"/>
29-
<Property name="tableNamePattern" value=""/>
29+
<Property name="tableShardSeparator" value="="/>
3030
<Tables>
31-
<Property name="shardByDb" value="true"/>
32-
<Property name="shardByTable" value="true"/>
33-
<Property name="dbShardKey" value="id"/>
34-
<Property name="dbShardMod" value="2"/>
35-
<Property name="tableShardKey" value="age"/>
31+
<Property name="tableSharding" value="true"/>
32+
<Property name="dbShardColumn" value="id"/>
33+
<Property name="dbShardMod" value="4"/>
34+
<Property name="tableShardColumn" value="age"/>
3635
<Property name="tableShardMod" value="3"/>
37-
<Table name="table11"/>
36+
<Table name="table11">
37+
<Property name="dbShardOffset" value="1"/>
38+
<Property name="tableShardOffset" value="1"/>
39+
</Table>
3840
<Table name="table12"/>
3941
<Table name="table13">
40-
<Property name="shardByDb" value="true"/>
41-
<Property name="shardByTable" value="true"/>
42-
<Property name="dbShardKey" value="age"/>
42+
<Property name="tableSharding" value="true"/>
43+
<Property name="dbShardColumn" value="age"/>
4344
<Property name="dbShardMod" value="2"/>
44-
<Property name="tableShardKey" value="age"/>
45+
<Property name="tableShardColumn" value="age"/>
4546
<Property name="tableShardMod" value="4"/>
4647
</Table>
4748
<Table name="table14">
48-
<Property name="shardByDb" value="true"/>
49-
<Property name="shardByTable" value="true"/>
50-
<Property name="dbShardKey" value="id"/>
49+
<Property name="tableSharding" value="true"/>
50+
<Property name="dbShardColumn" value="id"/>
5151
<Property name="dbShardMod" value="2"/>
52-
<Property name="tableShardKey" value="age"/>
53-
<Property name="tableShardMod" value="1"/>
52+
<Property name="tableShardColumn" value="age"/>
53+
<Property name="tableShardMod" value="4"/>
54+
</Table>
55+
<Table name="table15">
56+
<Property name="tableSharding" value="false"/>
5457
</Table>
5558
</Tables>
5659
</ModStrategy>

0 commit comments

Comments
 (0)