What is the appropriated way to handle projections data while replaying events

Hello All,

After running a business for while using Axon now some events replaying and new view models are necessary and I would like to have some feedback about it.

My service is using Axon Framework with MongoDB as event-store and read-model

I have views with data aggregated (sum) in a monthly, weekly and record basis. The preferable way to replay events is partially - based in a given date-time - and I have provided an endpoint to invoke the TrackingEventProcessor#resetTokens(Instant.class)

If I decide to replay from the 16.09.2020 my monthly and weekly views have to deduct all data accumulated from 16.10.2020 to ensure consistency in the end of process.

:question: How would be the best approach to delete the projection data that matches with the replay criteria?

Hello @habutre,

When using the TrackingEventProcessor (TEP) and initiate a reset of the tokens, there is functionality you can add within your Event Handling Components (EHC) to react to the reset.
More specifically, you can add an @ResetHandler annotated method to your EHC, which will be invoked once you have triggered the resetTokens operation. However, this will by default only tell you when the reset is invoked, not under what context.

To support reacting appropriately, we have introduced the option to provide a resetContext when you invoke the resetTokens operation. The resetContext can be any object you define, not to different from the messages you can use within an Axon application. Once a resetContext is provided, you can assume it being provided to your @ResetHandler annotated method as a parameter.

Thus, what you can do, is the following:

// Somewhere on an endpoint...
TrackingEventProcessor myTep = ...;
TrackingToken resetTo = ...;
MyResetContext resetContext = ...;
myTep.resetTokens(resetTo, resetContext);

// Somewhere inside an Event Handling Component...
@ResetHandler
public void reset(MyResetContext resetContext) {
    // Perform reset based on resetContext...
}

This should thus allow you to issue the reset and provide the timestamp until which point you are going to reset. Then for example, the reset handler can clear out your tables from that moment in time, so that the replay will happen accordingly.

This feature has been introduced in Axon Framework release 4.4 by the way, so you will be inclined to move over to that version, if you haven’t already. Additionally, here is the page in the reference guide on replaying events, also showing a sample using the @ResetHandler with a provided context.

Hope this helps you out @habutre!

Cheers,
Steven

Hello is there any tutorial or Sample around ? we also plan to go with Axon and MongoDB in our project. it will be nice to rebuild the projection view later when we in production :slight_smile:

There is something on Baeldung in a follow-up I probably switch to Mongo for persistance. There is a sneak peak of the code here.

1 Like