Skip to content

Commit

Permalink
Merge pull request #11 from sharding-sphere/dev
Browse files Browse the repository at this point in the history
update from origin
  • Loading branch information
beckhampu authored Jun 11, 2018
2 parents 6894fa6 + f27721d commit 90ef7e7
Show file tree
Hide file tree
Showing 261 changed files with 6,408 additions and 3,664 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
1. [ISSUE #719](https://github.com/sharding-sphere/sharding-sphere/issues/719) Support Spring bean namespace to inject key generator objects
1. [ISSUE #720](https://github.com/sharding-sphere/sharding-sphere/issues/720) Support Spring bean namespace to inject sharding algorithm objects
1. [ISSUE #773](https://github.com/sharding-sphere/sharding-sphere/issues/773) Support sharding and autoincrement primary key of INSERT statements without column names
1. [ISSUE #916](https://github.com/sharding-sphere/sharding-sphere/issues/916) Support authorization for Sharding Proxy

### Bug Fixes

Expand All @@ -31,6 +32,7 @@
1. [ISSUE #735](https://github.com/sharding-sphere/sharding-sphere/issues/735) Unexpected slave datasource routing result when using Round-robin load-balance algorithm in Mybatis
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) SQL parse error with `NOT IN`
1. [ISSUE #827](https://github.com/sharding-sphere/sharding-sphere/issues/827) Fix endless loop for bad SQL like `SELECT * FROM table WHERE id IN ()`
1. [ISSUE #919](https://github.com/sharding-sphere/sharding-sphere/issues/919) Use groovy to parse inline expression may cause memory leak

## 2.0.3

Expand Down
2 changes: 2 additions & 0 deletions RELEASE-NOTES_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
1. [ISSUE #719](https://github.com/sharding-sphere/sharding-sphere/issues/719) 支持Spring bean的方式在命名空间中注入自增序列生成器对象
1. [ISSUE #720](https://github.com/sharding-sphere/sharding-sphere/issues/720) 支持Spring bean的方式在命名空间中注入分片算法对象
1. [ISSUE #773](https://github.com/sharding-sphere/sharding-sphere/issues/773) 支持不包含列名的INSERT语句的分片与自增主键
1. [ISSUE #916](https://github.com/sharding-sphere/sharding-sphere/issues/916) 支持Sharding Proxy的登录权限认证

### 缺陷修正

Expand All @@ -31,6 +32,7 @@
1. [ISSUE #735](https://github.com/sharding-sphere/sharding-sphere/issues/735) 在Mybatis中使用RoundRobinMasterSlaveLoadBalanceAlgorithm算法路由存在问题
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) `NOT IN`解析异常
1. [ISSUE #827](https://github.com/sharding-sphere/sharding-sphere/issues/827)`SELECT * FROM table WHERE id IN ()`这种SQL跳出死循环
1. [ISSUE #919](https://github.com/sharding-sphere/sharding-sphere/issues/919) 使用Groovy解析行表达式可能导致内存泄漏

## 2.0.3

Expand Down
12 changes: 6 additions & 6 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
- [x] COM_INIT_DB Packet
- [x] COM_QUERY Packet
- [x] COM_FIELD_LIST Packet
- [ ] COM_STMT_PREPARE
- [ ] COM_STMT_EXECUTE
- [ ] COM_STMT_CLOSE
- [ ] COM_STMT_RESET
- [x] COM_STMT_PREPARE
- [x] COM_STMT_EXECUTE
- [x] COM_STMT_CLOSE
- [x] COM_STMT_RESET
- [ ] Oracle
- [ ] SQLServer
- [ ] PostgreSQL
Expand All @@ -62,9 +62,9 @@
- [x] Aggregation Functions
- [x] LIMIT, rownum, TOP
- [x] Simple Sub Query
- [x] OR
- [ ] DISTINCT
- [ ] HAVING
- [ ] OR
- [ ] UNION, UNION ALL
- [ ] Calculate Expression, eg: SUM(pv) / COUNT(uv)
- [ ] Complicated Sub Query
Expand All @@ -74,7 +74,7 @@
- [x] INSERT SET
- [x] UPDATE
- [x] DELETE
- [ ] INSERT INTO VALUES (xxx), (xxx)
- [x] INSERT INTO VALUES (xxx), (xxx)
- [ ] UPDATE Multiple Tables
- [ ] DELETE Multiple Tables
- [x] DDL
Expand Down
32 changes: 30 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@
<os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version>
<coveralls-maven-plugin.version>4.1.0</coveralls-maven-plugin.version>
<docker-maven-plugin.version>0.4.14</docker-maven-plugin.version>

<commons-codec.version>1.10</commons-codec.version>
<atomikos.version>4.0.4</atomikos.version>
<javax.transaction.version>1.1</javax.transaction.version>

<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand Down Expand Up @@ -315,6 +318,31 @@
<version>${opentracing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>${javax.transaction.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.constant;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* TransactionType Enum.
*
* @author zhaojun
*/
@RequiredArgsConstructor
@Getter
public enum TransactionType {

/**
* default local transaction.
*/
NONE(""),

/**
* XA distribute transaction provided by RDBMS vendor.
*/
XA("XA"),

/**
* TCC (Try-Confirm-Cancel) distribute transaction mode.
*/
TCC("TCC");

private final String type;

/**
* Find enum by type value.
*
* @param type property type
* @return value enum, return {@code NONE} if not found
*/
public static TransactionType findByValue(final String type) {
for (TransactionType each : TransactionType.values()) {
if (each.getType().equals(type)) {
return each;
}
}
return NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package io.shardingsphere.core.metadata;

import com.google.common.base.Strings;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.rule.DataNode;
import io.shardingsphere.core.rule.ShardingDataSourceNames;
Expand All @@ -34,15 +33,14 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Abstract Sharding metadata.
Expand All @@ -53,58 +51,78 @@
@Getter
@Setter
@Slf4j
public abstract class ShardingMetaData implements AutoCloseable {

private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(), 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Sharding-JDBC-%d").build());
public abstract class ShardingMetaData {

private final ListeningExecutorService executorService;

private Map<String, TableMetaData> tableMetaDataMap;

public ShardingMetaData() {
executorService = MoreExecutors.listeningDecorator(THREAD_POOL_EXECUTOR);
MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS);
public ShardingMetaData(final ListeningExecutorService executorService) {
this.executorService = executorService;
}

/**
* Initialize sharding metadata.
*
* @param shardingRule sharding rule
* @throws SQLException SQL exception
*/
public void init(final ShardingRule shardingRule) throws SQLException {
tableMetaDataMap = new HashMap<>(shardingRule.getTableRules().size(), 1);
for (TableRule each : shardingRule.getTableRules()) {
refresh(each, shardingRule);
public void init(final ShardingRule shardingRule) {
tableMetaDataMap = new HashMap<>();
try {
Collection<TableRule> tableRules = getTableRules(shardingRule);
for (TableRule each : tableRules) {
refresh(each, shardingRule);
}
} catch (SQLException ex) {
throw new ShardingException(ex);
}
}


private Collection<TableRule> getTableRules(final ShardingRule shardingRule) throws SQLException {
Collection<TableRule> result = new LinkedList<>();
result.addAll(shardingRule.getTableRules());
String defaultDataSourceName = shardingRule.getShardingDataSourceNames().getDefaultDataSourceName();
if (!Strings.isNullOrEmpty(defaultDataSourceName)) {
Collection<String> defaultTableNames = getTableNamesFromDefaultDataSource(shardingRule.getMasterDataSourceName(defaultDataSourceName));
for (String each : defaultTableNames) {
result.add(shardingRule.getTableRule(each));
}
}
return result;
}

/**
* Get table names from default data source.
*
* @param defaultDataSourceName default data source name.
* @return table names from default data source
* @throws SQLException SQL exception.
*/
public abstract Collection<String> getTableNamesFromDefaultDataSource(String defaultDataSourceName) throws SQLException;

/**
* refresh each tableMetaData by TableRule.
* Refresh each tableMetaData by TableRule.
*
* @param each table rule
* @param shardingRule sharding rule
* @throws SQLException SQL Exception
*/
public void refresh(final TableRule each, final ShardingRule shardingRule) throws SQLException {
public void refresh(final TableRule each, final ShardingRule shardingRule) {
refresh(each, shardingRule, Collections.<String, Connection>emptyMap());
}

/**
* refresh each tableMetaData by TableRule.
* Refresh each tableMetaData by TableRule.
*
* @param each table rule
* @param shardingRule sharding rule
* @param connectionMap connection map passing from sharding connection
* @throws SQLException SQL exception
*/
public void refresh(final TableRule each, final ShardingRule shardingRule, final Map<String, Connection> connectionMap) throws SQLException {
public void refresh(final TableRule each, final ShardingRule shardingRule, final Map<String, Connection> connectionMap) {
tableMetaDataMap.put(each.getLogicTable(), getFinalTableMetaData(each.getLogicTable(), each.getActualDataNodes(), shardingRule.getShardingDataSourceNames(), connectionMap));
}

private TableMetaData getFinalTableMetaData(final String logicTableName, final List<DataNode> actualDataNodes,
final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) throws SQLException {
final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) {
List<TableMetaData> actualTableMetaDataList = getAllActualTableMetaData(actualDataNodes, shardingDataSourceNames, connectionMap);
for (int i = 0; i < actualTableMetaDataList.size(); i++) {
if (actualTableMetaDataList.size() - 1 == i) {
Expand All @@ -114,10 +132,11 @@ private TableMetaData getFinalTableMetaData(final String logicTableName, final L
throw new ShardingException(getErrorMsgOfTableMetaData(logicTableName, actualTableMetaDataList.get(i), actualTableMetaDataList.get(i + 1)));
}
}
return new TableMetaData(new ArrayList<ColumnMetaData>());
return new TableMetaData();
}

private List<TableMetaData> getAllActualTableMetaData(final List<DataNode> actualDataNodes, final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) throws SQLException {
private List<TableMetaData> getAllActualTableMetaData(final List<DataNode> actualDataNodes, final ShardingDataSourceNames shardingDataSourceNames,
final Map<String, Connection> connectionMap) {
List<ListenableFuture<TableMetaData>> result = new ArrayList<>();
for (final DataNode each : actualDataNodes) {
result.add(executorService.submit(new Callable<TableMetaData>() {
Expand All @@ -126,6 +145,7 @@ public TableMetaData call() throws Exception {
}
}));
}

try {
return Futures.allAsList(result).get();
} catch (final InterruptedException | ExecutionException ex) {
Expand All @@ -143,32 +163,9 @@ public TableMetaData call() throws Exception {
* @throws SQLException SQL exception
*/
public abstract TableMetaData getTableMetaData(DataNode dataNode, ShardingDataSourceNames shardingDataSourceNames, Map<String, Connection> connectionMap) throws SQLException;

private String getErrorMsgOfTableMetaData(final String logicTableName, final TableMetaData oldTableMetaData, final TableMetaData newTableMetaData) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" Cannot get uniformed table structure for ").append(logicTableName).append(".");
stringBuilder.append("The different metadata of actual tables is as follows:");
stringBuilder.append(oldTableMetaData.toString());
stringBuilder.append("\n");
stringBuilder.append(newTableMetaData.toString());
return stringBuilder.toString();
}

@Override
public void close() {
THREAD_POOL_EXECUTOR.execute(new Runnable() {

@Override
public void run() {
try {
executorService.shutdown();
while (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (final InterruptedException ex) {
log.error("ExecutorEngine can not been terminated", ex);
}
}
});
private String getErrorMsgOfTableMetaData(final String logicTableName, final TableMetaData oldTableMetaData, final TableMetaData newTableMetaData) {
return String.format("Cannot get uniformed table structure for %s. The different metadata of actual tables is as follows:\n%s\n%s.",
logicTableName, oldTableMetaData.toString(), newTableMetaData.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
Expand All @@ -39,13 +39,17 @@ public final class TableMetaData {

private final Collection<ColumnMetaData> columnMetaData;

public TableMetaData() {
columnMetaData = new LinkedList<>();
}

/**
* Get all column names.
*
* @return column name list.
*/
public List<String> getAllColumnNames() {
List<String> columnNames = new ArrayList<>();
public Collection<String> getAllColumnNames() {
List<String> columnNames = new LinkedList<>();
for (ColumnMetaData each : columnMetaData) {
columnNames.add(each.getColumnName());
}
Expand Down
Loading

0 comments on commit 90ef7e7

Please sign in to comment.