Skip to content

Commit

Permalink
dataSourcePoolSize改为dataSourceMergePoolSize 优化聚合链接线程池单个connection获取互不…
Browse files Browse the repository at this point in the history
…影响 0.9.10
  • Loading branch information
xuejmnet committed May 29, 2023
1 parent 7cc6737 commit b582708
Show file tree
Hide file tree
Showing 22 changed files with 68 additions and 75 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.easy-query</groupId>
<artifactId>easy-query-all</artifactId>
<packaging>pom</packaging>
<version>0.9.9</version>
<version>0.9.10</version>
<name>easy-query</name>
<description>java object query distributed connector</description>
<url>https://github.com/xuejmnet/easy-query</url>
Expand Down
2 changes: 1 addition & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easy-query-all</artifactId>
<groupId>com.easy-query</groupId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion samples/springbootdemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-springboot-starter</artifactId>
<version>0.9.9</version>
<version>0.9.10</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion sql-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easy-query-all</artifactId>
<groupId>com.easy-query</groupId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EasyQueryOption {
private final EasyQueryReplicaOption replicaOption;
private final EasyQueryShardingOption shardingOption;
private final String defaultDataSourceName;
private final int defaultDataSourcePoolSize;
private final int defaultDataSourceMergePoolSize;
/**
* 默认查询是否查询large column默认true
*/
Expand All @@ -46,7 +46,7 @@ public class EasyQueryOption {

public EasyQueryOption(boolean deleteThrowError, SQLExecuteStrategyEnum insertStrategy, SQLExecuteStrategyEnum updateStrategy, ConnectionModeEnum connectionMode, int maxShardingQueryLimit, int executorMaximumPoolSize, int executorCorePoolSize,
boolean throwIfNotMatchRoute, long shardingExecuteTimeoutMillis,
EasyQueryShardingOption shardingOption, EasyQueryReplicaOption replicaOption, String defaultDataSourceName, int defaultDataSourcePoolSize, boolean queryLargeColumn, int maxShardingRouteCount, int executorQueueSize, long multiConnWaitTimeoutMillis,
EasyQueryShardingOption shardingOption, EasyQueryReplicaOption replicaOption, String defaultDataSourceName, int defaultDataSourceMergePoolSize, boolean queryLargeColumn, int maxShardingRouteCount, int executorQueueSize, long multiConnWaitTimeoutMillis,
boolean warningBusy) {

if (executorMaximumPoolSize > 0) {
Expand All @@ -62,16 +62,19 @@ public EasyQueryOption(boolean deleteThrowError, SQLExecuteStrategyEnum insertSt
if (executorQueueSize < maxShardingQueryLimit) {
throw new IllegalArgumentException("Invalid arguments: executorQueueSize < maxShardingQueryLimit");
}
if((executorMaximumPoolSize+executorQueueSize) < maxShardingQueryLimit){
if ((executorMaximumPoolSize + executorQueueSize) < maxShardingQueryLimit) {
throw new IllegalArgumentException("Invalid arguments: (executorMaximumPoolSize+executorQueueSize) < maxShardingQueryLimit");
}
}
if (shardingExecuteTimeoutMillis <= 0) {
throw new IllegalArgumentException("shardingExecuteTimeoutMillis less than zero:" + shardingExecuteTimeoutMillis);
}
if(multiConnWaitTimeoutMillis <= 0){
if (multiConnWaitTimeoutMillis <= 0) {
throw new IllegalArgumentException("multiConnWaitTimeoutMillis <= 0");
}
if (defaultDataSourceMergePoolSize > 0 && defaultDataSourceMergePoolSize < maxShardingQueryLimit) {
throw new IllegalArgumentException("invalid arguments: defaultDataSourceMergePoolSize > 0 && defaultDataSourceMergePoolSize < maxShardingQueryLimit.");
}
this.deleteThrowError = deleteThrowError;
this.insertStrategy = SQLExecuteStrategyEnum.getDefaultStrategy(insertStrategy, SQLExecuteStrategyEnum.ONLY_NOT_NULL_COLUMNS);
this.updateStrategy = SQLExecuteStrategyEnum.getDefaultStrategy(updateStrategy, SQLExecuteStrategyEnum.ALL_COLUMNS);
Expand All @@ -84,7 +87,7 @@ public EasyQueryOption(boolean deleteThrowError, SQLExecuteStrategyEnum insertSt
this.shardingOption = shardingOption;
this.replicaOption = replicaOption;
this.defaultDataSourceName = defaultDataSourceName;
this.defaultDataSourcePoolSize = defaultDataSourcePoolSize;
this.defaultDataSourceMergePoolSize = defaultDataSourceMergePoolSize;
this.queryLargeColumn = queryLargeColumn;
this.maxShardingRouteCount = maxShardingRouteCount;
this.executorQueueSize = executorQueueSize;
Expand Down Expand Up @@ -152,8 +155,8 @@ public int getExecutorQueueSize() {
return executorQueueSize;
}

public int getDefaultDataSourcePoolSize() {
return defaultDataSourcePoolSize;
public int getDefaultDataSourceMergePoolSize() {
return defaultDataSourceMergePoolSize;
}

public long getMultiConnWaitTimeoutMillis() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class EasyQueryOptionBuilder {
private EasyQueryReplicaOption replicaOption;
private EasyQueryShardingOption shardingOption;
private String defaultDataSourceName;
private int defaultDataSourcePoolSize;
private int defaultDataSourceMergePoolSize;
private boolean queryLargeColumn;
private int maxShardingRouteCount;
private int executorQueueSize;
Expand All @@ -39,7 +39,7 @@ public EasyQueryOptionBuilder() {
this.updateStrategy = SQLExecuteStrategyEnum.ALL_COLUMNS;
this.connectionMode = ConnectionModeEnum.SYSTEM_AUTO;
this.maxShardingQueryLimit = 5;
this.defaultDataSourcePoolSize = 0;
this.defaultDataSourceMergePoolSize = 0;
this.executorMaximumPoolSize = 0;
this.executorCorePoolSize = Math.min(Runtime.getRuntime().availableProcessors(), 4);
this.throwIfRouteNotMatch = true;
Expand Down Expand Up @@ -118,10 +118,10 @@ public void setExecutorQueueSize(int executorQueueSize) {

/**
* 如果当前没有分表操作建议设置为0
* @param defaultDataSourcePoolSize
* @param defaultDataSourceMergePoolSize
*/
public void setDefaultDataSourcePoolSize(int defaultDataSourcePoolSize) {
this.defaultDataSourcePoolSize = defaultDataSourcePoolSize;
public void setDefaultDataSourceMergePoolSize(int defaultDataSourceMergePoolSize) {
this.defaultDataSourceMergePoolSize = defaultDataSourceMergePoolSize;
}

public void setMultiConnWaitTimeoutMillis(long multiConnWaitTimeoutMillis) {
Expand Down Expand Up @@ -149,7 +149,7 @@ public EasyQueryOption build() {
this.shardingOption,
this.replicaOption,
this.defaultDataSourceName,
this.defaultDataSourcePoolSize,
this.defaultDataSourceMergePoolSize,
this.queryLargeColumn,
this.maxShardingRouteCount,
this.executorQueueSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
public final class ShardingDataSource {
private final String dataSourceName;
private final DataSource dataSource;
private final int dataSourcePoolSize;
private final int dataSourceMergePoolSize;

public ShardingDataSource(String dataSourceName, DataSource dataSource, int dataSourcePoolSize) {
public ShardingDataSource(String dataSourceName, DataSource dataSource, int dataSourceMergePoolSize) {
if (EasyStringUtil.isBlank(dataSourceName)) {
throw new IllegalArgumentException("dataSourceName is empty");
}
if (dataSourcePoolSize <= 0) {
throw new IllegalArgumentException("dataSourcePoolSize <= 0");
if (dataSourceMergePoolSize <= 0) {
throw new IllegalArgumentException("dataSourceMergePoolSize <= 0");
}
this.dataSourceName = dataSourceName;
this.dataSource = dataSource;
this.dataSourcePoolSize = dataSourcePoolSize;
this.dataSourceMergePoolSize = dataSourceMergePoolSize;
}

public String getDataSourceName() {
Expand All @@ -36,8 +36,8 @@ public DataSource getDataSource() {
return dataSource;
}

public int getDataSourcePoolSize() {
return dataSourcePoolSize;
public int getDataSourceMergePoolSize() {
return dataSourceMergePoolSize;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DefaultDataSourceManager(EasyQueryOption easyQueryOption, DataSource defa
this.defaultDataSourceName = easyQueryOption.getDefaultDataSourceName();
this.defaultDataSource = defaultDataSource;
this.dataSourceUnitFactory = dataSourceUnitFactory;
this.dataSourceMap.putIfAbsent(defaultDataSourceName, dataSourceUnitFactory.createDataSourceUnit(defaultDataSourceName,defaultDataSource,easyQueryOption.getDefaultDataSourcePoolSize()));
this.dataSourceMap.putIfAbsent(defaultDataSourceName, dataSourceUnitFactory.createDataSourceUnit(defaultDataSourceName,defaultDataSource,easyQueryOption.getDefaultDataSourceMergePoolSize()));
initShardingConfig(easyQueryOption);
}

Expand All @@ -39,7 +39,7 @@ private void initShardingConfig(EasyQueryOption easyQueryOption) {
Set<ShardingDataSource> shardingDataSources = shardingOption.getShardingDataSources();
if (shardingDataSources != null) {
for (ShardingDataSource shardingDataSource : shardingDataSources) {
this.dataSourceMap.putIfAbsent(shardingDataSource.getDataSourceName(), dataSourceUnitFactory.createDataSourceUnit(shardingDataSource.getDataSourceName(),shardingDataSource.getDataSource(),shardingDataSource.getDataSourcePoolSize()));
this.dataSourceMap.putIfAbsent(shardingDataSource.getDataSourceName(), dataSourceUnitFactory.createDataSourceUnit(shardingDataSource.getDataSourceName(),shardingDataSource.getDataSource(),shardingDataSource.getDataSourceMergePoolSize()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class DefaultDataSourceUnit implements DataSourceUnit {
protected final Semaphore semaphore;
private final boolean warningBusy;

public DefaultDataSourceUnit(String dataSourceName, DataSource dataSource, int dataSourcePool, boolean warningBusy) {
public DefaultDataSourceUnit(String dataSourceName, DataSource dataSource, int mergePoolSize, boolean warningBusy) {

this.dataSourceName = dataSourceName;
this.dataSource = dataSource;
this.semaphore = dataSourcePool <= 0 ? null : new Semaphore(dataSourcePool, true);
this.semaphore = mergePoolSize <= 0 ? null : new Semaphore(mergePoolSize, true);
this.warningBusy = warningBusy;
}

Expand All @@ -50,51 +50,45 @@ public DataSource getDataSource() {

@Override
public List<Connection> getConnections(int count, long timeout, TimeUnit unit) throws SQLException {
if (semaphore == null) {
if (count > 1) {
throw new EasyQueryInvalidOperationException("sharding table should set dataSourceName:[" + dataSourceName + "] dataSourcePool,current value <= 0.");
}
if (count <= 1) {
Connection connection = getConnection();
return Collections.singletonList(connection);
}
return tryGetConnection(count, timeout, unit, () -> getConnections(count));

if (semaphore == null) {
throw new EasyQueryInvalidOperationException("sharding table should set dataSourceName:[" + dataSourceName + "] dataSourcePool,current value <= 0.");
}
return getMergeConnections(count, timeout, unit);
}

@Override
public Connection getConnection(long timeout, TimeUnit unit) throws SQLException {
if (semaphore == null) {
return getConnection();
}
return tryGetConnection(1, timeout, unit, this::getConnection);
return getConnection();
}

protected Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

protected <TR> TR tryGetConnection(int count, long timeout, TimeUnit unit, SQLSupplier<TR> supplier) throws SQLException {
protected List<Connection> getMergeConnections(int count, long timeout, TimeUnit unit) throws SQLException {

SemaphoreReleaseOnlyOnce semaphoreReleaseOnlyOnce = tryAcquire(count, timeout, unit);

if (semaphoreReleaseOnlyOnce == null) {
throw new EasyQuerySQLException("dataSourceName:" + dataSourceName + " get connections:" + 1 + " busy.");
}
try {
return supplier.get();
ArrayList<Connection> result = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Connection connection = getConnection();
result.add(connection);
}
return result;
} finally {
semaphoreReleaseOnlyOnce.release();
}
}

protected Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

protected List<Connection> getConnections(int count) throws SQLException {
ArrayList<Connection> result = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Connection connection = getConnection();
result.add(connection);
}
return result;
}

protected SemaphoreReleaseOnlyOnce tryAcquire(int count, long timeout, TimeUnit unit) {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void initReplicaConnector(EasyQueryReplicaOption replicaOption) {
ArrayList<ReplicaNode> replicaNodes = new ArrayList<>(entries.size());
for (Map.Entry<String, ShardingDataSource> replicaKv : entries) {
ShardingDataSource shardingDataSource = replicaKv.getValue();
replicaNodes.add(new ReplicaNode(replicaKv.getKey(), dataSourceUnitFactory.createDataSourceUnit(shardingDataSource.getDataSourceName(),shardingDataSource.getDataSource(),shardingDataSource.getDataSourcePoolSize())));
replicaNodes.add(new ReplicaNode(replicaKv.getKey(), dataSourceUnitFactory.createDataSourceUnit(shardingDataSource.getDataSourceName(),shardingDataSource.getDataSource(),shardingDataSource.getDataSourceMergePoolSize())));
}
ReplicaConnector replicaConnector = Objects.equals(ReplicaUseStrategyEnum.Loop, replicaOption.getReplicaUseStrategy()) ? new LoopReplicaConnector(dataSource, replicaNodes) : new RandomReplicaConnector(dataSource, replicaNodes);
replicaDataSource.put(dataSource, replicaConnector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private EasyDynamicUtil(){}
public static void addDataSource(QueryRuntimeContext runtimeContext, ShardingDataSource shardingDataSource){
String dataSourceName = shardingDataSource.getDataSourceName();
DataSource dataSource = shardingDataSource.getDataSource();
int dataSourcePoolSize = shardingDataSource.getDataSourcePoolSize();
int dataSourcePoolSize = shardingDataSource.getDataSourceMergePoolSize();
EasyQueryDataSource easyQueryDataSource = runtimeContext.getEasyQueryDataSource();
easyQueryDataSource.addDataSource(dataSourceName,dataSource,dataSourcePoolSize);
}
Expand Down
2 changes: 1 addition & 1 deletion sql-h2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.easy-query</groupId>
<artifactId>easy-query-all</artifactId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>

<artifactId>sql-h2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sql-mssql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.easy-query</groupId>
<artifactId>easy-query-all</artifactId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>

<artifactId>sql-mssql</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sql-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easy-query-all</artifactId>
<groupId>com.easy-query</groupId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion sql-pgsql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.easy-query</groupId>
<artifactId>easy-query-all</artifactId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>

<artifactId>sql-pgsql</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sql-springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easy-query-all</artifactId>
<groupId>com.easy-query</groupId>
<version>0.9.9</version>
<version>0.9.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.easy.query.sql.starter;

import com.easy.query.core.basic.jdbc.con.EasyConnectionFactory;
import com.easy.query.core.basic.jdbc.con.ConnectionManager;
import com.easy.query.core.basic.plugin.conversion.ValueConverter;
import com.easy.query.core.basic.plugin.encryption.EncryptionStrategy;
Expand Down Expand Up @@ -157,7 +156,7 @@ public EasyQuery easyQuery(DatabaseConfiguration databaseConfiguration, StarterC
builder.setMaxShardingRouteCount(easyQueryProperties.getMaxShardingRouteCount());
builder.setExecutorQueueSize(easyQueryProperties.getExecutorQueueSize());
builder.setDefaultDataSourceName(easyQueryProperties.getDefaultDataSourceName());
builder.setDefaultDataSourcePoolSize(easyQueryProperties.getDefaultDataSourcePoolSize());
builder.setDefaultDataSourceMergePoolSize(easyQueryProperties.getDefaultDataSourceMergePoolSize());
builder.setMultiConnWaitTimeoutMillis(easyQueryProperties.getMultiConnWaitTimeoutMillis());
builder.setWarningBusy(easyQueryProperties.isWarningBusy());
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* @author xuejiaming
*/
public class SpringDataSourceUnit extends DefaultDataSourceUnit {
public SpringDataSourceUnit(String dataSourceName, DataSource dataSource, int dataSourcePool, boolean warningBusy) {
super(dataSourceName,dataSource,dataSourcePool,warningBusy);
public SpringDataSourceUnit(String dataSourceName, DataSource dataSource, int mergePoolSize, boolean warningBusy) {
super(dataSourceName,dataSource,mergePoolSize,warningBusy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SpringDataSourceUnitFactory(EasyQueryOption easyQueryOption){
this.easyQueryOption = easyQueryOption;
}
@Override
public DataSourceUnit createDataSourceUnit(String dataSourceName, DataSource dataSource, int dataSourcePool) {
return new SpringDataSourceUnit(dataSourceName,dataSource,dataSourcePool,easyQueryOption.isWarningBusy());
public DataSourceUnit createDataSourceUnit(String dataSourceName, DataSource dataSource, int mergePoolSize) {
return new SpringDataSourceUnit(dataSourceName,dataSource,mergePoolSize,easyQueryOption.isWarningBusy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class EasyQueryProperties {
* 如果当前没有分表操作建议设置为0
* 当小于maxShardingQueryLimit后启动会抛出警告
*/
private int defaultDataSourcePoolSize = 0;
private int defaultDataSourceMergePoolSize = 0;
/**
* 默认5秒分表聚合多链接获取分表插入更新删除同理多个线程间等待获取时间单位毫秒(ms)
*/
Expand Down Expand Up @@ -219,12 +219,12 @@ public void setDefaultDataSourceName(String defaultDataSourceName) {
this.defaultDataSourceName = defaultDataSourceName;
}

public int getDefaultDataSourcePoolSize() {
return defaultDataSourcePoolSize;
public int getDefaultDataSourceMergePoolSize() {
return defaultDataSourceMergePoolSize;
}

public void setDefaultDataSourcePoolSize(int defaultDataSourcePoolSize) {
this.defaultDataSourcePoolSize = defaultDataSourcePoolSize;
public void setDefaultDataSourceMergePoolSize(int defaultDataSourceMergePoolSize) {
this.defaultDataSourceMergePoolSize = defaultDataSourceMergePoolSize;
}

public long getMultiConnWaitTimeoutMillis() {
Expand Down
Loading

0 comments on commit b582708

Please sign in to comment.