序
本文主要研究一下jedis的borrow行為
borrowObject
org/apache/commons/pool2/impl/GenericObjectPool.java
public T borrowObject(final Duration borrowMaxWaitDuration) throws Exception {
assertOpen();
final AbandonedConfig ac = this.abandonedConfig;
if (ac != null && ac.getRemoveAbandonedOnBorrow() && (getNumIdle() < 2) &&
(getNumActive() > getMaxTotal() - 3)) {
removeAbandoned(ac);
}
PooledObject<T> p = null;
// Get local copy of current config so it is consistent for entire
// method execution
final boolean blockWhenExhausted = getBlockWhenExhausted();
boolean create;
final long waitTimeMillis = System.currentTimeMillis();
while (p == null) {
create = false;
p = idleObjects.pollFirst();
if (p == null) {
p = create();
if (p != null) {
create = true;
}
}
if (blockWhenExhausted) {
if (p == null) {
if (borrowMaxWaitDuration.isNegative()) {
p = idleObjects.takeFirst();
} else {
p = idleObjects.pollFirst(borrowMaxWaitDuration);
}
}
if (p == null) {
throw new NoSuchElementException(appendStats(
"Timeout waiting for idle object, borrowMaxWaitDuration=" + borrowMaxWaitDuration));
}
} else if (p == null) {
throw new NoSuchElementException(appendStats("Pool exhausted"));
}
if (!p.allocate()) {
p = null;
}
if (p != null) {
try {
factory.activateObject(p);
} catch (final Exception e) {
try {
destroy(p, DestroyMode.NORMAL);
} catch (final Exception e1) {
// Ignore - activation failure is more important
}
p = null;
if (create) {
final NoSuchElementException nsee = new NoSuchElementException(
appendStats("Unable to activate object"));
nsee.initCause(e);
throw nsee;
}
}
if (p != null && getTestOnBorrow()) {
boolean validate = false;
Throwable validationThrowable = null;
try {
validate = factory.validateObject(p);
} catch (final Throwable t) {
PoolUtils.checkRethrow(t);
validationThrowable = t;
}
if (!validate) {
try {
destroy(p, DestroyMode.NORMAL);
destroyedByBorrowValidationCount.incrementAndGet();
} catch (final Exception e) {
// Ignore - validation failure is more important
}
p = null;
if (create) {
final NoSuchElementException nsee = new NoSuchElementException(
appendStats("Unable to validate object"));
nsee.initCause(validationThrowable);
throw nsee;
}
}
}
}
}
updateStatsBorrow(p, Duration.ofMillis(System.currentTimeMillis() - waitTimeMillis));
return p.getObject();
}
- borrowObject方法會開啟一個while迴圈,條件是p為null,也就是要獲取到p或者是內部自己跳出迴圈;idleObjects.pollFirst()從連線池獲取,如果為null則執行create,之後是blockWhenExhausted的判斷邏輯,如果create出來的為null,則阻塞等待takeFirst或者pollFirst(borrowMaxWaitDuration),如果還是null則丟擲NoSuchElementException;如果blockWhenExhausted為false但是create為null則丟擲
Pool exhausted
- 如果不是null,則再次確認下object的狀態,如果變更狀態(
PooledObjectState.IDLE-->PooledObjectState.ALLOCATED
)不成功則返回null;接著執行factory.activateObject(p)方法,如果出現異常則destory掉(jedis這裡只是在db不一樣的時候會重新select,預設可以理解為空操作
),緊接著是testOnBorrow的邏輯 - 這裡就是如果idleObjects.pollFirst()為null會觸發create,如果還是null則直接丟擲NoSuchElementException異常,跳出迴圈;只有在不為null且allocate失敗的時候會重置為null繼續迴圈;另外如果是create出來的但是activate不成功也會丟擲NoSuchElementException異常,跳出迴圈
create
/**
* Attempts to create a new wrapped pooled object.
* <p>
* If there are {@link #getMaxTotal()} objects already in circulation
* or in process of being created, this method returns null.
* </p>
*
* @return The new wrapped pooled object
*
* @throws Exception if the object factory's {@code makeObject} fails
*/
private PooledObject<T> create() throws Exception {
int localMaxTotal = getMaxTotal();
// This simplifies the code later in this method
if (localMaxTotal < 0) {
localMaxTotal = Integer.MAX_VALUE;
}
final long localStartTimeMillis = System.currentTimeMillis();
final long localMaxWaitTimeMillis = Math.max(getMaxWaitDuration().toMillis(), 0);
// Flag that indicates if create should:
// - TRUE: call the factory to create an object
// - FALSE: return null
// - null: loop and re-test the condition that determines whether to
// call the factory
Boolean create = null;
while (create == null) {
synchronized (makeObjectCountLock) {
final long newCreateCount = createCount.incrementAndGet();
if (newCreateCount > localMaxTotal) {
// The pool is currently at capacity or in the process of
// making enough new objects to take it to capacity.
createCount.decrementAndGet();
if (makeObjectCount == 0) {
// There are no makeObject() calls in progress so the
// pool is at capacity. Do not attempt to create a new
// object. Return and wait for an object to be returned
create = Boolean.FALSE;
} else {
// There are makeObject() calls in progress that might
// bring the pool to capacity. Those calls might also
// fail so wait until they complete and then re-test if
// the pool is at capacity or not.
makeObjectCountLock.wait(localMaxWaitTimeMillis);
}
} else {
// The pool is not at capacity. Create a new object.
makeObjectCount++;
create = Boolean.TRUE;
}
}
// Do not block more if maxWaitTimeMillis is set.
if (create == null &&
(localMaxWaitTimeMillis > 0 &&
System.currentTimeMillis() - localStartTimeMillis >= localMaxWaitTimeMillis)) {
create = Boolean.FALSE;
}
}
if (!create.booleanValue()) {
return null;
}
final PooledObject<T> p;
try {
p = factory.makeObject();
if (getTestOnCreate() && !factory.validateObject(p)) {
createCount.decrementAndGet();
return null;
}
} catch (final Throwable e) {
createCount.decrementAndGet();
throw e;
} finally {
synchronized (makeObjectCountLock) {
makeObjectCount--;
makeObjectCountLock.notifyAll();
}
}
final AbandonedConfig ac = this.abandonedConfig;
if (ac != null && ac.getLogAbandoned()) {
p.setLogAbandoned(true);
p.setRequireFullStackTrace(ac.getRequireFullStackTrace());
}
createdCount.incrementAndGet();
allObjects.put(new IdentityWrapper<>(p.getObject()), p);
return p;
}
create方法不會判斷createCount,如果超出則返回null,如果等待超出maxWait也會返回null;如果判斷要建立則透過factory.makeObject(),另外針對testOnCreate且validateObject不透過的也返回null,如果是有異常則直接丟擲
makeObject
redis/clients/jedis/JedisFactory.java
@Override
public PooledObject<Jedis> makeObject() throws Exception {
Jedis jedis = null;
try {
jedis = new Jedis(jedisSocketFactory, clientConfig);
jedis.connect();
return new DefaultPooledObject<>(jedis);
} catch (JedisException je) {
if (jedis != null) {
try {
jedis.quit();
} catch (RuntimeException e) {
logger.warn("Error while QUIT", e);
}
try {
jedis.close();
} catch (RuntimeException e) {
logger.warn("Error while close", e);
}
}
throw je;
}
}
JedisFactory的makeObject會建立Jedis然後執行connect,如果有JedisException則丟擲,這個也會直接跳出borrowObject的迴圈,直接給到呼叫方
activateObject
redis/clients/jedis/JedisFactory.java
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
final BinaryJedis jedis = pooledJedis.getObject();
if (jedis.getDB() != clientConfig.getDatabase()) {
jedis.select(clientConfig.getDatabase());
}
}
JedisFactory的activateObject就是判斷db跟配置的是不是一樣,不一樣則重新select
testOnBorrow
if (p != null && getTestOnBorrow()) {
boolean validate = false;
Throwable validationThrowable = null;
try {
validate = factory.validateObject(p);
} catch (final Throwable t) {
PoolUtils.checkRethrow(t);
validationThrowable = t;
}
if (!validate) {
try {
destroy(p, DestroyMode.NORMAL);
destroyedByBorrowValidationCount.incrementAndGet();
} catch (final Exception e) {
// Ignore - validation failure is more important
}
p = null;
if (create) {
final NoSuchElementException nsee = new NoSuchElementException(
appendStats("Unable to validate object"));
nsee.initCause(validationThrowable);
throw nsee;
}
}
}
public static void checkRethrow(final Throwable t) {
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
if (t instanceof VirtualMachineError) {
throw (VirtualMachineError) t;
}
// All other instances of Throwable will be silently swallowed
}
testOnBorrow的邏輯就是執行validateObject方法,如果是ThreadDeath或者VirtualMachineError才會重新丟擲,否則吞掉,之後判斷validate結果,如果不成功則執行destory方法,重新設定為null,但是如果這個是create出來的則丟擲NoSuchElementException
小結
jedis的borrow行為是在while迴圈裡頭去獲取的,一般是在allocate變更狀態不成功(PooledObjectState.IDLE-->PooledObjectState.ALLOCATED
)的時候會重新設定null,繼續迴圈
- idleObjects.pollFirst()為null會觸發create,如果還是null則丟擲NoSuchElementException(
Pool exhausted
)跳出迴圈;如果blockWhenExhausted為true,block之後獲取到的還是null,也會丟擲NoSuchElementException(Timeout waiting for idle object
)跳出迴圈;如果觸發create操作,且create丟擲JedisException,這個也會直接跳出borrowObject的迴圈,直接給到呼叫方 - borrow出來不會null的執行activateObject,jedis這裡只是在db不一樣的時候會重新select,預設可以理解為空操作
最後是testOnBorrow的邏輯,如果有異常,則針對create出來的則丟擲NoSuchElementException跳出迴圈,否則重置為null繼續迴圈
總結一下就是如果是create有異常(
JedisException
)則直接丟擲,如果borrow不到(即使經過create)也會丟擲NoSuchElementException(具體可能是Pool exhausted或者Timeout waiting for idle object
),如果有testOnBorrow不透過且是create出來的,也會丟擲NoSuchElementException(Unable to validate object
)