前言
Elasticserach分析之前先了解一个配置:indices.fielddata.cache.size = 30% 这个配置的作用是,如果对一个字段进行排序或者做聚合操作,那么
就会将这个字段的所有数据都加载到内存当中,为了加快排序速度。如果配置过于大,会导致jvm内存不够使用而崩溃。所以ES又提供了两个配置:
indices.breaker.fielddata.limit = 60% 和 indices.breaker.fielddata.overhead=1.03 来限制,防止jvm内存溢出。
indicesServices分析
1:dostart方法:启动调度器,定时清除和刷新缓存中的field数据。默认调度时间为一分钟,作用范围为Node节点
@Override
protected void doStart() {
// Start thread that will manage cleaning the field data cache periodically
threadPool.schedule(this.cleanInterval, ThreadPool.Names.SAME, this.cacheCleaner);
}
cleanInterval是个TimeValue对象,通过构造函数进行初始化,默认清除周期为一分钟。TimeValue是ES内部基于java的TimeUnit实现的时间调度服务,有:纳秒,微秒,毫秒,秒,分,时,天
ThreadPool.Names.SAME是es内部自己实现的调度器
cacheCleaner:是indicesService内部实现的Runnable线程接口,主要实现如下:
@Override
public void run() {
long startTimeNS = System.nanoTime();
if (logger.isTraceEnabled()) {
logger.trace("running periodic field data cache cleanup");
}
try {
this.cache.getCache().refresh();
} catch (Exception e) {
logger.warn("Exception during periodic field data cache cleanup:", e);
}
if (logger.isTraceEnabled()) {
logger.trace("periodic field data cache cleanup finished in {} milliseconds", TimeValue.nsecToMSec(System.nanoTime() - startTimeNS));
}
try {
this.requestCache.cleanCache();
} catch (Exception e) {
logger.warn("Exception during periodic request cache cleanup:", e);
}
// Reschedule itself to run again if not closed
if (closed.get() == false) {
threadPool.schedule(interval, ThreadPool.Names.SAME, this);
}
主要方法是cache对象进行缓存刷新,执行refresh方法,requestCache对象进行缓存清除调用cleanCache方法。
网友评论