Elasticsearch2.3.3 java client search源码分析

问题

  1. es集群只配置一个节点,client是否能够自动发现集群中的所有节点?是如何发现的?如下配置了一个节点:


    单个node配置
  2. es client如何做到负载均衡?
  3. 一个es node挂掉之后,es client如何摘掉该节点?
  4. es client node检测分为两种模式,有什么不同?

核心类

  • TransportClient : es client对外API类
  • TransportClientNodesService : 维护node节点的类
  • ScheduledNodeSampler : 定期维护正常节点类
  • NettyTransport : 进行数据传输
  • NodeSampler : 节点嗅探类

Client初始化过程

初始化代码

Settings.Builder builder = Settings.settingsBuilder()                                   
                                   .put("cluster.name", clusterName)
                                   .put("client.transport.sniff", true);
Settings settings = builder.build(); 
TransportClient client = TransportClient.builder().settings(settings).build(); 
for (TransportAddress transportAddress : transportAddresses) {
      client.addTransportAddress(transportAddress);
}
  1. ES 通过builder模式构造了基础的配置参数;
  2. 通过build构造了client,这个时候包括构造client、初始化ThreadPool、构造TransportClientNodesService、启动定时任务、定制化嗅探类型;
  3. 添加集群可用地址,比如我只配了集群中的一个节点;

构建client

调用build API

build code

其中,关于依赖注入的简单说明:Guice 是 Google 用于 Java™ 开发的开放源码依赖项注入框架(感兴趣的可以了解下,这里不做重点讲解),具体可参考如下:

Google Guice Started
Guice 博客1
Guice 博客2

初始化TransportClientNodesService

在上一幅图的modules.createInjectorTransportClientNodesService进行实例化,在TransportClient进行注入,可以看到TransportClient里边的绝大部分API都是通过TransportClientNodesService进行代理的:

TransportClient code

Guice通过注解进行注入

Guice 注解注入code

在上图中:注入了集群名称、线程池等,重点是如下代码:该段代码选择了节点嗅探器的类型 嗅探同一集群中的所有节点SniffNodesSampler或者是只关注配置文件配置的节点SimpleNodeSampler

if (this.settings.getAsBoolean("client.transport.sniff", false)) {
    this.nodesSampler = new SniffNodesSampler();
} else {
    this.nodesSampler = new SimpleNodeSampler();
}

特点
SniffNodesSampler:client会主动发现集群里的其他节点,会创建fully connect(什么叫fully connect?后边说)
SimpleNodeSampler:ping listedNodes中的所有node,区别在于这里创建的都是light connect;
其中TransportClientNodesService维护了三个节点存储数据结构:

// nodes that are added to be discovered
1 private volatileListlistedNodes= Collections.emptyList();
2 private volatileListnodes= Collections.emptyList();
3 private volatileListfilteredNodes= Collections.emptyList();
  1. 代表配置文件中主动加入的节点;
  2. 代表参与请求的节点;
  3. 过滤掉的不能进行请求处理的节点;

Client如何做到负载均衡

负载均衡code

如上图,我们发现每次 execute 的时候,是从 nodes 这个数据结构中获取节点,然后通过简单的 rouund-robbin 获取节点服务器,核心代码如下:

private final AtomicInteger randomNodeGenerator = new AtomicInteger();
......
private int getNodeNumber() {
    int index = randomNodeGenerator.incrementAndGet();  
    if (index < 0) {
        index = 0;
        randomNodeGenerator.set(0);
    }
    return index;
}

然后通过netty的channel将数据写入,核心代码如下:

public void sendRequest(final DiscoveryNode node, final long requestId, final String action, final TransportRequest request, TransportRequestOptions options) 
throws IOException, TransportException {
1 Channel targetChannel = nodeChannel(node, options);     
  if (compress) {        
      options = TransportRequestOptions.builder(options).withCompress(true).build();    
  }    
byte status = 0;    
status = TransportStatus.setRequest(status);    
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);    
boolean addedReleaseListener = false;    
try {        
    bStream.skip(NettyHeader.HEADER_SIZE);        
    StreamOutput stream = bStream;        
    // only compress if asked, and, the request is not bytes, since then only
    // the header part is compressed, and the "body" can't be extracted as compressed      
    if (options.compress() && (!(request instanceof BytesTransportRequest))) {            
        status = TransportStatus.setCompress(status);            
        stream = CompressorFactory.defaultCompressor().streamOutput(stream);       
    }
    // we pick the smallest of the 2, to support both backward and forward compatibility
    // note, this is the only place we need to do this, since from here on, we use the serialized version
    // as the version to use also when the node receiving this request will send the     response with
    Version version = Version.smallest(this.version, node.version());
    stream.setVersion(version);
    stream.writeString(action);
    ReleasablePagedBytesReference bytes;
    ChannelBuffer buffer;
    // it might be nice to somehow generalize this optimization, maybe a smart "paged" bytes output        
    // that create paged channel buffers, but its tricky to know when to do it (where this option is
    // more explicit).      
    if (request instanceof BytesTransportRequest) {
        BytesTransportRequest bRequest = (BytesTransportRequest) request;  
        assert node.version().equals(bRequest.version());
        bRequest.writeThin(stream);
        stream.close();
        bytes = bStream.bytes();
        ChannelBuffer headerBuffer = bytes.toChannelBuffer();
        ChannelBuffer contentBuffer = bRequest.bytes().toChannelBuffer();
        buffer = ChannelBuffers.wrappedBuffer(NettyUtils.DEFAULT_GATHERING,         headerBuffer, contentBuffer);      
    } else {
        request.writeTo(stream);
        stream.close();
        bytes = bStream.bytes();            
        buffer = bytes.toChannelBuffer();
    }
    NettyHeader.writeHeader(buffer, requestId, status, version);
2 ChannelFuture future = targetChannel.write(buffer);        
  ReleaseChannelFutureListener listener= new ReleaseChannelFutureListener(bytes);
  future.addListener(listener);
  addedReleaseListener = true;
  transportServiceAdapter.onRequestSent(node, requestId, action, request, options);  
  } finally {      
    if (!addedReleaseListener) {
    Releasables.close(bStream.bytes());       
  }
  }
}

其中最重要的就是1和2

  • 1代表拿到一个连接;
  • 2代表通过拿到的连接写数据;

这时候就会有新的问题

  1. nodes的数据是何时写入的?
  2. 连接是什么时候创建的?

Nodes数据何时写入

核心是调用doSampler,代码如下:

protected void doSample() {    
// the nodes we are going to ping include the core listed nodes that were added    
// and the last round of discovered nodes    
SetnodesToPing = Sets.newHashSet();   
for (DiscoveryNode node : listedNodes) {       
    nodesToPing.add(node);  
}
for (DiscoveryNode node : nodes) {        
    nodesToPing.add(node);    
}   
final CountDownLatch latch = new CountDownLatch(nodesToPing.size());    
final ConcurrentMapclusterStateResponses = ConcurrentCollections.newConcurrentMap();   
for (final DiscoveryNode listedNode : nodesToPing) {        
    threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(new Runnable() {           
       @Override   
       public void run() {                
          try {
               if (!transportService.nodeConnected(listedNode)) {                        
                    try {                           
                     // if its one of the actual nodes we will talk to, not to listed nodes, fully connect           
                     if (nodes.contains(listedNode)) {                                        
                        logger.trace("connecting to cluster node [{}]", listedNode);                                    
                        transportService.connectToNode(listedNode);                            
                     } else {
                         // its a listed node, light connect to it...                                    
                        logger.trace("connecting to listed node (light) [{}]", listedNode);                                
                        transportService.connectToNodeLight(listedNode);                                      
                     }
                   } catch (Exception e) {                       
                     logger.debug("failed to connect to node [{}], ignoring...", e, listedNode);
                      latch.countDown();                        
                      return;                       
                   }                
                }
//核心是在这里,刚刚开始初始化的时候,可能只有配置的一个节点,这个时候会通过这个地址发送一个state状态监测                    
//"cluster:monitor/state"                    
transportService.sendRequest(listedNode, ClusterStateAction.NAME,                          
headers.applyTo(Requests.clusterStateRequest().clear().nodes(true).local(true)),                            
TransportRequestOptions.builder().withType(TransportRequestOptions.Type.STAE).withTimeout(pingTimeout).build(),                            
new BaseTransportResponseHandler() {                                
@Override                                
public ClusterStateResponse newInstance() {                                
    return new ClusterStateResponse();                                
}                                
@Override                                
public String executor() {                                    
    return ThreadPool.Names.SAME;                               
}                                
@Override                              
public void handleResponse(ClusterStateResponse response) {
/*通过回调,会在这个地方返回集群中类似下边所有节点的信息
{  "version" : 27,  "state_uuid" : "YSI9d_HiQJ-FFAtGFCVOlw",  "master_node" : "TXHHx-XRQaiXAxtP1EzXMw",  "blocks" : { },  "nodes" : {    "poxubF0LTVue84GMrZ7rwA" : {      "name" : "node1",      "transport_address" : "1.1.1.1:8888",      "attributes" : {        "data" : "false",        "master" : "true"      }    },    "9Cz8m3GkTza7vgmpf3L65Q" : {      "name" : "node2",      "transport_address" : "1.1.1.2:8889",      "attributes" : {        "master" : "false"      }    }  },  "metadata" : {    "cluster_uuid" : "_na_",    "templates" : { },    "indices" : { }  },  "routing_table" : {    "indices" : { }  },  "routing_nodes" : {    "unassigned" : [ ],    "nodes" : {      "lZqD-WExRu-gaSUiCXaJcg" : [ ],      "hR6PbFrgQVSY0MHajNDmgA" : [ ],    }  }}*/                                    
clusterStateResponses.put(listedNode, response);                                  
latch.countDown();                                
}                                
@Override                                
public void handleException(TransportException e) {                                    logger.info("failed to get local cluster state for {}, disconnecting...", e, listedNode);                                    transportService.disconnectFromNode(listedNode);                                    latch.countDown();                                
}                            
});} catch (Throwable e) {                    
logger.info("failed to get local cluster state info for {}, disconnecting...", e, listedNode);                    
transportService.disconnectFromNode(listedNode);                    latch.countDown();                
}}});}    
try {     
   latch.await();    
} catch (InterruptedException e) {       
 return;    
}    
HashSetnewNodes = new HashSet<>();    HashSetnewFilteredNodes = new HashSet<>();   
for (Map.Entryentry : clusterStateResponses.entrySet()) {      
    if (!ignoreClusterName &&!clusterName.equals(entry.getValue().getClusterName())) {            
        logger.warn("node {} not part of the cluster {}, ignoring...",     
        entry.getValue().getState().nodes().localNode(), clusterName);            
        newFilteredNodes.add(entry.getKey());            
        continue;        
}
//接下来在这个地方拿到所有的data nodes 写入到nodes节点里边       
 for (ObjectCursorcursor : entry.getValue().getState().nodes().dataNodes().values()){
    newNodes.add(cursor.value);}}
    nodes = validateNewNodes(newNodes);
    filteredNodes = Collections.unmodifiableList(new ArrayList<(newFilteredNodes));
  }

其中调用时机分为两部分:

  • client.addTransportAddress(transportAddress);
  • ScheduledNodeSampler,默认每隔5s会进行一次对各个节点的请求操作;

连接是何时创建的呢

也是在doSampler调用,最终由NettryTransport创建

创建连接code

这个时候发现,如果是light则创建轻连接,也就是,否则创建fully connect,其中包括:
recovery:做数据恢复recovery,默认个数2个;

  • bulk:用于bulk请求,默认个数3个;
  • med/reg:典型的搜索和单doc索引,默认个数6个;
  • high:如集群state的发送等,默认个数1个;
  • ping:就是node之间的ping咯。默认个数1个;

对应的代码为:

public void start() {    
    List<Channel> newAllChannels = new ArrayList<>();    
    newAllChannels.addAll(Arrays.asList(recovery));    
    newAllChannels.addAll(Arrays.asList(bulk));    
    newAllChannels.addAll(Arrays.asList(reg));    
    newAllChannels.addAll(Arrays.asList(state));    
    newAllChannels.addAll(Arrays.asList(ping));    
    this.allChannels = Collections.unmodifiableList(newAllChannels);
}

END

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容