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

    View full-size slide

  2. 👨💻 whoami
    Intro
    @suzushin
    54
    - Shinichiro





    Go 2


    C#, PHP


    SWE@Showcase Gig


    2

    View full-size slide

  3. 🍔
    Intro
    5
    😋
    👩🍳
    Order Created
    Order Received
    Pickup Time Changed
    Order Completed
    Order Cancelled
    🤵
    Preparation Completed
    Customer
    Server
    Kitchen

    Sta
    f


    Ωϟϯηϧ
    Ω
    ϟϯ
    η
    ϧ
    ड෇
    डऔ࣌ؒมߋ


    ׬

    ४උ׬ྃ

    View full-size slide

  4. 🗣
    Intro




    😮💨





    Learning Domain-Driven Design 📚


    OK


    MSA


    C# JVM


    Go
    6

    View full-size slide

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


    2
    . Go examples


    3
    . Cyclomatic complexity and Cognitive complexity


    4
    . Conclusion
    8

    View full-size slide

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

    View full-size slide

  7. State sourcing

    View full-size slide

  8. 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 ͷΠϝʔδ
    ૬ޓม׵

    View full-size slide

  9. 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

    View full-size slide

  10. Event Sourcing

    View full-size slide

  11. 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 ͸ແ͍

    View full-size slide

  12. 15
    Event Sourcing
    What is Aggregate


    Eric Evans. (Japanese Edition) (p.
    12
    5
    ). Kindle Edition.
    ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ

    Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.174

    View full-size slide

  13. 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

    View full-size slide

  14. 17
    Event Sourcing
    Example of Order context
    1. Order Created
    😋
    υϝΠϯ

    Πϕϯτ

    View full-size slide

  15. 18
    Event Sourcing
    Example of Order context
    1. Order Created
    😋
    🤵
    2. Order Received
    ௥ه͍ͯ͘͠

    View full-size slide

  16. 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
    🤵
    👩🍳
    😡
    ͚͍ܽͯͨετʔϦʔ͕ݟ͑ͯ͘Δ

    View full-size slide

  17. 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
    .

    View full-size slide

  18. 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

    View full-size slide

  19. Cons 🫤
    Event Sourcing
    Learning curve



    Evolving the model






    Architectural complexity


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

    View full-size slide

  20. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. UseCase: OrderCancel State Sourcing
    Go examples
    27
    UseCase
    Controller
    UseCase
    Domain
    Infrastructure
    ӬଓԽ

    View full-size slide

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

    View full-size slide

  25. UseCase: OrderCancel Event Sourcing
    Go examples
    29
    Controller
    UseCase
    Domain
    Infrastructure
    UseCase
    ू໿Λ෮ݩ

    View full-size slide

  26. OrderCreatedEvent
    Go examples
    30
    Domain
    Controller
    UseCase
    Domain
    Infrastructure

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. UseCase: OrderCancel Event Sourcing
    Go examples
    33
    Controller
    UseCase
    Domain
    Infrastructure
    UseCase
    Ωϟϯηϧ

    ίϚϯυΛ࡞੒

    View full-size slide

  30. What is Command?
    Go examples
    34


    CQRS Documents by Greg Young. http://www.minato.tv/cqrs/cqrs_documents_jp.pdf p.
    11
    ίϚϯυͱΠϕϯτͷҧ͍


    View full-size slide

  31. Go examples
    35
    Controller
    UseCase
    Domain
    Infrastructure
    Domain

    View full-size slide

  32. UseCase: OrderCancel Event Sourcing
    Go examples
    36
    Controller
    UseCase
    Domain
    Infrastructure
    UseCase
    ίϚϯυΛ࣮ߦ

    View full-size slide

  33. Go examples
    37
    Controller
    UseCase
    Domain
    Infrastructure
    Domain
    ৚݅νΣοΫ
    Πϕϯτ࡞੒
    append

    View full-size slide

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

    View full-size slide

  35. 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

    View full-size slide

  36. 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

    View full-size slide

  37. Cyclomatic complexity and
    Cognitive complexity

    View full-size slide

  38. 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

    View full-size slide

  39. 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

    View full-size slide

  40. Cyclomatic complexity and Cognitive Complexity
    44
    https://pkg.go.dev/github.com/uudashr/gocognit#section-readme
    ܦ࿏ͷ਺͚ͩՃࢉ
    ωετΛߟྀ

    View full-size slide

  41. 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

    View full-size slide

  42. https://github.com/uudashr/gocognit
    Cognitive Complexity
    Gocognit Go

    46
    $ go install github.com/uudashr/gocognit/cmd/gocognit@latest

    # e.g.

    $ gocognit .

    $ gocognit main.go

    $ gocognit -top 10 src/

    $ gocognit -over 25 docker

    $ gocognit -avg .
    https://github.com/golangci/golangci-lint

    View full-size slide

  43. 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

    View full-size slide

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

    View full-size slide

  45. 49
    Controller
    UseCase
    Domain
    Infrastructure
    Domain
    // +
    1
    // Cognitive Complexity =
    2
    // +
    1
    Execute: 5 → 1

    ExecuteCancel: 2

    શମͷೝ஌తෳࡶ౓ΛԼ͛Δ͜ͱ͕Ͱ͖Δ
    Cognitive Complexity

    View full-size slide

  46. Conclusion
    Pros/Cons


    Go






    💪
    50

    View full-size slide

  47. Projection Logic ReadModel
    Projection Logic
    52


    View full-size slide

  48. 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

    View full-size slide

  49. CQRS
    DB


    Materialized view
    54

    View full-size slide

  50. EventStore RDB
    Event Store
    55
    MicrosoftSQLServer Oracle MySQL
    NoSQL MongoDB RavenDB Azure Blob


    . (Japanese Edition) (p.
    52 5
    ). Kindle Edition.

    View full-size slide