Skip to content

Commit

Permalink
Use HikariJDBCParameterDecorator on JDBCRawBackendDataSourceFactory (a…
Browse files Browse the repository at this point in the history
…pache#7265)

* Move HikariJDBCParameterDecorator

* Refactor JDBCRawBackendDataSourceFactory

* For code style

* add new package for proxy's datasource
  • Loading branch information
terrymanu authored Sep 5, 2020
1 parent 374222a commit 2d0e0a5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public DataSource createDataSource() {
@SuppressWarnings("rawtypes")
private Optional<JDBCParameterDecorator> findJDBCParameterDecorator(final DataSource dataSource) {
return ShardingSphereServiceLoader.newServiceInstances(JDBCParameterDecorator.class).stream().filter(each -> each.getType() == dataSource.getClass()).findFirst();

}

private Optional<Method> findSetterMethod(final Method[] methods, final String property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.datasource;
package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.decorator;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecorator;
Expand All @@ -28,7 +28,7 @@ public final class HikariJDBCParameterDecorator implements JDBCParameterDecorato
@Override
public void decorate(final HikariDataSource dataSource) {
dataSource.getDataSourceProperties().setProperty("useServerPrepStmts", Boolean.TRUE.toString());
dataSource.getDataSourceProperties().setProperty("cachePrepStmts", "true");
dataSource.getDataSourceProperties().setProperty("cachePrepStmts", Boolean.TRUE.toString());
dataSource.getDataSourceProperties().setProperty("prepStmtCacheSize", "250");
dataSource.getDataSourceProperties().setProperty("prepStmtCacheSqlLimit", "2048");
dataSource.getDataSourceProperties().setProperty("useLocalSessionState", Boolean.TRUE.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource;
package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory;

import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource;
package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecorator;
import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;

import javax.sql.DataSource;
import java.util.Optional;

/**
* Backend data source factory using {@code HikariDataSource} for JDBC raw.
Expand All @@ -35,6 +38,10 @@ public final class JDBCRawBackendDataSourceFactory implements JDBCBackendDataSou

private static final JDBCRawBackendDataSourceFactory INSTANCE = new JDBCRawBackendDataSourceFactory();

static {
ShardingSphereServiceLoader.register(JDBCParameterDecorator.class);
}

/**
* Get instance of {@code JDBCBackendDataSourceFactory}.
*
Expand All @@ -44,6 +51,7 @@ public static JDBCBackendDataSourceFactory getInstance() {
return INSTANCE;
}

@SuppressWarnings("unchecked")
@Override
public DataSource build(final String dataSourceName, final DataSourceParameter dataSourceParameter) {
HikariConfig config = new HikariConfig();
Expand All @@ -59,19 +67,9 @@ public DataSource build(final String dataSourceName, final DataSourceParameter d
config.setMaximumPoolSize(dataSourceParameter.getMaxPoolSize());
config.setMinimumIdle(dataSourceParameter.getMinPoolSize());
config.setReadOnly(dataSourceParameter.isReadOnly());
config.addDataSourceProperty("useServerPrepStmts", Boolean.TRUE.toString());
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", 250);
config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
config.addDataSourceProperty("useLocalSessionState", Boolean.TRUE.toString());
config.addDataSourceProperty("rewriteBatchedStatements", Boolean.TRUE.toString());
config.addDataSourceProperty("cacheResultSetMetadata", Boolean.FALSE.toString());
config.addDataSourceProperty("cacheServerConfiguration", Boolean.TRUE.toString());
config.addDataSourceProperty("elideSetAutoCommits", Boolean.TRUE.toString());
config.addDataSourceProperty("maintainTimeStats", Boolean.FALSE.toString());
config.addDataSourceProperty("netTimeoutForStreamingResults", 0);
config.addDataSourceProperty("tinyInt1isBit", Boolean.FALSE.toString());
return new HikariDataSource(config);
DataSource result = new HikariDataSource(config);
findJDBCParameterDecorator(result).ifPresent(decorator -> decorator.decorate(result));
return result;
}

private void validateDriverClassName(final String driverClassName) {
Expand All @@ -81,4 +79,9 @@ private void validateDriverClassName(final String driverClassName) {
throw new ShardingSphereException("Cannot load JDBC driver class `%s`, make sure it in ShardingSphere-Proxy's classpath.", driverClassName);
}
}

@SuppressWarnings("rawtypes")
private Optional<JDBCParameterDecorator> findJDBCParameterDecorator(final DataSource dataSource) {
return ShardingSphereServiceLoader.newServiceInstances(JDBCParameterDecorator.class).stream().filter(each -> each.getType() == dataSource.getClass()).findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.JDBCRawBackendDataSourceFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;

import javax.sql.DataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

org.apache.shardingsphere.proxy.backend.datasource.HikariJDBCParameterDecorator
org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.decorator.HikariJDBCParameterDecorator
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.JDBCRawBackendDataSourceFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
import org.junit.Test;

import java.lang.reflect.Field;
Expand All @@ -40,15 +40,15 @@
import static org.mockito.Mockito.reset;

public final class ProxyDataSourceContextTest {

@Test
public void assertEmptySchemaDataSources() {
Map<String, Map<String, DataSourceParameter>> schemaDataSources = new HashMap<>();
ProxyDataSourceContext proxyDataSourceContext = new ProxyDataSourceContext(schemaDataSources);
assertThat(proxyDataSourceContext.getDatabaseType(), instanceOf(MySQLDatabaseType.class));
assertTrue(proxyDataSourceContext.getDataSourcesMap().isEmpty());
}

@Test(expected = ShardingSphereException.class)
public void assertWrongSchemaDataSources() {
DataSourceParameter dataSourceParameter = new DataSourceParameter();
Expand All @@ -59,15 +59,15 @@ public void assertWrongSchemaDataSources() {
schemaDataSources.put("order", dataSourceParameterMap);
new ProxyDataSourceContext(schemaDataSources);
}

@Test(expected = ShardingSphereException.class)
public void assertThrowByBuild() throws Exception {
JDBCRawBackendDataSourceFactory jdbcRawBackendDataSourceFactory = mock(JDBCRawBackendDataSourceFactory.class);
when(jdbcRawBackendDataSourceFactory.build(anyString(), any())).thenThrow(new ShardingSphereException(""));
build(jdbcRawBackendDataSourceFactory);
reset(jdbcRawBackendDataSourceFactory);
}

@Test
public void assertRightMysqlSchemaDataSources() throws Exception {
JDBCRawBackendDataSourceFactory jdbcRawBackendDataSourceFactory = mock(JDBCRawBackendDataSourceFactory.class);
Expand All @@ -77,7 +77,7 @@ public void assertRightMysqlSchemaDataSources() throws Exception {
assertTrue(proxyDataSourceContext.getDataSourcesMap().size() == 1);
reset(jdbcRawBackendDataSourceFactory);
}

private ProxyDataSourceContext build(final JDBCRawBackendDataSourceFactory jdbcRawBackendDataSourceFactory) throws Exception {
JDBCRawBackendDataSourceFactory jdbcBackendDataSourceFactory = (JDBCRawBackendDataSourceFactory) JDBCRawBackendDataSourceFactory.getInstance();
Class<?> jdbcBackendDataSourceFactoryClass = jdbcBackendDataSourceFactory.getClass();
Expand Down

0 comments on commit 2d0e0a5

Please sign in to comment.