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