jedis链接池参数minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis探索

咱们一般在使用JedisPoolConfig进行链接池配置的时候,minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis这两个参数常常会不懂其含义,
查各类资料也没有很是明确的说到底该如何设置,即便知道如何设置,也不知道其原理,只知道这两个参数是和逐出线程有关的。下面根据源码进行探索。
咱们一般是经过JedisPool构造线程池,追溯其父类的建立过程,发现Pool<T>这个泛型类的构造方法调用过程以下:java

public Pool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) { this.initPool(poolConfig, factory); } public void initPool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) { if(this.internalPool != null) { try { this.closeInternalPool(); } catch (Exception var4) { ; } } this.internalPool = new GenericObjectPool(factory, poolConfig); }

发现其建立了一个GenericObjectPool对象,构造方法以下:apache

public GenericObjectPool(PooledObjectFactory<T> factory, GenericObjectPoolConfig config) { super(config, "org.apache.commons.pool2:type=GenericObjectPool,name=", config.getJmxNamePrefix()); this.factoryType = null; this.maxIdle = 8; this.minIdle = 0; this.allObjects = new ConcurrentHashMap(); this.createCount = new AtomicLong(0L); this.abandonedConfig = null; if(factory == null) { this.jmxUnregister(); throw new IllegalArgumentException("factory may not be null"); } else { this.factory = factory; this.idleObjects = new LinkedBlockingDeque(config.getFairness()); this.setConfig(config); this.startEvictor(this.getTimeBetweenEvictionRunsMillis()); } } 

其中this.startEvictor(this.getTimeBetweenEvictionRunsMillis());方法的调用,正是开启逐出线程运行的做用,this

咱们能够发现,源码经过周期性的调度逐出任务(timeBetweenEvictionRunsMillis大于0时),将空闲的链接逐出线程池。spa

final void startEvictor(long delay) { Object var3 = this.evictionLock; synchronized(this.evictionLock) { if(null != this.evictor) { EvictionTimer.cancel(this.evictor); this.evictor = null; this.evictionIterator = null; } if(delay > 0L) { this.evictor = new BaseGenericObjectPool.Evictor(); EvictionTimer.schedule(this.evictor, delay, delay); } } } 

下面将是咱们今天研究的重点,this.evictor。线程

逐出有逐出策略,若是不配置则采用默认的逐出策略DefaultEvictionPolicy,其中的evict方法返回true时才执行逐出的操做code

public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> { public DefaultEvictionPolicy() { } public boolean evict(EvictionConfig config, PooledObject<T> underTest, int idleCount) { return config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() && config.getMinIdle() < idleCount || config.getIdleEvictTime() < underTest.getIdleTimeMillis(); } }

真正的逐出方法执行的是如下内容对象

public void evict() throws Exception { this.assertOpen(); if(this.idleObjects.size() > 0) { PooledObject<T> underTest = null; EvictionPolicy<T> evictionPolicy = this.getEvictionPolicy(); Object var3 = this.evictionLock; synchronized(this.evictionLock) { EvictionConfig evictionConfig = new EvictionConfig(this.getMinEvictableIdleTimeMillis(), this.getSoftMinEvictableIdleTimeMillis(), this.getMinIdle()); boolean testWhileIdle = this.getTestWhileIdle(); int i = 0; int m = this.getNumTests(); while(true) { if(i >= m) { break; } if(this.evictionIterator == null || !this.evictionIterator.hasNext()) { this.evictionIterator = new EvictionIterator(this, this.idleObjects); } if(!this.evictionIterator.hasNext()) { return; } label81: { try { underTest = this.evictionIterator.next(); } catch (NoSuchElementException var15) { --i; this.evictionIterator = null; break label81; } if(!underTest.startEvictionTest()) { --i; } else { boolean evict; try { evict = evictionPolicy.evict(evictionConfig, underTest, this.idleObjects.size()); } catch (Throwable var14) { PoolUtils.checkRethrow(var14); this.swallowException(new Exception(var14)); evict = false; } if(evict) { this.destroy(underTest); this.destroyedByEvictorCount.incrementAndGet(); } else { if(testWhileIdle) { boolean active = false; try { this.factory.activateObject(underTest); active = true; } catch (Exception var13) { this.destroy(underTest); this.destroyedByEvictorCount.incrementAndGet(); } if(active) { if(!this.factory.validateObject(underTest)) { this.destroy(underTest); this.destroyedByEvictorCount.incrementAndGet(); } else { try { this.factory.passivateObject(underTest); } catch (Exception var12) { this.destroy(underTest); this.destroyedByEvictorCount.incrementAndGet(); } } } } if(!underTest.endEvictionTest(this.idleObjects)) { ; } } } } ++i; } } } AbandonedConfig ac = this.abandonedConfig; if(ac != null && ac.getRemoveAbandonedOnMaintenance()) { this.removeAbandoned(ac); } } 

咱们重点看两行代码,第8行是建立了逐出配置,根据你配置的minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis,若是存在负数,则设为long类型的最大值。blog

public EvictionConfig(long poolIdleEvictTime, long poolIdleSoftEvictTime, int minIdle) { if(poolIdleEvictTime > 0L) { this.idleEvictTime = poolIdleEvictTime; } else { this.idleEvictTime = 9223372036854775807L; } if(poolIdleSoftEvictTime > 0L) { this.idleSoftEvictTime = poolIdleSoftEvictTime; } else { this.idleSoftEvictTime = 9223372036854775807L; } this.minIdle = minIdle; } 

再看第40行代码,再结合DefaultEvictionPolicy的evict方法,咱们能够看到,真正的逐出依据是:进程

1.链接空闲时间大于softMinEvictableIdleTimeMillis而且当前链接池的空闲链接数大于最小空闲链接数minIdle;rem

2.链接空闲时间大于minEvictableIdleTimeMillis。

1或者2成当即可逐出,注意是或的关系。

因此,结论以下:

若是要链接池只根据softMinEvictableIdleTimeMillis进程逐出,那么须要将minEvictableIdleTimeMillis设置为负数(即最大值);
若是要链接池只根据minEvictableIdleTimeMillis进程逐出,那么须要将softMinEvictableIdleTimeMillis设置为负数(即最大值),理论上设置minIdle很大也是能够的,可是实际上不行;

相关文章
相关标签/搜索