elasticsearch索引的創(chuàng)建過(guò)程index?create邏輯分析
索引的創(chuàng)建過(guò)程
從本篇開(kāi)始,就進(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ò)程很慢。
在開(kāi)始具體源碼分析之前,首先回顧一下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)然也不例外,它的開(kāi)始點(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());
// 如果能夠獲取鎖離開(kāi)創(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īng)顟B(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)文章
Servlet開(kāi)發(fā)JavaWeb工程示例詳解
這篇文章主要介紹了Servlet開(kāi)發(fā)JavaWeb工程示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Eclipse使用maven搭建spring mvc圖文教程
這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下2016-05-05
Java web Hibernate如何與數(shù)據(jù)庫(kù)鏈接
這篇文章主要介紹了Java web Hibernate如何與數(shù)據(jù)庫(kù)鏈接,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
解決IDEA2020 創(chuàng)建maven項(xiàng)目沒(méi)有src/main/java目錄和webapp目錄問(wèn)題
這篇文章主要介紹了IDEA2020 創(chuàng)建maven項(xiàng)目沒(méi)有src/main/java目錄和webapp目錄問(wèn)題解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java中的ReentrantReadWriteLock實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java中的ReentrantReadWriteLock實(shí)現(xiàn)原理詳解,讀寫鎖實(shí)現(xiàn)了接口ReadWriteLock,適合于讀多寫少的情況,支持公平鎖和非公平鎖,支持可沖入(進(jìn)入讀鎖后可再進(jìn)入讀鎖,進(jìn)入寫鎖后可再進(jìn)入寫鎖和讀鎖),需要的朋友可以參考下2024-01-01
關(guān)于Spring Bean實(shí)例過(guò)程中使用反射和遞歸處理的Bean屬性填充問(wèn)題
本文帶領(lǐng)大家一起學(xué)習(xí)下在Spring Bean實(shí)例過(guò)程中如何使用反射和遞歸處理的Bean屬性填充,需要在類 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加補(bǔ)全屬性方法,具體操作方法跟隨小編一起學(xué)習(xí)下吧2021-06-06
SpringBoot使用OkHttp完成高效網(wǎng)絡(luò)請(qǐng)求詳解
OkHttp 是一個(gè)高效的 HTTP 客戶端,支持同步和異步請(qǐng)求,且具備自動(dòng)處理 cookie、緩存和連接池等高級(jí)功能,下面我們來(lái)看看SpringBoot如何利用 OkHttp 完成高效網(wǎng)絡(luò)請(qǐng)求吧2025-03-03

