欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

ES源碼學(xué)習(xí)之--GetAPI的實(shí)現(xiàn)邏輯

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

“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。

-- 添加文檔
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點(diǎn):
1 檢測添加的文檔跟預(yù)期是否相符, 這在問題排查時(shí)超級實(shí)用。

2 根據(jù)id獲取整個(gè)文檔明細(xì), 用于搜索的fetch階段。

研究ES的內(nèi)部機(jī)制, Get API是一個(gè)極佳的切入點(diǎn)。通過Get API, 可以了解到的知識點(diǎn)有:

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

b. ES的文檔路由方式。

c. ES的RPC實(shí)現(xiàn)機(jī)制。

d. ES的translog.

e. ES如何使用lucene 的IndexSearcher。

f. ES如何根據(jù)id獲取到lucene的doc_id

g. ES如何根據(jù)lucene的doc_id 獲取文檔明細(xì)。

.......

研究ES的內(nèi)部機(jī)制,有助于釋放ES的洪荒之力。例如:根據(jù)業(yè)務(wù)開發(fā)ES的plugin時(shí),其內(nèi)部流程是很好的借鑒。 內(nèi)部細(xì)節(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: 在當(dāng)前節(jié)點(diǎn)執(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);
    }
}

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

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

s3: 定位文檔所在分片

文檔的定位思路很簡單, 默認(rèn)根據(jù)文檔id, 用hash函數(shù)計(jì)算出文檔的分片ShardId, 通過分片ShardId定位出NodeId。 
ES內(nèi)部維護(hù)了一張類似路由表的對象,類名就是RoutingTable. 通過RoutingTable, 可以根據(jù)索引名稱找到所有的分片;可以通過分片Id找到分片對應(yīng)的集群Node. 
關(guān)于文檔的定位,從應(yīng)用的角度有兩個(gè)知識點(diǎn):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: 將請求轉(zhuǎn)發(fā)到分片所在的節(jié)點(diǎn)

請求的分發(fā),涉及到ES的RPC通信。上一步定位到NodeId, 將請求發(fā)送到該NodeId即可。
由于ES的每個(gè)Node代碼都是一樣的, 因此每個(gè)Node既承擔(dān)Server也承擔(dān)Client的責(zé)任,這跟其他的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對應(yīng)的文檔信息


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

step2: 根據(jù)doc_id從正向信息中獲取明細(xì)。

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的內(nèi)部實(shí)現(xiàn), 這里不展開贅述。

最后總結(jié)一下:

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

網(wǎng)頁標(biāo)題:ES源碼學(xué)習(xí)之--GetAPI的實(shí)現(xiàn)邏輯
標(biāo)題路徑:http://chinadenli.net/article18/pgjigp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)網(wǎng)站排名外貿(mào)網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化搜索引擎優(yōu)化響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)