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 ...
随机推荐
- python3----scrapy(笔记)
import scrapy import sys # import io # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb ...
- iOS开发之--TableViewCell重用机制避免重复显示问题
常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复 // 这样配置的话超过页面显示的内容会重复出现 - (UITableViewCell *)tableView:(U ...
- Hadoop1.2.1 出现Warning: $HADOOP_HOME is deprecated.的解决方案
通过启动或停止hadoop我们会发现会出现 “Warning: $HADOOP_HOME is deprecated” 这样一个警告,下面给出解决方案: 不过我们一般推荐第二种,因为我们还是需要$HA ...
- SensorManager
光照传感器 Android 中每个传感器的用法其实都比较类似,真的可以说是一通百通了.首先第一步要获取到 SensorManager 的实例 SensorManager senserManager = ...
- public, protected, private,internal,protected internal的区别
虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal ...
- 电力项目十三--js添加浮动框
修改page/menu/loading.jsp页面 首先,页面中引入浮动窗样式css <!-- 浮动窗口样式css begin --> <style type="text/ ...
- PHP 开发环境的搭建和使用02--整合让apache处理php
PHP5.3.5直接下载解压即可.但是怎样才能让apache处理php呢? 1/ 在apache 的conf目录下 的 httpd.conf(用于指定apache的设置)加入如下代码: Load ...
- JDK的图文安装教程
JDK的安装 什么是JDK? JDK就是Java开发工具包,即Java Development Kit.就是做Java开发所需要的最基本的工具.包括Java编译器(把人使用的Java语言变成JVM能运 ...
- hdu2254 奥运 矩阵的应用
hdu2254 奥运 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2254 题意:题目让我们求得是的可以得到的金牌数量,而和金牌数量=在t1到t2天( ...
- 网络编程 - 1.简单的套接字通信/2.加上通信循环/3.bug修复/4.加上链接循环/5.模拟ssh远程执行命令
1.简单的套接字通信 服务端 ''' 服务端 接电话 客户端 打电话 1.先启动服务端 2.服务端有两种套接字 1.phone 用来干接收链接的 2.conn 用来干收发消息的 ''' import ...