http://www.cnblogs.com/bonelee/p/6078947.html 里分析了ES bulk实现,其中路由代码:

ShardId shardId = clusterService.operationRouting().indexShards(clusterState, concreteIndex, request.id(), request.routing()).shardId();

其实现: https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java

    public ShardIterator indexShards(ClusterState clusterState, String index, String id, @Nullable String routing) {return shards(clusterState, index, id, routing).shardsIt();}protected IndexShardRoutingTable shards(ClusterState clusterState, String index, String id, String routing) {int shardId = generateShardId(indexMetaData(clusterState, index), id, routing);return clusterState.getRoutingTable().shardRoutingTable(index, shardId);}static int generateShardId(IndexMetaData indexMetaData, String id, @Nullable String routing) { final int hash; if (routing == null) { hash = Murmur3HashFunction.hash(id); } else { hash = Murmur3HashFunction.hash(routing); } // we don't use IMD#getNumberOfShards since the index might have been shrunk such that we need to use the size // of original index to hash documents return Math.floorMod(hash, indexMetaData.getRoutingNumShards()) / indexMetaData.getRoutingFactor(); }

可以看到最新的Es代码实现路由是:

Math.floorMod(hash, indexMetaData.getRoutingNumShards()) / indexMetaData.getRoutingFactor();

在https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java 里可以看到getRoutingFactor实现:

    /*** Returns the routing factor for this index. The default is <tt>1</tt>.** @see #getRoutingFactor(IndexMetaData, int) for details*/public int getRoutingFactor() {return routingFactor;}

构造函数里有:

        assert numberOfShards * routingFactor == routingNumShards :  routingNumShards + " must be a multiple of " + numberOfShards;

反正默认是1,也就是所有的shard节点都会负责路由!

当心,ES2.4版本的路由实现:https://github.com/elastic/elasticsearch/blob/2.4/core/src/main/java/org/elasticsearch/cluster/routing/

    @SuppressForbidden(reason = "Math#abs is trappy")private int generateShardId(ClusterState clusterState, String index, String type, String id, @Nullable String routing) {IndexMetaData indexMetaData = clusterState.metaData().index(index);if (indexMetaData == null) {throw new IndexNotFoundException(index);}final Version createdVersion = indexMetaData.getCreationVersion();final HashFunction hashFunction = indexMetaData.getRoutingHashFunction();final boolean useType = indexMetaData.getRoutingUseType();final int hash;if (routing == null) {if (!useType) {hash = hash(hashFunction, id);} else {hash = hash(hashFunction, type, id);}} else {hash = hash(hashFunction, routing);}if (createdVersion.onOrAfter(Version.V_2_0_0_beta1)) {return MathUtils.mod(hash, indexMetaData.getNumberOfShards());} else {return Math.abs(hash % indexMetaData.getNumberOfShards());}}

    @Deprecatedprotected int hash(HashFunction hashFunction, String type, String id) {if (type == null || "_all".equals(type)) {throw new IllegalArgumentException("Can't route an operation with no type and having type part of the routing (for backward comp)");}return hashFunction.hash(type, id);}

而该hash function实现由:

DjbHashFunction.java

SimpleHashFunction.java

Murmur3HashFunction.java

三种。

hash相关设置如下:

#分片数
index.number_of_shards
#副本数
index.number_of_replicas

#该index各索引的routing规则,采用何种Hash方式,默认使用Murmur3,还有一种普通的Hash算法
index.legacy.routing.hash.type#routing计算是否使用type,内部计算shard id的方法已经废弃,建议不使用,不设置,默认false即可
index.legacy.routing.use_type

转载于:https://www.cnblogs.com/bonelee/p/6078956.html

最新的ES 5.0路由算法底层实现相关推荐

  1. 连线自动路由算法:在GEF中实现连线的自动直角路由,智能避障并绕开模型,选择最佳路径进行布线,仿Visio效果

    在使用GEF(图形编辑框架)开发建模工具时,比如利用GEF实现程序流程图建模功能,有时对连线的路由方式会有比较高的要求,比如连线自动采用直角布局,要能够智能地避障并绕开模型,选择最佳路径进行布线.在建 ...

  2. 拍立淘---试妆魔镜 OpenGL ES 2.0 框架及性能优化

    手机淘宝(搜索框->摄像头->试妆魔镜): 最初的设计原型及性能问题: 单线程模型,优先级过低:从Camera获取到CMSampleBufferRef YUV图像帧,拷贝像素数据到内存(多 ...

  3. ES bulk源码分析——ES 5.0

    对bulk request的处理流程: 1.遍历所有的request,对其做一些加工,主要包括:获取routing(如果mapping里有的话).指定的timestamp(如果没有带timestamp ...

  4. 【计算机网络】网络层 : 总结 ( 功能 | 数据交换 | IP 数据报 | IPv4 地址 | IPv6 地址 | 路由选择协议 | 路由算法 )★★★

    文章目录 一.网络层功能 二.数据交换方式 ★ 三.IP 数据报 ★ 四. IPv4 地址 ★★ 1 . IP 地址 发展 : 2 . 分类 IP 地址 3 . NAT 转换 4 . 子网划分 5 . ...

  5. 【计算机网络】网络层 : OSPF 协议 ( 协议简介 | 链路状态路由算法 | OSPF 区域 | OSPF 特点 )

    文章目录 一.路由选择协议分类 二.OSPF 协议 简介 三.链路状态路由算法 四.OSPF 区域 五.OSPF 特点 一.路由选择协议分类 路由选择协议分类 : ① 内部网管协议 IGP : 在 自 ...

  6. 【学习笔记】路由算法与路由协议:RIP协议与距离向量算法、OSPF协议与链路状态算法、BGP协议

    文章目录 一. 路由算法与路由协议概述 ① 路由算法的分类 ② 分层次的路由选择协议 二. RIP协议和距离向量算法 ① RIP协议定义 ② RIP协议:交换对象.交换周期.交换内容 ③ 距离向量算法 ...

  7. TCP/IP之路由算法

    转载自https://www.jianshu.com/p/e5cce2958790 网络层的重要功能就是路由和转发.而路由是根据路由器根据所维护的路由表进行路由选择.所以,如果创建和更新转发表就是一个 ...

  8. 编程实现路由算法_TCP/IP 之路由算法

    (给算法爱好者加星标,修炼编程内功) 作者:六尺帐篷 www.jianshu.com/p/e5cce2958790 网络层的重要功能就是路由和转发.而路由是根据路由器根据所维护的路由表进行路由选择.所 ...

  9. 路由算法之——ECMP算法

    摘要 本文主要讲述ECMP算法,Equal-CostMultipathRouting,即最大限度地使用最短路径,各条最短路径之间均分流量.比如,如下图所示,节点3到节点8之间的最短路有两条,分别是3, ...

最新文章

  1. Oracle的SQL语句
  2. “请给我一个五彩斑斓的黑”,只需一行命令就能让AI画画,OpenAI的Dall-E被大神复现...
  3. [【Android】Android之ContentProvider总结
  4. xtrabackup 9.0备份出错的解决方法
  5. vb冒泡排序法流程图_VB算法-冒泡排序教案
  6. 遭遇“烧钱瓶颈” 优酷成本结构堪忧
  7. SQL Server 2014数据访问层
  8. 基于Dialog的MFC程序在启动时隐藏为托盘程序(三)
  9. JeDate日期控件,未选择日,出现undefined错误
  10. 51单片机-直流电机
  11. 【转】D3DXLoadSkinMeshFromXof函数及.x在不同dx版本中
  12. mysql生成 my.cnf_mysql配置文件my.cnf一键生成工具
  13. 怎么修改docker镜像的名字_docker镜像如何重命名
  14. 基于SqlServer基本表的插入、修改和删除
  15. 使用R语言进行协整关系检验
  16. 【写论文用到的几个偷懒的网站和软件】
  17. StoneDT开源舆情系统大数据技术栈介绍
  18. DEMATEL复杂因素分析算法最新进展综述
  19. 关于:-1: error: collect2.exe: error: ld returned 1 exit status和The process was ended forcefully.解决方法
  20. 货币转换 I----Python

热门文章

  1. matlab温度数据怎么滤波_卡尔曼滤波算法思想理解 Kalman filter 第一篇
  2. springboot的jsp应该放在哪_详解SpringBoot 添加对JSP的支持(附常见坑点)
  3. 请键入 net helpmsg 3534 以获得更多的帮助。_相遇不易,请珍惜
  4. java构造方法可以重载吗_Java基础教程之构造器与方法重载
  5. win10系统配置apache 2.4的虚拟主机以及查看 apache的版本
  6. 搭建Ubuntu18.04+Anaconda3.x+Pycharm+SimpleITK(一)
  7. 习题1.9 有序数组的插入 (20 分)
  8. linux无法运行病毒,{转}为什么linux系统不容易中病毒?
  9. curl -h php,PHP下使用curl问题小结
  10. java jtable行标题_如何设置JTable的标题——如图