Skip to content

Commit

Permalink
Merge pull request #33 from sharding-sphere/dev
Browse files Browse the repository at this point in the history
update from origin
  • Loading branch information
beckhampu committed Sep 20, 2018
2 parents 4bad532 + 1f7c0be commit aff5341
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 161 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) Support SQL like `SELECT id, COUNT(*) FROM table GROUP BY 1,2`
1. [ISSUE #1120](https://github.com/sharding-sphere/sharding-sphere/issues/1120) Derived columns of `GROUP BY / ORDER BY` appear in query result
1. [ISSUE #1186](https://github.com/sharding-sphere/sharding-sphere/issues/1186) Dead lock may occur on MEMORY_STRICTLY mode when get connection on concurrency environment
1. [ISSUE #1265](https://github.com/sharding-sphere/sharding-sphere/issues/1265) RoundRobinMasterSlaveLoadBalanceAlgorithm throw an ArrayIndexOutOfBoundsException when AtomicInteger overflow

#### Sharding-JDBC

Expand Down
1 change: 1 addition & 0 deletions RELEASE-NOTES_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) 支持SQL `SELECT id, COUNT(*) FROM table GROUP BY 1,2`
1. [ISSUE #1120](https://github.com/sharding-sphere/sharding-sphere/issues/1120) `GROUP BY / ORDER BY`产生的补列不应展现在查询结果中
1. [ISSUE #1186](https://github.com/sharding-sphere/sharding-sphere/issues/1186) 在MEMORY_STRICTLY模式中,并发环境下可能产生死锁
1. [ISSUE #1265](https://github.com/sharding-sphere/sharding-sphere/issues/1265) 当AtomicInteger溢出后,RoundRobinMasterSlaveLoadBalanceAlgorithm抛出ArrayIndexOutOfBoundsException异常

#### Sharding-JDBC

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public String getDataSource(final String name, final String masterDataSourceName
AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0);
COUNT_MAP.putIfAbsent(name, count);
count.compareAndSet(slaveDataSourceNames.size(), 0);
return slaveDataSourceNames.get(count.getAndIncrement() % slaveDataSourceNames.size());
return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.shardingsphere.core.exception.ShardingException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -53,72 +52,6 @@ public ShardingExecuteEngine(final int executorSize) {
MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS);
}

/**
* Execute.
*
* @param inputs input values
* @param callback sharding execute callback
* @param <I> type of input value
* @param <O> type of return value
* @return execute result
* @throws SQLException throw if execute failure
*/
public <I, O> List<O> execute(final Collection<I> inputs, final ShardingExecuteCallback<I, O> callback) throws SQLException {
return execute(inputs, null, callback);
}

/**
* Execute.
*
* @param inputs input values
* @param firstCallback first sharding execute callback
* @param callback sharding execute callback
* @param <I> type of input value
* @param <O> type of return value
* @return execute result
* @throws SQLException throw if execute failure
*/
public <I, O> List<O> execute(final Collection<I> inputs, final ShardingExecuteCallback<I, O> firstCallback, final ShardingExecuteCallback<I, O> callback) throws SQLException {
if (inputs.isEmpty()) {
return Collections.emptyList();
}
Iterator<I> inputIterator = inputs.iterator();
I firstInput = inputIterator.next();
Collection<ListenableFuture<O>> restFutures = asyncExecute(Lists.newArrayList(inputIterator), callback);
return getResults(syncExecute(firstInput, null == firstCallback ? callback : firstCallback), restFutures);
}

private <I, O> Collection<ListenableFuture<O>> asyncExecute(final Collection<I> inputs, final ShardingExecuteCallback<I, O> callback) {
Collection<ListenableFuture<O>> result = new ArrayList<>(inputs.size());
for (final I each : inputs) {
result.add(executorService.submit(new Callable<O>() {

@Override
public O call() throws SQLException {
return callback.execute(each);
}
}));
}
return result;
}

private <I, O> O syncExecute(final I input, final ShardingExecuteCallback<I, O> callback) throws SQLException {
return callback.execute(input);
}

private <O> List<O> getResults(final O firstResult, final Collection<ListenableFuture<O>> restFutures) throws SQLException {
List<O> result = new LinkedList<>();
result.add(firstResult);
for (ListenableFuture<O> each : restFutures) {
try {
result.add(each.get());
} catch (final InterruptedException | ExecutionException ex) {
return throwException(ex);
}
}
return result;
}

/**
* Execute for group.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,6 @@ public final class SQLExecuteTemplate {

private final ShardingExecuteEngine executeEngine;

/**
* Execute.
*
* @param statementExecuteUnits SQL execute units
* @param callback SQL execute callback
* @param <T> class type of return value
* @return execute result
* @throws SQLException SQL exception
*/
public <T> List<T> execute(final Collection<? extends StatementExecuteUnit> statementExecuteUnits, final SQLExecuteCallback<T> callback) throws SQLException {
return execute(statementExecuteUnits, null, callback);
}

/**
* Execute.
*
* @param statementExecuteUnits SQL execute units
* @param firstExecuteCallback first SQL execute callback
* @param callback SQL execute callback
* @param <T> class type of return value
* @return execute result
* @throws SQLException SQL exception
*/
@SuppressWarnings("unchecked")
public <T> List<T> execute(
final Collection<? extends StatementExecuteUnit> statementExecuteUnits, final SQLExecuteCallback<T> firstExecuteCallback, final SQLExecuteCallback<T> callback) throws SQLException {
try {
return executeEngine.execute((Collection) statementExecuteUnits, firstExecuteCallback, callback);
} catch (final SQLException ex) {
ExecutorExceptionHandler.handleException(ex);
return Collections.emptyList();
}
}



/**
* Execute group.
*
Expand Down Expand Up @@ -111,4 +75,3 @@ public <T> List<T> executeGroup(final Collection<ShardingExecuteGroup<? extends
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
SelectStatementTest.class,
InsertStatementParserTest.class,
UpdateStatementParserTest.class
InsertStatementParserTest.class
})
public final class AllStatementParserTests {
}

This file was deleted.

26 changes: 26 additions & 0 deletions sharding-core/src/test/resources/parser/update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,30 @@
</or-condition>
</parser-result>

<parser-result sql-case-id="update_with_or" parameters="1000, 0, 10">
<tables>
<table name="t_order" />
</tables>
<tokens>
<table-token begin-position="7" original-literals="t_order" />
</tokens>
<or-condition>
<and-condition>
<condition column-name="order_id" table-name="t_order" operator="EQUAL">
<value index="0" literal="1000" type="int" />
</condition>
<condition column-name="user_id" table-name="t_order" operator="EQUAL">
<value index="2" literal="10" type="int" />
</condition>
</and-condition>
<and-condition>
<condition column-name="order_id" table-name="t_order" operator="EQUAL">
<value index="1" literal="0" type="int" />
</condition>
<condition column-name="user_id" table-name="t_order" operator="EQUAL">
<value index="2" literal="10" type="int" />
</condition>
</and-condition>
</or-condition>
</parser-result>
</parser-result-sets>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class AbstractStatementExecutor {

private final Collection<ShardingExecuteGroup<StatementExecuteUnit>> executeGroups = new LinkedList<>();

@Getter
@Getter(AccessLevel.PROTECTED)
@Setter
private SQLType sqlType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public final List<Connection> getConnections(final String dataSourceName, final
}

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private synchronized List<Connection> createConnections(final DataSource dataSource, final int connectionSize) throws SQLException {
private List<Connection> createConnections(final DataSource dataSource, final int connectionSize) throws SQLException {
List<Connection> result = new ArrayList<>(connectionSize);
synchronized (dataSource) {
for (int i = 0; i < connectionSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
<assertion expected-data-file="update.xml" />
</dml-test-case>

<dml-test-case sql-case-id="update_with_or">
<assertion parameters="1000:int, 0:int, 10:int" expected-data-file="update.xml" />
</dml-test-case>

<dml-test-case sql-case-id="delete_with_sharding_value">
<assertion parameters="1000:int, 10:int, init:String" expected-data-file="delete_with_sharding_value.xml" />
</dml-test-case>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.executor.ShardingExecuteEngine;
import io.shardingsphere.core.executor.ShardingExecuteGroup;
import io.shardingsphere.core.executor.StatementExecuteUnit;
import io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback;
import io.shardingsphere.core.executor.sql.execute.SQLExecuteTemplate;
Expand All @@ -36,6 +37,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -73,8 +75,9 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) {
return 0;
}
};
sqlExecuteTemplate.execute(Collections.singleton(new StatementExecuteUnit(new RouteUnit("ds_0",
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)), executeCallback);
ShardingExecuteGroup<StatementExecuteUnit> shardingExecuteGroup = new ShardingExecuteGroup<>(Collections.singletonList(new StatementExecuteUnit(new RouteUnit("ds_0",
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)));
sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback);
assertThat(getTracer().finishedSpans().size(), is(1));
}

Expand Down Expand Up @@ -102,7 +105,8 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) {
return 0;
}
};
sqlExecuteTemplate.execute(statementExecuteUnits, executeCallback);
ShardingExecuteGroup<StatementExecuteUnit> shardingExecuteGroup = new ShardingExecuteGroup<>(statementExecuteUnits);
sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback);
assertThat(getTracer().finishedSpans().size(), is(2));
}

Expand All @@ -121,7 +125,8 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) th
throw new SQLException();
}
};
sqlExecuteTemplate.execute(Collections.singleton(new StatementExecuteUnit(new RouteUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)), executeCallback);
ShardingExecuteGroup<StatementExecuteUnit> shardingExecuteGroup = new ShardingExecuteGroup<>(Collections.singletonList(new StatementExecuteUnit(new RouteUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)));
sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback);
}
}
1 change: 1 addition & 0 deletions sharding-sql-test/src/main/resources/sql/dml/update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<sql-case id="update_with_special_character" value="UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND user_id = ?" db-types="MySQL" />
<sql-case id="update_with_special_comments" value="UPDATE /*+ index(field1) */ ONLY t_order SET status=? WHERE order_id = ? AND user_id = ? RETURN * LOG ERRORS INTO TABLE_LOG" db-types="Oracle" />
<sql-case id="update_without_parameters" value="UPDATE t_order SET status = 'update' WHERE order_id = 1000 AND user_id = 10" />
<sql-case id="update_with_or" value="UPDATE t_order SET status = 'update' WHERE (order_id = ? OR order_id = ?) AND user_id = ?" />
</sql-cases>

0 comments on commit aff5341

Please sign in to comment.