Slide 1

Slide 1 text

PHPͰΞΫλʔϞσϧΛ ׆༻ͨ͠ Sagaύλʔϯͷ࣮ફ๏ PHPerKaigi 2025 ytake

Slide 2

Slide 2 text

Pro fi le • ஛ᖒ ༗و a.k.a ytake • ઍגࣜձࣾ CTO גࣜձࣾACESɺגࣜձࣾωοτϓϩςΫγϣϯζٕज़ސ໰ • Go / Scala / Kotlin

Slide 3

Slide 3 text

https://github.com/ytake/ phluxor-saga-example

Slide 4

Slide 4 text

SagaύλʔϯͬͯͳΜͩʁ

Slide 5

Slide 5 text

Sagaύλʔϯͱ͸ • ෳ਺ͷαʔϏεʢ෺ཧతʹ෼͔Ε͍ͯΔ΋ͷʣʹ·͕ͨΔॲཧͰ Ұͭʹ·ͱ·ͬͨτϥϯβΫγϣϯͷΑ͏ʹѻ͏ͨΊͷύλʔϯ • ్தͷॲཧ͕ࣦഊͨ͠৔߹ɺ γεςϜશମΛໃ६ͷͳ͍ঢ়ଶʹ໭ͨ͢Ίͷ ʮิঈτϥϯβΫγϣϯʯΛ࣮ߦ

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

X

Slide 10

Slide 10 text

X

Slide 11

Slide 11 text

ฦ඼ • ϨγʔτΛ΋ͱʹɺฦ඼ɾฦۚͯ͠΋Β͏ ߪೖͷࣄ࣮͕࢒͍ͬͯΔ • ঎඼͸͓ళʹ໭Γɺ͓ۚ͸ࣗ෼ͷखݩʹฦͬͯ͘Δ

Slide 12

Slide 12 text

࣮ફ͢Δʹ͸ɾɾɾʁ

Slide 13

Slide 13 text

ࣦഊ͔Βͷճ෮ઓུ • ࣦഊͨ͠৔߹ʹલͷঢ়ଶʹ໭ͤΔ͜ͱ • ్தͷॲཧ͕ࣦഊͨ͠৔߹ɺϦτϥΠ͢Δ • ଞͷϥϯλΠϜͰॲཧ͕ߦΘΕͯ΋ໃ६͕ൃੜ͠ͳ͍Α͏ʹ͢Δ

Slide 14

Slide 14 text

࣮૷͢Δ৔߹ • ΠϕϯτιʔγϯάͰલͷঢ়ଶʹ໭ͤΔΑ͏ʹ • Πϕϯτ͸ঢ়ଶͷεφοϓγϣοτʹ • ෭໰߹ͤΛߦͬͯঢ়ଶ෮ݩ͢Δܗʹ͸͠ͳ͍ʂ

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

ؾΛ͚ͭΑ͏ • ର৅ͷ΋ͷҎ֎͸௨ৗʹՔಇ • ଞͷ΋ͷΛελοΫͤ͞ͳ͍Α͏ʹ • ࣌ܥྻ͕લޙ͠ͳ͍Α͏ʹ

Slide 19

Slide 19 text

ૹۚͰߟ͑ͯΈΑ͏

Slide 20

Slide 20 text

γφϦΦ

Slide 21

Slide 21 text

ۚમৼସγφϦΦ • Account1 ͔Β Account2 ΁ 10ԁΛૹۚ͢ΔγφϦΦ • ޱ࠲͕ϦϞʔταʔϏεͱͯ͠ಈ࡞͢Δ৔߹ʹى͜Γ͏Δ ϦΫΤετڋ൱ɺΫϥογϡɺϏδʔঢ়ଶɺλΠϜΞ΢τͳͲΛ ߟྀ

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

੒ޭ • Account1 ͷ࢒ߴ͸ 0 ԁɺAccount2 ͷ࢒ߴ͸ 20 ԁ • ظ଴௨Γʹ͓ޓ͍ͷॲཧ͕ਖ਼ৗʹऴΘΔ

Slide 24

Slide 24 text

ࣦഊ • Account1 ͱ Account2 ͷ࢒ߴ͸ͦΕͧΕ 10 ԁ (Ұ؏ੑͷ͋Δঢ়ଶ) • ظ଴௨Γʹ͓ޓ͍ͷঢ়ଶ͕΋ͱʹ໭Δ

Slide 25

Slide 25 text

ࣦഊγφϦΦ΁ͷରԠ:ϦτϥΠ • Ұ࣌తͳΤϥʔʢϏδʔঢ়ଶɺλΠϜΞ΢τʣʹରͯ͠ɺ Ұఆճ਺ϦτϥΠΛࢼΈΔ

Slide 26

Slide 26 text

ิঈॲཧ (ϩʔϧόοΫ) • Debit ͕੒ޭ͕ͨ͠ Credit ͕ࣦഊͨ͠৔߹ɺ Debit ΛऔΓফͨ͢Ίʹ Credit Λ Account1 ʹ໭͢

Slide 27

Slide 27 text

ΤεΧϨʔγϣϯ • ϦτϥΠ΍ิঈॲཧ͕͏·͍͔͘ͳ͍৔߹ɺ खಈհೖ͕ඞཁͱͳΔՄೳੑ͕͋ΔͨΊɺ ΤεΧϨʔγϣϯઓུΛઃ͚Δ

Slide 28

Slide 28 text

ႈ౳ੑ • Account αʔϏε͕ϦτϥΠ࣌ʹಉ͡ϝοηʔδΛ ෳ਺ճड͚औͬͯ΋ಉ݁͡Ռ͕ಘΒΕΔΑ͏ʹɺ ႈ౳Ͱ͋Δඞཁ͕͋Γ • ॲཧࡁΈͷϝοηʔδΛอ؅͠ɺ ॏෳͨ͠ϝοηʔδʹରͯ͠͸ಉ͡Ԡ౴Λฦ͢

Slide 29

Slide 29 text

ΦʔέετϨʔγϣϯͷSaga࣮૷

Slide 30

Slide 30 text

ΞΫλʔϞσϧͰ࣮ફʂ

Slide 31

Slide 31 text

Phluxor

Slide 32

Slide 32 text

Similar to akka, pekko and Proto Actor

Slide 33

Slide 33 text

ɹPHPΛ௨ͯ͡ ΞΫλʔϞσϧ͕ ମݧͰ͖ΔπʔϧΩοτ *঎༻΍࣮ફ͸ଞݴޠΛ͓નΊ͠·͢

Slide 34

Slide 34 text

ΞΫλʔϞσϧͷ͓͞Β͍

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

ߏ੒ • Runner - సૹϓϩηεΛ։࢝͠ɺ݁ՌΛऩू͢ΔओཁͳΞΫλʔ • Account - ࢒ߴΛ࣋ͪɺೖۚ/ग़ۚૢ࡞Λॲཧ͢ΔΞΫλʔ • TransferProcess - ΞΧ΢ϯτؒͷૹۚΛ੍ޚ͢ΔΞΫλʔ • AccountProxy - TransferProcessͱAccountΞΫλʔؒͷ௨৴Λ஥հ

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

TransferProcessΞΫλʔ

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

TransferProcess ΞΫλʔ • సૹΛ؅ཧ͢Δ੹೚Λ࣋ͭ • Process Manager • ׬ྃͨ͠εςοϓΛ௥੻͠ɺࣦഊ࣌ʹิঈાஔΛద༻ • BehaviorɺPersistenceɺSupervisionར༻ʂ

Slide 44

Slide 44 text

஌Βͳ͍༻ޠ͹͔Γɾɾ

Slide 45

Slide 45 text

࣮ફ͍ͨ͠ํͷͨΊʹղઆʂ

Slide 46

Slide 46 text

Process Manager

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

ΑΓཧղ͢ΔͨΊʹ

Slide 49

Slide 49 text

Behavior

Slide 50

Slide 50 text

Behavior • ΞΫλʔ͕ड৴͢Δϝοηʔδʹର͢Δॲཧͷఆٛ • ΞΫλʔ͸ ঢ়ଶΛ࣋ͨͣʢεςʔτϨεʣͰ͸ͳ͍ • ΞΫλʔ͸ঢ়ଶΛ࣋ͪಈతʹ ৼΔ෣͍Λมߋ ͢Δ

Slide 51

Slide 51 text

Become / Unbecome • Become ৽͍͠ৼΔ෣͍ʹʮมԽʯ͢Δʢྫ: ঢ়ଶભҠΛ൐͏ಈ࡞ʣ มԽҎ߱ͷϝοηʔδ͸ ৽͍͠ৼΔ෣͍Ͱॲཧ • Unbecome ҎલͷৼΔ෣͍ʹʮ໭Δʯɺ·ͨ͸σϑΥϧτͷಈ࡞ʹϦηοτ

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

class TransferProcess implements ActorInterface, PersistentInterface { use Mixin; private bool $processCompleted = false; private bool $restarting = false; private bool $stopping = false; public function __construct( private readonly Ref $from, private readonly Ref $to, private readonly float $amount, private readonly float $availability, private readonly Behavior $behavior = new Behavior() ) { $this->behavior->become( new ReceiveFunction( fn($context) => $this->starting($context) ) ); }

Slide 55

Slide 55 text

class TransferProcess implements ActorInterface, PersistentInterface { use Mixin; private bool $processCompleted = false; private bool $restarting = false; private bool $stopping = false; public function __construct( private readonly Ref $from, private readonly Ref $to, private readonly float $amount, private readonly float $availability, private readonly Behavior $behavior = new Behavior() ) { $this->behavior->become( new ReceiveFunction( fn($context) => $this->starting($context) ) ); } 1FSTJTUFODFΛ࢖͏͜ͱͰɺ ΞΫλʔࣗ਎͕Ͳ͏ͳ͔ͬͨΛอ؅ ͭ·Γ&WFOU4PVSDJOH

Slide 56

Slide 56 text

class TransferProcess implements ActorInterface, PersistentInterface { use Mixin; private bool $processCompleted = false; private bool $restarting = false; private bool $stopping = false; public function __construct( private readonly Ref $from, private readonly Ref $to, private readonly float $amount, private readonly float $availability, private readonly Behavior $behavior = new Behavior() ) { $this->behavior->become( new ReceiveFunction( fn($context) => $this->starting($context) ) ); } 1IMVYPSͰ͸.JYJO5SBJUʹجຊ࣮૷͋Γ ར༻͢Δͱঢ়ଶ෮ݩͳͲΛࣗಈͰߦ͏

Slide 57

Slide 57 text

class TransferProcess implements ActorInterface, PersistentInterface { use Mixin; private bool $processCompleted = false; private bool $restarting = false; private bool $stopping = false; public function __construct( private readonly Ref $from, private readonly Ref $to, private readonly float $amount, private readonly float $availability, private readonly Behavior $behavior = new Behavior() ) { $this->behavior->become( new ReceiveFunction( fn($context) => $this->starting($context) ) ); } ΞΫλʔͷৼΔ෣͍Λมߋ͢Δ #FIBJWPS

Slide 58

Slide 58 text

class TransferProcess implements ActorInterface, PersistentInterface { use Mixin; private bool $processCompleted = false; private bool $restarting = false; private bool $stopping = false; public function __construct( private readonly Ref $from, private readonly Ref $to, private readonly float $amount, private readonly float $availability, private readonly Behavior $behavior = new Behavior() ) { $this->behavior->become( new ReceiveFunction( fn($context) => $this->starting($context) ) ); } ͜ͷΞΫλʔͷॳظঢ়ଶͱͯ͠ TUBSUJOHϝιου͕ಈ͘

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

private function starting(ContextInterface $context): void { if ($context->message() instanceof Started) { $context->spawnNamed($this->tryDebit($this->from, -$this->amount), 'DebitAttempt'); $this->persistEvent(new ProtoBuf\TransferStarted()); } } private function tryDebit(Ref $targetActor, float $amount): Props { return Props::fromProducer( fn() => new AccountProxy( $targetActor, fn($sender) => new Debit($amount, $sender) ) ); }

Slide 61

Slide 61 text

private function starting(ContextInterface $context): void { if ($context->message() instanceof Started) { $context->spawnNamed($this->tryDebit($this->from, -$this->amount), 'DebitAttempt'); $this->persistEvent(new ProtoBuf\TransferStarted()); } } private function tryDebit(Ref $targetActor, float $amount): Props { return Props::fromProducer( fn() => new AccountProxy( $targetActor, fn($sender) => new Debit($amount, $sender) ) ); } %FCJUΞΫλʔΛੜ੒

Slide 62

Slide 62 text

private function starting(ContextInterface $context): void { if ($context->message() instanceof Started) { $context->spawnNamed($this->tryDebit($this->from, -$this->amount), 'DebitAttempt'); $this->persistEvent(new ProtoBuf\TransferStarted()); } } private function tryDebit(Ref $targetActor, float $amount): Props { return Props::fromProducer( fn() => new AccountProxy( $targetActor, fn($sender) => new Debit($amount, $sender) ) ); } సૹ։࢝ΠϕϯτΛอ؅

Slide 63

Slide 63 text

private function starting(ContextInterface $context): void { if ($context->message() instanceof Started) { $context->spawnNamed($this->tryDebit($this->from, -$this->amount), 'DebitAttempt'); $this->persistEvent(new ProtoBuf\TransferStarted()); } } private function tryDebit(Ref $targetActor, float $amount): Props { return Props::fromProducer( fn() => new AccountProxy( $targetActor, fn($sender) => new Debit($amount, $sender) ) ); } "DDPVOU1SPYZΛੜ੒͠ɺ ग़ۚϝοηʔδΛૹ৴

Slide 64

Slide 64 text

Persistence

Slide 65

Slide 65 text

Persistence • ΞΫλʔͷӬଓԽΛߦ͏ • ΞΫλʔͷϥΠϑαΠΫϧͱରԠ͓ͯ͠Γɺ ىಈ࣌ʹӬଓԽ͞Εͨঢ়ଶʹΞΫλʔΛ෮ݩ͢Δ • δϟʔφϧͱεφοϓγϣοτΛ૊Έ߹Θͤͯ࠷୹Ͱ෮ݩ͞ΕΔ • ࠷৽ঢ়ଶΛ͚࣋ͭͩɺͰ͸ͳ͍ͷ͕ϙΠϯτ

Slide 66

Slide 66 text

PersistEvent

Slide 67

Slide 67 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } }

Slide 68

Slide 68 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } } ͍͔ͭ͘ͷΠϕϯτΛӬଓԽͨ͠ޙʹ ࣗΞΫλʔͷৼΔ෣͍Λมߋ͢Δ

Slide 69

Slide 69 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } } ૹ͕ۚ։࢝͢Δͱɺ Ҿ͖མͱ͠ʹؔ͢ΔৼΔ෣͍ʹมԽ

Slide 70

Slide 70 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } } ೖۚʹؔ͢ΔৼΔ෣͍ʹมԽ

Slide 71

Slide 71 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } } ೖ͕ࣦۚഊ͢Δͱ ϩʔϧόοΫͷৼΔ෣͍ʹ

Slide 72

Slide 72 text

private function applyEvent(Message $event): void { switch (true) { case $event instanceof ProtoBuf\TransferStarted: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingDebitConfirmation($context) ) ); break; case $event instanceof ProtoBuf\AccountDebited: $this->behavior->become( new ReceiveFunction( fn($context) => $this->awaitingCreditConfirmation($context) ) ); break; case $event instanceof ProtoBuf\CreditRefused: $this->behavior->become( new ReceiveFunction( fn($context) => $this->rollingBackDebit($context) ) ); break; case $event instanceof ProtoBuf\AccountCredited: case $event instanceof ProtoBuf\DebitRolledBack: case $event instanceof ProtoBuf\TransferFailed: $this->processCompleted = true; break; } } ਖ਼ৗʹೖग़͕ۚ׬ྃɺ ϩʔϧόοΫ͕׬ྃɺ ࢦఆճ਺Ҏ্ૹۚʹࣦഊ͢Δͱऴྃ

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Supervision

Slide 75

Slide 75 text

Supervision • ΞΫλʔ͸਌ࢠؔ܎Λ࣋ͪɺ਌͸ࢠΛ؂ࢹ͢Δ੹຿Λ࣋ͭ • ࢠΞΫλʔͷΤϥʔൃੜ࣌ɺ਌ΞΫλʔ͕ઓུʹج͖ͮ ࢠΞΫλʔͷ࠶ىಈ΍෮چΛ؅ཧ • ਌͕μ΢ϯ͢Δͱ഑ԼͷࢠΞΫλʔ΋μ΢ϯ

Slide 76

Slide 76 text

Supervision Strategies • OneForOneStrategy • AllForOneStrategy • ExponentialBackoffStrategy • RestartStrategy

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } }

Slide 80

Slide 80 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } } ΞΫλʔੜ੒࣌ʹ ো֐ઓུΛࢦࣔ

Slide 81

Slide 81 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } } 0OF'PS0OF͸ ࢠΞΫλʔͷ͏ͪର৅ͷҰ͚ͭͩ

Slide 82

Slide 82 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } } ඵؒͷ͏ͪࢦఆճ਺ ճ ෼ ֘౰ͷΞΫλʔΛ࠶ىಈ͢Δ

Slide 83

Slide 83 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } } ϝοηʔδΛอ؅ͯ͠ ΞΫλʔΛ෮ݩ͢Δ

Slide 84

Slide 84 text

readonly class TransferFactory { public function __construct( private ContextInterface $context, private float $availability, private int $retryAttempts, private ProviderInterface $provider, ) { } public function createTransfer( string $actorName, Ref $fromAccount, Ref $toAccount, float $amount, ): SpawnResult { $props = Props::fromProducer( fn() => new TransferProcess($fromAccount, $toAccount, $amount, $this->availability), Props::withReceiverMiddleware( new EventSourcedFactory($this->provider) ), Props::withSupervisor( new OneForOneStrategy( $this->retryAttempts, new DateInterval('PT10S'), new DefaultDecider(), ) ) ); return $this->context->spawnNamed($props, $actorName); } } ࢠΞΫλʔΛ࠶ىಈ͢Δͱɺ ϝοηʔδʹରԠͨ͠ॲཧΛ಺෦Ͱߦ͍ ࠷৽ঢ়ଶʹ෮ݩ ো֐ൃੜ࣌ͷঢ়ଶʹ໭Δʂ

Slide 85

Slide 85 text

TransferProcess + State Machine + Persistence

Slide 86

Slide 86 text

TransferProcess ͸ฒߦͰଘࡏ • ֤ग़ۚ͝ͱʹಠཱͯ͠ੜ੒ɾঢ়ଶ΋ڞ༗͞Εͳ͍ • ࢦఆ͞Εͨϝοηʔδ͕དྷͳ͍ݶΓৼΔ෣͍͸มΘΒͳ͍ɾ໭Βͳ͍ • ӬଓԽ͞Ε͍ͯΔͨΊɺ࠶ىಈ͢ΔͱͦΕͧΕͷ࠷৽ঢ়ଶ΁෮ݩ • Error Kernel Patternར༻Ͱো֐Λ੾Γ཭͠

Slide 87

Slide 87 text

Error Kernel Pattern?

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

AccountΞΫλʔ

Slide 90

Slide 90 text

AccountΞΫλʔ • ग़ۚͱೖ͕ۚͦΕͧΕݸͷΞΫλʔͰɺ FromAccount{*} / ToAccount{*} ͱಠཱͯ͠ੜ੒͞ΕΔ • ͦΕͧΕcoroutineͰ੍ޚ͞ΕΔΞΫλʔͰ ϝοηʔδͷ΍ΓऔΓΛߦ͏

Slide 91

Slide 91 text

class Account implements ActorInterface { private float $balance = 10.0; private array $processedMessages = []; public function __construct( private readonly float $serviceUptime, private readonly float $refusalProbability, private readonly float $busyProbability ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Message\Credit: case $message instanceof Message\Debit: $this->handleBalanceChange($context, $message); break; case $message instanceof Message\GetBalance: $context->respond($this->balance); break; } }

Slide 92

Slide 92 text

class Account implements ActorInterface { private float $balance = 10.0; private array $processedMessages = []; public function __construct( private readonly float $serviceUptime, private readonly float $refusalProbability, private readonly float $busyProbability ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Message\Credit: case $message instanceof Message\Debit: $this->handleBalanceChange($context, $message); break; case $message instanceof Message\GetBalance: $context->respond($this->balance); break; } } ΞΫλʔʹϝοηʔδ͕౸ୡ͢Δͱ ࣮ߦ͞ΕΔSFDFJWFϝιου

Slide 93

Slide 93 text

class Account implements ActorInterface { private float $balance = 10.0; private array $processedMessages = []; public function __construct( private readonly float $serviceUptime, private readonly float $refusalProbability, private readonly float $busyProbability ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Message\Credit: case $message instanceof Message\Debit: $this->handleBalanceChange($context, $message); break; case $message instanceof Message\GetBalance: $context->respond($this->balance); break; } } ೖग़ۚϝοηʔδʹରԠ

Slide 94

Slide 94 text

class Account implements ActorInterface { private float $balance = 10.0; private array $processedMessages = []; public function __construct( private readonly float $serviceUptime, private readonly float $refusalProbability, private readonly float $busyProbability ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Message\Credit: case $message instanceof Message\Debit: $this->handleBalanceChange($context, $message); break; case $message instanceof Message\GetBalance: $context->respond($this->balance); break; } } ࢒ߴௐ੔ʹؔ͢Δॲཧ

Slide 95

Slide 95 text

class Account implements ActorInterface { private float $balance = 10.0; private array $processedMessages = []; public function __construct( private readonly float $serviceUptime, private readonly float $refusalProbability, private readonly float $busyProbability ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Message\Credit: case $message instanceof Message\Debit: $this->handleBalanceChange($context, $message); break; case $message instanceof Message\GetBalance: $context->respond($this->balance); break; } } ࢒ߴ֬ೝͷϝοηʔδ͕དྷΔͱ ૹ৴ݩʹฦ٫

Slide 96

Slide 96 text

private function handleBalanceChange( ContextInterface $context, Message\ChangeBalance $message ): void { if ($this->alreadyProcessed($message->replyTo)) { $context->send($message->replyTo, $this->processedMessages[(string) $message->replyTo]); return; } if ($message instanceof Message\Debit && ($message->amount + $this->balance) < 0) { $context->send($message->replyTo, new Message\InsufficientFunds()); return; } if ($this->refusePermanently()) { $this->processedMessages[(string) $message->replyTo] = new Message\Refused(); $context->send($message->replyTo, new Message\Refused()); return; } if ($this->isBusy()) { $context->send($message->replyTo, new Message\ServiceUnavailable()); return; } if ($this->shouldFailBeforeProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } usleep(random_int(0, 150) * 1000); $this->balance += $message->amount; $this->processedMessages[(string) $message->replyTo] = new Message\Ok(); if ($this->shouldFailAfterProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } $context->send($message->replyTo, new Message\Ok()); }

Slide 97

Slide 97 text

private function handleBalanceChange( ContextInterface $context, Message\ChangeBalance $message ): void { if ($this->alreadyProcessed($message->replyTo)) { $context->send($message->replyTo, $this->processedMessages[(string) $message->replyTo]); return; } if ($message instanceof Message\Debit && ($message->amount + $this->balance) < 0) { $context->send($message->replyTo, new Message\InsufficientFunds()); return; } if ($this->refusePermanently()) { $this->processedMessages[(string) $message->replyTo] = new Message\Refused(); $context->send($message->replyTo, new Message\Refused()); return; } if ($this->isBusy()) { $context->send($message->replyTo, new Message\ServiceUnavailable()); return; } if ($this->shouldFailBeforeProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } usleep(random_int(0, 150) * 1000); $this->balance += $message->amount; $this->processedMessages[(string) $message->replyTo] = new Message\Ok(); if ($this->shouldFailAfterProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } $context->send($message->replyTo, new Message\Ok()); } ࢒ߴௐ੔ʹؔ͢Δॲཧ

Slide 98

Slide 98 text

private function handleBalanceChange( ContextInterface $context, Message\ChangeBalance $message ): void { if ($this->alreadyProcessed($message->replyTo)) { $context->send($message->replyTo, $this->processedMessages[(string) $message->replyTo]); return; } if ($message instanceof Message\Debit && ($message->amount + $this->balance) < 0) { $context->send($message->replyTo, new Message\InsufficientFunds()); return; } if ($this->refusePermanently()) { $this->processedMessages[(string) $message->replyTo] = new Message\Refused(); $context->send($message->replyTo, new Message\Refused()); return; } if ($this->isBusy()) { $context->send($message->replyTo, new Message\ServiceUnavailable()); return; } if ($this->shouldFailBeforeProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } usleep(random_int(0, 150) * 1000); $this->balance += $message->amount; $this->processedMessages[(string) $message->replyTo] = new Message\Ok(); if ($this->shouldFailAfterProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } $context->send($message->replyTo, new Message\Ok()); } Ҿ਺ͰͦΕͧΕͷঢ়ଶΛ γϛϡϨʔτ

Slide 99

Slide 99 text

private function handleBalanceChange( ContextInterface $context, Message\ChangeBalance $message ): void { if ($this->alreadyProcessed($message->replyTo)) { $context->send($message->replyTo, $this->processedMessages[(string) $message->replyTo]); return; } if ($message instanceof Message\Debit && ($message->amount + $this->balance) < 0) { $context->send($message->replyTo, new Message\InsufficientFunds()); return; } if ($this->refusePermanently()) { $this->processedMessages[(string) $message->replyTo] = new Message\Refused(); $context->send($message->replyTo, new Message\Refused()); return; } if ($this->isBusy()) { $context->send($message->replyTo, new Message\ServiceUnavailable()); return; } if ($this->shouldFailBeforeProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } usleep(random_int(0, 150) * 1000); $this->balance += $message->amount; $this->processedMessages[(string) $message->replyTo] = new Message\Ok(); if ($this->shouldFailAfterProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } $context->send($message->replyTo, new Message\Ok()); } ϝοηʔδʹؚ·ΕΔૹ৴ઌʹ ฦ৴

Slide 100

Slide 100 text

private function handleBalanceChange( ContextInterface $context, Message\ChangeBalance $message ): void { if ($this->alreadyProcessed($message->replyTo)) { $context->send($message->replyTo, $this->processedMessages[(string) $message->replyTo]); return; } if ($message instanceof Message\Debit && ($message->amount + $this->balance) < 0) { $context->send($message->replyTo, new Message\InsufficientFunds()); return; } if ($this->refusePermanently()) { $this->processedMessages[(string) $message->replyTo] = new Message\Refused(); $context->send($message->replyTo, new Message\Refused()); return; } if ($this->isBusy()) { $context->send($message->replyTo, new Message\ServiceUnavailable()); return; } if ($this->shouldFailBeforeProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } usleep(random_int(0, 150) * 1000); $this->balance += $message->amount; $this->processedMessages[(string) $message->replyTo] = new Message\Ok(); if ($this->shouldFailAfterProcessing()) { $context->send($message->replyTo, new Message\InternalServerError()); return; } $context->send($message->replyTo, new Message\Ok()); } ΞΫλʔࣗ਎͕ঢ়ଶ؅ཧ

Slide 101

Slide 101 text

AccountProxy ΞΫλʔ • TransferProcessΞΫλʔʹੜ੒͞ΕΔΞΫλʔ • AccountΞΫλʔͱͷ௨৴Λ୲౰͢ΔΞΫλʔ • AccountΞΫλʔʹԿ͔͕ى͖ͨ৔߹ʹ਌Ͱ͋Δ TransferProcessʹରԠΛҕৡ͢Δʢ࠶ىಈͳͲΛߦ͏ʣ

Slide 102

Slide 102 text

Error Kernel Pattern • Reactive Design Patterns • ো֐ͱ৴པੑͷཁ͕͍݅ࠞͬͯ͟Δ৔߹ʹ ར༻Ͱ͖Δύλʔϯ(Akka,OTPͳͲͰ͸͓ͳ͡Έ) • ఀࢭͯ͠͸͍͚ͳ͍΋ͷ͕͍ࠞͬͯ͟Δ৔߹ʹִ཭͢Δ

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

4BHB࣮ߦ

Slide 105

Slide 105 text

5SBOTGFS1SPDFTT͕਌

Slide 106

Slide 106 text

"DDPVOU1SPYZͱ"DDPVOU͸ ਌ࢠؔ܎Ͱ͸ͳ͍ "DDPVOU1SPYZ͕μ΢ϯͯ͠΋ "DDPVOU͸Өڹͳ͠

Slide 107

Slide 107 text

"DDPVOUΞΫλʔͷ ࣮ߦͰ͖ͳ͍ཧ༝Λड৴ ࣗ਎Λμ΢ϯͤ͞Δ

Slide 108

Slide 108 text

ࢠΞΫλʔͷμ΢ϯΛݕ஌ ઓུʹج͍ͮͯ "DDPVOU1SPYZΞΫλʔ࠶ੜ੒ ࠶ॲཧ࣮ߦ

Slide 109

Slide 109 text

௨৴ͱೖग़ۚΛ෼཭͍ͯ͠Δύλʔϯ ΞΫλʔϞσϧͳΒͰ͸ͷ΋ͷ

Slide 110

Slide 110 text

readonly class AccountProxy implements ActorInterface { public function __construct( private Ref $target, private Closure $createMessage ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Started: $context->send($this->target, ($this->createMessage)($context->self())); $context->setReceiveTimeout(new DateInterval('PT2S')); break; case $message instanceof Refused: case $message instanceof Ok: $context->cancelReceiveTimeout(); $context->send($context->parent(), $message); break; case $message instanceof InsufficientFunds: case $message instanceof InternalServerError: case $message instanceof ReceiveTimeout: case $message instanceof ServiceUnavailable: throw new RuntimeException('Unexpected message'); } } }

Slide 111

Slide 111 text

readonly class AccountProxy implements ActorInterface { public function __construct( private Ref $target, private Closure $createMessage ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Started: $context->send($this->target, ($this->createMessage)($context->self())); $context->setReceiveTimeout(new DateInterval('PT2S')); break; case $message instanceof Refused: case $message instanceof Ok: $context->cancelReceiveTimeout(); $context->send($context->parent(), $message); break; case $message instanceof InsufficientFunds: case $message instanceof InternalServerError: case $message instanceof ReceiveTimeout: case $message instanceof ServiceUnavailable: throw new RuntimeException('Unexpected message'); } } } "DUPS͕ىಈ͢ΔͱྲྀΕͯ͘Δ ಺෦ϝοηʔδ 4UBSUFE

Slide 112

Slide 112 text

readonly class AccountProxy implements ActorInterface { public function __construct( private Ref $target, private Closure $createMessage ) { } public function receive(ContextInterface $context): void { $message = $context->message(); switch (true) { case $message instanceof Started: $context->send($this->target, ($this->createMessage)($context->self())); $context->setReceiveTimeout(new DateInterval('PT2S')); break; case $message instanceof Refused: case $message instanceof Ok: $context->cancelReceiveTimeout(); $context->send($context->parent(), $message); break; case $message instanceof InsufficientFunds: case $message instanceof InternalServerError: case $message instanceof ReceiveTimeout: case $message instanceof ServiceUnavailable: throw new RuntimeException('Unexpected message'); } } } -FU*U$SBTI ͜Ε·Ͱͷ1)1ͱҟͳΔߟ͑ํ

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

RunnerΞΫλʔ

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

RunnerΞΫλʔ • SagaΛ࣮ߦ͢Δ໾ׂ • Scatter Gather PatternͰ͢΂͕ͯऴΘΔ·Ͱ଴ͭ • ࢦఆ਺෼AccountΞΫλʔͱTransferProcessΞΫλʔੜ੒ • TransferProcessΞΫλʔͷϦϑΝϨϯεΛRunner͕อ࣋

Slide 118

Slide 118 text

Scatter Gatherʁ

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

Scatter Gather • ෳ਺ʹϒϩʔυΩϟετޙɺฦ৴ΛҰͭʹू໿͢Δ • ΞΫλʔϞσϧͳΒͰ͸ͷύλʔϯ • ϩʔΧϧ΍ϦϞʔτͱͷ૊Έ߹ΘͤͰར༻

Slide 121

Slide 121 text

$inMemoryProvider = $this->inMemoryProvider(); (new ForWithProgress( $this->numberOfIterations, $this->intervalBetweenConsoleUpdates, true, false ))->everyNth( fn($i) => print("Started {$i}/{$this->numberOfIterations} processes\n"), function ($i, $nth) use ($context, $inMemoryProvider) { $fromAccount = $this->createAccount($context, "FromAccount{$i}"); $toAccount = $this->createAccount($context, "ToAccount{$i}"); $actorName = "Transfer Process {$i}"; $factory = new TransferFactory( $context, $this->uptime, $this->retryAttempts, $inMemoryProvider ); $transfer = $factory->createTransfer($actorName, $fromAccount, $toAccount, 10); $this->transfers[] = (string) $transfer->getRef(); if ($i === $this->numberOfIterations && !$nth) { print("Started {$i}/{$this->numberOfIterations} processes\n"); } } );

Slide 122

Slide 122 text

$inMemoryProvider = $this->inMemoryProvider(); (new ForWithProgress( $this->numberOfIterations, $this->intervalBetweenConsoleUpdates, true, false ))->everyNth( fn($i) => print("Started {$i}/{$this->numberOfIterations} processes\n"), function ($i, $nth) use ($context, $inMemoryProvider) { $fromAccount = $this->createAccount($context, "FromAccount{$i}"); $toAccount = $this->createAccount($context, "ToAccount{$i}"); $actorName = "Transfer Process {$i}"; $factory = new TransferFactory( $context, $this->uptime, $this->retryAttempts, $inMemoryProvider ); $transfer = $factory->createTransfer($actorName, $fromAccount, $toAccount, 10); $this->transfers[] = (string) $transfer->getRef(); if ($i === $this->numberOfIterations && !$nth) { print("Started {$i}/{$this->numberOfIterations} processes\n"); } } ); 'SPN"DDPVOU9ͱ5P"DDPVOU9ͱ͍͏໊લͰ "DDPVOUΞΫλʔΛੜ੒

Slide 123

Slide 123 text

$inMemoryProvider = $this->inMemoryProvider(); (new ForWithProgress( $this->numberOfIterations, $this->intervalBetweenConsoleUpdates, true, false ))->everyNth( fn($i) => print("Started {$i}/{$this->numberOfIterations} processes\n"), function ($i, $nth) use ($context, $inMemoryProvider) { $fromAccount = $this->createAccount($context, "FromAccount{$i}"); $toAccount = $this->createAccount($context, "ToAccount{$i}"); $actorName = "Transfer Process {$i}"; $factory = new TransferFactory( $context, $this->uptime, $this->retryAttempts, $inMemoryProvider ); $transfer = $factory->createTransfer($actorName, $fromAccount, $toAccount, 10); $this->transfers[] = (string) $transfer->getRef(); if ($i === $this->numberOfIterations && !$nth) { print("Started {$i}/{$this->numberOfIterations} processes\n"); } } ); 5SBOTGFS1SPDFTTΞΫλʔʹ ̎ͭͷ"DDPVOUΞΫλʔϦϑΝϨϯε "DDPVOUΞΫλʔؒͰ૬ޓॲཧ

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

ΤϯτϦϙΠϯτ • ΞΫλʔγεςϜͷىಈ • RunnerΞΫλʔͷੜ੒ͱ؂ࢹɺ Runnerͷ࠶ࢼߦʹؔ͢Δ੹຿Λ࣋ͭ • ͦΕҎ֎͸Կ΋ͳ͠ʂ

Slide 126

Slide 126 text

go(function () { $system = ActorSystem::create(); $system->getLogger()->info('Starting'); $numberOfTransfers = 10; $intervalBetweenConsoleUpdates = 1; $uptime = 99.99; $retryAttempts = 3; $refusalProbability = 0.01; $busyProbability = 0.01; $props = ActorSystem\Props::fromProducer( fn() => new Runner( $numberOfTransfers, $intervalBetweenConsoleUpdates, $uptime, $refusalProbability, $busyProbability, $retryAttempts ), ActorSystem\Props::withSupervisor( new ActorSystem\Strategy\OneForOneStrategy( $retryAttempts, new DateInterval('PT10S'), new ActorSystem\Supervision\DefaultDecider(), ) ), ); $system->root()->spawnNamed($props, 'runner'); });

Slide 127

Slide 127 text

go(function () { $system = ActorSystem::create(); $system->getLogger()->info('Starting'); $numberOfTransfers = 10; $intervalBetweenConsoleUpdates = 1; $uptime = 99.99; $retryAttempts = 3; $refusalProbability = 0.01; $busyProbability = 0.01; $props = ActorSystem\Props::fromProducer( fn() => new Runner( $numberOfTransfers, $intervalBetweenConsoleUpdates, $uptime, $refusalProbability, $busyProbability, $retryAttempts ), ActorSystem\Props::withSupervisor( new ActorSystem\Strategy\OneForOneStrategy( $retryAttempts, new DateInterval('PT10S'), new ActorSystem\Supervision\DefaultDecider(), ) ), ); $system->root()->spawnNamed($props, 'runner'); }); 3VOOFSΞΫλʔੜ੒

Slide 128

Slide 128 text

go(function () { $system = ActorSystem::create(); $system->getLogger()->info('Starting'); $numberOfTransfers = 10; $intervalBetweenConsoleUpdates = 1; $uptime = 99.99; $retryAttempts = 3; $refusalProbability = 0.01; $busyProbability = 0.01; $props = ActorSystem\Props::fromProducer( fn() => new Runner( $numberOfTransfers, $intervalBetweenConsoleUpdates, $uptime, $refusalProbability, $busyProbability, $retryAttempts ), ActorSystem\Props::withSupervisor( new ActorSystem\Strategy\OneForOneStrategy( $retryAttempts, new DateInterval('PT10S'), new ActorSystem\Supervision\DefaultDecider(), ) ), ); $system->root()->spawnNamed($props, 'runner'); }); 3VOOFSΞΫλʔʹର͢Δઓུ

Slide 129

Slide 129 text

go(function () { $system = ActorSystem::create(); $system->getLogger()->info('Starting'); $numberOfTransfers = 10; $intervalBetweenConsoleUpdates = 1; $uptime = 99.99; $retryAttempts = 3; $refusalProbability = 0.01; $busyProbability = 0.01; $props = ActorSystem\Props::fromProducer( fn() => new Runner( $numberOfTransfers, $intervalBetweenConsoleUpdates, $uptime, $refusalProbability, $busyProbability, $retryAttempts ), ActorSystem\Props::withSupervisor( new ActorSystem\Strategy\OneForOneStrategy( $retryAttempts, new DateInterval('PT10S'), new ActorSystem\Supervision\DefaultDecider(), ) ), ); $system->root()->spawnNamed($props, 'runner'); }); نఆ৚݅Ҏ্ࣦഊ͢Δͱ ଞͷγεςϜʹΤεΧϨʔτ͢ΔͳͲ͕Մೳ

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

·ͱΊ • ΞΫλʔϞσϧΛ׆༻ͨ͠ิঈτϥϯβΫγϣϯ • ϦτϥΠઓུ • ΤϥʔΧʔωϧύλʔϯͱ؂ಜ • BehaviorʹΑΔState Machine • Persistenceͷ׆༻

Slide 132

Slide 132 text

Let It Crash