Upgrade to Pro — share decks privately, control downloads, hide ads and more …

About: consistent hashing

Harukasan
February 27, 2017

About: consistent hashing

Harukasan

February 27, 2017
Tweet

More Decks by Harukasan

Other Decks in Technology

Transcript

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

    ׏ׂ ׏׃ ׏ׄ ˅૝ˤҍґѵѧҌ҆҈ҝѹѥ Ӧـӡ
  2. ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %3 = 1 %3

    = 2 %3 = 0 ׏ׂ %3 = 1 ӺӐӾԅʂ Ӧـӡ਺Ґ৒²ҴҊҭ ׏׃ ׏ׄ %3 = 2 %3 = 0
  3. ӺӐӾԅʂ ׏ֿ ׏׀ ׏ׁ ׏ֿ ׏׀ ׏ׁ %3 = 1

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

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

    = 2 %4 = 3 ׏ׂ %4 = 0 ӺӐӾԅʂ ׏ׂ ׏׃ ׏ׄ %4 = 1 %4 = 2 ӦـӡҴϟՃѻҭҊѯҍŖő׎׎׎ ׏׀¦߱ѻқ҈҉ȳѯȈѩѮˠੜ
  6. ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ӧӜӏӾ̌Б

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

    ׯ ׯ ۙѥÃҴબ୒ ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ֿ˕…ҐÃҴҊҭ ؼ ӧӜӏӾ̌Б ӋԍӏӑӞԍӠӧӜӏӾʂ
 Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ
  8. ׏ֿ ׏׀ ׏ׁ ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ

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

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

    @ZK׮ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ׯ ׯ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮ @ZK׮ ׯ ׯ ׯ ۙѥÃҴબ୒ ׏ׂ @ZK׮ ׯ ȳѯȈҰ҆ҁҐґ׏ׄ҂ѳ׎
  11. ӧӜӏӾ̌Б ׏ֿ ׏׀ @ZK׮ @ZK׮ ׯ ׯ ׏ׁ @ZK׮ ׯ

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

    ׏ׂ @ZK׮ ׯ ˅૝ˤҍґ ؟ׁذׂ׋׀׃أ ׏ׂҴϟՃѻҭҊ
  13. # ϋογϡؔ਺͸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) ֿאӦـӡӶӜӯҐܭࢉ
  14. # ϋογϡ஋Ͱιʔτ͓͚ͯ͠͹ೋ෼୳ࡧͰ୳ࡧͰ͖Δ
 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] } ׀אӧӜӏӾҐ୳ࡧ
  15. ׀אӧӜӏӾҐ୳ࡧ ӧӜӏӾ̌Б _ @ZK׮ ׯ ׏ֿ ׏׀ ׏ׁ @ZK׮ @ZK׮

    @ZK׮ ׯ ׯ ׯ ׏ׂ @ZK׮ ׯ ™å୳ࡧ҉€˕ۙѥӧӜӏӾҴʹŲ