博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊Elasticsearch的SingleObjectCache
阅读量:7014 次
发布时间:2019-06-28

本文共 4301 字,大约阅读时间需要 14 分钟。

  hot3.png

本文主要研究一下Elasticsearch的SingleObjectCache

SingleObjectCache

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/SingleObjectCache.java

public abstract class SingleObjectCache
{ private volatile T cached; private Lock refreshLock = new ReentrantLock(); private final TimeValue refreshInterval; protected long lastRefreshTimestamp = 0; protected SingleObjectCache(TimeValue refreshInterval, T initialValue) { if (initialValue == null) { throw new IllegalArgumentException("initialValue must not be null"); } this.refreshInterval = refreshInterval; cached = initialValue; } /** * Returns the currently cached object and potentially refreshes the cache before returning. */ public T getOrRefresh() { if (needsRefresh()) { if(refreshLock.tryLock()) { try { if (needsRefresh()) { // check again! cached = refresh(); assert cached != null; lastRefreshTimestamp = System.currentTimeMillis(); } } finally { refreshLock.unlock(); } } } assert cached != null; return cached; } /** Return the potentially stale cached entry. */ protected final T getNoRefresh() { return cached; } /** * Returns a new instance to cache */ protected abstract T refresh(); /** * Returns
true iff the cache needs to be refreshed. */ protected boolean needsRefresh() { if (refreshInterval.millis() == 0) { return true; } final long currentTime = System.currentTimeMillis(); return (currentTime - lastRefreshTimestamp) > refreshInterval.millis(); }}
  • SingleObjectCache的构造器要求refreshInterval、initialValue两个参数;它提供了getOrRefresh方法,该方法会判断该cached值是否过期,如果过期则调用refresh方法刷新值,同时更新lastRefreshTimestamp,如果没过期则返回cached值

实例

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/monitor/fs/FsService.java

public class FsService {    private static final Logger logger = LogManager.getLogger(FsService.class);    private final FsProbe probe;    private final TimeValue refreshInterval;    private final SingleObjectCache
cache; private final ClusterInfoService clusterInfoService; public static final Setting
REFRESH_INTERVAL_SETTING = Setting.timeSetting( "monitor.fs.refresh_interval", TimeValue.timeValueSeconds(1), TimeValue.timeValueSeconds(1), Property.NodeScope); public FsService(final Settings settings, final NodeEnvironment nodeEnvironment, ClusterInfoService clusterInfoService) { this.probe = new FsProbe(nodeEnvironment); this.clusterInfoService = clusterInfoService; refreshInterval = REFRESH_INTERVAL_SETTING.get(settings); logger.debug("using refresh_interval [{}]", refreshInterval); cache = new FsInfoCache(refreshInterval, stats(probe, null, logger, null)); } public FsInfo stats() { return cache.getOrRefresh(); } private static FsInfo stats(FsProbe probe, FsInfo initialValue, Logger logger, @Nullable ClusterInfo clusterInfo) { try { return probe.stats(initialValue, clusterInfo); } catch (IOException e) { logger.debug("unexpected exception reading filesystem info", e); return null; } } private class FsInfoCache extends SingleObjectCache
{ private final FsInfo initialValue; FsInfoCache(TimeValue interval, FsInfo initialValue) { super(interval, initialValue); this.initialValue = initialValue; } @Override protected FsInfo refresh() { return stats(probe, initialValue, logger, clusterInfoService.getClusterInfo()); } }}
  • FsService的构造器使用FsInfoCache创建了cache,其无参的stats方法执行的是cache.getOrRefresh();FsInfoCache继承了SingleObjectCache,它的refresh方法调用的是stats方法,该方法通过probe.stats(initialValue, clusterInfo)来刷新值

小结

SingleObjectCache的构造器要求refreshInterval、initialValue两个参数;它提供了getOrRefresh方法,该方法会判断该cached值是否过期,如果过期则调用refresh方法刷新值,同时更新lastRefreshTimestamp,如果没过期则返回cached值

doc

转载于:https://my.oschina.net/go4it/blog/3059908

你可能感兴趣的文章
查看name的状态,是属于active还是standby
查看>>
<LeetCode OJ> 337. House Robber III
查看>>
PSR规范
查看>>
[Javascript] this in Function Calls
查看>>
MinGW32和64位交叉编译环境的安装和使用
查看>>
laravel 增加不存在数据库的字段
查看>>
什么是“单播”“组播”和“多播”
查看>>
flex---->图表控件
查看>>
Android Developers:在命令行构建和运行
查看>>
firefox 不识别background-position-y / background-position-x
查看>>
分析函数调用关系图(call graph)的几种方法
查看>>
Dynamic Web Module 3.0 requires Java 1.6 or newer
查看>>
11.0592M晶振与12M晶振
查看>>
Web Service学习笔记
查看>>
[转帖]cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND
查看>>
A380上11万一张的机票什么享受?来看看
查看>>
LeetCode: 103_Binary Tree Zigzag Level Order Traversal | 二叉树Zigzag层次遍历 | Medium
查看>>
JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结
查看>>
Java程序猿JavaScript学习笔记(2——复制和继承财产)
查看>>
ubuntu15.10下编译安装wine1.8 rc4
查看>>