Skip to content

Commit

Permalink
Merge pull request #145 from wangweicugw/warm_up
Browse files Browse the repository at this point in the history
warm up
  • Loading branch information
wlx5575 authored Nov 24, 2023
2 parents 41b3601 + 3e4c872 commit a4363c5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
<commons-lang3.version>3.11</commons-lang3.version>
<emoji-java.version>5.1.1</emoji-java.version>
<dbcp2.version>2.8.0</dbcp2.version>
<dbcp.version>1.4</dbcp.version>
<druid.version>1.1.16</druid.version>
<c3p0.version>0.9.5.2</c3p0.version>
<tomcat.jdbc.version>9.0.38</tomcat.jdbc.version>
<tomcat-dbcp.version>10.0.8</tomcat-dbcp.version>
<spring-context.version>3.2.9.RELEASE</spring-context.version>
<dynamic-datasource.version>3.4.1</dynamic-datasource.version>
<cglib.version>3.2.10</cglib.version>
<grpc.testing.version>1.45.1</grpc.testing.version>
<grpc.mockserver.version>1.97.1</grpc.mockserver.version>
Expand Down Expand Up @@ -203,12 +205,24 @@
<version>${tomcat-dbcp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${dbcp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-context.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${dynamic-datasource.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.jd.jdbc.spring;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.ds.ItemDataSource;
import com.jd.jdbc.sqlparser.support.logging.Log;
import com.jd.jdbc.sqlparser.support.logging.LogFactory;
import com.jd.jdbc.vitess.VitessJdbcUrlParser;
Expand All @@ -25,8 +27,9 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -54,12 +57,35 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
private void initVitessDataSource(Map<String, DataSource> dataSources) {
for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
DataSource dataSource = entry.getValue();
if (isVitessDataSource(dataSource)) {
LOG.info("VtDriver datasource start init: " + entry.getKey());
try (Connection connection = dataSource.getConnection()) {
warmUpConnectionPool(entry.getKey(), dataSource);
warmUpDynamicRoutingDataSource(dataSource);
warmAopProxyDataSource(entry.getKey(), dataSource);
}
}

private void warmUpConnectionPool(String dataSourceName, DataSource dataSource) {
if (isVitessDataSource(dataSource)) {
LOG.info("VtDriver datasource start init: " + dataSourceName);
try (Connection connection = dataSource.getConnection()) {
} catch (SQLException e) {
LOG.info("VtDriver datasource init error: " + e.getMessage());
}
}
}

} catch (SQLException e) {
LOG.info("VtDriver datasource init error: " + e.getMessage());
private void warmUpDynamicRoutingDataSource(DataSource dataSource) {
String className = dataSource.getClass().getName();
if (Objects.equals("com.baomidou.dynamic.datasource.DynamicRoutingDataSource", className)) {
com.baomidou.dynamic.datasource.DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
Map<String, DataSource> currentDataSources = dynamicRoutingDataSource.getCurrentDataSources();
for (Map.Entry<String, DataSource> dataSourceEntry : currentDataSources.entrySet()) {
DataSource entryDataSource = dataSourceEntry.getValue();
String entryDataSourceClassName = entryDataSource.getClass().getName();
if (Objects.equals("com.baomidou.dynamic.datasource.ds.ItemDataSource", entryDataSourceClassName)) {
com.baomidou.dynamic.datasource.ds.ItemDataSource itemDataSource = (ItemDataSource) entryDataSource;
warmUpConnectionPool(itemDataSource.getName(), itemDataSource.getRealDataSource());
} else {
warmUpConnectionPool(dataSourceEntry.getKey(), entryDataSource);
}
}
}
Expand All @@ -69,14 +95,18 @@ private boolean isVitessDataSource(DataSource dataSource) {
String className = dataSource.getClass().getName();
switch (className) {
case "com.alibaba.druid.pool.DruidDataSource":
case "com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper":
DruidDataSource druidDataSource = (DruidDataSource) dataSource;
return VitessJdbcUrlParser.acceptsUrl(druidDataSource.getUrl());
case "com.zaxxer.hikari.HikariDataSource":
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
return VitessJdbcUrlParser.acceptsUrl(hikariDataSource.getJdbcUrl());
case "org.apache.commons.dbcp2.BasicDataSource":
BasicDataSource basicDataSource = (BasicDataSource) dataSource;
case "org.apache.commons.dbcp.BasicDataSource":
org.apache.commons.dbcp.BasicDataSource basicDataSource = (org.apache.commons.dbcp.BasicDataSource) dataSource;
return VitessJdbcUrlParser.acceptsUrl(basicDataSource.getUrl());
case "org.apache.commons.dbcp2.BasicDataSource":
org.apache.commons.dbcp2.BasicDataSource basicDataSource2 = (org.apache.commons.dbcp2.BasicDataSource) dataSource;
return VitessJdbcUrlParser.acceptsUrl(basicDataSource2.getUrl());
case "com.mchange.v2.c3p0.ComboPooledDataSource":
ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource) dataSource;
return VitessJdbcUrlParser.acceptsUrl(comboPooledDataSource.getJdbcUrl());
Expand All @@ -90,4 +120,45 @@ private boolean isVitessDataSource(DataSource dataSource) {
return false;
}
}

private void warmAopProxyDataSource(String dataSourceName, DataSource dataSource) {
if (!AopUtils.isAopProxy(dataSource)) {
return;
}
Class<?> targetClass = AopUtils.getTargetClass(dataSource);
String className = targetClass.getName();
Class clazz;
switch (className) {
case "com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper":
case "com.alibaba.druid.pool.DruidDataSource":
clazz = com.alibaba.druid.pool.DruidDataSource.class;
break;
case "com.zaxxer.hikari.HikariDataSource":
clazz = com.zaxxer.hikari.HikariDataSource.class;
break;
case "org.apache.commons.dbcp.BasicDataSource":
clazz = org.apache.commons.dbcp.BasicDataSource.class;
break;
case "org.apache.commons.dbcp2.BasicDataSource":
clazz = org.apache.commons.dbcp2.BasicDataSource.class;
break;
case "com.mchange.v2.c3p0.ComboPooledDataSource":
clazz = com.mchange.v2.c3p0.ComboPooledDataSource.class;
break;
case "org.apache.tomcat.dbcp.dbcp2.BasicDataSource":
clazz = org.apache.tomcat.dbcp.dbcp2.BasicDataSource.class;
break;
case "org.apache.tomcat.jdbc.pool.DataSource":
clazz = org.apache.tomcat.jdbc.pool.DataSource.class;
break;
default:
return;
}
try {
DataSource targetDataSource = (DataSource) dataSource.unwrap(clazz);
warmUpConnectionPool(dataSourceName, targetDataSource);
} catch (Exception e) {
LOG.info("init AopProxy datasource error: " + e.getMessage());
}
}
}

0 comments on commit a4363c5

Please sign in to comment.