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 ...
随机推荐
- Myeclipse 10 Maven 构建 Java Web 项目
主要介绍如何使用 Myeclipse 10 构建 Maven Web 项目,关于 Maven 的介绍就略过了. 工具/原料 myeclipse apache-maven-3.1.0 方法/步骤 1 下 ...
- sgu 326(经典网络流构图)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13349 题目大意:有N个球队在同一个赛区,已知他们胜利的场数,还剩 ...
- ios模拟器键盘不弹出
本文转载至 http://blog.csdn.net/cerastes/article/details/39547967 xcode6键盘iPhone-Portrait-Numb不弹出Cant fin ...
- weX5如何绑定KO对象
define(function(require){ var $ = require("jquery"); var justep = require("$UI/system ...
- JS-Zepto.js中文链接
附上zepto.js的中文链接:http://www.css88.com/doc/zeptojs_api/ 小伙伴再也不用担心“这特么到底啥意思!”
- Fiddler实现手机抓包——小白入门 - 做一个不动声色的大人
手机用fiddler抓包 电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤了. 一.对PC(笔记本)参数进行配置 1. 配置fiddler允许监听到https(fiddle ...
- 【BZOJ3416】Poi2013 Take-out 栈
[BZOJ3416]Poi2013 Take-out Description 小F喜欢玩一个消除游戏——take-out 保证k+1|n,保证输入数据有解这是一个单人游戏 游戏者的目标是消除初始时给定 ...
- 【BZOJ2553】[BeiJing2011]禁忌 AC自动机+期望DP+矩阵乘法
[BZOJ2553][BeiJing2011]禁忌 Description Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平. ...
- Arduino开发版学习计划--直流电机
代码来源:http://www.cnblogs.com/starsnow/p/4579547.html // --------------------------------------------- ...
- java中Object转换成int或String类型方法
转载: http://www.cnblogs.com/1020182600HENG/p/6137206.html Object obj = getObject(); if(obj instanceof ...