Scala With Cats: Case Study: CRDTs

1f745ff900e1be51aedae18cae76593c?s=47 Kurochan
August 07, 2018
82

Scala With Cats: Case Study: CRDTs

1f745ff900e1be51aedae18cae76593c?s=128

Kurochan

August 07, 2018
Tweet

Transcript

  1. Scala with Cats Case Study: CRDTs https://underscore.io/books/scala-with-cats/

  2. CRDTs • Commutative Replicated Data Types • or Conflict-free Replicated

    Data Types • ݁Ռ੔߹ੑͷ͋Δσʔλߏ଄ • ϞϊΠυ͕ͲͷΑ͏ʹ໾ཱ͔ͭͷྫͱͯ͠CRDTsΛ঺հ
  3. Consistency • γεςϜ͕εέʔϧ͢Δͱ୯ҰͷϚγϯΛ௒͑ͯσʔλ Λ؅ཧ͠ͳ͍ͱ͍͚ͳ͘ͳΔ • શͯͷϚγϯ͕ಉ͡σʔλΛ࣋ͭ؀ڥΛ૝ఆ͢Δ • 1ͭͷϚγϯ͕ϢʔβͷύεϫʔυΛߋ৽͢Δͱɺ
 શ୆ͷϚγϯ͕ಉ͡ίϐʔΛ࣋ͭ •

    શ୆ͷϚγϯ͕ͦΕΛड͚ೖΕͨ͋ͱ
 ͦͷૢ࡞͸׬ྃ͢Δ
  4. Eventual Consistency • લϖʔδͷྫͩͱ੔߹ੑ͕อͯΔ͕ɺ1ͭͷૢ࡞ͷͨΊʹશ୆ ͱಉظΛͱΔͷͰϨΠςϯγ͕ѱԽ͢Δ • ωοτϫʔΫো֐ʹΑΔ෼அ΋ൃੜ͢Δ • ੔߹ੑΛอͭͨΊʹ͸ૢ࡞Λڋ൱͢Δ͔͠ͳ͍ •

    ݁Ռ੔߹ੑͷ͋ΔγεςϜͰ͸֤Ϛγϯ͕Ұ࣌తʹҟͳΔ
 ঢ়ଶΛ࣋ͭࣄΛڐ༰͢Δ • ݁Ռతʹ੔߹ੑ͕औΕΕ͹͍͍ͷͰૢ࡞ʹର͢ΔԠ౴͸
 ૣ͘ͳΔ • ωοτϫʔΫো֐ʹΑΔ෼அ͕ൃੜͯ͠΋ॲཧΛܧଓͰ͖Δ
  5. CRDT • ͲͷΑ͏ʹͯ͠ϚγϯؒͰ݁Ռ੔߹ੑΛऔΔͷ͔ʁ • CRDT͕ͻͱͭͷղͱͳΔ • Commutative Replicated Data Types

    • CRDTͷ࣮૷ͷͻͱͭͱͯ͠G-CounterΛ঺հ͢Δ
  6. G-Counters • ෼ࢄΧ΢ϯλͱͯ͠࢖͑Δ • ૿Ճ͢ΔΦϖϨʔγϣϯʹͷΈରԠ͢Δ • webαʔόͷ๚໰ऀͷΧ΢ϯτ౳ʹ࢖͑Δ

  7. Simple counters • ୯ʹड͚औͬͨϦΫΤετ਺Λ௨஌ͯ͠଍͠߹ΘͤΔ

  8. Simple counters • ௥ՃͰϦΫΤετ͕དྷͨͱ͖ʹΧ΢ϯλͷ஋Λ଍্͠ ͛Δͱ͓͔͘͠ͳΔ • 2୆ͱ΋"6"Λอ͍࣋ͯ͠ͳ͍ͱ͍͚ͳ͍͸ͣ…

  9. G-Counters • G-Counter͸֤ϚγϯͷΧ΢ϯλΛ͍࣋ͬͯΔ • ஋ΛΠϯΫϦϝϯτͰ͖Δͷ͸ࣗ෼ͷΧ΢ϯλ͚ͩ • ঢ়ଶߋ৽ͷϝοηʔδ͕དྷͨΒɺͦͷϚγϯͷΧ΢ ϯλͷ࠷େ஋Ͱஔ͖׵͑Δ(Grow only)

  10. G-Counters • ͦͷϚγϯͷΧ΢ϯλͷ࠷େ஋Ͱஔ͖׵͑Δ

  11. 11.2.3 Exercise: GCounter Implementation • G-CounterΛ࣮૷ͯ͠Έ·͠ΐ͏ • counter: Map[String, Int]

    • key͕machine id, value͕Χ΢ϯτ • increment: GCounter • ࢦఆͨ͠machineͷΧ΢ϯτΛࢦఆͨ͠
 amount͚ͩ଍͢ • merge: GCounter • 2ͭͷGCounterΛϚʔδ͢Δ
  12. 11.2.3 Exercise: GCounter Implementation • G-CounterΛ࣮૷ͯ͠Έ·͠ΐ͏

  13. Additional Exercise: PN-Counter Implementation • PN-CounterΛ࣮૷ͯ͠Έ·͠ΐ͏ • Ҿ͖ࢉ΋Մೳʹͨ͠CRDTs (Positive, Negative)

  14. Additional Exercise: PN-Counter Implementation • PN-CounterΛ࣮૷ͯ͠Έ·͠ΐ͏ • Ҿ͖ࢉ΋Մೳʹͨ͠CRDTs

  15. Generalisation • ͖͞΄ͲͷG-CounterΛந৅Խ͍ͯ͘͠ • G-Counter͸ • Ճࢉ: ΠϯΫϦϝϯτ͞Εͨ߹ܭ஋Λ࣋ͭ • ࠷େ஋:

    Ϛʔδ࣌͸࠷େ஋Λ࣋ͭ • ॳظ஋͸ 0 • => ϞϊΠυͰ͋Δ • (a + b) + c == a + (b + c) • 0 + a == a + 0 == a • a max a = a (mergeͷૢ࡞͸ႈ౳ੑ͕͋Δ)
  16. Generalisation • ͖͞΄ͲͷG-CounterΛந৅Խ͍ͯ͘͠ • increment: monoid • total: monoid +

    Մ׵ • merge: monoid + ႈ౳ + Մ׵ • => ༗ք൒ଋͱݴ͏
  17. Generalisation • લड़ͷಛ௃ΑΓɺGCounter͸ࣗવ਺ͱಉ༷ͳૢ࡞͕Ͱ ͖Δ΋ͷʹ΋ஔ͖׵͑ΒΕΔ • ࣗવ਺ͷ୅ΘΓʹSet[A]Λ࢖ͬͯGSetͱ͍͏ܕΛ࣮૷ ͢Δ͜ͱ΋Ͱ͖Δ͸ͣ

  18. Bounded SemiLattice • ༗ք൒ଋ • https://qiita.com/yasuabe2613/items/ ba2c5136a4e0822b6ba4

  19. 11.3.2 Exercise: BoundedSemiLattice Instances • IntͱSet༻ͷ༗ք൒ଋΛ࣮૷ͯ͠Έ·͠ΐ͏ • Int͸ࣗવ਺(0ΛؚΉ)͔࣋ͨ͠ͳ͍ͱԾఆͯ͠Α͍

  20. 11.3.2 Exercise: BoundedSemiLattice Instances • IntͱSet༻ͷ༗ք൒ଋΛ࣮૷ͯ͠Έ·͠ΐ͏ • Int͸ࣗવ਺(0ΛؚΉ)͔࣋ͨ͠ͳ͍ͱԾఆͯ͠Α͍

  21. 11.3.3 Exercise:Generic G-Counter • MonoidͱBoundedSemiLatticeΛ࢖ͬͯGCounterΛ
 ࣮૷͠·͠ΐ͏ • Monoidͷจ๏Λ࢖ͬͯγϯϓϧʹॻ͖·͠ΐ͏

  22. Abstracting G-Counter to a Type Class • TypeClassΛ࢖ͬͯG-CounterΛ͞Βʹந৅Խ͢Δ • Map΋2ͭͷܕύϥϝʔλΛऔΔԿ͔ͳΒΑ͍ͷͰ͸

  23. Abstracting a Key Value Store • Map͸KVSʹஔ͖׵͑ΒΕΔ͸ͣ • Mapͩͬͨ෦෼Λந৅Խ͢Δ

  24. Abstracting a Key Value Store • Map͸KVSʹஔ͖׵͑ΒΕΔ͸ͣ • σʔλܕͷૢ࡞Λ֦ு͢Δimplicit class

  25. Abstracting a Key Value Store • KVS͕ந৅ԽͰ͖ͨ͜ͱͰKVSͷ࢓༷Λຬ͍ͨͯ͠Ε͹ ͲΜͳܕͰ΋G-Counter͕࡞ΕΔΑ͏ʹͳͬͨ

  26. Summary • γϯϓϧͳCRDTͰ͋ΔG-CounterΛTypeClassΛ༻͍ͯ Ͳ͏࣮૷͢Δ͔Λࣔͨ͠ • ίʔυͷ࠶ར༻ੑΛߴΊɺΧ΢ϯτ͢Δͱ͍͏ૢ࡞ʹ ͚ͩʹറΒΕͳ͘ͳͬͨ • ଞʹ΋CRDTs͸͍Ζ͍Ζ͋ΔͷͰҎԼΛࢀߟʹ •

    https://hal.inria.fr/inria-00609399v2/document
  27. None