在ActionModule模块,主要是注册各种es操作,以TransportAction为接口的一系列实现。
上图有2个非常重要的es action,一个是index索引,另一个是search检索。
IndexAction
取其中的IndexAction
来看具体实现TransportIndexAction
,一步步调试进去。
TransportIndexAction
继承了TransportReplicationAction
,而TransportReplicationAction
实现了TransportAction
的doExecute方法。
所以其调用链如下,中间涉及了transportService的传输request/response等,
Node.modules.add(new ActionModule(false));
ActionModule.configure().registerAction(IndexAction.INSTANCE, ransportIndexAction.class);
-
TransportIndexAction.doExecute().createIndexAction.execute()
,client端的send任务结束,并设置ActionListener监听response TransportReplicationAction.doExecute{new ReroutePhase((ReplicationTask) task, request, listener).run()};
TransportReplicationAction.ReroutePhase.doRun().performAction(node, actionName, false);
TransportReplicationAction.ReroutePhase.performAction().transportService.sendRequest()
TransportService.sendRequest.transport.sendRequest(node, requestId, action, request, options);
NettyTransport.sendRequest()
NettyTransport.doStart().createServerBootstrap().configureServerChannelPipelineFactory()
NettyTransport.ServerChannelPipelineFactory.getPipeline().MessageChannelHandler
MessageChannelHandler.messageReceived().handleRequest()
MessageChannelHandler.handleRequest().reg.processMessageReceived(request, transportChannel);
RequestHandlerRegistry.processMessageReceived().handler.messageReceived(request, channel);
TransportReplicationAction.OperationTransportHandler & PrimaryOperationTransportHandler & ReplicaOperationTransportHandler
-
TransportReplicationAction .PrimaryOperationTransportHandler.messageReceived().PrimaryPhase.doRun().shardOperationOnPrimary() -> TransportIndexAction.shardOperationOnPrimary() -> TransportIndexAction.executeIndexRequestOnPrimary() -> executeIndexRequestOnPrimary().operation.execute(indexShard); -> Engine.Index.execute.shard.index(this); -> IndexShard.index().engine().index(index); -> InternalEngine.index().innerIndex(index); -> InternalEngine.innerIndex().indexWriter.addDocuments(index.docs()); or indexWriter.updateDocuments(index.uid(), index.docs());
。index/update doc至Lucene -
InternalEngine.innerIndex().translog.add(new Translog.Index(index));
doc写入到WAL - 在步骤15的时候,primary与replica是同时被call的,即,
TransportReplicationAction.ReplicaOperationTransportHandler.messageReceived().AsyncReplicaAction.duRun().shardOperationOnReplica() -> TransportIndexAction.shardOperationOnReplica() -> TransportIndexAction.executeIndexRequestOnReplica() -> executeIndexRequestOnReplica().operation.execute(indexShard); -> ...后续流程与Primary的一致
。server端index任务结束,返回response到client - TransportIndexAction.doExecute().onResponse()。client利用
ActionListener
监听之前的createIndexAction.execute()
的运行情况
SearchAction
search的调用方式与上述的index是一致的,本地节点走local,非本地节点走netty,都是通过handler来实现具体类的回调。
Node.modules.add(new ActionModule(false));
registerAction(SearchAction.INSTANCE, TransportSearchAction.class);
-
TransportSearchAction.doExecute().SearchQueryThenFetchAsyncAction.start()
。这里可以是默认Q_T_F或者D_Q_A_F等 AbstractSearchAsyncAction.start().performFirstPhase()
-
AbstractSearchAsyncAction.performFirstPhase().sendExecuteFirstPhase()
。client端的send任务结束,并设置ActionListener监听response SearchQueryThenFetchAsyncAction.sendExecuteFirstPhase().searchService.sendExecuteQuery(node, request, listener);
SearchServiceTransportAction.sendExecuteQuery().transportService.sendRequest()
TransportService.sendRequest().sendRequest(node, action, request, TransportRequestOptions.EMPTY, handler);
TransportService.sendRequest().transport.sendRequest(node, requestId, action, request, options);
NettyTransport.sendRequest()
NettyTransport.doStart().createServerBootstrap().configureServerChannelPipelineFactory()
NettyTransport.ServerChannelPipelineFactory.getPipeline().MessageChannelHandler
MessageChannelHandler.messageReceived().handleRequest()
MessageChannelHandler.handleRequest().reg.processMessageReceived(request, transportChannel);
RequestHandlerRegistry.processMessageReceived().handler.messageReceived(request, channel);
-
SearchQueryQueryFetchTransportHandler.messageReceived().searchService.executeFetchPhase(request);
。首先执行具体的searchService,然后回写response SearchService.executeFetchPhase().queryPhase.execute(context); & fetchPhase.execute(context);
SearchQueryQueryFetchTransportHandler.messageReceived().channel.sendResponse(result);
NettyTransport.sendResponse().sendResponse().
-
NettyTransportChannel.sendResponse().future.addListener(onResponseSentListener);
。回应步骤5的监听
RPC回调
RPC调用有LocalTransport和NettyTransport;回调有RequestHandler、MessageChannelHandler等。理清es关于request和response的异步回调,对于后续的模块化衔接会有帮助。
- client/TransportClient的构造函数注入了TransportProxyClient.execute()方法,接着进入TransportClientNodesService来选择这次request的hash到的node,然后根据action选取对应的实现类TransportActionNodeProxy.execute(),其中execute().transportService.sendRequest()最终会调用NettyTransport.sendRequest()方法,对request进行处理(压缩、版本等),最后发送数据到server端。在Netty中通信的处理类是MessageChannelHandler,其中messageReceived方法用来处理消息,根据不同的状态来调用handleRequest或者handleResponse,即该方法是server端接收请求的入口
- server端通过messageReceived接收到message,解析协议,根据action生成不同request和transportXXXAction,进而执行transportXXXAction.execute(Request request, final ActionListener
listener)。server端处理该message后,将response结果(此结果中包含了前面的requestID)发送给client端
Reference
- elasticsearch源码分析之客户端(三)
- elasticsearch源码分析之服务端(四)
- [源码]Elasticsearch源码2(RPC)