rathoud96

rathoud96

I am working with commanded by @slashdotdash. Recently I encountered an issue where my aggregate’s state was different than my projection.
For example the aggregate state is %Aggregate.Example{is_valid: false} and my projection is %Projection.Example{is_valid: true}
Now I am stuck on how to sync them or is there a way the state can be updated?

Thanks

Showing Posts 1 to 7

tcoopman

tcoopman

I’m lacking a bit of context to give a good response.

Do you mean your projection in production has a bug so that it’s state is wrong? Did you forget to handle an event? Or something else?

If it’s a bug in your projection, the default way to solve this would be to fix the bug, deploy it and then replay the eventstream.

rathoud96

rathoud96 OP

Hello, @tcoopman thanks for the quick response.
My projection is correct but my aggregate state is incorrect.

tcoopman

tcoopman

Do you know why they have a different internal state? Did you find the bug?

In event sourced systems your aggragate is the thing that makes decisions.
Syncing is not something we want to do manually. We always look at the stream of events. They are the source of truth.

Depending on the kind of bug you made, you can either rebuild the aggregate state:
https://github.com/commanded/commanded/blob/master/guides/Aggregates.md#rebuilding-an-aggregate-snapshot

Or send in a compensating command to the aggregate. That means you need to implement that on the aggregate, and then make sure that it uses the command to fix it’s internal state.

rathoud96

rathoud96 OP

I could not find the bug. In the event stream as well i just have error logs but those aren’t helpful.
I tried to delete the event stream but now if i run the same command again it says command_uuid not found.
I am pretty new with commanded that’s why having difficulty figuring out the issue.

tcoopman

tcoopman

Do you have some code that you can share so we can take a look at it?

rathoud96

rathoud96 OP

It would be difficult to share the sample code, but I will try to explain in a more detail.
I have a table ‘books’. It looks something like this

%MyApp.Projection.Book{name: "My book", is_published: true}

And my aggregate looks looking like this

%MyApp.Aggregate.Book{name: "My book", is_published: false}

Now before the recent change, the is_published was true for aggregate as well but now after I put a validation for the author_id it’s giving me the wrong state in aggregate.

What the validation is
if author_id is in DB then the 'is_published: true' else 'is_published: false'

And the above validation would work only when we insert or update the operation.
Now I am not getting why this validation would change the aggregate state.
After adding the validation I didn’t do any insert or update.

tcoopman

tcoopman

On my phone, so I’ll do my best to respond.

I’m going to take a step back from commanded and talk about event sourcing in general for a moment.

In event sourcing an aggregate takes a list of events (it’s own history) and a command and it makes a decision. That decision is communicated by returning one or more events.
The aggregate should be pure. Meaning it should make the decision only based on the information in the history of the events and the command. It should do no queries or other side effects to make a decision.

I write should. I mean I strongly suggest doing it that way. It will give you easier to understand code. Easier to test code,…

Looking at your code it looks like you’re doing a query in the aggregate. From a design perspective I would try to extract that.
It’s not clear though where you’re doing the query in the aggregate. Is it when making the decision? So where you handle the command? Or is it in the apply function?

If it’s in the apply function it is definitely wrong. Your apply function MUST be pure.

If it’s in the code making a decision. The only thing I can imply is that at the time of the command the author was not in the DB. I would expect a different event though depending on if the author is in or not, so the projection should follow and have the same information.

Hopefully this makes sense. If not let me know.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews