摘自【工匠小豬豬的技術世界】
這是一個系列,有興趣的朋友可以持續關註
如果你有HikariCP使用上的問題,可以給我留言,我們一起溝通討論
希望大家可以提供我一些案例,我也希望可以支援你們做一些調優
概述
很多人都會問HikariCP為什麼那麼快?之前的兩篇文章【追光者系列】HikariCP原始碼分析之FastList 和 【追光者系列】HikariCP原始碼分析之ConcurrentBag 是第一第二彈,本文就是第三彈。
在Down-the-Rabbit-Hole中,作者提到了We’re in your bytecodez的位元組碼最佳化
In order to make HikariCP as fast as it is, we went down to bytecode-level engineering, and beyond. We pulled out every trick we know to help the JIT help you. We studied the bytecode output of the compiler, and even the assembly output of the JIT to limit key routines to less than the JIT inline-threshold. We flattened inheritance hierarchies, shadowed member variables, eliminated casts.
位元組碼最佳化這塊作者還提到了 this change removed a static field access, a push and pop from the stack, and made the invocation easier for the JIT to optimize because the callsite is guaranteed not to change.感興趣的可以看一下 https://github.com/brettwooldridge/HikariCP/wiki/Down-the-Rabbit-Hole。
作者提升效能的秘方是:大量的效能收益來自於代理的最佳化,如包裝Connection,Statement等。那麼本文就來帶讀者揭開這神秘的面紗。
原始碼綱要
hikariCP主要對java.sql.*提供了五個代理類
-
ProxyConnection(proxy class for java.sql.Connection)
-
ProxyStatement(proxy class for java.sql.Statement)
-
ProxyPreparedStatement(proxy class for java.sql.PreparedStatement)
-
ProxyCallableStatement(proxy class for java.sql.CallableStatement)
-
ProxyResultSet(proxy class for java.sql.ResultSet)
緊密結合以上五個代理類的還有兩個類ProxyFactory(A factory class that produces proxies around instances of the standard JDBC interfaces)和JavassistProxyFactory(This class generates the proxy objects for {@link Connection}, {@link Statement},{@link PreparedStatement}, and {@link CallableStatement}.Additionally it injects method bodies into the {@link ProxyFactory} class methods that can instantiate instances of the generated proxies.)。
我們看一下ProxyFactory這個工廠類,大家是不是可以看到對上面的五個代理類提供的方法只有一行直接拋異常IllegalStateException的程式碼,並且提示你You need to run the CLI build and you need target/classes in your classpath to run。
註釋寫著“Body is replaced (injected) by JavassistProxyFactory”,其實方法body中的程式碼是在編譯時呼叫JavassistProxyFactory才生成的。
package com.zaxxer.hikari.pool;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.zaxxer.hikari.util.FastList;
/**
* A factory class that produces proxies around instances of the standard
* JDBC interfaces.
*
* @author Brett Wooldridge
*/
@SuppressWarnings("unused")
public final class ProxyFactory {
private ProxyFactory() {
// unconstructable
}
/**
* Create a proxy for the specified {@link Connection} instance.
* @param poolEntry the PoolEntry holding pool state
* @param connection the raw database Connection
* @param openStatements a reusable list to track open Statement instances
* @param leakTask the ProxyLeakTask for this connection
* @param now the current timestamp
* @param isReadOnly the default readOnly state of the connection
* @param isAutoCommit the default autoCommit state of the connection
* @return a proxy that wraps the specified {@link Connection}
*/
static ProxyConnection getProxyConnection(final PoolEntry poolEntry, final Connection connection, final FastList<Statement> openStatements, final ProxyLeakTask leakTask, final long now, final boolean isReadOnly, final boolean isAutoCommit) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
static Statement getProxyStatement(final ProxyConnection connection, final Statement statement) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
static CallableStatement getProxyCallableStatement(final ProxyConnection connection, final CallableStatement statement) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
static PreparedStatement getProxyPreparedStatement(final ProxyConnection connection, final PreparedStatement statement) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
static ResultSet getProxyResultSet(final ProxyConnection connection, final ProxyStatement statement, final ResultSet resultSet) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
}
JavassistProxyFactory
JavassistProxyFactory存在於工具包裡com.zaxxer.hikari.util裡,之所以使用Javassist生成動態代理,是因為其速度更快,相比於JDK Proxy生成的位元組碼更少,精簡了很多不必要的位元組碼。
javassist
javassist是一個位元組碼類庫,可以用他來動態生成類,動態修改類等等,還有一個比較常見的用途是AOP,比如對一些類統一加許可權過濾,加日誌監控等等。
Javassist 不僅是一個處理位元組碼的庫,還有一項優點:可以用 Javassist 改變 Java 類的位元組碼,而無需真正瞭解關於位元組碼或者 Java 虛擬機器(Java virtual machine JVM)結構的任何內容。比起在單條指令水平上工作的框架,它確實使位元組碼操作更可具有可行性了。
Javassist 使您可以檢查、編輯以及建立 Java 二進位制類。檢查方面基本上與透過 Reflection API 直接在 Java 中進行的一樣,但是當想要修改類而不只是執行它們時,則另一種訪問這些資訊的方法就很有用了。這是因為 JVM 設計上並沒有提供在類裝載到 JVM 中後訪問原始類資料的任何方法,這項工作需要在 JVM 之外完成。
Javassist 使用 javassist.ClassPool 類跟蹤和控制所操作的類。這個類的工作方式與 JVM 類裝載器非常相似,但是有一個重要的區別是它不是將裝載的、要執行的類作為應用程式的一部分連結,類池使所裝載的類可以透過 Javassist API 作為資料使用。可以使用預設的類池,它是從 JVM 搜尋路徑中裝載的,也可以定義一個搜尋您自己的路徑串列的類池。甚至可以直接從位元組陣列或者流中裝載二進位制類,以及從頭開始建立新類。
裝載到類池中的類由 javassist.CtClass 實體表示。與標準的 Java java.lang.Class 類一樣, CtClass 提供了檢查類資料(如欄位和方法)的方法。不過,這隻是 CtClass 的部分內容,它還定義了在類中新增新欄位、方法和建構式、以及改變類、父類和介面的方法。奇怪的是,Javassist 沒有提供刪除一個類中欄位、方法或者建構式的任何方法。
欄位、方法和建構式分別由 javassist.CtField、javassist.CtMethod 和 javassist.CtConstructor 的實體表示。這些類定義了修改由它們所表示的物件的所有方法的方法,包括方法或者建構式中的實際位元組碼內容。
這篇來自阿裡的文章做了一個動態代理的效能對比(http://javatar.iteye.com/blog/814426),得出的結論如下:
-
ASM和JAVAASSIST位元組碼生成方式不相上下,都很快,是CGLIB的5倍。
-
CGLIB次之,是JDK自帶的兩倍。
-
JDK自帶的再次之,因JDK1.6對動態代理做了最佳化,如果用低版本JDK更慢,要註意的是JDK也是透過位元組碼生成來實現動態代理的,而不是反射。
-
JAVAASSIST提供者動態代理介面最慢,比JDK自帶的還慢。 (這也是為什麼網上有人說JAVAASSIST比JDK還慢的原因,用JAVAASSIST最好別用它提供的動態代理介面,而可以考慮用它的位元組碼生成方式)
差異的原因是各方案生成的位元組碼不一樣,像JDK和CGLIB都考慮了很多因素,以及繼承或包裝了自己的一些類,所以生成的位元組碼非常大,而我們很多時候用不上這些,而手工生成的位元組碼非常小,所以速度快。
最終該阿裡團隊決定使用JAVAASSIST的位元組碼生成代理方式,雖然ASM稍快,但並沒有快一個數量級,而JAVAASSIST的位元組碼生成方式比ASM方便,JAVAASSIST只需用字串拼接出Java原始碼,便可生成相應位元組碼,而ASM需要手工寫位元組碼。
該測試可能還是有些問題的。其實效能的根本原因還是在於反射。JdkHandler中的 return method.invoke(delegate, objects); 是影響效能的關鍵。如果JAVAASSIST Bytecode Proxy生成的代理類,也是透過JdkHanlder去實現的話,效能就和JDK自身的動態代理沒什麼區別了。 javassit採用的是直接呼叫,而cglib走了methodProxy.invoke(),說白了還是反射呼叫。如果實施cglib的直接呼叫,比如使用的Dispatcher或則LazyLoader。最後的生成的位元組就是一個直接呼叫,效能上就可以和javassist持平。
但是綜上所述,javassist相比於JDK Proxy生成的位元組碼更少,精簡了很多不必要的位元組碼。透過最佳化並精簡位元組碼,提升了hikariCP的效能。
原始碼解析
我們看一下JavassistProxyFactory的原始碼
/**
* This class generates the proxy objects for {@link Connection}, {@link Statement},
* {@link PreparedStatement}, and {@link CallableStatement}. Additionally it injects
* method bodies into the {@link ProxyFactory} class methods that can instantiate
* instances of the generated proxies.
*
* @author Brett Wooldridge
*/
public final class JavassistProxyFactory {
private static ClassPool classPool;
private static String genDirectory = "";
public static void main(String... args) {
classPool = new ClassPool();
classPool.importPackage("java.sql");
classPool.appendClassPath(new LoaderClassPath(JavassistProxyFactory.class.getClassLoader()));
if (args.length > 0) {
genDirectory = args[0];
}
try {
// Cast is not needed for these
String methodBody = "{ try { return delegate.method($); } catch (SQLException e) { throw checkException(e); } }";
generateProxyClass(Connection.class, ProxyConnection.class.getName(), methodBody);
generateProxyClass(Statement.class, ProxyStatement.class.getName(), methodBody);
generateProxyClass(ResultSet.class, ProxyResultSet.class.getName(), methodBody);
// For these we have to cast the delegate
methodBody = "{ try { return ((cast) delegate).method($); } catch (SQLException e) { throw checkException(e); } }";
generateProxyClass(PreparedStatement.class, ProxyPreparedStatement.class.getName(), methodBody);
generateProxyClass(CallableStatement.class, ProxyCallableStatement.class.getName(), methodBody);
modifyProxyFactory();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void modifyProxyFactory() throws Exception {
System.out.println("Generating method bodies for com.zaxxer.hikari.proxy.ProxyFactory");
String packageName = ProxyConnection.class.getPackage().getName();
CtClass proxyCt = classPool.getCtClass("com.zaxxer.hikari.pool.ProxyFactory");
for (CtMethod method : proxyCt.getMethods()) {
switch (method.getName()) {
case "getProxyConnection":
method.setBody("{return new " + packageName + ".HikariProxyConnection($);}");
break;
case "getProxyStatement":
method.setBody("{return new " + packageName + ".HikariProxyStatement($);}");
break;
case "getProxyPreparedStatement":
method.setBody("{return new " + packageName + ".HikariProxyPreparedStatement($);}");
break;
case "getProxyCallableStatement":
method.setBody("{return new " + packageName + ".HikariProxyCallableStatement($);}");
break;
case "getProxyResultSet":
method.setBody("{return new " + packageName + ".HikariProxyResultSet($);}");
break;
default:
// unhandled method
break;
}
}
proxyCt.writeFile(genDirectory + "target/classes");
}
/**
* Generate Javassist Proxy Classes
*/
private static <T> void generateProxyClass(Class<T> primaryInterface, String superClassName, String methodBody) throws Exception {
String newClassName = superClassName.replaceAll("(.+)\\.(\\w+)", "$1.Hikari$2");
CtClass superCt = classPool.getCtClass(superClassName);
CtClass targetCt = classPool.makeClass(newClassName, superCt);
targetCt.setModifiers(Modifier.FINAL);
System.out.println("Generating " + newClassName);
targetCt.setModifiers(Modifier.PUBLIC);
// Make a set of method signatures we inherit implementation for, so we don't generate delegates for these
Set<String> superSigs = new HashSet<>();
for (CtMethod method : superCt.getMethods()) {
if ((method.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
superSigs.add(method.getName() + method.getSignature());
}
}
Set<String> methods = new HashSet<>();
Set<Class>> interfaces = getAllInterfaces(primaryInterface);
for (Class> intf : interfaces) {
CtClass intfCt = classPool.getCtClass(intf.getName());
targetCt.addInterface(intfCt);
for (CtMethod intfMethod : intfCt.getDeclaredMethods()) {
final String signature = intfMethod.getName() + intfMethod.getSignature();
// don't generate delegates for methods we override
if (superSigs.contains(signature)) {
continue;
}
// Ignore already added methods that come from other interfaces
if (methods.contains(signature)) {
continue;
}
// Track what methods we've added
methods.add(signature);
// Clone the method we want to inject into
CtMethod method = CtNewMethod.copy(intfMethod, targetCt, null);
String modifiedBody = methodBody;
// If the super-Proxy has concrete methods (non-abstract), transform the call into a simple super.method() call
CtMethod superMethod = superCt.getMethod(intfMethod.getName(), intfMethod.getSignature());
if ((superMethod.getModifiers() & Modifier.ABSTRACT) != Modifier.ABSTRACT && !isDefaultMethod(intf, intfCt, intfMethod)) {
modifiedBody = modifiedBody.replace("((cast) ", "");
modifiedBody = modifiedBody.replace("delegate", "super");
modifiedBody = modifiedBody.replace("super)", "super");
}
modifiedBody = modifiedBody.replace("cast", primaryInterface.getName());
// Generate a method that simply invokes the same method on the delegate
if (isThrowsSqlException(intfMethod)) {
modifiedBody = modifiedBody.replace("method", method.getName());
}
else {
modifiedBody = "{ return ((cast) delegate).method($); }".replace("method", method.getName()).replace("cast", primaryInterface.getName());
}
if (method.getReturnType() == CtClass.voidType) {
modifiedBody = modifiedBody.replace("return", "");
}
method.setBody(modifiedBody);
targetCt.addMethod(method);
}
}
targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_8);
targetCt.writeFile(genDirectory + "target/classes");
}
private static boolean isThrowsSqlException(CtMethod method) {
try {
for (CtClass clazz : method.getExceptionTypes()) {
if (clazz.getSimpleName().equals("SQLException")) {
return true;
}
}
}
catch (NotFoundException e) {
// fall thru
}
return false;
}
private static boolean isDefaultMethod(Class> intf, CtClass intfCt, CtMethod intfMethod) throws Exception {
List<Class>> paramTypes = new ArrayList<>();
for (CtClass pt : intfMethod.getParameterTypes()) {
paramTypes.add(toJavaClass(pt));
}
return intf.getDeclaredMethod(intfMethod.getName(), paramTypes.toArray(new Class[paramTypes.size()])).toString().contains("default ");
}
private static Set<Class>> getAllInterfaces(Class> clazz)
{
Set<Class>> interfaces = new HashSet<>();
for (Class> intf : Arrays.asList(clazz.getInterfaces())) {
if (intf.getInterfaces().length > 0) {
interfaces.addAll(getAllInterfaces(intf));
}
interfaces.add(intf);
}
if (clazz.getSuperclass() != null) {
interfaces.addAll(getAllInterfaces(clazz.getSuperclass()));
}
if (clazz.isInterface()) {
interfaces.add(clazz);
}
return interfaces;
}
private static Class> toJavaClass(CtClass cls) throws Exception
{
if (cls.getName().endsWith("[]")) {
return Array.newInstance(toJavaClass(cls.getName().replace("[]", "")), 0).getClass();
}
else {
return toJavaClass(cls.getName());
}
}
private static Class> toJavaClass(String cn) throws Exception
{
switch (cn) {
case "int":
return int.class;
case "long":
return long.class;
case "short":
return short.class;
case "byte":
return byte.class;
case "float":
return float.class;
case "double":
return double.class;
case "boolean":
return boolean.class;
case "char":
return char.class;
case "void":
return void.class;
default:
return Class.forName(cn);
}
}
}
generateProxyClass負責生成實際使用的代理類位元組碼,modifyProxyFactory對應修改工廠類中的代理類獲取方法。
private static void modifyProxyFactory() throws Exception {
System.out.println("Generating method bodies for com.zaxxer.hikari.proxy.ProxyFactory");
String packageName = ProxyConnection.class.getPackage().getName();
CtClass proxyCt = classPool.getCtClass("com.zaxxer.hikari.pool.ProxyFactory");
for (CtMethod method : proxyCt.getMethods()) {
switch (method.getName()) {
case "getProxyConnection":
method.setBody("{return new " + packageName + ".HikariProxyConnection($);}");
break;
case "getProxyStatement":
method.setBody("{return new " + packageName + ".HikariProxyStatement($);}");
break;
case "getProxyPreparedStatement":
method.setBody("{return new " + packageName + ".HikariProxyPreparedStatement($);}");
break;
case "getProxyCallableStatement":
method.setBody("{return new " + packageName + ".HikariProxyCallableStatement($);}");
break;
case "getProxyResultSet":
method.setBody("{return new " + packageName + ".HikariProxyResultSet($);}");
break;
default:
// unhandled method
break;
}
}
proxyCt.writeFile(genDirectory + "target/classes");
}
generateProxyClass核心程式碼如下:
/**
* Generate Javassist Proxy Classes
*/
private static <T> void generateProxyClass(Class<T> primaryInterface, String superClassName, String methodBody) throws Exception
{
String newClassName = superClassName.replaceAll("(.+)\\.(\\w+)", "$1.Hikari$2");
CtClass superCt = classPool.getCtClass(superClassName);
CtClass targetCt = classPool.makeClass(newClassName, superCt);
targetCt.setModifiers(Modifier.FINAL);
System.out.println("Generating " + newClassName);
targetCt.setModifiers(Modifier.PUBLIC);
// Make a set of method signatures we inherit implementation for, so we don't generate delegates for these
Set<String> superSigs = new HashSet<>();
for (CtMethod method : superCt.getMethods()) {
if ((method.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
superSigs.add(method.getName() + method.getSignature());
}
}
Set<String> methods = new HashSet<>();
Set<Class>> interfaces = getAllInterfaces(primaryInterface);
for (Class> intf : interfaces) {
CtClass intfCt = classPool.getCtClass(intf.getName());
targetCt.addInterface(intfCt);
for (CtMethod intfMethod : intfCt.getDeclaredMethods()) {
final String signature = intfMethod.getName() + intfMethod.getSignature();
// don't generate delegates for methods we override
if (superSigs.contains(signature)) {
continue;
}
// Ignore already added methods that come from other interfaces
if (methods.contains(signature)) {
continue;
}
// Track what methods we've added
methods.add(signature);
// Clone the method we want to inject into
CtMethod method = CtNewMethod.copy(intfMethod, targetCt, null);
String modifiedBody = methodBody;
// If the super-Proxy has concrete methods (non-abstract), transform the call into a simple super.method() call
CtMethod superMethod = superCt.getMethod(intfMethod.getName(), intfMethod.getSignature());
if ((superMethod.getModifiers() & Modifier.ABSTRACT) != Modifier.ABSTRACT && !isDefaultMethod(intf, intfCt, intfMethod)) {
modifiedBody = modifiedBody.replace("((cast) ", "");
modifiedBody = modifiedBody.replace("delegate", "super");
modifiedBody = modifiedBody.replace("super)", "super");
}
modifiedBody = modifiedBody.replace("cast", primaryInterface.getName());
// Generate a method that simply invokes the same method on the delegate
if (isThrowsSqlException(intfMethod)) {
modifiedBody = modifiedBody.replace("method", method.getName());
}
else {
modifiedBody = "{ return ((cast) delegate).method($); }".replace("method", method.getName()).replace("cast", primaryInterface.getName());
}
if (method.getReturnType() == CtClass.voidType) {
modifiedBody = modifiedBody.replace("return", "");
}
method.setBody(modifiedBody);
targetCt.addMethod(method);
}
}
targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_8);
targetCt.writeFile(genDirectory + "target/classes");
}
以 generateProxyClass(ResultSet.class, ProxyResultSet.class.getName(), methodBody); 來看
我看的是3.1.1-SNAPSHOT版本的原始碼,這段程式碼可以看到採用java8並放到了target/classes目錄下。
透過繼承ProxyResultSet來生成HikariProxyResultSet,methodBody中的method替換成對應方法的方法名。
這裡展示一下生成的HikariProxyResultSet的部分程式碼:
public class HikariProxyResultSet extends ProxyResultSet implements ResultSet, AutoCloseable, Wrapper {
public boolean next() throws SQLException {
try {
return super.delegate.next();
} catch (SQLException var2) {
throw this.checkException(var2);
}
}
public void close() throws SQLException {
try {
super.delegate.close();
} catch (SQLException var2) {
throw this.checkException(var2);
}
}
public boolean wasNull() throws SQLException {
try {
return super.delegate.wasNull();
} catch (SQLException var2) {
throw this.checkException(var2);
}
}
public String getString(int var1) throws SQLException {
try {
return super.delegate.getString(var1);
} catch (SQLException var3) {
throw this.checkException(var3);
}
}
ProxyResultSet
該代理類主要為updateRow、insertRow、deleteRow增加了執行記錄connection.markCommitStateDirty()
原始碼如下:
/**
* This is the proxy class for java.sql.ResultSet.
*
* @author Brett Wooldridge
*/
public abstract class ProxyResultSet implements ResultSet {
protected final ProxyConnection connection;
protected final ProxyStatement statement;
final ResultSet delegate;
protected ProxyResultSet(ProxyConnection connection, ProxyStatement statement, ResultSet resultSet) {
this.connection = connection;
this.statement = statement;
this.delegate = resultSet;
}
@SuppressWarnings("unused")
final SQLException checkException(SQLException e) {
return connection.checkException(e);
}
/** {@inheritDoc} */
@Override
public String toString() {
return this.getClass().getSimpleName() + '@' + System.identityHashCode(this) + " wrapping " + delegate;
}
// **********************************************************************
// Overridden java.sql.ResultSet Methods
// **********************************************************************
/** {@inheritDoc} */
@Override
public final Statement getStatement() throws SQLException {
return statement;
}
/** {@inheritDoc} */
@Override
public void updateRow() throws SQLException {
connection.markCommitStateDirty();
delegate.updateRow();
}
/** {@inheritDoc} */
@Override
public void insertRow() throws SQLException {
connection.markCommitStateDirty();
delegate.insertRow();
}
/** {@inheritDoc} */
@Override
public void deleteRow() throws SQLException {
connection.markCommitStateDirty();
delegate.deleteRow();
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("unchecked")
public final <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(delegate)) {
return (T) delegate;
}
else if (delegate != null) {
return delegate.unwrap(iface);
}
throw new SQLException("Wrapped ResultSet is not an instance of " + iface);
}
}
ProxyStatement
該類主要implements了java.sql.Statement並實現了其方法。
執行過程中除了傳回ResultSet的需要傳回ProxyFactory.getProxyResultSet(connection, this, resultSet);
static ResultSet getProxyResultSet(final ProxyConnection connection, final ProxyStatement statement, final ResultSet resultSet) {
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
}
就是上文提及的位元組碼,其他直接傳回delegate代理物件。
/** {@inheritDoc} */
@Override
public boolean execute(String sql) throws SQLException {
connection.markCommitStateDirty();
return delegate.execute(sql);
}
/** {@inheritDoc} */
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
connection.markCommitStateDirty();
return delegate.execute(sql, autoGeneratedKeys);
}
/** {@inheritDoc} */
@Override
public ResultSet executeQuery(String sql) throws SQLException {
connection.markCommitStateDirty();
ResultSet resultSet = delegate.executeQuery(sql);
return ProxyFactory.getProxyResultSet(connection, this, resultSet);
}
關於其close方法得提一下上一節的 【追光者系列】HikariCP原始碼分析之FastList
// **********************************************************************
// Overridden java.sql.Statement Methods
// **********************************************************************
/** {@inheritDoc} */
@Override
public final void close() throws SQLException
{
// 放置重覆關閉
synchronized (this) {
if (isClosed) {
return;
}
isClosed = true;
}
// 移出快取
connection.untrackStatement(delegate);
try {
// 關閉代理
delegate.close();
}
catch (SQLException e) {
throw connection.checkException(e);
}
}
connection.untrackStatement(delegate); 這裡我們可以看到原始碼如下
final synchronized void untrackStatement(final Statement statement) {
openStatements.remove(statement);
}
private final FastList<Statement> openStatements;
protected ProxyConnection(final PoolEntry poolEntry, final Connection connection, final FastList<Statement> openStatements, final ProxyLeakTask leakTask, final long now, final boolean isReadOnly, final boolean isAutoCommit) {
this.poolEntry = poolEntry;
this.delegate = connection;
this.openStatements = openStatements;
this.leakTask = leakTask;
this.lastAccess = now;
this.isReadOnly = isReadOnly;
this.isAutoCommit = isAutoCommit;
}
FastList是一個List介面的精簡實現,只實現了介面中必要的幾個方法。JDK ArrayList每次呼叫get()方法時都會進行rangeCheck檢查索引是否越界,FastList的實現中去除了這一檢查,只要保證索引合法那麼rangeCheck就成為了不必要的計算開銷(當然開銷極小)。此外,HikariCP使用List來儲存開啟的Statement,當Statement關閉或Connection關閉時需要將對應的Statement從List中移除。通常情況下,同一個Connection建立了多個Statement時,後開啟的Statement會先關閉。ArrayList的remove(Object)方法是從頭開始遍歷陣列,而FastList是從陣列的尾部開始遍歷,因此更為高效。
簡而言之就是 自定義陣列型別(FastList)代替ArrayList:避免每次get()呼叫都要進行range check,避免呼叫remove()時的從頭到尾的掃描
ProxyConnection
該類主要實現了java.sql.Connection的一系列方法,凡涉及Statement、CallableStatement、PreparedStatement的方法都用到了先快取statement,然後透過ProxyFactory工廠生成的位元組碼代理類
/** {@inheritDoc} */
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int concurrency, int holdability) throws SQLException {
return ProxyFactory.getProxyCallableStatement(this, trackStatement(delegate.prepareCall(sql, resultSetType, concurrency, holdability)));
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return ProxyFactory.getProxyPreparedStatement(this, trackStatement(delegate.prepareStatement(sql)));
}
其關閉程式碼需要註意一點的是,因為closeStatements裡會evict poolEntry,所以放到判斷外。
// **********************************************************************
// "Overridden" java.sql.Connection Methods
// **********************************************************************
/** {@inheritDoc} */
@Override
public final void close() throws SQLException {
// Closing statements can cause connection eviction, so this must run before the conditional below
closeStatements();
if (delegate != ClosedConnection.CLOSED_CONNECTION) {
leakTask.cancel();
try {
if (isCommitStateDirty && !isAutoCommit) {
delegate.rollback();
lastAccess = currentTime();
LOGGER.debug("{} - Executed rollback on connection {} due to dirty commit state on close().", poolEntry.getPoolName(), delegate);
}
if (dirtyBits != 0) {
poolEntry.resetConnectionState(this, dirtyBits);
lastAccess = currentTime();
}
delegate.clearWarnings();
}
catch (SQLException e) {
// when connections are aborted, exceptions are often thrown that should not reach the application
if (!poolEntry.isMarkedEvicted()) {
throw checkException(e);
}
}
finally {
delegate = ClosedConnection.CLOSED_CONNECTION;
poolEntry.recycle(lastAccess);
}
}
}
ProxyConnection如下圖,可以看到該類是真正使用FastList來進行最佳化的 private final FastList openStatements;
// 用於標識連線被訪問或存在可提交資料
final void markCommitStateDirty() {
if (isAutoCommit) {
lastAccess = currentTime();
}
else {
isCommitStateDirty = true;
}
}
// 快取statement
private synchronized <T extends Statement> T trackStatement(final T statement) {
openStatements.add(statement);
return statement;
}
// 移出statement快取
final synchronized void untrackStatement(final Statement statement) {
openStatements.remove(statement);
}
// 關閉全部已開啟的statement(只在close方法中呼叫)
@SuppressWarnings("EmptyTryBlock")
private synchronized void closeStatements() {
final int size = openStatements.size();
if (size > 0) {
for (int i = 0; i < size && delegate != ClosedConnection.CLOSED_CONNECTION; i++) {
try (Statement ignored = openStatements.get(i)) {
// automatic resource cleanup
}
catch (SQLException e) {
LOGGER.warn("{} - Connection {} marked as broken because of an exception closing open statements during Connection.close()",
poolEntry.getPoolName(), delegate);
leakTask.cancel();
poolEntry.evict("(exception closing Statements during Connection.close())");
delegate = ClosedConnection.CLOSED_CONNECTION;
}
}
openStatements.clear();
}
}
In order to generate proxies for Connection, Statement, and ResultSet instances HikariCP was initially using a singleton factory, held in the case of ConnectionProxy in a static field (PROXY_FACTORY).
ClosedConnection是ProxyConnection中動態代理實現的唯一實體化物件。全域性唯一變數,作為已關閉連線的代理取用,為連線關閉後外界代理連線的取用呼叫提供處理,同時唯一類減少了記憶體消耗和比對代價。
// **********************************************************************
// Private classes
// **********************************************************************
private static final class ClosedConnection
{
static final Connection CLOSED_CONNECTION = getClosedConnection();
private static Connection getClosedConnection()
{
InvocationHandler handler = (proxy, method, args) -> {
// 只保留3個方法的快速傳回,其他均丟擲異常
final String methodName = method.getName();
if ("abort".equals(methodName)) {
return Void.TYPE;
}
else if ("isValid".equals(methodName)) {
return Boolean.FALSE;
}
else if ("toString".equals(methodName)) {
return ClosedConnection.class.getCanonicalName();
}
throw new SQLException("Connection is closed");
};
return (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), new Class[] { Connection.class }, handler);
}
}
這裡InvocationHandler還是使用到了反射,之前提過的,多多少少對效能也會有點損失,一次全域性唯一變數一次反射,個人認為,這裡反射如果能最佳化為直接呼叫位元組碼,或許效能還能再上一個臺階。
參考資料
-
http://yonglin4605.iteye.com/blog/1396494
-
http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
-
http://zhxing.iteye.com/blog/1703305
-
http://www.cnblogs.com/taisenki/p/7716724.html
END