開源專案剖析之apache-common-pool

李佔衛發表於2014-01-08

前沿

     該工程提供了物件池解決方案,該方案主要用於提高像檔案控制程式碼,資料庫連線,socket通訊這類大物件的呼叫效率。簡單的說就是一種物件一次建立多次使用的技術。

整體結構

     整個專案有三個包分別是org.apache.commons.pool2,org.apache.commons.pool2.impl和org.apache.commons.pool2.proxy。org.apache.commons.pool2主要定義整個專案要實現的介面;org.apache.commons.pool2.impl主要定義對介面的一般實現;org.apache.commons.pool2.proxy主要定義對介面的代理實現。

     接下來我們看看整體的程式碼關係,如下類圖

      整個方案從ObjectPool,PooledObjectFactory和PooledObject三個介面展開,其中ObjectPool定義了物件池要實現的功能【比如怎麼存取,怎麼過期】;PooledObjectFactory定義了被池化的物件的建立,初始化,啟用,鈍化以及銷燬功能;PooledObject定了一被池化物件的一些附加資訊【建立時間,池中狀態】;大概流程就是由PooledObjectFactory建立的物件經過PooledObject的包裝然後放到ObjectPool裡面來。

ObjectPool 

//從池中獲取物件
T borrowObject() throws Exception, NoSuchElementException,IllegalStateException;
//將物件放回池中
void returnObject(T obj) throws Exception;
//廢棄物件
void invalidateObject(T obj) throws Exception;
//新增物件
void addObject() throws Exception, IllegalStateException,UnsupportedOperationException;
//獲取物件個數
int getNumIdle();
//獲取活躍物件個數
int getNumActive();
//清除池,池可用
void clear() throws Exception, UnsupportedOperationException;
//關閉池,池不可用
void close();

PooledObjectFactory

//建立一個新物件;當物件池中的物件個數不足時,將會使用此方法來"輸出"一個新的"物件",並交付給物件池管理
PooledObject<T> makeObject() throws Exception;
//銷燬物件,如果物件池中檢測到某個"物件"idle的時間超時,或者操作者向物件池"歸還物件"時檢測到"物件"已經無效,那麼此時將會導致"物件銷燬";"銷燬物件"的操作設計相差甚遠,但是必須明確:當呼叫此方法時,"物件"的生命週期必須結束.如果object是執行緒,那麼此時執行緒必須退出;如果object是socket操作,那麼此時socket必須關閉;如果object是檔案流操作,那麼此時"資料flush"且正常關閉.
void destroyObject(PooledObject<T> p) throws Exception;
//檢測物件是否"有效";Pool中不能儲存無效的"物件",因此"後臺檢測執行緒"會週期性的檢測Pool中"物件"的有效性,如果物件無效則會導致此物件從Pool中移除,並destroy;此外在呼叫者從Pool獲取一個"物件"時,也會檢測"物件"的有效性,確保不能講"無效"的物件輸出給呼叫者;當呼叫者使用完畢將"物件歸還"到Pool時,仍然會檢測物件的有效性.所謂有效性,就是此"物件"的狀態是否符合預期,是否可以對呼叫者直接使用;如果物件是Socket,那麼它的有效性就是socket的通道是否暢通/阻塞是否超時等.
boolean validateObject(PooledObject<T> p);
// "啟用"物件,當Pool中決定移除一個物件交付給呼叫者時額外的"啟用"操作,比如可以在activateObject方法中"重置"引數列表讓呼叫者使用時感覺像一個"新建立"的物件一樣;如果object是一個執行緒,可以在"啟用"操作中重置"執行緒中斷標記",或者讓執行緒從阻塞中喚醒等;如果object是一個socket,那麼可以在"啟用操作"中重新整理通道,或者對socket進行連結重建(假如socket意外關閉)等.
void activateObject(PooledObject<T> p) throws Exception;
//"鈍化"物件,當呼叫者"歸還物件"時,Pool將會"鈍化物件";鈍化的言外之意,就是此"物件"暫且需要"休息"一下.如果object是一個socket,那麼可以passivateObject中清除buffer,將socket阻塞;如果object是一個執行緒,可以在"鈍化"操作中將執行緒sleep或者將執行緒中的某個物件wait.需要注意的時,activateObject和passivateObject兩個方法需要對應,避免死鎖或者"物件"狀態的混亂.
void passivateObject(PooledObject<T> p) throws Exception;

PooledObject

T getObject();
long getCreateTime();
long getActiveTimeMillis();
long getIdleTimeMillis();
long getLastBorrowTime();
long getLastReturnTime();
long getLastUsedTime();
int compareTo(PooledObject<T> other);
boolean equals(Object obj);
int hashCode();
String toString();
//後臺清理執行緒
boolean startEvictionTest();
boolean endEvictionTest(Deque<PooledObject<T>> idleQueue);
boolean allocate();
boolean deallocate();
void invalidate()
void setLogAbandoned(boolean logAbandoned);
void use();
void printStackTrace(PrintWriter writer);
PooledObjectState getState();
//自動補償功能
void markAbandoned();
void markReturning();

      方案提供了三種型別的pool,分別是GenericKeyedObjectPool,SoftReferenceObjectPool和GenericObjectPool。其中GenericObjectPool是一般意義上的物件池;SoftReferenceObjectPool是弱引用的物件池;GenericKeyedObjectPool是具備分組的物件池。

以下是各個類的詳細分析

SoftReferenceObjectPool

//可用物件列表
private final LinkedBlockingDeque<PooledSoftReference<T>> idleReferences = new LinkedBlockingDeque<PooledSoftReference<T>>();
//所有物件列表
private final ArrayList<PooledSoftReference<T>> allReferences = new ArrayList<PooledSoftReference<T>>();

borrowObject

//可用物件列表
private final LinkedBlockingDeque<PooledSoftReference<T>> idleReferences = new LinkedBlockingDeque<PooledSoftReference<T>>();
//所有物件列表
private final ArrayList<PooledSoftReference<T>> allReferences = new ArrayList<PooledSoftReference<T>>();


    public synchronized T borrowObject() throws Exception {
        assertOpen();//確定池開啟
        T obj = null;
        boolean newlyCreated = false;
        PooledSoftReference<T> ref = null;
        while (null == obj) {
            if (idleReferences.isEmpty()) {
                if (null == factory) {
                    throw new NoSuchElementException();
                } else {
                    //如果可用列表為空則建立新的物件
                    newlyCreated = true;
                    obj = factory.makeObject().getObject();
                    //累加計數器
                    createCount++;
                    // Do not register with the queue
                    //關聯
                    ref = new PooledSoftReference<T>(new SoftReference<T>(obj));
                    //新增進所有列表
                    allReferences.add(ref);
                }
            } else {
                //從可用佇列獲取物件
                ref = idleReferences.pollFirst();
                obj = ref.getObject();
                // Clear the reference so it will not be queued, but replace with a
                // a new, non-registered reference so we can still track this object
                // in allReferences
                //重建關聯
                ref.getReference().clear();
                ref.setReference(new SoftReference<T>(obj));
            }
            if (null != factory && null != obj) {
                try {
                    //啟用物件
                    factory.activateObject(ref);
                    if (!factory.validateObject(ref)) {
                        throw new Exception("ValidateObject failed");
                    }
                } catch (Throwable t) {
                    PoolUtils.checkRethrow(t);
                    try {
                        destroy(ref);
                    } catch (Throwable t2) {
                        PoolUtils.checkRethrow(t2);
                        // Swallowed
                    } finally {
                        obj = null;
                    }
                    if (newlyCreated) {
                        throw new NoSuchElementException(
                                "Could not create a validated object, cause: " +
                                        t.getMessage());
                    }
                }
            }
        }
        
        numActive++;
        //鎖定
        ref.allocate();
        return obj;
    }

returnObject

    public synchronized void returnObject(T obj) throws Exception {
        boolean success = !isClosed();
        //判斷物件來自於池【通過物件的equals方法】
        final PooledSoftReference<T> ref = findReference(obj);
        if (ref == null) {
            throw new IllegalStateException(
                "Returned object not currently part of this pool");
        }
        if (factory != null) {
            //判斷物件合格
            if (!factory.validateObject(ref)) {
                success = false;
            } else {
                try {
                    //鈍化物件
                    factory.passivateObject(ref);
                } catch (Exception e) {
                    success = false;
                }
            }
        }
        
        boolean shouldDestroy = !success;
        numActive--;
        if (success) {
            //如果物件合格並且鈍化成功則解除鎖定並新增到可用列表中
            // Deallocate and add to the idle instance pool
            ref.deallocate();
            idleReferences.add(ref);
        }
        notifyAll(); // numActive has changed

        if (shouldDestroy && factory != null) {
            try {
                destroy(ref);
            } catch (Exception e) {
                // ignored
            }
        }
    }

弱引用物件池最簡單,沒有後臺清理執行緒只有當記憶體不夠的情況下由虛擬機器清除。

GenericObjectPool

//預設出隊方式
private volatile boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO; 
//後臺清理邏輯
class Evictor extends TimerTask
//所有物件列表
private final Map<T, PooledObject<T>> allObjects = new ConcurrentHashMap<T, PooledObject<T>>();
//可用物件列表【雙向連結串列】
private final LinkedBlockingDeque<PooledObject<T>> idleObjects = new LinkedBlockingDeque<PooledObject<T>>();

borrowObject

    public T borrowObject(long borrowMaxWaitMillis) throws Exception {
        assertOpen();
//是否在獲取物件的時候檢查物件,開啟的話則檢查【主要是檢查過期】
        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
//當池耗盡的時候是否block,如果block的話則會idleObjects.pollFirst(borrowMaxWaitMillis,TimeUnit.MILLISECONDS);否則直接throw new NoSuchElementException("Pool exhausted");
        boolean blockWhenExhausted = getBlockWhenExhausted();

        boolean create;
        long waitTime = 0;

        while (p == null) {
            create = false;
            if (blockWhenExhausted) {
                p = idleObjects.pollFirst();
                if (p == null) {
                    create = true;
                    p = create();
                }
                if (p == null) {
                    if (borrowMaxWaitMillis < 0) {
                        p = idleObjects.takeFirst();
                    } else {
                        waitTime = System.currentTimeMillis();
                        p = idleObjects.pollFirst(borrowMaxWaitMillis,
                                TimeUnit.MILLISECONDS);
                        waitTime = System.currentTimeMillis() - waitTime;
                    }
                }
                if (p == null) {
                    throw new NoSuchElementException(
                            "Timeout waiting for idle object");
                }
                if (!p.allocate()) {
                    p = null;
                }
            } else {
                p = idleObjects.pollFirst();
                if (p == null) {
                    create = true;
                    p = create();
                }
                if (p == null) {
                    throw new NoSuchElementException("Pool exhausted");
                }
                if (!p.allocate()) {
                    p = null;
                }
            }

            if (p != null) {
                try {
//啟用物件
                    factory.activateObject(p);
                } catch (Exception e) {
                    try {
                        destroy(p);
                    } catch (Exception e1) {
                        // Ignore - activation failure is more important
                    }
                    p = null;
                    if (create) {
                        NoSuchElementException nsee = new NoSuchElementException(
                                "Unable to activate object");
                        nsee.initCause(e);
                        throw nsee;
                    }
                }
//如果獲取物件是檢查則validateObject
                if (p != null && getTestOnBorrow()) {
                    boolean validate = false;
                    Throwable validationThrowable = null;
                    try {
                        validate = factory.validateObject(p);
                    } catch (Throwable t) {
                        PoolUtils.checkRethrow(t);
                        validationThrowable = t;
                    }
//檢查不通過則destroy
                    if (!validate) {
                        try {
                            destroy(p);
                            destroyedByBorrowValidationCount.incrementAndGet();
                        } catch (Exception e) {
                            // Ignore - validation failure is more important
                        }
                        p = null;
                        if (create) {
                            NoSuchElementException nsee = new NoSuchElementException(
                                    "Unable to validate object");
                            nsee.initCause(validationThrowable);
                            throw nsee;
                        }
                    }
                }
            }
        }

        updateStatsBorrow(p, waitTime);

        return p.getObject();
    }

returnObject

    public void returnObject(T obj) {
        PooledObject<T> p = allObjects.get(obj);

        if (!isAbandonedConfig()) {
            if (p == null) {
                throw new IllegalStateException(
                        "Returned object not currently part of this pool");
            }
        } else {
            if (p == null) {
                return;  // Object was abandoned and removed
            } else {
                // Make sure object is not being reclaimed
                synchronized(p) {
                    final PooledObjectState state = p.getState();
                    if (state == PooledObjectState.ABANDONED ||
                            state == PooledObjectState.INVALID) {
                        return;
                    } else {
                        p.markReturning(); // Keep from being marked abandoned
                    }
                }
            }
        }

        long activeTime = p.getActiveTimeMillis();
//驗證合格
        if (getTestOnReturn()) {
            if (!factory.validateObject(p)) {
                try {
                    destroy(p);
                } catch (Exception e) {
                    swallowException(e);
                }
                updateStatsReturn(activeTime);
                return;
            }
        }
//鈍化
        try {
            factory.passivateObject(p);
        } catch (Exception e1) {
            swallowException(e1);
            try {
                destroy(p);
            } catch (Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }

        if (!p.deallocate()) {
            throw new IllegalStateException(
                    "Object has already been retured to this pool or is invalid");
        }
//池大小
        int maxIdleSave = getMaxIdle();
        if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
            try {
                destroy(p);
            } catch (Exception e) {
                swallowException(e);
            }
        } else {
            if (getLifo()) {
                idleObjects.addFirst(p);
            } else {
                idleObjects.addLast(p);
            }
        }
        updateStatsReturn(activeTime);
    }

GenericKeyedObjectPool和GenericObjectPool類似。這裡不再累述,下面主要說說怎麼呼叫