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

認知的複雑度から見るGo言語のイベントソーシング実装 / Event Sourcing with Go

suzushin54
October 15, 2022

認知的複雑度から見るGo言語のイベントソーシング実装 / Event Sourcing with Go

Go Conference mini 2022 Autumn IN SENDAI の登壇資料です。

suzushin54

October 15, 2022
Tweet

More Decks by suzushin54

Other Decks in Programming

Transcript

  1. @suzushin 5 4 - Shinichiro 
 Go Go Conference mini

    2 0 2 2 Autumn IN SENDAI #SendaiGo
  2. 👨💻 whoami Intro @suzushin 54 - Shinichiro Go 2 C#,

    PHP [email protected] Gig 2
  3. 3

  4. 4

  5. 🍔 Intro 5 😋 👩🍳 Order Created Order Received Pickup

    Time Changed Order Completed Order Cancelled 🤵 Preparation Completed Customer Server Kitchen
 Sta f ஫ จ Ωϟϯηϧ Ω ϟϯ η ϧ ड෇ डऔ࣌ؒมߋ ड ౉ ׬ ྃ ४උ׬ྃ
  6. 🗣 Intro 😮💨 Learning Domain-Driven Design 📚 OK MSA C#

    JVM Go 6
  7. 🙇 Intro 7

  8. Intro 📋 Agenda 1 . State Sourcing and Event Sourcing

    2 . Go examples 3 . Cyclomatic complexity and Cognitive complexity 4 . Conclusion 8
  9. ʰਓ݄ͷਆ࿩ʱʢϑϨσϦοΫɾϒϧοΫεɺୌ୔ప ༁ɺؙળग़൛ɺ2014೥ʣ “ࢲʹϑϩʔνϟʔτ͚ͩΛݟͤͯɺςʔϒϧ͸ݟͤͳ͍ͱͨ͠Βɺ ࢲ͸ͣͬͱԎʹר͔Εͨ··ʹͳΔͩΖ͏ɻٯʹςʔϒϧ͕ݟͤͯ ΋Β͑ΔͳΒɺϑϩʔνϟʔτ͸͍͍ͨͯඞཁͳ͘ͳΔɻͦΕ͚ͩ ͰɺΈΜͳ໌നʹ෼͔ͬͯ͠·͏͔Βͩɻ”

  10. State sourcing

  11. 11 State Sourcing What is State Sourcing Entity CRUD API

    ID ঎඼ ݸ਺ ঢ়ଶ Version डऔ༧ఆ೔࣌ ࡞੒೔࣌ ߋ৽೔࣌ 1 ಛ্ڇλϯห౰ 2 Complete 4 2022-10-15 09:25:00 2022-10-15 09:00:00 2022-10-15 09:30:00 2 ͣΜͩγΣΠΫ 1 Ready 2 2022-10-15 15:00:00 2022-10-15 10:00:00 2022-10-15 10:15:00 3 Ή͢ͼؙห౰ 2 Create 1 2022-10-15 14:30:00 2022-10-15 10:30:00 2022-10-15 10:30:00 4 ڇλϯห౰ 4 Cancel 5 2022-10-15 13:00:00 2022-10-15 12:00:00 2022-10-15 12:40:00 Orders Order{ id: 4, status: Cancel, orderItem: “ڇλϯห౰”, 
 version: 5, pickupTime: "2022-10-15 13:00:00", orderedAt: "2022-10-15 12:00:00", } Order Entity ͷΠϝʔδ ૬ޓม׵
  12. 12 State Sourcing Orders Table (Create) (Ready) (Complete) (Cancel) 🤔

    ID: 4 ID ঎඼ ݸ਺ ঢ়ଶ Version डऔ༧ఆ೔࣌ ࡞੒೔࣌ ߋ৽೔࣌ 1 ಛ্ڇλϯห౰ 2 Complete 4 2022-10-15 09:25:00 2022-10-15 09:00:00 2022-10-15 09:30:00 2 ͣΜͩγΣΠΫ 1 Ready 2 2022-10-15 15:00:00 2022-10-15 10:00:00 2022-10-15 10:15:00 3 Ή͢ͼؙห౰ 2 Create 1 2022-10-15 14:30:00 2022-10-15 10:30:00 2022-10-15 10:30:00 4 ڇλϯห౰ 4 Cancel 5 2022-10-15 13:00:00 2022-10-15 12:00:00 2022-10-15 12:40:00 Orders
  13. Event Sourcing

  14. 14 Event Sourcing What is Event Sourcing 
 貢 SSOT

    Figure 7-1ʰLearning Domain-Driven Design: Aligning Software Architecture and Business Strategyʱ
 Vladik Khononov Oreilly & Associates Inc (2021/11/2) Interface ͸͜Μͳײ͡ Update, Delete ͸ແ͍
  15. 15 Event Sourcing What is Aggregate Eric Evans. (Japanese Edition)

    (p. 12 5 ). Kindle Edition. ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ
 Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.174
  16. 16 Event Sourcing What is Domain Event “υϝΠϯΠϕϯτͱ͸ɺϏδωευϝΠϯͰൃੜͨ͠ॏཁͳΠϕϯτΛهड़ͨ͠
 ϝοηʔδͷ͜ͱͰ͋Δɻ” ʰLearning

    Domain-Driven Design: Aligning Software Architecture and Business Strategyʱ
 Vladik Khononov Oreilly & Associates Inc (2021/11/2) “DDDͰυϝΠϯΠϕϯτͱ͍͑͹ɺΞάϦήʔτʹى͖ͨԿ͔ͷ͜ͱͰ͢ɻ
 υϝΠϯΠϕϯτ͸ɺυϝΠϯϞσϧͷΫϥεʹΑͬͯදݱ͞Ε·͢ɻ ” ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ
 Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.178
  17. 17 Event Sourcing Example of Order context 1. Order Created

    😋 υϝΠϯ
 Πϕϯτ
  18. 18 Event Sourcing Example of Order context 1. Order Created

    😋 🤵 2. Order Received ௥ه͍ͯ͘͠
  19. 19 Event Sourcing Example of Order context 2 3. Pickup

    Time Changed 4. Pickup Time Changed 5. Order Cancelled 1. Order Created 😋 2. Order Received 🤵 👩🍳 😡 ͚͍ܽͯͨετʔϦʔ͕ݟ͑ͯ͘Δ
  20. How to Restore Aggregate ? Event Sourcing 20 Order{ id:

    4, status: Cancel, orderItem: “ڇλϯห౰”, 
 version: 5, pickupTime: "2022-10-15 13:00:00", orderedAt: "2022-10-15 12:00:00", } 1 . Order structure 2 . 1 3 .
  21. Pros 😄 Event Sourcing Time traveling Deep insight Audit log

    Advanced optimistic Concurrency management 
 21 7MBEJL,IPOPOPWʰ-FBSOJOH%PNBJO%SJWFO%FTJHO"MJHOJOH4PGUXBSF"SDIJUFDUVSFBOE#VTJOFTT4USBUFHZʱ0SFJMMZ"TTPDJBUFT*OD 
  22. Cons 🫤 Event Sourcing Learning curve Evolving the model Architectural

    complexity 22 7MBEJL,IPOPOPWʰ-FBSOJOH%PNBJO%SJWFO%FTJHO"MJHOJOH4PGUXBSF"SDIJUFDUVSFBOE#VTJOFTT4USBUFHZʱ0SFJMMZ"TTPDJBUFT*OD 
  23. Go examples

  24. Sample system Go examples Controller UseCase Domain Infrastructure Layered Architecture

    HTTP Request Repository pattern (DIP) MySQL 
 24 MySQL 📱 ίʔυ͸ͪ͜Βʹ্͛ͯ͋Γ·͢ɻ https://github.com/suzushin54/event-sourcing-with-go
  25. UseCase: OrderCancel State Sourcing Go examples 25 UseCase Controller UseCase

    Domain Infrastructure EntityΛऔಘ
  26. UseCase: OrderCancel State Sourcing Go examples 26 UseCase Controller UseCase

    Domain Infrastructure ΩϟϯηϧΛ࣮ߦ
  27. UseCase: OrderCancel State Sourcing Go examples 27 UseCase Controller UseCase

    Domain Infrastructure ӬଓԽ
  28. UseCase: OrderCancel Event Sourcing Go examples 28 Controller UseCase Domain

    Infrastructure UseCase EventsΛऔಘ
  29. UseCase: OrderCancel Event Sourcing Go examples 29 Controller UseCase Domain

    Infrastructure UseCase ू໿Λ෮ݩ
  30. OrderCreatedEvent Go examples 30 Domain Controller UseCase Domain Infrastructure

  31. Entity Go examples 31 Domain Controller UseCase Domain Infrastructure (1)

    (2)
  32. Entity Go examples 32 Domain Controller UseCase Domain Infrastructure (2)

    (3)
  33. UseCase: OrderCancel Event Sourcing Go examples 33 Controller UseCase Domain

    Infrastructure UseCase Ωϟϯηϧ
 ίϚϯυΛ࡞੒
  34. What is Command? Go examples 34 CQRS Documents by Greg

    Young. http://www.minato.tv/cqrs/cqrs_documents_jp.pdf p. 11 ίϚϯυͱΠϕϯτͷҧ͍
  35. Go examples 35 Controller UseCase Domain Infrastructure Domain

  36. UseCase: OrderCancel Event Sourcing Go examples 36 Controller UseCase Domain

    Infrastructure UseCase ίϚϯυΛ࣮ߦ
  37. Go examples 37 Controller UseCase Domain Infrastructure Domain ৚݅νΣοΫ Πϕϯτ࡞੒

    append
  38. UseCase: OrderCancel Event Sourcing Go examples 38 Controller UseCase Domain

    Infrastructure UseCase ӬଓԽ
  39. Go examples Overload 39 https://github.com/vladikk/learning-ddd/blob/main/listings/07-06.cs https://github.com/vladikk/learning-ddd/blob/main/listings/07-07.cs

  40. Overloading Go examples Go - - Go - 40 Why

    does Go not support overloading of methods and operators? Method dispatch is simpli fi ed if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but di ff erent signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system. Regarding operator overloading, it seems more a convenience than an absolute requirement. Again, things are simpler without it. 
 https://go.dev/doc/faq#overloading
  41. Cyclomatic complexity and Cognitive complexity

  42. Overview Cyclomatic complexity and Cognitive Complexity 42 Cyclomatic complexity 1976

    Cognitive Complexity 2016 Software metric 1 Fortran developed by SonarSource. 
 if, for +1 
 +1 https://docs.codeclimate.com/docs/cognitive-complexity
  43. Go Conference 2 0 2 2 Spring Online Go Conference

    2022 Spring Online https://gocon.jp/ 2022 spring/ https://www.youtube.com/watch?v=GZCr-Dbi 0 YM&t= 814 s 13 : 3 4 [A 1 -C] Cognitive Complexity go/ast 43
  44. Cyclomatic complexity and Cognitive Complexity 44 https://pkg.go.dev/github.com/uudashr/gocognit#section-readme ܦ࿏ͷ਺͚ͩՃࢉ ωετΛߟྀ

  45. Score Cyclomatic complexity and Cognitive Complexity Interpretation In his presentation

    'Software Quality Metrics to Identify Risk'[ 8 ] for the Department of Homeland Security, Tom McCabe introduces the following categorisation to interpret cyclomatic complexity: • 1 - 10 Simple procedure, little risk • 1 1 - 20 More complex, moderate risk • 2 1 - 50 Complex, high risk • > 50 Untestable code, very high risk 45 https://en.wikipedia.org/wiki/Cyclomatic_complexity
  46. https://github.com/uudashr/gocognit Cognitive Complexity Gocognit Go 
 46 $ go install

    github.com/uudashr/gocognit/cmd/[email protected] # e.g. $ gocognit . $ gocognit main.go $ gocognit -top 10 src/ $ gocognit -over 25 docker $ gocognit -avg . https://github.com/golangci/golangci-lint
  47. https://github.com/uudashr/gocognit Cognitive Complexity 47 $ gocognit -top 5 ./app 5

    aggregate (*Order).Execute app/domain/aggregate/order.go:94:1 5 usecase (*orderUseCaseV1).ReceiveV1 app/usecase/receive_v1.go:16:1 5 vo (OrderItem).Equals app/domain/vo/order_item.go:26:1 5 usecase (*orderUseCaseV1).CancelV1 app/usecase/cancel_v1.go:15:1 4 controller (*OrderController).CancelOrderV2 app/controller/cancel_order_v2.go:13:1 Sample system v 1 (StateSourcing) v 2 (EventSourcing) ES
  48. 48 Controller UseCase Domain Infrastructure Domain Cognitive Complexity // +

    1 // + 2 (nesting = 1 ) // + 2 (nesting = 1 ) // Cognitive Complexity = 5
  49. 49 Controller UseCase Domain Infrastructure Domain // + 1 //

    Cognitive Complexity = 2 // + 1 Execute: 5 → 1 ExecuteCancel: 2 શମͷೝ஌తෳࡶ౓ΛԼ͛Δ͜ͱ͕Ͱ͖Δ Cognitive Complexity
  50. Conclusion Pros/Cons Go 💪 50

  51. Appendix

  52. Projection Logic ReadModel Projection Logic 52

  53. Snapshot CQRS https://docs.google.com/presentation/d/ 1 YKL 0 UPIDLN 34 A 1

    REB 4 l 7 omsXv 9 f 4 hmmcvL 4 fdwVXweA/ edit#slide=id.g 13 e 14442 5 65 _ 0 _ 3 5 5 53
  54. CQRS DB Materialized view 54

  55. EventStore RDB Event Store 55 MicrosoftSQLServer Oracle MySQL NoSQL MongoDB

    RavenDB Azure Blob . (Japanese Edition) (p. 52 5 ). Kindle Edition.