ElasticSearch recovery过程源码分析
[ES版本]
5.5.0
[分析过程]
找到Recovery有6种状态
public class RecoveryState implements ToXContent, Streamable {
public enum Stage {
//初始化状态
INIT((byte) 0),
/**
* recovery of lucene files, either reusing local ones are copying new ones
*/
//从lucene或本地文件复制新文件
INDEX((byte) 1),
/**
* potentially running check index
*/
//检查确认索引
VERIFY_INDEX((byte) 2),
/**
* starting up the engine, replaying the translog
*/
//启动引擎重放translog
TRANSLOG((byte) 3),
/**
* performing final task after all translog ops have been done
*/
//translog结束后执行最终任务
FINALIZE((byte) 4),
//完成状态
DONE((byte) 5);
...
}
...
}
shard有4种状态
public enum ShardRoutingState {
/**
* The shard is not assigned to any node.
*/
//分片未分配
UNASSIGNED((byte) 1),
/**
* The shard is initializing (probably recovering from either a peer shard
* or gateway).
*/
//分片正在初始化(可能正在从peer shard或者gateway进行恢复)
INITIALIZING((byte) 2),
/**
* The shard is started.
*/
//分片已经启动
STARTED((byte) 3),
/**
* The shard is in the process being relocated.
*/
//分片正在迁移
RELOCATING((byte) 4);
...
}
找到一处调用位置:PeerRecoverySourceService
/**
* The source recovery accepts recovery requests from other peer shards and start the recovery process from this
* source shard to the target shard.
*/
public class PeerRecoverySourceService extends AbstractComponent implements IndexEventListener { public static class Actions {
public static final String START_RECOVERY = "internal:index/shard/recovery/start_recovery";
} private final TransportService transportService;
private final IndicesService indicesService;
private final RecoverySettings recoverySettings; private final ClusterService clusterService; private final OngoingRecoveries ongoingRecoveries = new OngoingRecoveries(); @Inject
public PeerRecoverySourceService(Settings settings, TransportService transportService, IndicesService indicesService,
RecoverySettings recoverySettings, ClusterService clusterService) {
super(settings);
this.transportService = transportService;
this.indicesService = indicesService;
this.clusterService = clusterService;
this.recoverySettings = recoverySettings;
transportService.registerRequestHandler(Actions.START_RECOVERY, StartRecoveryRequest::new, ThreadPool.Names.GENERIC, new StartRecoveryTransportRequestHandler());
} //在分片关闭前,要把所有正在recovery的动作中止掉
@Override
public void beforeIndexShardClosed(ShardId shardId, @Nullable IndexShard indexShard,
Settings indexSettings) {
if (indexShard != null) {
ongoingRecoveries.cancel(indexShard, "shard is closed");
}
} private RecoveryResponse recover(StartRecoveryRequest request) throws IOException {
final IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
final IndexShard shard = indexService.getShard(request.shardId().id()); // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
// the index operations will not be routed to it properly
//先判断目的节点是否正常存在集群中
RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().getId());
//如果不在集群,则推迟进行recovery
if (node == null) {
logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
} ShardRouting routingEntry = shard.routingEntry();
//是主分片并且当前分片非迁移状态
// 或者
//是主分片且处于迁移状态,但是目标节点与正在迁移的目标节点不一致
if (request.isPrimaryRelocation() && (routingEntry.relocating() == false || routingEntry.relocatingNodeId().equals(request.targetNode().getId()) == false)) {
logger.debug("delaying recovery of {} as source shard is not marked yet as relocating to {}", request.shardId(), request.targetNode());
throw new DelayRecoveryException("source shard is not marked yet as relocating to [" + request.targetNode() + "]");
} ShardRouting targetShardRouting = node.getByShardId(request.shardId());
//节点上未获取到目标分片
if (targetShardRouting == null) {
logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
}
//目标分片非初始化状态
if (!targetShardRouting.initializing()) {
logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
request.shardId(), request.targetNode(), targetShardRouting.state());
throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
} //请求中未携带分配的ID,需要重新构造请求
if (request.targetAllocationId() == null) {
// ES versions < 5.4.0 do not send targetAllocationId as part of recovery request, just assume that we have the correct id
request = new StartRecoveryRequest(request.shardId(), targetShardRouting.allocationId().getId(), request.sourceNode(),
request.targetNode(), request.metadataSnapshot(), request.isPrimaryRelocation(), request.recoveryId());
}
`
//请求中携带的目标分配ID与分片的分配唯一标识ID不一致
if (request.targetAllocationId().equals(targetShardRouting.allocationId().getId()) == false) {
logger.debug("delaying recovery of {} due to target allocation id mismatch (expected: [{}], but was: [{}])",
request.shardId(), request.targetAllocationId(), targetShardRouting.allocationId().getId());
throw new DelayRecoveryException("source node has the state of the target shard to have allocation id [" +
targetShardRouting.allocationId().getId() + "], expecting to be [" + request.targetAllocationId() + "]");
} //往正在recovery的列表中增加一个新的recovery
RecoverySourceHandler handler = ongoingRecoveries.addNewRecovery(request, shard);
logger.trace("[{}][{}] starting recovery to {}", request.shardId().getIndex().getName(), request.shardId().id(), request.targetNode());
try {
return handler.recoverToTarget();
} finally {
ongoingRecoveries.remove(shard, handler);
}
}
...
}
StartRecoveryTransportRequestHandler中的messageReceived负责从transport channel接收启动recovery的请求并执行recover操作。
<未完待续>
ElasticSearch recovery过程源码分析的更多相关文章
- 转:InnoDB Crash Recovery 流程源码实现分析
此文章转载给登博的文章,给大家分享 InnoDB Crash Recovery 流程源码实现分析 Crash Recovery问题 本文主要分析了InnoDB整个crash recovery的源码处理 ...
- ElasticSearch Index操作源码分析
ElasticSearch Index操作源码分析 本文记录ElasticSearch创建索引执行源码流程.从执行流程角度看一下创建索引会涉及到哪些服务(比如AllocationService.Mas ...
- [Android]从Launcher开始启动App流程源码分析
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5017056.html 从Launcher开始启动App流程源码 ...
- [Android]Android系统启动流程源码分析
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5013863.html Android系统启动流程源码分析 首先 ...
- Android系统默认Home应用程序(Launcher)的启动过程源码分析
在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还须要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应 ...
- Android Content Provider的启动过程源码分析
本文參考Android应用程序组件Content Provider的启动过程源码分析http://blog.csdn.net/luoshengyang/article/details/6963418和 ...
- Android应用程序绑定服务(bindService)的过程源码分析
Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...
- Spring加载流程源码分析03【refresh】
前面两篇文章分析了super(this)和setConfigLocations(configLocations)的源代码,本文来分析下refresh的源码, Spring加载流程源码分析01[su ...
- 【高速接口-RapidIO】5、Xilinx RapidIO核例子工程源码分析
提示:本文的所有图片如果不清晰,请在浏览器的新建标签中打开或保存到本地打开 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:Vivado2015.4.2 ...
随机推荐
- C++之函数模板
C++之函数模板与模版函数 直接上代码: C++ Code 12345678910111213141516171819202122232425262728293031323334353637383 ...
- 使用CAtlRegExp类进行密码校验
前言 最近做了一个小需求,新建用户时输入的密码必须包含数字.小写字母.大写字符以及特殊字符,目的是为了增强密码的强度,如果没有其中一项,就需要弹出窗口进行提示. 正则表达式 对于此类 ...
- 提高ASP.NET网站性能的方法
http://www.360doc.com/content/14/0705/18/7662927_392224856.shtml Asp.NET有许多秘密,当你了解了这些秘密后,可以使得你的ASP ...
- 工作流JBPM_day01:1-说明_MyProcessDesigner_流程设计器
工作流JBPM_day01:1-说明 先只做请假功能,怎么做? (请假可以和考勤整合到一起) 1,银行(拿号---叫号---办理) 2,餐馆(点菜---上菜---结账) 3,网购(下订单--配送--收 ...
- NUC972 MDK NON-OS
NUC972直接可以在BSP包里模板进行编程,烧录用Nu writer http://www2.keil.com/mdk5/legacy 下载对应的安装包的插件 是直接下载到DDR 里面去运行,所 ...
- 第4章 Vim编辑器与Shell命令脚本
章节简述: 本章节将教给您如何使用Vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库 ,熟练使用各个模式和命令快捷键. 我们可以通过Vim编辑器将Linux命令放入合适的逻辑测试语句(if. ...
- CentOS7.2安装配置FTP服务器VSFTP
1,查看系统版本 2,yum安装vsftpd yum -y install vsftpd 3,修改配置文件 vim /etc/vsftpd/vsftpd.conf local_enable=YES w ...
- Apache Kafka Replication Design – High level
参考,https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Replication Kafka Replication High-level ...
- rabbitmq延迟队列相关
https://blog.csdn.net/qq_26656329/article/details/77891793 --------------rabbitmq queue_decla ...
- Myeclipse下配置struts2和hibernate
最近维护一个项目,是用struts2做的,所以特意学了下struts的入门,否则代码都看不懂啊.下面记录下过程.Myeclipse 版本为2014.struts2,hibernate为4.1. 1.既 ...