【Elasticsearch源码】写入源码分析(三)

本文深入分析了Elasticsearch的主分片节点写入流程,从TransportReplicationAction.PrimaryOperationTransportHandler的messageReceived方法开始,详细阐述了请求检查、主分片迁移判断、主分片操作、写入Lucene和Translog的过程,以及后续如何转发请求到副本分片。主分片写入的关键在于先写Lucene后写Translog,以确保数据的完整性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上一篇:【Elasticsearch源码】写入源码分析(二)

3.2.5 写主分片节点流程

代码入口:TransportReplicationAction.PrimaryOperationTransportHandler#messageReceived,然后进入AsyncPrimaryAction#doRun方法。
检查请求: 1.当前是否为主分片;2.allocationId是否是预期值;3.PrimaryTerm是否是预期值

            if (shardRouting.primary() == false) {
   
                .....
            }
            final String actualAllocationId = shardRouting.allocationId().getId();
            if (actualAllocationId.equals(targetAllocationID) == false) {
   
                ......
            }
            final long actualTerm = indexShard.getPendingPrimaryTerm();
            if (actualTerm != primaryTerm) {
   
              	......
            }

查看主分片是否迁移:
如果已经迁移:1.将phase状态设为“primary_delegation”;2.关闭当前分片的primaryShardReference,及时释放资源;3.获取已经迁移到的目标节点,将请求转发到该节点,并等待执行结果;4.拿到结果后,将task状态更新为“finish”。

                    transportService.sendRequest(relocatingNode, transportPrimaryAction,
                        new ConcreteShardRequest<>(request, primary.allocationId().getRelocationId(), primaryTerm),
                        transportOptions,
                        new TransportChannelResponseHandler<Response>(logger, channel, "rerouting indexing to target primary " + primary,
                            reader) {
   
                            @Override
                            public void handleResponse(Response response) {
   
                                setPhase(replicationTask, "finished");
                                super.handleResponse(response);
                            }
                            @Override
                            public 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值