溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ES源碼學習之--Get API的實現(xiàn)邏輯

發(fā)布時間:2020-06-18 10:55:22 來源:網絡 閱讀:19613 作者:sbp810050504 欄目:大數據

Github上es項目講述其易用性時,用來舉例說明ES開箱即用的特性,用的就是Get API。片段摘取如下:

-- 添加文檔
curl -XPUT 'http://localhost:9200/twitter/doc/1?pretty' -H 'Content-Type: application/json' -d '
{
    "user": "kimchy",
    "post_date": "2009-11-15×××3:12:00",
    "message": "Trying out Elasticsearch, so far so good?"
}'

-- 讀取文檔
curl -XGET 'http://localhost:9200/twitter/doc/1?pretty=true'

Get API通常的用途有2點:
1 檢測添加的文檔跟預期是否相符, 這在問題排查時超級實用。

2 根據id獲取整個文檔明細, 用于搜索的fetch階段。

研究ES的內部機制, Get API是一個極佳的切入點。通過Get API, 可以了解到的知識點有:

a. ES的rest api實現(xiàn)方式。

b. ES的文檔路由方式。

c. ES的RPC實現(xiàn)機制。

d. ES的translog.

e. ES如何使用lucene 的IndexSearcher。

f. ES如何根據id獲取到lucene的doc_id。

g. ES如何根據lucene的doc_id 獲取文檔明細。

.......

研究ES的內部機制,有助于釋放ES的洪荒之力。例如:根據業(yè)務開發(fā)ES的plugin時,其內部流程是很好的借鑒。 內部細節(jié)了解越多,越不容易踩坑。

GET API的核心流程如下:

s1: 接收客戶端請求

看到controller.registerHandler()方法,很容易就聯(lián)想到http的請求

public class RestGetAction extends BaseRestHandler {

     @Inject
    public RestGetAction(Settings settings, RestController controller, Client client) {
        super(settings, controller, client);
        controller.registerHandler(GET, "/{index}/{type}/{id}", this);
    } 

    @Override
    public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
           ...
        client.get(getRequest, new RestBuilderListener<GetResponse>(channel) {
            ...
        });
    }
}

s2: 在當前節(jié)點執(zhí)行該請求

public class NodeClient extends AbstractClient {
    ...
    @Override
    public <Request extends ActionRequest, Response extends ActionResponse, 
            RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> 
       void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
        TransportAction<Request, Response> transportAction = actions.get(action);
        ...
        transportAction.execute(request, listener);
    }
}

這里隱含了一個actions的映射表, 如下:
public class ActionModule extends AbstractModule {
    ...

    @Override
    protected void configure() {
        ...
        registerAction(GetAction.INSTANCE, TransportGetAction.class);
        ...
    }
}

s3: 定位文檔所在分片

文檔的定位思路很簡單, 默認根據文檔id, 用hash函數計算出文檔的分片ShardId, 通過分片ShardId定位出NodeId。 
ES內部維護了一張類似路由表的對象,類名就是RoutingTable. 通過RoutingTable, 可以根據索引名稱找到所有的分片;可以通過分片Id找到分片對應的集群Node. 
關于文檔的定位,從應用的角度有兩個知識點:routing和preference

public class TransportGetAction extends TransportSingleShardAction<GetRequest, GetResponse> {

    ...

    @Override
    protected ShardIterator shards(ClusterState state, InternalRequest request) {
        return clusterService.operationRouting()
                .getShards(clusterService.state(), request.concreteIndex(), request.request().type(), request.request().id(), request.request().routing(), request.request().preference());
    }
}

s4: 將請求轉發(fā)到分片所在的節(jié)點

請求的分發(fā),涉及到ES的RPC通信。上一步定位到NodeId, 將請求發(fā)送到該NodeId即可。
由于ES的每個Node代碼都是一樣的, 因此每個Node既承擔Server也承擔Client的責任,這跟其他的RPC框架有所不同。
核心方法是transportService.sendRequest() 和 messageReceived()。 

public abstract class TransportSingleShardAction<Request extends SingleShardRequest, Response extends ActionResponse> extends TransportAction<Request, Response> {

    class AsyncSingleAction {

        public void start() {
                transportService.sendRequest(clusterService.localNode(), transportShardAction, internalRequest.request(), new BaseTransportResponseHandler<Response>() {
                    ...     
                });
        }

    }

    private class ShardTransportHandler extends TransportRequestHandler<Request> {

        @Override
        public void messageReceived(final Request request, final TransportChannel channel) throws Exception {

            ...
            Response response = shardOperation(request, request.internalShardId);
            channel.sendResponse(response);
        }
    }

}

s5: 通過id讀取索引文件獲取該id對應的文檔信息


這里分兩個階段:
step1: 將type和id合并成一個字段,從lucene的倒排索引中定位lucene的doc_id

step2: 根據doc_id從正向信息中獲取明細。

public final class ShardGetService extends AbstractIndexShardComponent {

      ...

    private GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) {
        fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, gFields);
                ...
                get = indexShard.get(new Engine.Get(realtime, new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(typeX, id)))
                        .version(version).versionType(versionType));

                ...
               innerGetLoadFromStoredFields(type, id, gFields, fetchSourceContext, get, docMapper, ignoreErrorsOnGeneratedFields); 
        }
    }

(注: 如果是realtime=true, 則先從translog中讀取source, 沒有讀取到才從索引中讀取)

s5涉及到Lucene的內部實現(xiàn), 這里不展開贅述。

最后總結一下:

Get API是ES內部打通了整個流程的功能點。從功能上看,它足夠簡單;從實現(xiàn)上看,他又串聯(lián)了ES的主流程,以它為切入口,不會像展示You Know, for SearchRestMainAction那樣浮于表面;又不會像實現(xiàn)搜索的接口那樣龐雜難懂。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI