Link Search Menu Expand Document Documentation Menu

分层缓存

分层缓存是一种多级缓存,其中每个层级都有其自身的特性和性能水平。通过组合不同的层级,可以在缓存性能和大小之间取得平衡。

分层缓存类型

OpenSearch 提供了一种 _tiered 溢出 cache_ 的实现。此实现会将从上层缓存中移除的任何项目溢出到下层缓存。上层(例如堆内层)尺寸较小但延迟更低。下层(例如磁盘缓存)尺寸较大但延迟较高。OpenSearch 提供堆内层和磁盘层。

安装所需插件

要使用分层缓存,请安装 cache-ehcache 插件。此插件提供了一个磁盘缓存实现,可以用作分层缓存中的磁盘层。有关安装非捆绑插件的更多信息,请参阅附加插件

如果未安装 cache-ehcache 插件或未设置磁盘缓存属性,分层缓存将无法初始化。

分层缓存设置

在 OpenSearch 2.14 中,请求缓存可以在分层缓存中使用。首先,在 opensearch.yml 文件中配置以下设置。

缓存存储名称

要使用 OpenSearch 提供的分层溢出缓存实现,请将缓存存储名称设置为 tiered_spillover,示例如下

indices.requests.cache.store.name: tiered_spillover

设置堆内和磁盘存储层

将堆内和磁盘存储层设置为 opensearch_onheapehcache_disk,示例如下

indices.requests.cache.tiered_spillover.onheap.store.name: opensearch_onheap
indices.requests.cache.tiered_spillover.disk.store.name: ehcache_disk

opensearch_onheap 设置使用 OpenSearch 中内置的堆内缓存。

ehcache_disk 设置是来自 Ehcache 的磁盘缓存实现,并且需要安装 cache-ehcache 插件。

配置堆内和磁盘存储

下表列出了 opensearch_onheap 存储的缓存设置。

设置 数据类型 默认值 描述
indices.requests.cache.opensearch_onheap.size 百分比 堆大小的 1% 堆内缓存的大小。可选。
indices.requests.cache.opensearch_onheap.expire 时间单位 MAX_VALUE(禁用) 指定缓存结果的生存时间 (TTL)。可选。

下表列出了 ehcache_disk 存储的磁盘缓存设置。

设置 数据类型 默认值 描述
indices.requests.cache.ehcache_disk.max_size_in_bytes 长整型 1073741824(1 GB) 定义磁盘缓存的大小。可选。
indices.requests.cache.ehcache_disk.storage.path 字符串 {data.paths}/nodes/{node.id}/request_cache 定义磁盘缓存的存储路径。可选。
indices.requests.cache.ehcache_disk.expire_after_access 时间单位 MAX_VALUE(禁用) 指定缓存结果的 TTL。可选。
indices.requests.cache.ehcache_disk.alias 字符串 ehcacheDiskCache#INDICES_REQUEST_CACHE 指定磁盘缓存的别名。可选。
indices.requests.cache.ehcache_disk.segments 整数 16 定义磁盘缓存被分割成的段数。用于并发。可选。
indices.requests.cache.ehcache_disk.concurrency 整数 1 定义为磁盘存储创建的独立写入队列数,其中一组段共享一个写入队列。可选。
indices.requests.cache.ehcache_disk.min_threads 整数 2 定义池中 ehcache 磁盘线程的最小数量。可选。
indices.requests.cache.ehcache_disk.max_threads 整数 CPU 核心数 定义池中 ehcache 磁盘线程的最大数量。允许的最大值为 10 * num_cpu_cores。磁盘操作通常受 I/O 限制而不是 CPU 限制,因此您可以将此值设置为大于 CPU 核心数的数字。可选。

tiered_spillover 存储的附加设置

下表列出了 tiered_spillover 存储设置的附加设置。

设置 数据类型 默认值 描述
indices.requests.cache.tiered_spillover.policies.took_time.threshold 时间单位 0ms 一种策略,用于根据查询阶段执行时间确定是否将查询缓存到缓存中。这是一项动态设置。可选。
indices.requests.cache.tiered_spillover.disk.store.policies.took_time.threshold 时间单位 10ms 一种策略,用于根据查询阶段执行时间确定是否将查询缓存到缓存的磁盘层。这是一项动态设置。可选。
indices.requests.cache.tiered_spillover.disk.store.enabled 布尔型 True 在分层溢出缓存中动态启用或禁用磁盘缓存。注意:禁用磁盘缓存后,条目不会自动移除,需要手动清除缓存。可选。
indices.requests.cache.tiered_spillover.onheap.store.size 百分比 堆大小的 1% 定义分层缓存中堆内缓存的大小。可选。
indices.requests.cache.tiered_spillover.disk.store.size 长整型 1073741824(1 GB) 定义分层缓存中磁盘缓存的大小。可选。
indices.requests.cache.tiered_spillover.segments 整数 2 ^ (ceil(log2(CPU_CORES * 1.5))) 这决定了分层缓存中的段数,每个段都由可重入的读/写锁保护。这些锁支持多个并发读取器而不会发生争用,同时分段允许多个写入器同时操作,从而实现更高的写入吞吐量。可选。

删除过期条目设置

下表列出了与从缓存中删除过期条目相关的设置。

设置 数据类型 默认值 描述
indices.requests.cache.cleanup.staleness_threshold 字符串 0% 定义缓存中过期键的百分比。识别后,所有过期缓存条目都将被删除。可选。
indices.requests.cache.cleanup.interval 时间单位 1m 定义请求缓存中过期条目的删除频率。可选。

获取 tiered_spillover 存储的统计信息

要评估使用分层溢出缓存的影响,请使用节点统计 API,示例如下

GET /_nodes/stats/caches/request_cache?level=tier