Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation of elasticsearch source code index action?

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "what is the implementation of elasticsearch source index action", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the implementation of elasticsearch source index action?"

The role of action

The previous article analyzed the structure of action, this article will take index action as an example to analyze the implementation of action.

Summarize the role of action again: for each function (such as index) action will include two basic classes * action (IndexAction) and Transport*action (TransportIndexAction). There will be an instance in the former class (IndexAction INSTANCE = new IndexAction ()). This instance is used for the TransportAction (registerAction (IndexAction.INSTANCE, TransportIndexAction.class)) corresponding to the client binding, and the binding process is sent in ActionModuel.

In addition, an action name (String NAME = "indices:data/write/index") is defined in the Action class for the handle of the TransportService binding, which is used to process the information received by the NettyTransport. TransportAction is the final logical processor. When a request is received, it will first determine whether the node can handle it. If it can, call the relevant method to get the result returned, otherwise the request will be forwarded to the corresponding node for processing through NettyTransport. All Transport structures are of this type.

Class diagram of TransportAction

First take a look at the class diagram of TransportAction, from which all the Transport*action is inherited.

It is mainly implemented by two methods, the execute method and the doExecute,execute method. The first implementation needs to add its own actionListener. The final logic is in the doExecute method, which is implemented in each functional module. The following is the inheritance relationship of TransportIndexAction:

In implementation, due to functional partition, TransportIndexAction inherits directly from TranspShardReplicationOperationAction. The method in this abstract class is the parent of all functional action that needs to operate copies of shard, so its implementation also includes functional action such as delete,bulk. It implements a number of inner classes, which are used to assist in the completion of related functions. Here we mainly talk about three subcategories: OperationTransportHandler,ReplicaOperationTransportHandler and AsyncShardOperationAction.

The code for OperationTransportHandler

As follows:

Class OperationTransportHandler extends BaseTransportRequestHandler {/ / inherits from BaseTransportRequestHanlder... Override public void messageReceived (final Request request, final TransportChannel channel) throws Exception {/ / no need to have a threaded listener since we just send back a response request.listenerThreaded (false); / / if we have a local operation, execute it on a thread since we don't spawn request.operationThreaded (true) / / call the execute method of Transport, and return the result execute (request, new ActionListener () {@ Override public void onResponse (Response result) {try {channel.sendResponse (result);} catch (Throwable e) {onFailure (e) via channel) } @ Override public void onFailure (Throwable e) {try {channel.sendResponse (e);} catch (Throwable E1) {logger.warn ("Failed to send response for" + actionName, E1) });}

Students who have seen the sending and processing of NettyTransport requests must be familiar with this code, which is a typical mode of processing information between elasticsearch nodes. When the request is sent to this node through NettyTransport, the corresponding action is found according to the requested handler name, and the corresponding handler is used to process the request. This handler corresponds to "indices:data/write/index", and you can see that it calls the execute method to handle it. Its registration is done in the TransportShardReplicationOperationAction constructor.

If you know OperationTransportHandler,ReplicaOperationTransportHandler, you can understand that it is implemented in exactly the same way as the former, with a "[r]" added to the corresponding action name, which is used to handle the operations that need to be performed on the copy, as shown in the following code:

Class ReplicaOperationTransportHandler extends BaseTransportRequestHandler {... @ Override public void messageReceived (final ReplicaOperationRequest request, final TransportChannel channel) throws Exception {try {shardOperationOnReplica (request);} catch (Throwable t) {failReplicaIfNeeded (request.shardId.getIndex (), request.shardId.id (), t); throw t;} channel.sendResponse (TransportResponse.Empty.INSTANCE);}}

You can see that the code structure is very similar, except that the copy operation method shardOperationOnReplica is called, which is abstract in this TransportShardReplicationOperationAction, and its implementation implements how to handle delete requests on the copy in various subclasses, such as deleteaction.

After analyzing these two handle, do you have some idea about the processing process of action? But this is the tip of the iceberg. These two Handler are used to receive requests from other nodes. What if the request happens to be this node? This logic is all in the AsyncShardOperationAction class. First, take a look at its internal structure:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 292

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report