elasticsearch索引的創(chuàng)建過(guò)程index?create邏輯分析
索引的創(chuàng)建過(guò)程
從本篇開始,就進(jìn)入了Index的核心代碼部分。這里首先分析一下索引的創(chuàng)建過(guò)程。elasticsearch中的索引是多個(gè)分片的集合,它只是邏輯上的索引,并不具備實(shí)際的索引功能,所有對(duì)數(shù)據(jù)的操作最終還是由每個(gè)分片完成。
創(chuàng)建索引的過(guò)程,從elasticsearch集群上來(lái)說(shuō)就是寫入索引元數(shù)據(jù)的過(guò)程,這一操作只能在master節(jié)點(diǎn)上完成。這是一個(gè)阻塞式動(dòng)作,在加上分配在集群上均衡的過(guò)程也非常耗時(shí),因此在一次創(chuàng)建大量索引的過(guò)程master節(jié)點(diǎn)會(huì)出現(xiàn)單點(diǎn)性能瓶頸,能夠看到響應(yīng)過(guò)程很慢。
在開始具體源碼分析之前,首先回顧一下Action部分的內(nèi)容(參考index action分析),elasticsearch的每一個(gè)功能都對(duì)應(yīng)兩個(gè)Action,*action和Transport*action。*action中定義了每個(gè)功能對(duì)應(yīng)的路徑,同時(shí)Action的instance綁定對(duì)應(yīng)的Transport*Action。所有功能請(qǐng)求都需要在集群上轉(zhuǎn)發(fā),這大概也是每個(gè)功能都有Transport*Action的原因吧。對(duì)于create當(dāng)然也不例外,它的開始點(diǎn)也是TransportCreateAction。另外,在action support分析中分析過(guò),不同的action需要經(jīng)過(guò)和需要操作的節(jié)點(diǎn)也不同。create index只能由master節(jié)點(diǎn)進(jìn)行,而且也只在master節(jié)點(diǎn)上進(jìn)行,保證集群數(shù)據(jù)的一致性。
materOperation方法實(shí)現(xiàn)
因此TransportCreateAction繼承了TransportMasterNodeOperationAction,并實(shí)現(xiàn)了materOperation方法。它的方法如下所示:
protected void masterOperation(final CreateIndexRequest request, final ClusterState state, final ActionListener<CreateIndexResponse> listener) throws ElasticsearchException { String cause = request.cause(); if (cause.length() == 0) { cause = "api"; } final CreateIndexClusterStateUpdateRequest updateRequest = new CreateIndexClusterStateUpdateRequest(request, cause, request.index()) .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()) .settings(request.settings()).mappings(request.mappings()) .aliases(request.aliases()).customs(request.customs()); createIndexService.createIndex(updateRequest, new ActionListener<ClusterStateUpdateResponse>() { @Override public void onResponse(ClusterStateUpdateResponse response) { listener.onResponse(new CreateIndexResponse(response.isAcknowledged())); } @Override public void onFailure(Throwable t) { if (t instanceof IndexAlreadyExistsException) { logger.trace("[{}] failed to create", t, request.index()); } else { logger.debug("[{}] failed to create", t, request.index()); } listener.onFailure(t); } }); }
這里看上很簡(jiǎn)單,只是調(diào)用了createIndexService(它其實(shí)是MetaDataCreateIndexService)的方法,就是修改集群matedata過(guò)程。
clusterservice處理
修改前首先獲取到index名稱對(duì)應(yīng)的lock,這樣保證操作數(shù)據(jù)一致性,然后生成updatetask,交給clusterservice處理。代碼如下所示:
public void createIndex(final CreateIndexClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) { // 獲取鎖,只對(duì)該索引的操作加鎖,而不是整個(gè)cluster final Semaphore mdLock = metaDataService.indexMetaDataLock(request.index()); // 如果能夠獲取鎖離開創(chuàng)建索引,否則在下面啟動(dòng)新的線程進(jìn)行 if (mdLock.tryAcquire()) { createIndex(request, listener, mdLock); return; } threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(new ActionRunnable(listener) { @Override public void doRun() throws InterruptedException { if (!mdLock.tryAcquire(request.masterNodeTimeout().nanos(), TimeUnit.NANOSECONDS)) { listener.onFailure(new ProcessClusterEventTimeoutException(request.masterNodeTimeout(), "acquire index lock")); return; } createIndex(request, listener, mdLock); } }); }
createIndex方法,會(huì)封裝create請(qǐng)求,然后向cluster發(fā)送一個(gè)updatetask。代碼如下所示:
private void createIndex(final CreateIndexClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener, final Semaphore mdLock) { ImmutableSettings.Builder updatedSettingsBuilder = ImmutableSettings.settingsBuilder(); updatedSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX); request.settings(updatedSettingsBuilder.build()); clusterService.submitStateUpdateTask("create-index [" + request.index() + "], cause [" + request.cause() + "]", Priority.URGENT, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, listener)
建立索引 修改配置
增加或者修改mapping都是對(duì)集群狀態(tài)修改,它們的過(guò)程都很相似,都是通過(guò)clusterService提交一個(gè)更新操作,同時(shí)附帶有優(yōu)先級(jí)。clusterservice會(huì)根據(jù)優(yōu)先級(jí)和更新狀態(tài)task的類型來(lái)進(jìn)行對(duì)應(yīng)的操作。如下所示:
public void submitStateUpdateTask(final String source, Priority priority, final ClusterStateUpdateTask updateTask) { if (!lifecycle.started()) { return; } try { final UpdateTask task = new UpdateTask(source, priority, updateTask);//根據(jù)優(yōu)先級(jí)新建不同的task if (updateTask instanceof TimeoutClusterStateUpdateTask) {//超時(shí)任務(wù),這類任務(wù)需要即時(shí)返回,因此立刻執(zhí)行。 final TimeoutClusterStateUpdateTask timeoutUpdateTask = (TimeoutClusterStateUpdateTask) updateTask; updateTasksExecutor.execute(task, threadPool.scheduler(), timeoutUpdateTask.timeout(), new Runnable() { @Override public void run() { threadPool.generic().execute(new Runnable() { @Override public void run() { timeoutUpdateTask.onFailure(task.source(), new ProcessClusterEventTimeoutException(timeoutUpdateTask.timeout(), task.source())); } }); } }); } else {//其它類型,可以延遲執(zhí)行,則交給線程池來(lái)執(zhí)行。 updateTasksExecutor.execute(task); } } catch (EsRejectedExecutionException e) { // ignore cases where we are shutting down..., there is really nothing interesting // to be done here... if (!lifecycle.stoppedOrClosed()) { throw e; } } }
說(shuō)完它們的執(zhí)行過(guò)程,再來(lái)看一下create index的具體邏輯。這個(gè)邏輯在matedataservice所提交的AckedClusterStateUpdateTask中的execute方法中??傮w來(lái)說(shuō),這一過(guò)程就是將request中關(guān)于索引的配置mapping等取出來(lái)加入到當(dāng)前的clustermatedata中,構(gòu)造一個(gè)新的matedata的過(guò)程。這一過(guò)程還是比較復(fù)雜,限于篇幅將在下次中進(jìn)行分析。
總結(jié)
創(chuàng)建索引的過(guò)程就是master節(jié)點(diǎn)更新集群matedata的過(guò)程,為了保證數(shù)據(jù)一致性,需要獲取鎖。
因此存在單點(diǎn)瓶頸。對(duì)于外部調(diào)用來(lái)說(shuō),跟其它功能一樣,外部接口調(diào)用CreateIndexAction的相關(guān)方法,然后通過(guò)TransPortCreateIndexAction講請(qǐng)求發(fā)送到集群上,進(jìn)行索引創(chuàng)建。
以上就是elasticsearch索引創(chuàng)建過(guò)程index create的詳細(xì)內(nèi)容,更多關(guān)于elasticsearch索引創(chuàng)建過(guò)程index create的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS獲取AppIcon and LaunchImage''s name(app圖標(biāo)和啟動(dòng)圖片名字)
這篇文章主要介紹了iOS獲取AppIcon and LaunchImage's name(app圖標(biāo)和啟動(dòng)圖片名字)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08關(guān)于log4j日志擴(kuò)展---自定義PatternLayout
這篇文章主要介紹了關(guān)于log4j日志擴(kuò)展---自定義PatternLayout,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12關(guān)于Hystrix的監(jiān)控及可視化面板
這篇文章主要介紹了關(guān)于Hystrix的監(jiān)控及可視化面板,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08SpringBoot多環(huán)境切換的配置實(shí)現(xiàn)
在日常的開發(fā)中,一般都會(huì)分好幾種環(huán)境,本文就來(lái)介紹一下SpringBoot多環(huán)境切換的配置實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Java實(shí)現(xiàn)經(jīng)典游戲黃金礦工的示例代碼
《黃金礦工》游戲是一個(gè)經(jīng)典的抓金子小游戲,它可以鍛煉人的反應(yīng)能力。本文將用Java實(shí)現(xiàn)這一經(jīng)典的游戲,感興趣的小伙伴可以了解一下2022-02-02