为什么80%的码农都做不了架构师?>>>   

本文主要研究一下elasticsearch的RoutingService

RoutingService

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/cluster/routing/RoutingService.java

public class RoutingService extends AbstractLifecycleComponent {private static final Logger logger = LogManager.getLogger(RoutingService.class);private static final String CLUSTER_UPDATE_TASK_SOURCE = "cluster_reroute";private final ClusterService clusterService;private final AllocationService allocationService;private AtomicBoolean rerouting = new AtomicBoolean();@Injectpublic RoutingService(ClusterService clusterService, AllocationService allocationService) {this.clusterService = clusterService;this.allocationService = allocationService;}@Overrideprotected void doStart() {}@Overrideprotected void doStop() {}@Overrideprotected void doClose() {}/*** Initiates a reroute.*/public final void reroute(String reason) {try {if (lifecycle.stopped()) {return;}if (rerouting.compareAndSet(false, true) == false) {logger.trace("already has pending reroute, ignoring {}", reason);return;}logger.trace("rerouting {}", reason);clusterService.submitStateUpdateTask(CLUSTER_UPDATE_TASK_SOURCE + "(" + reason + ")",new ClusterStateUpdateTask(Priority.HIGH) {@Overridepublic ClusterState execute(ClusterState currentState) {rerouting.set(false);return allocationService.reroute(currentState, reason);}@Overridepublic void onNoLongerMaster(String source) {rerouting.set(false);// no biggie}@Overridepublic void onFailure(String source, Exception e) {rerouting.set(false);ClusterState state = clusterService.state();if (logger.isTraceEnabled()) {logger.error(() -> new ParameterizedMessage("unexpected failure during [{}], current state:\n{}",source, state), e);} else {logger.error(() -> new ParameterizedMessage("unexpected failure during [{}], current state version [{}]",source, state.version()), e);}}});} catch (Exception e) {rerouting.set(false);ClusterState state = clusterService.state();logger.warn(() -> new ParameterizedMessage("failed to reroute routing table, current state:\n{}", state), e);}}
}
  • RoutingService的构造器要求输入clusterService及allocationService;其reroute方法主要是向clusterService提交ClusterStateUpdateTask,其execute方法是委托给allocationService.reroute

AllocationService.reroute

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java

public class AllocationService {//....../*** Reroutes the routing table based on the live nodes.* <p>* If the same instance of ClusterState is returned, then no change has been made.*/public ClusterState reroute(ClusterState clusterState, String reason) {return reroute(clusterState, reason, false);}/*** Reroutes the routing table based on the live nodes.* <p>* If the same instance of ClusterState is returned, then no change has been made.*/protected ClusterState reroute(ClusterState clusterState, String reason, boolean debug) {ClusterState fixedClusterState = adaptAutoExpandReplicas(clusterState);RoutingNodes routingNodes = getMutableRoutingNodes(fixedClusterState);// shuffle the unassigned nodes, just so we won't have things like poison failed shardsroutingNodes.unassigned().shuffle();RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, fixedClusterState,clusterInfoService.getClusterInfo(), currentNanoTime());allocation.debugDecision(debug);reroute(allocation);if (fixedClusterState == clusterState && allocation.routingNodesChanged() == false) {return clusterState;}return buildResultAndLogHealthChange(clusterState, allocation, reason);}private void reroute(RoutingAllocation allocation) {assert hasDeadNodes(allocation) == false : "dead nodes should be explicitly cleaned up. See disassociateDeadNodes";assert AutoExpandReplicas.getAutoExpandReplicaChanges(allocation.metaData(), allocation.nodes()).isEmpty() :"auto-expand replicas out of sync with number of nodes in the cluster";// now allocate all the unassigned to available nodesif (allocation.routingNodes().unassigned().size() > 0) {removeDelayMarkers(allocation);gatewayAllocator.allocateUnassigned(allocation);}shardsAllocator.allocate(allocation);assert RoutingNodes.assertShardStats(allocation.routingNodes());}//......
}
  • AllocationService的reroute方法主要是构建RoutingAllocation,然后在进行gatewayAllocator.allocateUnassigned及shardsAllocator.allocate(allocation)

BalancedShardsAllocator.allocate

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

public class BalancedShardsAllocator implements ShardsAllocator {//......public void allocate(RoutingAllocation allocation) {if (allocation.routingNodes().size() == 0) {/* with no nodes this is pointless */return;}final Balancer balancer = new Balancer(logger, allocation, weightFunction, threshold);balancer.allocateUnassigned();balancer.moveShards();balancer.balance();}//......
}
  • BalancedShardsAllocator的allocate方法,则创建Balancer,然后执行Balancer的allocateUnassigned()、moveShards()、balance()方法

小结

  • RoutingService的构造器要求输入clusterService及allocationService;其reroute方法主要是向clusterService提交ClusterStateUpdateTask,其execute方法是委托给allocationService.reroute
  • AllocationService的reroute方法主要是构建RoutingAllocation,然后在进行gatewayAllocator.allocateUnassigned及shardsAllocator.allocate(allocation)
  • BalancedShardsAllocator的allocate方法,则创建Balancer,然后执行Balancer的allocateUnassigned()、moveShards()、balance()方法

doc

  • RoutingService

转载于:https://my.oschina.net/go4it/blog/3049600

聊聊elasticsearch的RoutingService相关推荐

  1. 聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

    序 本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAverage ExponentiallyWeightedMovingAverage elasti ...

  2. 聊聊Elasticsearch的TimedRunnable

    序 本文主要研究一下Elasticsearch的TimedRunnable TimedRunnable elasticsearch-7.0.1/server/src/main/java/org/ela ...

  3. 聊聊Elasticsearch的Iterables

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究一下Elasticsearch的Iterables Iterables elasticsearch-7.0.1/ser ...

  4. 聊聊Elasticsearch RestClient的RequestLogger

    序 本文主要研究一下Elasticsearch RestClient的RequestLogger RequestLogger elasticsearch-7.0.1/client/rest/src/m ...

  5. 聊聊Elasticsearch的CachedSupplier

    序 本文主要研究一下Elasticsearch的CachedSupplier CachedSupplier elasticsearch-7.0.1/server/src/main/java/org/e ...

  6. 聊聊Elasticsearch的BootstrapCheck

    序 本文主要研究一下Elasticsearch的BootstrapCheck BootstrapCheck elasticsearch-7.0.1/server/src/main/java/org/e ...

  7. 聊聊Elasticsearch的NodesSniffer

    序 本文主要研究一下Elasticsearch的NodesSniffer NodesSniffer elasticsearch-7.0.1/client/sniffer/src/main/java/o ...

  8. 聊聊Elasticsearch的RunOnce

    序 本文主要研究一下Elasticsearch的RunOnce RunOnce elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/c ...

  9. 【ElasticSearch从入门到放弃系列 零】ElasticSearch看这一篇就够了

    大数据时代系统和业务每分每秒都产生成千上万的数据,其存储一定是不能通过关系型数据库了,当然因为数据的持久性也不能存储到内存型Nosql数据库Redis中,我们通常会将这些数据存储在能够不丢失数据的非关 ...

最新文章

  1. 练习2:课工场响应式导航条_作业帮直播课APP下载最新版入口
  2. 【408预推免复习】计算机网络(谢希仁第七版)第二章——物理层
  3. Python的故事_two
  4. 你所不知道的setTimeout
  5. qt绘制一圈圆_Qt绘制圆形,矩形等图形   绘制同心圆
  6. linux 多个秘钥,linux管理多个ssh公钥密钥
  7. 复盘阿里城市大脑这3年
  8. ubuntu开机时网络图标不见了且不能上网
  9. 一些常见的iOS面试问题,一眼就能看出 初级和高级工程师的区别
  10. ArcEngine中多边形内外环的处理(转)
  11. mysql c接口返回自增id_详解mysql插入数据后返回自增ID的七种方法
  12. java的循环控制结构有哪些_java中的控制结构(if,循环)详解
  13. matlab用辛普森公式求积分_1.1高等数学公式大全
  14. 特殊教育学校计算机教学计划,2017年特殊教育学校教学计划
  15. DedeCMS 栏目三级联动树形菜单的代码
  16. Python批量爬取堆糖图片
  17. 关于复制粘贴快捷键失效问题的解决方法
  18. 初学者如何从零开始搭建一个阿里云数据库
  19. llg的农场(farm)
  20. 如何用易语言做锁机软件

热门文章

  1. Mybatis学习错误之:重复加载mapper.xml
  2. HADOOP2单机版
  3. 我的公众号 - Old Artist
  4. shelve 序列化模块——day18
  5. centos7 mysql安装
  6. ZooKeeper配额
  7. AngularJS模块——module
  8. 【原创】Kakfa metrics包源代码分析
  9. C#在异常中获取HttpStatusCode用法
  10. 强人总结的哄老婆秘籍