-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: State Reconstructor #160
base: main
Are you sure you want to change the base?
Conversation
Co-Authored-By: Skyler Katz <[email protected]>
Co-Authored-By: Skyler Katz <[email protected]>
Co-Authored-By: Skyler Katz <[email protected]>
Co-Authored-By: Skyler Katz <[email protected]>
Co-Authored-By: Skyler Katz <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DanielCoulbourne @skylerkatz I just did a pass on this and added some notes in prep for Monday.
/** @param Event[] $events */ | ||
public function write(array $events): bool; | ||
|
||
public function summarize(State ...$states): AggregateStateSummary; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure this needs to be on the StoresEvents
interface. I think maybe it needs access to some store data, but it seems a little odd for it to live here.
use Thunk\Verbs\State; | ||
use Thunk\Verbs\Support\StateIdentity; | ||
|
||
class AggregateStateSummary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is… obtuse. But the basic point of this class is to find ALL the States and Events that are related to a given set of starting States. We need this because if we're reconstituting state, and it relies on some other related state, we need that state to be in sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class FindAllTheThings
{
public static function leeroyyyyyyJenkins(State ...$states): static
}
// reconstituting in a recursive call, the root call is responsible for applying | ||
// events, so we should also skip in that case. | ||
|
||
if (! $this->is_replaying && ! $this->is_reconstituting) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to combine is_replaying
and is_reconstituting
into a single concept that means "should the state manager try to reconstitute state or not" — I don't know that the distinction matters inside the state manager.
try { | ||
$this->is_reconstituting = true; | ||
|
||
$summary = $this->events->summarize($state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably should be able to take multiple states
src/Lifecycle/StateManager.php
Outdated
// FIXME: Swap out existing state manager, push all related states into new state manager | ||
// FIXME: run all the event on them, swap them out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the correct approach, I think. Ultimately, we just need an isolated state manager to handle everything and then pull those values over into the global state manager after it's done.
src/Lifecycle/StateManager.php
Outdated
$min = $last_event_ids->min() ?? PHP_INT_MIN; | ||
$max = $last_event_ids->max() ?? PHP_INT_MIN; | ||
|
||
// If all states have had this or future events applied, just ignore them | ||
if ($min >= $event->id && $max >= $event->id) { | ||
return false; | ||
} | ||
|
||
// We should never be in a situation where some events are ahead and | ||
// others are behind, so if that's the case we'll throw an exception | ||
if ($max > $event->id && $min <= $event->id) { | ||
throw new RuntimeException('Trying to apply an event to states that are out of sync.'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all to account for cases where one state is ahead of another. We may want to decide that in the beginning we just don't support that…?
use InvalidArgumentException; | ||
use Thunk\Verbs\State; | ||
|
||
class StateIdentity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are too many different things that can point to a state. This class exists to normalize them all into one thing for ease of use around the codebase.
foreach ($this->states as $type => $states) { | ||
foreach ($states as $id => $state) { | ||
if (in_array($id, $ids)) { | ||
uniqid($this->states[$type][$id]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, I think this should be unset
not uniqid
/* | ||
* The Problem(s) | ||
* | ||
* FIRST PROBLEM: | ||
* - We try to load state1, but we don't have an up-to-date snapshot | ||
* - StateManager::load tries to reconstitute state from events | ||
* - One of those Event::apply methods load state2 | ||
* - Best case scenario: we reconstitute state2 before continuing | ||
* - Worst case scenario: reconstituting state2 tries to reconstitute state1, and we're in an infinite loop | ||
* - (if no loop) state1 continues to reconstitute, but it's acting with state2 FULLY up-to-date, not | ||
* just up-to-date with where state1 happens to be | ||
* | ||
* TO TEST FIRST PROBLEM: | ||
* - Event1 adds State1::counter to State2::counter and increments State2::counter | ||
* - Event2 increments State2::counter | ||
* | ||
* SECOND PROBLEM: | ||
* - We try to load state1, but we don't have an up-to-date snapshot | ||
* - StateManager::load tries to reconstitute state from events | ||
* - One of those Event::apply methods requires state1 and state2, so we need to load state2 | ||
* - Reconstituting state2 re-runs the same apply method on state2 before also running it on state1 | ||
* - Double-apply happens | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should probably start here and review whether these are actually the problems and if so confirm that the tests effectively test them.
$state_id = data_get($source, 'state_id'); | ||
$state_type = data_get($source, 'state_type'); | ||
|
||
if (is_int($state_id) && is_string($state_type)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (is_int($state_id) && is_string($state_type)) { | |
if ( | |
( | |
\Illuminate\Support\Str::isUlid($state_id) | |
|| \Illuminate\Support\Str::isUuid($state_id) | |
|| is_int($state_id) | |
) | |
&& is_string($state_type) | |
) { |
This is formatted terribly, but I think we need to check for more than just an int
for the $state_id
use Thunk\Verbs\State; | ||
use Thunk\Verbs\Support\StateIdentity; | ||
|
||
class AggregateStateSummary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class FindAllTheThings
{
public static function leeroyyyyyyJenkins(State ...$states): static
}
Co-Authored-By: Daniel Coulbourne <[email protected]> Co-Authored-By: Skyler Katz <[email protected]>
Just posting this as a draft for discussion.