本文主要研究一下dapr的consistent hash

consistent_hash

dapr/pkg/placement/hashing/consistent_hash.go

var replicationFactor int// ErrNoHosts is an error for no hosts
var ErrNoHosts = errors.New("no hosts added")// ConsistentHashTables is a table holding a map of consistent hashes with a given version
type ConsistentHashTables struct {Version stringEntries map[string]*Consistent
}// Host represents a host of stateful entities with a given name, id, port and load
type Host struct {Name  stringPort  int64Load  int64AppID string
}// Consistent represents a data structure for consistent hashing
type Consistent struct {hosts     map[uint64]stringsortedSet []uint64loadMap   map[string]*HosttotalLoad int64sync.RWMutex
}

ConsistentHashTables定义了Version、Entries属性,Entries是个map,value为Consistent;Consistent定义了hosts、sortedSet、loadMap、totalLoad、sync.RWMutex属性

GetInternals

dapr/pkg/placement/hashing/consistent_hash.go

// GetInternals returns the internal data structure of the consistent hash
func (c *Consistent) GetInternals() (map[uint64]string, []uint64, map[string]*Host, int64) {c.RLock()defer c.RUnlock()return c.hosts, c.sortedSet, c.loadMap, c.totalLoad
}

GetInternals方法返回hosts、sortedSet、loadMap、totalLoad属性

Add

dapr/pkg/placement/hashing/consistent_hash.go

// Add adds a host with port to the table
func (c *Consistent) Add(host, id string, port int64) bool {c.Lock()defer c.Unlock()if _, ok := c.loadMap[host]; ok {return true}c.loadMap[host] = &Host{Name: host, AppID: id, Load: 0, Port: port}for i := 0; i < replicationFactor; i++ {h := c.hash(fmt.Sprintf("%s%d", host, i))c.hosts[h] = hostc.sortedSet = append(c.sortedSet, h)}// sort hashes ascendinglysort.Slice(c.sortedSet, func(i int, j int) bool {return c.sortedSet[i] < c.sortedSet[j]})return false
}

Add方法创建Host并添加到loadMap中,之后根据replicationFactor次数对host进行hash并添加到hosts及sortedSet中,最后对sortedSet进行排序

Get

dapr/pkg/placement/hashing/consistent_hash.go

// Get returns the host that owns `key`.
//
// As described in https://en.wikipedia.org/wiki/Consistent_hashing
//
// It returns ErrNoHosts if the ring has no hosts in it.
func (c *Consistent) Get(key string) (string, error) {c.RLock()defer c.RUnlock()if len(c.hosts) == 0 {return "", ErrNoHosts}h := c.hash(key)idx := c.search(h)return c.hosts[c.sortedSet[idx]], nil
}

Get方法先对key进行hash,然后通过search查找idx,最后找到idx在sortedSet中对应的host,最后从hosts中返回对应host

GetHost

dapr/pkg/placement/hashing/consistent_hash.go

// GetHost gets a host
func (c *Consistent) GetHost(key string) (*Host, error) {h, err := c.Get(key)if err != nil {return nil, err}return c.loadMap[h], nil
}

GetHost方法先get key,之后再去loadMap获取host

GetLeast

dapr/pkg/placement/hashing/consistent_hash.go

// GetLeast uses Consistent Hashing With Bounded loads
//
// https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
//
// to pick the least loaded host that can serve the key
//
// It returns ErrNoHosts if the ring has no hosts in it.
//
func (c *Consistent) GetLeast(key string) (string, error) {c.RLock()defer c.RUnlock()if len(c.hosts) == 0 {return "", ErrNoHosts}h := c.hash(key)idx := c.search(h)i := idxfor {host := c.hosts[c.sortedSet[i]]if c.loadOK(host) {return host, nil}i++if i >= len(c.hosts) {i = 0}}
}

GetLeast方法先对key进行hash,然后通过search获取idx,之后通过loadOK来获取least loaded host

search

dapr/pkg/placement/hashing/consistent_hash.go

func (c *Consistent) search(key uint64) int {idx := sort.Search(len(c.sortedSet), func(i int) bool {return c.sortedSet[i] >= key})if idx >= len(c.sortedSet) {idx = 0}return idx
}

search方法通过sort.Search根据key获取idx

小结

dapr的Consistent定义了hosts、sortedSet、loadMap、totalLoad、sync.RWMutex属性;它定义了GetInternals、Add、Get、GetHost、GetLeast、search等方法。

doc

  • dapr

dapr的consistent hash相关推荐

  1. 关于consistent hash的思考及改进方案

    这里默认读者已经知道了一致性hash算法的原理. 1. 为什么在某台机器宕机之后consistent hash算法能够避免所有或者大部分key重新hash? 首先需要弄清的是,如果某一台机器宕机之后, ...

  2. 聊聊jump consistent hash

    序 本文主要简介一下jump Consistent hash. jump consistent hash jump consistent hash是一致性哈希的一种实现,论文见A Fast, Mini ...

  3. 主从mysql replication 集群的sharding memcache集群使用consistent hash

    sharding 实现跨越DB的分区与扩展功能 consistent hash 一致哈希实现memcache的扩展 http://cache.baidu.com/c?m=9f65cb4a8c8507e ...

  4. haproxy Consistent Hash浅析

    http://blog.sina.com.cn/s/blog_502c8cc40100kfz2.html Haproxy实现了Map-based 和consistent hash算法,来完成通过哈希值 ...

  5. Google Jump Consistent Hash 一致性哈希算法

    接触到这个一致性哈希算法是在腾讯音乐的讲座中,用于在线扩容 如图中的例子,本来只有group0和group1,现在要增加一个group2用于推送新的数据,如果使用不满足单调性要求的hash方法,首先向 ...

  6. Upstream Consistent Hash

    介绍 https://www.nginx.com/resources/wiki/modules/consistent_hash/地址 ngx_http_upstream_consistent_hash ...

  7. 2016 -Nginx的负载均衡 - 一致性哈希 (Consistent Hash)

    Nginx版本:1.9.1 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减集群的缓存服务器时,只有少量的缓存会失效,回源量较小. 在nginx+ ...

  8. consistent hash

    https://www.youtube.com/watch?v=ffE1mQWxyKM https://www.youtube.com/watch?v=zaRkONvyGr8

  9. 一文搞懂一致性hash的原理和实现

    在 go-zero 的分布式缓存系统分享里,Kevin 重点讲到过一致性hash的原理和分布式缓存中的实践.本文来详细讲讲一致性hash的原理和在 go-zero 中的实现. 以存储为例,在整个微服务 ...

最新文章

  1. linux操作系统学习网站整理(100个)
  2. git如何利用分支进行多人开发
  3. HDU 2570 迷瘴
  4. 数据库时区那些事儿 - MySQL的时区处理
  5. MySQL数据库select语句的使用方法
  6. 【小程序】一个提醒休息的小程序,供大家娱乐
  7. 《敏捷无敌》试读:第5章 成长的烦恼
  8. Linux之间ssh免密码登录
  9. PHPMailer如何获取QQ邮箱授权码
  10. java end_Java Matcher end()用法及代码示例
  11. Xcode 5设置Deployment Target
  12. 程序员996与工地施工人员谁更累?
  13. Java学习里程-----Java基础_26 BigDecimal类
  14. ROC曲线、AUC、Gini系数和KS值
  15. built a JNCIS LAB系列:Chapter 1 Routing Policy Processing v1.0
  16. Android App耗电分析
  17. 横道图时间标尺在P6软件中的设置
  18. 如何使用并且使用过滤器
  19. 3dmax打开错误html,安装3dmax出现错误怎么办?3dmax出现错误解决办法
  20. opencv 手选roi区域_如何用opencv实现感兴趣区域ROI的选取

热门文章

  1. you-get下载b站选集_Flash选集:酷炫效果和实用的ActionScript-第1章:Flash基本知识
  2. 【docker】基于dockerfile编写LNMP
  3. 第十篇,STM32串口蓝牙编程
  4. 不正确的移动类型的更新控制(输入101 X X)
  5. 设置默认浏览器为Chrome
  6. [XCTF] [NJUPT CTF 2017] maze
  7. 少年碎碎念:那年我十八,心里满是她
  8. webgoat靶场通关记录
  9. create-react-app :无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\create-react-app.ps1,因为在此系统上禁止运行
  10. 处理:“更改会计科目表前重置公司码数据”报错