Save 37% off PRO during our Black Friday Sale! »

About: consistent hashing

124da56a613b15fa980427533e4e3839?s=47 Harukasan
February 27, 2017

About: consistent hashing

124da56a613b15fa980427533e4e3839?s=128

Harukasan

February 27, 2017
Tweet

Transcript

  1. ӋԍӏӑӞԍӠӧӜӏӾʂҍ҅ѥ҈ ӬӆӏӮװɒױґҭѭѷҵ

  2. ѵѵ҉ѥѧӧӜӏԍӇҊґ

  3. Ό਺ҐӄـҴΌ਺ҐӦـӡҍðҬƾ҈ҁѥ
 Ҋѥѧī୊

  4. ׏ֿ ׏׀ ׏ׁ ׏ׅ ׏׆ ׏ׇ ׏ֿ־ ׏ֿ ׏׀ ׏ׁ

    ׏ׂ ׏׃ ׏ׄ ӄـ Ӧـӡ
  5. ׏ֿ ׏׀ ׏ׁ ׏ׅ ׏׆ ׏ׇ ׏ֿ־ ׏ֿ ׏׀ ׏ׁ

    ׏ׂ ׏׃ ׏ׄ ˅૝ˤҍґѵѧҌ҆҈ҝѹѥ Ӧـӡ
  6. ѵҮѮ҉ѯҭҊ ؆!׌ҐӏӼـӟҺԍӇ ؆ӄӼӜӏӾҐåࢄشࠓ´҆҈ҭҥ҅ ؆Ҍҍѭѣҭҥ҅ҴҌҵѭͮѥײѺҍåð ……ҊѭҠѣѥүѥүҊ¸ê

  7. €˕ӏԍӯԃҌȠʂ פӺӐӾԅʂץ

  8. ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %3 = 1 %3

    = 2 %3 = 0 ׏ׂ %3 = 1 ӺӐӾԅʂ Ӧـӡ਺Ґ৒²ҴҊҭ ׏׃ ׏ׄ %3 = 2 %3 = 0
  9. ӺӐӾԅʂ׋ ؆ÅҬѮχѵҫҌѱ҈ͮѷѿѧ ؆ӏԍӯԃ ؆ӦـӡҴϟՃѻҭҊѯҍŖő

  10. ӺӐӾԅʂ ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %3 = 1

    %3 = 2 %3 = 0 ׏ׂ %3 = 1 ׏׃ ׏ׄ %3 = 2 %3 = 0
  11. ӺӐӾԅʂ ׏ׂ ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %3 =

    1 %3 = 2 %3 = 0 ׏ׂ %3 = 1 ׏׃ ׏ׄ %3 = 2 %3 = 0
  12. ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %4 = 1 %4

    = 2 %4 = 3 ׏ׂ %4 = 0 ӺӐӾԅʂ ׏ׂ ׏׃ ׏ׄ %4 = 1 %4 = 2 ӦـӡҴϟՃѻҭҊѯҍŖő׎׎׎ ׏׀¦߱ѻқ҈҉ȳѯȈѩѮˠੜ
  13. פӺӐӾԅʂץґ ࣮ΆѮ؆୯҂ѮӦـӡҐϟՃ؏࡟আѮŖő

  14. ӋԍӏӑӞԍӠӧӜӏӾʂ • @_LE@YJFYF[@Q׌،TSZLZ[FS[ @ZKLSJ@SE@SETR YFFZ׋LZ[YLC\[FE@DKLSJYT[TDTQZITYFQLF_LSJ T[ WT[ZTS[KF"TYQE"LEF"FC،׮ׇׇֿׅׯ • ࣮ΆҊѹ҈ґFRD@DKFEҐҁңҐF[@R@ѮȵĚ
 K[[WZ׋זזJL[K\CאDTRזזPF[@R@

    • SJLSa׍W@DKFY@IILDFY_FYҤ࠾ˉ
  15. ӋԍӏӑӞԍӠӧӜӏӾʂ׋ ؆ӧӜӏӾВ਺ґ€ɠҍåƖѻҭґѼ ؆ӧӜӏӾВ਺ҴԂԍӇ…ҍѹ҈࣮Ά

  16. ׏ֿ ׏׀ ׏ׁ ӋԍӏӑӞԍӠӧӜӏӾʂ
 Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ

    ׯ ӧӜӏӾ̌Б
  17. ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ӧӜӏӾ̌Б

    ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ӋԍӏӑӞԍӠӧӜӏӾʂ
 Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ
  18. ׏ֿ @ZK׮ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ

    ׯ ׯ ۙѥÃҴબ୒ ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ֿ˕…ҐÃҴҊҭ ؼ ӧӜӏӾ̌Б ӋԍӏӑӞԍӠӧӜӏӾʂ
 Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ
  19. ӋԍӏӑӞԍӠӧӜӏӾʂ׋ ؆ӧӜӏӾВ਺ґ€ɠҍåƖѻҭґѼ
 ؕҌҐ҉ÅҫҌѥ ؆ȳѯȈҰҭҐґӧӜӏӾ̌БҐ€϶҂ѳҐґѼ
 ؕҌҐ҉ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

  20. ׏ֿ ׏׀ ׏ׁ ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ

    ӧӜӏӾ̌Б ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ
  21. ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ ӧӜӏӾ̌Б ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮

    @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׏ׂ @ZK׮ ׯ ׏ׂҴϟՃ
  22. ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ ӧӜӏӾ̌Б ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮

    @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ۙѥÃҴબ୒ ׏ׂ @ZK׮ ׯ ȳѯȈҰ҆ҁҐґ׏ׄ҂ѳ׎
  23. ӋԍӏӑӞԍӠӧӜӏӾʂ׋ ؆ÅҫҌѥ ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

  24. ӧӜӏӾ̌Б ׏ֿ @ZK׮ ׯ ˅૝ˤҍґ ؟ֿذ׀׋

  25. ˅૝ˤҍґ ӧӜӏӾ̌Б ׏ֿ ׏׀ @ZK׮ @ZK׮ ׯ ׯ ׏׀ҴϟՃѻҭҊ ؟ֿذ׀׋׃־أ

  26. ӧӜӏӾ̌Б ׏ֿ ׏׀ @ZK׮ @ZK׮ ׯ ׯ ׏ׁ @ZK׮ ׯ

    ˅૝ˤҍґ ؟׀ذׁ׋׀׃أ ׏ׁҴϟՃѻҭҊ
  27. ӧӜӏӾ̌Б ׏ֿ ׏׀ @ZK׮ @ZK׮ ׯ ׯ ׏ׁ @ZK׮ ׯ

    ׏ׂ @ZK׮ ׯ ˅૝ˤҍґ ؟ׁذׂ׋׀׃أ ׏ׂҴϟՃѻҭҊ
  28. ӋԍӏӑӞԍӠӧӜӏӾʂ׋ ؆ÅҫҌѥ ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

  29. ӋԍӏӑӞԍӠӧӜӏӾʂҐ࣮Ά
 ׮F[@R@ҐńĘׯ

  30. # ϋογϡؔ਺͸32bit unsigned intͷϋογϡ஋Λฦ͢
 # CRC32, MD5ͷ্Ґ32bitͳͲͰ࣮૷
 func hash(key string)

    uint32 { ... } # ϊʔυͷϋογϡ஋Λܭࢉ for _, node := range nodes { # ͻͱͭͷϊʔυຖʹ160ݸͷϋογϡΛܭࢉ for i := 0; i < 160; i++ { k := fmt.Sprintf(“%s-%d”, node, i)
 
 # ϊʔυ໊ͱϋογϡ஋ͷϖΞΛ௥Ճ continuum.add(node, hash(k)) } } # ϋογϡ஋Ͱιʔτ͓ͯ͘͠ sort.Sort(continuum) ֿאӦـӡӶӜӯҐܭࢉ
  31. ֿאӦـӡӶӜӯҐܭࢉ ӧӜӏӾ̌Б ׏ֿ @ZK׮ ׯ ֿ҅ҐӦـӡҍରѹ
 Ό਺ҐӧӜӏӾҴܭࢉ
 ذӧӜӏӾܭࢉҍҩҭÅҬҴЕɦ ؕ׮׀־־¿ѱҫѥ҉ůŲׯ

  32. # ϋογϡ஋Ͱιʔτ͓͚ͯ͠͹ೋ෼୳ࡧͰ୳ࡧͰ͖Δ
 func search(key string) string { v := hash(key)

    i, j := 0, n for i < j { h := i + (j-i)/2 if (continuum.values[i] > v) { i = h + 1 } else { j = h } } return continuum.nodes[i] } ׀אӧӜӏӾҐ୳ࡧ
  33. ׀אӧӜӏӾҐ୳ࡧ ӧӜӏӾ̌Б _ @ZK׮ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮

    @ZK׮ ׯ ׯ ׯ ׏ׂ @ZK׮ ׯ ™å୳ࡧ҉€˕ۙѥӧӜӏӾҴʹŲ
  34. ҠҊң
 ׮ӋԍӏӑӞԍӠӧӜӏӾʂׯ ؆ÅҫҌѥ
 ؕӧӜӏӾВ਺Ҋѹ҈ґ׃…¯ׁ׀CL[׌ׁ׀׌ !؆ֿ̙ ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ
 ؆࣮ΆҤӏԍӯԃ