架构师训练营 第 5 周作业
用你熟悉的编程语言实现一致性 hash 算法。编写测试用例测试这个算法,测试100万 KV 数据,10 个服务器节点的情况下,计算这些 KV 数据在服务器上分布数量的标准差,以评估算法的存储负载不均衡性。
代码实现如下:
以上代码运行3次结果如下,平均来看,基于 FNV1_32_HASH 算法 和 10 个节点的情况下,随机生成 100万个 key 的负载均衡情况,每个节点设置 200 个左右的虚拟节点可以达到较好的平衡(标准差在4600左右)。
10个节点,每个节点有100个虚拟节点的情况
标准差分别为:8693,8683,8730
10个节点,每个节点有150个虚拟节点的情况
标准差分别为:5496,5386,5523
10个节点,每个节点有200个虚拟节点的情况
标准差分别为:4679,4515,4860
10个节点,每个节点有300个虚拟节点的情况
标准差分别为:5321,5227,5364
10个节点,每个节点有400个虚拟节点的情况
标准差分别为:5213,5058,5067
10个节点,每个节点有500个虚拟节点的情况
标准差分别为:5150,5074,5280
详细运行结果如下:
第一次运行:
Standard Deviation for 100 virtual nodes per node is: 8693.312544709295
192.168.0.0: 118791
192.168.0.1: 92913
192.168.0.2: 103922
192.168.0.3: 107664
192.168.0.4: 88269
192.168.0.5: 105893
192.168.0.6: 97984
192.168.0.7: 97126
192.168.0.8: 96432
192.168.0.9: 91006
Standard Deviation for 150 virtual nodes per node is: 5496.127090961416
192.168.0.0: 107064
192.168.0.1: 101915
192.168.0.2: 95442
192.168.0.3: 103929
192.168.0.4: 89248
192.168.0.5: 99471
192.168.0.6: 105860
192.168.0.7: 105254
192.168.0.8: 95207
192.168.0.9: 96610
Standard Deviation for 200 virtual nodes per node is: 4679.677552994437
192.168.0.0: 107498
192.168.0.1: 101070
192.168.0.2: 104508
192.168.0.3: 93105
192.168.0.4: 96091
192.168.0.5: 99643
192.168.0.6: 106865
192.168.0.7: 97630
192.168.0.8: 95203
192.168.0.9: 98387
Standard Deviation for 300 virtual nodes per node is: 5321.195730284689
192.168.0.0: 95601
192.168.0.1: 106554
192.168.0.2: 102375
192.168.0.3: 90274
192.168.0.4: 101705
192.168.0.5: 100535
192.168.0.6: 109052
192.168.0.7: 94047
192.168.0.8: 99808
192.168.0.9: 100049
Standard Deviation for 400 virtual nodes per node is: 5213.945147390793
192.168.0.0: 99042
192.168.0.1: 100168
192.168.0.2: 105234
192.168.0.3: 90787
192.168.0.4: 96798
192.168.0.5: 102825
192.168.0.6: 109440
192.168.0.7: 103093
192.168.0.8: 98993
192.168.0.9: 93620
Standard Deviation for 500 virtual nodes per node is: 5150.529487343995
192.168.0.0: 97573
192.168.0.1: 98579
192.168.0.2: 103108
192.168.0.3: 91767
192.168.0.4: 96772
192.168.0.5: 103092
192.168.0.6: 108037
192.168.0.7: 107834
192.168.0.8: 94261
192.168.0.9: 98977
第二次运行:
Standard Deviation for 100 virtual nodes per node is: 8683.248355310356
192.168.0.0: 119135
192.168.0.1: 92794
192.168.0.2: 104242
192.168.0.3: 107241
192.168.0.4: 88359
192.168.0.5: 105521
192.168.0.6: 98146
192.168.0.7: 96932
192.168.0.8: 96066
192.168.0.9: 91564
Standard Deviation for 150 virtual nodes per node is: 5386.41940439101
192.168.0.0: 107350
192.168.0.1: 101576
192.168.0.2: 95606
192.168.0.3: 103232
192.168.0.4: 88816
192.168.0.5: 100135
192.168.0.6: 105525
192.168.0.7: 104738
192.168.0.8: 95658
192.168.0.9: 97364
Standard Deviation for 200 virtual nodes per node is: 4515.555004647823
192.168.0.0: 107422
192.168.0.1: 100751
192.168.0.2: 104330
192.168.0.3: 92717
192.168.0.4: 96310
192.168.0.5: 100518
192.168.0.6: 105979
192.168.0.7: 97894
192.168.0.8: 95480
192.168.0.9: 98599
Standard Deviation for 300 virtual nodes per node is: 5227.366258451764
192.168.0.0: 95233
192.168.0.1: 106445
192.168.0.2: 101948
192.168.0.3: 90681
192.168.0.4: 101499
192.168.0.5: 100582
192.168.0.6: 109191
192.168.0.7: 94415
192.168.0.8: 99779
192.168.0.9: 100227
Standard Deviation for 400 virtual nodes per node is: 5058.7798924246545
192.168.0.0: 98366
192.168.0.1: 100031
192.168.0.2: 104660
192.168.0.3: 91247
192.168.0.4: 97449
192.168.0.5: 103261
192.168.0.6: 109327
192.168.0.7: 103020
192.168.0.8: 99018
192.168.0.9: 93621
Standard Deviation for 500 virtual nodes per node is: 5074.836154990622
192.168.0.0: 97299
192.168.0.1: 98732
192.168.0.2: 102883
192.168.0.3: 91981
192.168.0.4: 97387
192.168.0.5: 102633
192.168.0.6: 107817
192.168.0.7: 108204
192.168.0.8: 94319
192.168.0.9: 98745
第三次运行:
Standard Deviation for 100 virtual nodes per node is: 8730.952754424914
192.168.0.0: 119173
192.168.0.1: 93661
192.168.0.2: 104095
192.168.0.3: 107548
192.168.0.4: 88132
192.168.0.5: 105199
192.168.0.6: 98534
192.168.0.7: 96809
192.168.0.8: 96078
192.168.0.9: 90771
Standard Deviation for 150 virtual nodes per node is: 5523.682738898026
192.168.0.0: 107269
192.168.0.1: 101227
192.168.0.2: 95658
192.168.0.3: 103547
192.168.0.4: 88636
192.168.0.5: 100025
192.168.0.6: 106281
192.168.0.7: 104698
192.168.0.8: 95401
192.168.0.9: 97258
Standard Deviation for 200 virtual nodes per node is: 4860.374265424423
192.168.0.0: 108006
192.168.0.1: 100720
192.168.0.2: 104379
192.168.0.3: 92756
192.168.0.4: 96353
192.168.0.5: 100688
192.168.0.6: 106872
192.168.0.7: 97235
192.168.0.8: 94657
192.168.0.9: 98334
Standard Deviation for 300 virtual nodes per node is: 5364.141403803595
192.168.0.0: 94960
192.168.0.1: 106411
192.168.0.2: 102442
192.168.0.3: 90665
192.168.0.4: 101865
192.168.0.5: 100248
192.168.0.6: 109595
192.168.0.7: 94311
192.168.0.8: 99893
192.168.0.9: 99610
Standard Deviation for 400 virtual nodes per node is: 5067.849938583423
192.168.0.0: 99335
192.168.0.1: 100605
192.168.0.2: 104311
192.168.0.3: 91079
192.168.0.4: 96490
192.168.0.5: 102606
192.168.0.6: 109127
192.168.0.7: 103753
192.168.0.8: 99055
192.168.0.9: 93639
Standard Deviation for 500 virtual nodes per node is: 5280.030965818288
192.168.0.0: 97015
192.168.0.1: 98175
192.168.0.2: 103242
192.168.0.3: 91869
192.168.0.4: 97478
192.168.0.5: 102680
192.168.0.6: 108318
192.168.0.7: 108347
192.168.0.8: 93974
192.168.0.9: 98902
版权声明: 本文为 InfoQ 作者【Lingjun】的原创文章。
原文链接:【http://xie.infoq.cn/article/adcdc633094807bc05becab6f】。文章转载请联系作者。
评论