介绍
ES在7.10版本引入了searchable snapshots特性。这里我体验了下searchable snapshots特性,并窥探了内部原理。
es snapshots功能,可以将线上集群的数据备份到一些类似OSS、S3、HDFS等廉价的存储上,如果要使用备份的数据,需要通过restore接口将数据load到在线集群使用。
es主要存储的时序的数据,历史数据一般查询较少。如果跟热数据放在一起,成本消耗很大。所以es一般先使用冷热架构,一个集群中分为hot和warm两种机型,定时把历史数据从hot节点迁移到warm节点。warm节点还需要占用节点cpu、内存资源,而且一般还要有副本保证数据不丢,所以再冷一点的数据,就会做snapshot备份起来,然后在集群中删除数据。
snapshot需要查询时,只能手动restore到在线集群,查询完成后再手动删除。
对于正常索引,节点上的shard是如下图所示:
而使用searchable snapshots后,节点上的shard会是如下图所示:
这样,节点上只用存储主shard,而不再需要副本,节点离线后,snapshots会从其他节点自动恢复。
使用方式
使用searchable snapshot的方式的使用很简单,通过mount命令将snapshot中的索引挂载到线上集群的一个索引中,这样就能通过search接口访问snapshot索引的数据。
挂载的示例如下:
POST /_snapshot/my_repository/my_snapshot/_mount?wait_for_completion=true
{
"index": "my_docs",
"renamed_index": "docs",
"index_settings": {
"index.number_of_replicas": 0
},
"ignored_index_settings": [ "index.refresh_interval" ]
}
接下来就可以像正常索引一样使用。
原理
加载原理
mount接口主要的流程就是调用restoreSnapshot接口,跟正常restore不一样的地方主要体现在传入的参数上。
其中最主要的参数是如下参数:
"index.allocation.existing_shards_allocator" : "searchable_snapshot_allocator"
这是为了支持searchable snapshot增加的功能。主要解决的就是挂载snapshot节点离线后,上面的shard可以自动重新分配到其他节点上。
以前es对于没有replica的索引,是不支持主shard离线后,自动分配shard到其他节点,因为这样就意味着数据丢失。但是searchable snapshot可以做到主shard离线后,在其他节点重新restore该shard的数据,所以增加了existing_shards_allocator功能。
private void reroute(RoutingAllocation allocation) {
assert hasDeadNodes(allocation) == false : "dead nodes should be explicitly cleaned up. See disassociateDeadNodes";
assert AutoExpandReplicas.getAutoExpandReplicaChanges(allocation.metadata(), allocation).isEmpty() :
"auto-expand replicas out of sync with number of nodes in the cluster";
assert assertInitialized();
removeDelayMarkers(allocation);
//增加如下方法,处理节点离线导致的未分配的shard(将gatewayAllocator.allocateUnassigned合并入该方法)
allocateExistingUnassignedShards(allocation); // try to allocate existing shard copies first
shardsAllocator.allocate(allocation);
assert RoutingNodes.assertShardStats(allocation.routingNodes());
}
searchable snapshot会默认添加searchable_snapshot_allocator,该allocator在处理unassigned shard时,会将shard的recovery方式改成从snapshot恢复,这样该shard的状态就跟调用restore snapshot时一样。
之后的流程就是复用原来的逻辑,balance模块寻找合适的节点分配该shard,然后走recover from snapshot。
public class SearchableSnapshotAllocator implements ExistingShardsAllocator {
public static final String ALLOCATOR_NAME = "searchable_snapshot_allocator";
......
@Override
public void allocateUnassigned(
ShardRouting shardRouting,
RoutingAllocation allocation,
UnassignedAllocationHandler unassignedAllocationHandler
) {
if (shardRouting.primary()
&& (shardRouting.recoverySource().getType() == RecoverySource.Type.EXISTING_STORE
|| shardRouting.recoverySource().getType() == RecoverySource.Type.EMPTY_STORE)) {
// we always force snapshot recovery source to use the snapshot-based recovery process on the node
......
shardRouting = unassignedAllocationHandler.updateUnassigned(
shardRouting.unassignedInfo(),
new RecoverySource.SnapshotRecoverySource(
RecoverySource.SnapshotRecoverySource.NO_API_RESTORE_UUID,
snapshot,
Version.CURRENT,
indexId
),
allocation.changes()
);
}
final AllocateUnassignedDecision allocateUnassignedDecision = decideAllocation(allocation, shardRouting);
if (allocateUnassignedDecision.isDecisionTaken() && allocateUnassignedDecision.getAllocationDecision() != AllocationDecision.YES) {
unassignedAllocationHandler.removeAndIgnore(allocateUnassignedDecision.getAllocationStatus(), allocation.changes());
}
}
}
读snapshot原理
index.store.type设置为snapshot,这样会去加载SearchableSnapshotDirectory。
SearchableSnapshotDirectory实现了Directory的方法,其中跟写相关的接口都是直接返unsupportedException。
更多细节
es在文档中只列出了mount接口,在介绍文章中也只是说到了只用加载一个副本,以达到50%的成本节约。但是在查看es源码时,里面还有更多的细节。
searchable snapshots还支持stats接口和clear cache接口。
stats接口是获取一些searchable snapshots索引的指标信息。
从clear cache接口中,发现了searchable snapshots可以支持更加强大的功能。
调用clear cache接口,可以清楚snapshot索引在节点上的数据,在下次search再动态从snapshot中加载。
searchable snapshots最终应该是期望做成snapshot数据不一定要一直停留在节点上,可以支持按需加载与LRU。现在clear cache接口可以直接清掉节点上的shard数据,然后在search索引时,再动态加载数据。
这个特性从这个功能从issue上的名称“Lazy snapshot restores”可以看出来,他们是想做延迟加载snapshot,最后取名为“searchable snapshots”
资料
https://www.elastic.co/cn/blog/introducing-elasticsearch-searchable-snapshots