elasticsearch分布式及數(shù)據(jù)的功能源碼分析
從功能上說,可以分為兩部分,分布式功能和數(shù)據(jù)功能。分布式功能主要是節(jié)點集群及集群附屬功能如restful借口、集群性能檢測功能等,數(shù)據(jù)功能主要是索引和搜索。代碼上這些功能并不是完全獨立,而是由相互交叉部分。當然分布式功能是為數(shù)據(jù)功能服務,數(shù)據(jù)功能肯定也難以完全獨立于分布式功能。
它的源碼有以下幾個特點:
模塊化:
每個功能都以模塊化的方式實現(xiàn),最后以一個借口向外暴露,最終通過guice(google輕量級DI框架)進行管理。整個系統(tǒng)有30多個模塊(version1.5)。
接口解耦:
es代碼中使用了大量的接口進行代碼解耦,剛開始看的感覺是非常難以找到相關功能的實現(xiàn),但是也正是這些接口使得代碼實現(xiàn)的非常優(yōu)雅。
異步通信:
作為一個高效的分布式系統(tǒng),es中異步通信實現(xiàn)非常之多,從集群通信到搜索功能,使用了異步通信框架netty作為節(jié)點間的通信框架。
以上的這些特點在后面的代碼分析中會一一體現(xiàn)。概述的結(jié)尾以es的啟動過程來結(jié)束,es的啟動類是Bootstrap,啟動腳本調(diào)研這個類的main方法開始啟動node。它的類圖如下所示:

上圖僅僅顯示了它的field,其中node是要啟動的節(jié)點。keepAliveThread線程保證節(jié)點運行期間Bootstrap會一直存在,可以接收關機命令進行從而優(yōu)雅關閉。下面是啟動前的屬性設置,代碼如下:
private void setup(boolean addShutdownHook, Tuple<Settings, Environment> tuple) throws Exception {
if (tuple.v1().getAsBoolean("bootstrap.mlockall", false)) {//嘗試鎖定內(nèi)存
Natives.tryMlockall();
}
tuple = setupJmx(tuple);
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(tuple.v1()).loadConfigSettings(false);
node = nodeBuilder.build();//初始化node
if (addShutdownHook) {//添加關閉node的hook
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
node.close();
}
});
}
}嘗試鎖定內(nèi)存左右是保證節(jié)點運行期間的內(nèi)存不變動,以防因為內(nèi)存變得帶來性能上的波動,這里調(diào)用的是c方法。最后來看一下main方法:
public static void main(String[] args) {
....
String stage = "Initialization";//標明啟動階段用于構(gòu)造錯誤信息。
try {
if (!foreground) {
Loggers.disableConsoleLogging();
System.out.close();
}
bootstrap.setup(true, tuple);
stage = "Startup";
bootstrap.start();//bootstrap的啟動過程也就是node的啟動過程
if (!foreground) {
System.err.close();
}
//構(gòu)造一個線程,保證bootstrap不退出,仍然可以接收命令。
keepAliveLatch = new CountDownLatch(1);
// keep this thread alive (non daemon thread) until we shutdown/
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
keepAliveLatch.countDown();
}
});
keepAliveThread = new Thread(new Runnable() {
@Override
public void run() {
try {
keepAliveLatch.await();
} catch (InterruptedException e) {
// bail out
}
}
}, "elasticsearch[keepAlive/" + Version.CURRENT + "]");
keepAliveThread.setDaemon(false);
keepAliveThread.start();
} catch (Throwable e) {
ESLogger logger = Loggers.getLogger(Bootstrap.class);
if (bootstrap.node != null) {
logger = Loggers.getLogger(Bootstrap.class, bootstrap.node.settings().get("name"));
}
String errorMessage = buildErrorMessage(stage, e);
if (foreground) {
System.err.println(errorMessage);
System.err.flush();
} else {
logger.error(errorMessage);
}
Loggers.disableConsoleLogging();
if (logger.isDebugEnabled()) {
logger.debug("Exception", e);
}
System.exit(3);
}main函數(shù)有省略,這里start函數(shù)調(diào)用node的start函數(shù),node的start函數(shù)中將各個模塊加載啟動,從而啟動整個系統(tǒng)。這一過程將在接下來進行分析。node啟動后會注入hook,同時啟動keepAliveThread,至此整個node就啟動起來。
以上就是elasticsearch分布式及數(shù)據(jù)功能源碼分析的詳細內(nèi)容,更多關于elasticsearch分布式及數(shù)據(jù)功能的資料請關注腳本之家其它相關文章!
相關文章
Java的基本數(shù)據(jù)類型和運算方法(必看篇)
下面小編就為大家?guī)硪黄狫ava的基本數(shù)據(jù)類型和運算方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
SpringCloud使用Kafka Streams實現(xiàn)實時數(shù)據(jù)處理
使用Kafka Streams在Spring Cloud中實現(xiàn)實時數(shù)據(jù)處理可以幫助我們構(gòu)建可擴展、高性能的實時數(shù)據(jù)處理應用,Kafka Streams是一個基于Kafka的流處理庫,本文介紹了如何在SpringCloud中使用Kafka Streams實現(xiàn)實時數(shù)據(jù)處理,需要的朋友可以參考下2024-07-07
Java ArrayList如何實現(xiàn)生成不重復隨機數(shù)
這篇文章主要介紹了Java ArrayList如何實現(xiàn)生成不重復隨機數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09

