第五周作业
发布于: 2020 年 07 月 08 日
用你熟悉的编程语言实现一致性 hash 算法。
编写测试用例测试这个算法,测试 100 万 KV 数据,10 个服务器节点的情况下,计算这些 KV 数据在服务器上分布数量的标准差,以评估算法的存储负载不均衡性。
package testsimport ( "errors" "fmt" "hash/crc32" "math" "sort" "strconv" "testing")type Consistent struct { nodesReplicas int //添加虚拟节点数量 hashSortNodes []uint32 //所有节点的排序数组 circle map[uint32]string //所有节点对应的node nodes map[string]bool //node是否存在}func NewConsistent() *Consistent { return &Consistent{ nodesReplicas: 200, circle: make(map[uint32]string), nodes: make(map[string]bool), }}func (c *Consistent) AddNode(node string) error { if _, ok := c.nodes[node]; ok { //判断新加节点是否存在 return fmt.Errorf("%s already existed", node) } c.nodes[node] = true for i := 0; i < c.nodesReplicas; i++ { //添加虚拟节点 replicasKey := getReplicasKey(i, node) //虚拟节点 c.circle[replicasKey] = node c.hashSortNodes = append(c.hashSortNodes, replicasKey) //所有节点ID } sort.Slice(c.hashSortNodes, func(i, j int) bool { //排序 return c.hashSortNodes[i] < c.hashSortNodes[j] }) return nil}func (c *Consistent) GetNode(key string) (string, error) { if len(c.nodes) == 0 { return "", errors.New("not add node") } index := c.searchNearbyIndex(key) host := c.circle[c.hashSortNodes[index]] return host, nil}func (c *Consistent) searchNearbyIndex(key string) int { hashKey := hashKey(key) index := sort.Search(len(c.hashSortNodes), func(i int) bool { //key算出的节点,距离最近的node节点下标 sort.Search数组需要升序排列好 return c.hashSortNodes[i] >= hashKey }) if index >= len(c.hashSortNodes) { index = 0 } return index}func getReplicasKey(i int, node string) uint32 { return hashKey(fmt.Sprintf("%s#%d", node, i))}func hashKey(host string) uint32 { return crc32.ChecksumIEEE([]byte(host))}func calSTD(dataDistributeMap map[string]int) float64 { avg := 100000 var totalValue float64 = 0 for _, value := range dataDistributeMap { totalValue = totalValue + math.Pow(math.Abs(float64(avg-value)), 2) } return math.Sqrt(totalValue / 10)}func TestAddData(t *testing.T) { for nodesReplicas := 100; nodesReplicas <= 200; nodesReplicas++ { consistent := NewConsistent() consistent.nodesReplicas = nodesReplicas for i := 0; i < 10; i++ { consistent.AddNode("Node_" + strconv.Itoa(i)) } dataDistributeMap := make(map[string]int) for num := 0; num < 1000000; num++ { key := fmt.Sprintf("test_key_%d", num) node, _ := consistent.GetNode(key) count := dataDistributeMap[node] if count == 0 { dataDistributeMap[node] = 1 } else { dataDistributeMap[node] = count + 1 } } fmt.Printf("nodesReplicas=%v,std=%v\n", nodesReplicas, calSTD(dataDistributeMap)) consistent = nil }}
//测试用例结果,虚拟节点与标准差数据。这里比较关键的是散列函数是否可以把数据均匀的分散开nodesReplicas=100,std=18637.422590047154nodesReplicas=101,std=18336.45375747448nodesReplicas=102,std=18183.738702478102nodesReplicas=103,std=17865.044438791636nodesReplicas=104,std=17723.902911040783nodesReplicas=105,std=17508.814231694847nodesReplicas=106,std=17244.224812962744nodesReplicas=107,std=17411.35927491016nodesReplicas=108,std=17552.303034075045nodesReplicas=109,std=16764.82830809788nodesReplicas=110,std=16520.800918841676nodesReplicas=111,std=16218.94971322126nodesReplicas=112,std=15999.956606191156nodesReplicas=113,std=16317.221595602605nodesReplicas=114,std=16725.957808149582nodesReplicas=115,std=16635.338902469044nodesReplicas=116,std=16729.724976818954nodesReplicas=117,std=16284.605263868081nodesReplicas=118,std=16238.897388677595nodesReplicas=119,std=15772.494190837415nodesReplicas=120,std=15352.573829817591nodesReplicas=121,std=14919.7271422771nodesReplicas=122,std=14904.551016384225nodesReplicas=123,std=14875.371988626033nodesReplicas=124,std=14932.096456961426nodesReplicas=125,std=14966.524372745998nodesReplicas=126,std=15324.912384741388nodesReplicas=127,std=15977.175532615269nodesReplicas=128,std=16049.002666832603nodesReplicas=129,std=14840.80927038684nodesReplicas=130,std=13749.909665157804nodesReplicas=131,std=13691.367995930867nodesReplicas=132,std=13762.821229675259nodesReplicas=133,std=13907.914854499217nodesReplicas=134,std=13517.73202870955nodesReplicas=135,std=13590.586116867808nodesReplicas=136,std=13925.114484269061nodesReplicas=137,std=14399.252508376954nodesReplicas=138,std=14444.596373730905nodesReplicas=139,std=14218.760733622323nodesReplicas=140,std=13931.093532095749nodesReplicas=141,std=14141.265869786905nodesReplicas=142,std=14510.716612214574nodesReplicas=143,std=14847.132167526495nodesReplicas=144,std=14938.868538145718nodesReplicas=145,std=15491.923708823253nodesReplicas=146,std=15959.540381853107nodesReplicas=147,std=16162.299100066179nodesReplicas=148,std=16184.426959271681nodesReplicas=149,std=15832.311170514557nodesReplicas=150,std=15647.809987343277nodesReplicas=151,std=15789.255289594883nodesReplicas=152,std=16171.53785513301nodesReplicas=153,std=16341.903365275417nodesReplicas=154,std=16532.26842874262nodesReplicas=155,std=16774.0822401704nodesReplicas=156,std=17103.775817052796nodesReplicas=157,std=17533.222846926914nodesReplicas=158,std=18121.72146348133nodesReplicas=159,std=18015.368661229222nodesReplicas=160,std=17677.801362160397nodesReplicas=161,std=17691.103911288294nodesReplicas=162,std=17915.48197509629nodesReplicas=163,std=18381.738524960037nodesReplicas=164,std=18474.200469844425nodesReplicas=165,std=18035.491354548674nodesReplicas=166,std=18185.099829255818nodesReplicas=167,std=18212.419515264854nodesReplicas=168,std=18143.330581786795nodesReplicas=169,std=18255.693112013032nodesReplicas=170,std=18472.942185802454nodesReplicas=171,std=18520.654275699875nodesReplicas=172,std=18612.134138781614nodesReplicas=173,std=19346.22619013848nodesReplicas=174,std=19317.97076299682nodesReplicas=175,std=19339.8126723089nodesReplicas=176,std=19394.424956672472nodesReplicas=177,std=19446.01431656369nodesReplicas=178,std=19081.96204272506nodesReplicas=179,std=19225.92947037932nodesReplicas=180,std=18931.6345675697nodesReplicas=181,std=19091.27366625915nodesReplicas=182,std=19109.542009163903nodesReplicas=183,std=19296.986883967144nodesReplicas=184,std=19628.85686432096nodesReplicas=185,std=19611.381353693574nodesReplicas=186,std=19639.257592892864nodesReplicas=187,std=19618.200880814733nodesReplicas=188,std=19762.10710425384nodesReplicas=189,std=19914.156432046024nodesReplicas=190,std=19986.262401960004nodesReplicas=191,std=20223.049038164347nodesReplicas=192,std=20460.3679487931nodesReplicas=193,std=20661.062591260885nodesReplicas=194,std=21146.09271236651nodesReplicas=195,std=21306.66155454674nodesReplicas=196,std=21309.25824143112nodesReplicas=197,std=21638.773990224123nodesReplicas=198,std=21904.441814390066nodesReplicas=199,std=21973.870123398836nodesReplicas=200,std=21764.37291079162
划线
评论
复制
发布于: 2020 年 07 月 08 日阅读数: 47
倪惠华
关注
还未添加个人签名 2018.05.08 加入
还未添加个人简介
评论 (1 条评论)