commons-pool2对象池原理简析

所谓对象池,即一个放对象的池子。目的是为了复用对象,以减少创建对象的开销,如连接池、线程池等。

commons-pool2是apache下的一款对象池开源组件,在学习它的原理前,首先考虑下如果我们自实现对象池,会有哪些问题需要考虑?

  1. 底层用什么数据结构来做对象池的容器?

  2. 对象池要有什么属性,支持哪些方法?

  3. 对象在对象池中的生命周期是什么样的?

  4. 从对象池获取/归还的步骤?

接下来我们带着这些问题去学习commons-pool2。

首先看下专门用来生成对象的抽象工厂类BasePooledObjectFactory,它实现了以下接口:


/**

* Creates an instance that can be served by the pool and wrap it in a

* {@link PooledObject} to be managed by the pool.

*

* @return a {@code PooledObject} wrapping an instance that can be served by the pool

*

* @throws Exception if there is a problem creating a new instance,

*    this will be propagated to the code requesting an object.

*/

PooledObject makeObject()throws Exception;

/**

* Destroys an instance no longer needed by the pool.

* <p>* It is important for implementations of this method to be aware that there

* is no guarantee about what state <code>obj</code>will be in and the

* implementation should be prepared to handle unexpected errors.

* </p> * <p>* Also, an implementation must take in to consideration that instances lost

* to the garbage collector may never be destroyed.

* </p>*

* @param p a {@code PooledObject} wrapping the instance to be destroyed

*

* @throws Exception should be avoided as it may be swallowed by

*    the pool implementation.

*

* @see #validateObject

* @see ObjectPool#invalidateObject

*/

void destroyObject(PooledObject p)throws Exception;

/**

* Ensures that the instance is safe to be returned by the pool.

*

* @param p a {@code PooledObject} wrapping the instance to be validated

*

* @return <code>false</code> if <code>obj</code>is not valid and should

*        be dropped from the pool, <code>true</code>otherwise.

*/

boolean validateObject(PooledObject p);

/**

* Reinitializes an instance to be returned by the pool.

*

* @param p a {@code PooledObject} wrapping the instance to be activated

*

* @throws Exception if there is a problem activating <code>obj</code>,

*    this exception may be swallowed by the pool.

*

* @see #destroyObject

*/

void activateObject(PooledObject p)throws Exception;

/**

* Uninitializes an instance to be returned to the idle object pool.

*

* @param p a {@code PooledObject} wrapping the instance to be passivated

*

* @throws Exception if there is a problem passivating <code>obj</code>,

*    this exception may be swallowed by the pool.

*

* @see #destroyObject

*/

void passivateObject(PooledObject p)throws Exception;

前三个方法很容易理解,后面的activateObject和passivateObject看注释是从池中获取对象后再做一次初始化、归还对象给池前做一次取消初始化。也就是说通过实现这2个方法,实现在获取、归还对象前做一些操作。
抽象类BasePooledObjectFactory只实现了makeObject方法:

@Override
    public PooledObject<T> makeObject() throws Exception {
        return wrap(create());
    }

其中,wrap是抽象方法,由子类实现。这个方法就是用PooledObject封装池中的对象,而DefaultPooledObject就是PooledObject的实现类。

private final T object;
    private PooledObjectState state = PooledObjectState.IDLE; // @GuardedBy("this") to ensure transitions are valid
    private final long createTime = System.currentTimeMillis();
    private volatile long lastBorrowTime = createTime;
    private volatile long lastUseTime = createTime;
    private volatile long lastReturnTime = createTime;
    private volatile boolean logAbandoned = false;
    private volatile CallStack borrowedBy = NoOpCallStack.INSTANCE;
    private volatile CallStack usedBy = NoOpCallStack.INSTANCE;
    private volatile long borrowedCount = 0;

看类的属性可知DefaultPooledObject类主要做一些封装,如创建时间、归还时间等属性。
生成池化对象的工厂类BasePooledObjectFactory比较简单,接下来我们对象池的核心实现类GenericObjectPool,首先看下它的属性:


GenericObjectPool属性.png

父类BaseGenericObjectPool属性:


BaseGenericObjectPool属性.png

看属性名我们知道,对象池中有对象最大数量、最小空闲数量、空闲对象集合、所有对象映射map、创建/获取/归还/空闲时是否进行检测属性、驱逐属性等等,基本上大家在使用连接池都有过相关设置。
再看下前面的第一个问题:底层用什么数据结构来做对象池的容器?这里使用的是双端阻塞队列LinkedBlockingDeque,对象池需要有新增、移除、阻塞等方法,而这里新增对象时又有先进先出、后进先出的选择(lifo属性),所以选择LinkedBlockingDeque是比较合理的。下面主要看下获取、归还对象的实现:
public T borrowObject(final long borrowMaxWaitMillis) throws Exception {
        //检查池是否打开
        assertOpen();
        //配置了AbandonedConfig(防泄漏检查配置)并且相关条件满足后,会移除相关废弃对象(满足配置条件)
        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 waitTime = 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 (borrowMaxWaitMillis < 0) {
                        p = idleObjects.takeFirst();
                    } else {
                        //指定时间内获取池内对象
                        p = idleObjects.pollFirst(borrowMaxWaitMillis,
                                TimeUnit.MILLISECONDS);
                    }
                }
                if (p == null) {
                    throw new NoSuchElementException(
                            "Timeout waiting for idle object");
                }
            } else {
                if (p == null) {
                    throw new NoSuchElementException("Pool exhausted");
                }
            }
            if (!p.allocate()) {
                p = null;
            }

            if (p != null) {
                try {
                    //在返回对象给调用者前,调用激活对象方法
                    factory.activateObject(p);
                } catch (final Exception e) {
                    try {
                        destroy(p);
                    } catch (final Exception e1) {
                        // Ignore - activation failure is more important
                    }
                    p = null;
                    if (create) {
                        final NoSuchElementException nsee = new NoSuchElementException(
                                "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);
                            destroyedByBorrowValidationCount.incrementAndGet();
                        } catch (final Exception e) {
                            // Ignore - validation failure is more important
                        }
                        p = null;
                        if (create) {
                            final NoSuchElementException nsee = new NoSuchElementException(
                                    "Unable to validate object");
                            nsee.initCause(validationThrowable);
                            throw nsee;
                        }
                    }
                }
            }
        }
        //更新一些状态,如对象借出数、空闲时间、等待时间等
        updateStatsBorrow(p, System.currentTimeMillis() - waitTime);

        return p.getObject();
    }

创建对象create()

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(getMaxWaitMillis(), 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) {
                //创建完毕,唤醒等待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;
    }

归还方法returnObject

@Override
    public void returnObject(final T obj) {
        final PooledObject<T> p = allObjects.get(new IdentityWrapper<>(obj));

        if (p == null) {
             //如果AbandonedConfig配置不为空且对象池中没有此对象,则抛异常
            if (!isAbandonedConfig()) {
                throw new IllegalStateException(
                        "Returned object not currently part of this pool");
            }
            return; // Object was abandoned and removed
        }

        markReturningState(p);

        final long activeTime = p.getActiveTimeMillis();

        if (getTestOnReturn() && !factory.validateObject(p)) {
            try {
                //清理对象
                destroy(p);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                //保持空闲对象逻辑
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }

        try {
            //钝化对象,可以做一些清理操作
            factory.passivateObject(p);
        } catch (final Exception e1) {
            swallowException(e1);
            try {
                destroy(p);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }

        if (!p.deallocate()) {
            throw new IllegalStateException(
                    "Object has already been returned to this pool or is invalid");
        }

        final int maxIdleSave = getMaxIdle();
        if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
            try {
                destroy(p);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
        } else {
            if (getLifo()) {
                idleObjects.addFirst(p);
            } else {
                idleObjects.addLast(p);
            }
            if (isClosed()) {
                // Pool closed while object was being added to idle objects.
                // Make sure the returned object is destroyed rather than left
                // in the idle object pool (which would effectively be a leak)
                clear();
            }
        }
        updateStatsReturn(activeTime);
    }

目前已对对象池原理做了一些简析,通过以上的分析,基本解决最初提出的几个问题了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,029评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,238评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,576评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,214评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,324评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,392评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,416评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,196评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,631评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,919评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,090评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,767评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,410评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,090评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,328评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,952评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,979评论 2 351

推荐阅读更多精彩内容

  • 我们在服务器开发的过程中,往往会有一些对象,它的创建和初始化需要的时间比较长,比如数据库连接,网络IO,大数据对象...
    王广帅阅读 5,635评论 1 4
  • 之前学习了一下Jedis的操作原理和JedisPool的相关实现,但是在JedisPool的实现中对于JedisP...
    一只小哈阅读 2,829评论 3 10
  • 一、摘要 apache common pool2 作为对象池模式的一种实现,通过重用来分摊复杂对象的创建代价。被广...
    理查德成阅读 2,399评论 1 3
  • 一个对象池包含一组已经初始化过且可以使用的对象,而可以在有需求时创建和销毁对象。池的用户可以从池子中取得对象,对其...
    zfylin阅读 5,955评论 1 3
  • -Have you ever felt like you were just born to do somethi...
    林小言阅读 478评论 2 2