Skip to content

Commit c0ae170

Browse files
committed
dal 2.0.2, support sharding
1 parent 8295e54 commit c0ae170

13 files changed

Lines changed: 47 additions & 35 deletions

File tree

dal-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.ctrip.platform</groupId>
66
<artifactId>dal-client</artifactId>
7-
<version>2.0.2-SNAPSHOT</version>
7+
<version>2.0.2</version>
88
<properties>
99
<java.version>1.8</java.version>
1010
<maven.compiler.source>${java.version}</maven.compiler.source>

dal-client/src/main/java/com/ctrip/platform/dal/dao/datasource/DataSourceCreator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,9 @@ private SingleDataSource asyncCreateDataSourceWithPool(String name, DataSourceCo
143143
return ds;
144144
}
145145

146+
protected void returnAllDataSources() {
147+
for (SingleDataSource ds : targetDataSourceCache.values())
148+
returnDataSource(ds);
149+
}
150+
146151
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/task/DalBulkTaskRequest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public class DalBulkTaskRequest<K, T> implements DalRequest<K>{
3131
public DalBulkTaskRequest(String logicDbName, String rawTableName, DalHints hints, List<T> rawPojos, BulkTask<K, T> task) {
3232
this.logicDbName = logicDbName;
3333
this.rawTableName = rawTableName;
34-
this.hints = hints.clone();
34+
this.hints = hints != null ? hints.clone() : new DalHints();
3535
this.rawPojos = rawPojos;
3636
this.task = task;
3737
this.caller = LogContext.getRequestCaller();
38+
prepareRequestContext();
3839
}
3940

4041
@Override
@@ -64,11 +65,9 @@ public void validateAndPrepare() throws SQLException {
6465

6566
detectBulkTaskDistributedTransaction(logicDbName, hints, daoPojos);
6667
taskContext = task.createTaskContext(hints, daoPojos, rawPojos);
67-
68-
prepareRequestContext(hints);
6968
}
7069

71-
private void prepareRequestContext(DalHints hints) {
70+
private void prepareRequestContext() {
7271
hints.setRequestContext(null);
7372
if (task instanceof TaskAdapter) {
7473
RequestContext ctx = new DalRequestContext().setLogicTableName(((TaskAdapter) task).rawTableName);

dal-client/src/main/java/com/ctrip/platform/dal/dao/task/DalSingleTaskRequest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public class DalSingleTaskRequest<T> implements DalRequest<int[]> {
3333
private DalSingleTaskRequest(String logicDbName, DalHints hints, SingleTask<T> task) {
3434
this.logicDbName = logicDbName;
3535
this.task = task;
36-
this.hints = hints.clone();
36+
this.hints = hints != null ? hints.clone() : new DalHints();
3737
this.caller = LogContext.getRequestCaller();
38+
prepareRequestContext();
3839
}
3940

4041
public DalSingleTaskRequest(String logicDbName, DalHints hints, T rawPojo, SingleTask<T> task) {
@@ -84,11 +85,9 @@ public void validateAndPrepare() throws SQLException {
8485

8586
detectDistributedTransaction(logicDbName, hints, daoPojos);
8687
dalTaskContext = task.createTaskContext();
87-
88-
prepareRequestContext(hints);
8988
}
9089

91-
private void prepareRequestContext(DalHints hints) {
90+
private void prepareRequestContext() {
9291
hints.setRequestContext(null);
9392
if (task instanceof TaskAdapter) {
9493
RequestContext ctx = new DalRequestContext().setLogicTableName(((TaskAdapter) task).rawTableName);

dal-client/src/main/java/com/ctrip/platform/dal/dao/task/DalSqlTaskRequest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ public DalSqlTaskRequest(String logicDbName, SqlBuilder builder, DalHints hints,
4747
this.logicDbName = logicDbName;
4848
this.builder = builder;
4949
this.parameters = builder.buildParameters();
50-
this.hints = hints.clone();
50+
this.hints = hints != null ? hints.clone() : new DalHints();
5151
this.task = task;
5252
this.merger = merger;
53-
this.shards = getShards();
5453
this.caller = LogContext.getRequestCaller();
54+
prepareRequestContext();
55+
this.shards = getShards();
5556
}
5657

5758
@Override
@@ -68,11 +69,9 @@ public boolean isAsynExecution() {
6869
public void validateAndPrepare() throws SQLException {
6970
DalShardingHelper.detectDistributedTransaction(shards);
7071
taskContext = task.createTaskContext();
71-
72-
prepareRequestContext(hints);
7372
}
7473

75-
private void prepareRequestContext(DalHints hints) {
74+
private void prepareRequestContext() {
7675
hints.setRequestContext(null);
7776
if (task instanceof TaskAdapter) {
7877
RequestContext ctx = new DalRequestContext().setLogicTableName(((TaskAdapter) task).rawTableName);

dal-client/src/test/java/com/ctrip/platform/dal/dao/configure/AllTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
@RunWith(Suite.class)
88
@SuiteClasses({
99
SlaveFreshnessScannerMysqlTest.class,
10-
DataSourceConfigureLocatorTest.class
10+
DataSourceConfigureLocatorTest.class,
11+
ClusterConfigParserTest.class,
12+
ClusterConfigValidatorTest.class
1113
})
1214
public class AllTest {
1315

dal-client/src/test/java/com/ctrip/platform/dal/dao/datasource/ForceSwitchableDataSourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void testProviderThrowException() throws Exception {
205205
MockSwitchListener listener = new MockSwitchListener();
206206
dataSource.addListener(listener);
207207

208-
assertEquals("nullDataSource", dataSource.getSingleDataSource().getName());
208+
assertTrue("nullDataSource".equalsIgnoreCase(dataSource.getSingleDataSource().getName()));
209209
SwitchableDataSourceStatus status0 = dataSource.getStatus();
210210
assertFalse(status0.isForceSwitched());
211211
assertFalse(status0.isPoolCreated());

dal-client/src/test/java/com/ctrip/platform/dal/dao/datasource/RefreshableDataSourceTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,19 @@ public void run() {
343343

344344

345345
while (true) {
346-
Connection connection = refreshableDataSource.getConnection();
347-
String currentServer = DataSourceSwitchChecker.getDBServerName(connection, refreshableDataSource.getSingleDataSource().getDataSourceConfigure());
348-
System.out.println(currentServer);
349-
if ("DST56614".equalsIgnoreCase(currentServer)) {
350-
break;
346+
try {
347+
Connection connection = refreshableDataSource.getConnection();
348+
String currentServer = DataSourceSwitchChecker.getDBServerName(connection, refreshableDataSource.getSingleDataSource().getDataSourceConfigure());
349+
System.out.println(currentServer);
350+
if ("DST56614".equalsIgnoreCase(currentServer)) {
351+
break;
352+
}
353+
connection.close();
354+
} catch (SQLException e) {
355+
e.printStackTrace();
356+
} finally {
357+
Thread.sleep(10);
351358
}
352-
connection.close();
353359
}
354360
latch.await();
355361
Assert.assertTrue(switched.get());
@@ -517,6 +523,8 @@ public void testReusedSingleDataSource() {
517523

518524
@Test
519525
public void testDataSourceSwitch() throws Exception {
526+
DataSourceCreator.getInstance().returnAllDataSources();
527+
520528
Properties p1 = new Properties();
521529
p1.setProperty("userName", "root");
522530
p1.setProperty("password", "!QAZ@WSX1qaz2wsx");

dal-client/src/test/java/com/ctrip/platform/dal/dao/task/DalSqlTaskRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void testIsCrossShard() throws SQLException {
163163
Set<String> shards = new HashSet<>();
164164
shards.add("0");
165165
shards.add("1");
166-
hints.inAllShards();
166+
hints.inShards(shards);
167167
test = new DalSqlTaskRequest<>("dao_test_sqlsvr_dbShard", new TestSqlBuilder(), hints, null, null);
168168
assertTrue(test.isCrossShard());
169169
}

dal-cluster-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.ctrip.framework.dal</groupId>
77
<artifactId>dal-cluster-client</artifactId>
8-
<version>2.0.2-SNAPSHOT</version>
8+
<version>2.0.2</version>
99
<!-- TODO: mvn package sources & javadoc -->
1010
<properties>
1111
<java.version>1.8</java.version>

0 commit comments

Comments
 (0)