Gaps in the GapAwareTackingTokens are not cleaned up in the TokeStore after the specified gapTimeout.
Basic information
We are launching a new service with very low throughput. Initially we had a bad integration to this new service causing a lot of concurrency issues and gaps in the global event sequence. We would expect these gaps to go away from out tracking tokens either after reaching the gapCleaningThreshold (not happening because of low throughput) or the gapTimeout. However, the gap timeout does not seem to have the expected effect, instead we are seeing gaps several weeks back in the event sequence still persisted in our GapAwareTrackingTokens. The gaps cause slow and heavy queries to fetch new events, causing the CPU usage of our (relatively small) DB instances to increase significantly.
- Axon Framework version: 4.7.5
- JDK version: Amazon Coretto 17
- JPA with PSQL for persistence of our events and tracking tokens.
Steps to reproduce
You can reproduce this by building a simple Axon application and running a client causing a lot of concurrency issues (producing gaps in the global event sequence). We would expect the gaps to be cleaned up over time from the tokens in the token store, but this does not seem to happen.
We also tried reproducing this with a unit test on the JapEventStorageEngine, but the tracking tokens produced from it looks OK and gaps are cleaned up as expected based on gapTimeout. Still we see in our databases that this is not the case in our live setup. Maybe the TackingEventProcessors are not saving the tracking tokens appropriately?
Expected behaviour
Gaps in the global event sequence should be cleaned up from GapAwareTrackingToken after the configured gap timeout.
Actual behaviour
Time-based cleanup of GapAwareTrackingToken gaps does not seem to be persisted.
First and foremost, thanks for filing this concern with us, @magnusdurr. To be completely frank with you, I believe we're dealing with a minor misconception instead of a bug.
The gapTimeout is the age a gap needs to have to be considered "deletable."
However, the cleanupThreshold defines the moments the gaps are checked and potentially cleaned.
More on an implementation level, when events are fetched by the RDBMS EventStorageEngines, the gapTimeout is substracted from the current time.
This gapTimeoutThreshold is then used to:
- deduce whether gaps are allowed on the tracked
EventMessage(this is a POJO combining theEventMessageand theTrackingToken), and - to clean the
TrackingTokenbefore it is used to fetch events from the store.
It is in situation 2 where the cleanupThreshold comes in to dictate whether cleaning a token used to retrieve events can even start.
So if the application doesn't produce many events, it may take too long for the cleanupThreshold ever to occur.
Due this, there's a chance that gaps older than the timeout are never removed.
In situation 1, we interplay with the maxGapOffset configuration option, which sets a maximum distance between the token attached to the event and the gaps that were detected.
Furthermore, the gapTimeoutThreshold is compared against the timestamp of the received event.
If the given event occurs after the gapTimeoutThreshold, which would be the case for relatively new events, the allowGaps boolean is set to true.
It's this flag that cases the gaps to stick, as they're allowed.
So, the above gives some explanation, I assume.
I do believe that, although you haven't specified this in your issue, that more than 250 gaps shouldn't happen for your tokens.
This holds (it should at least) since the cleanupThreshold is 250 by default.
As it dictates whether the TrackingToken is cleaned before it is used to retrieve events, it is key.
Given the fact that I believe we're not dealing with a bug, I will remove the label and replace it with the question label. If I am proven wrong, because (for example) you have more than 250 gaps, please do tell me so. Perhaps further investigation is necessary.
Hi Steven, and thanks for the detailed explanation.
We will do some more testing and get back to you. If i understand you right the gapTimeoutThreshold should not affect the persisted TrackingToken but should clean up gaps in the TrackingToken before it is used to fetch events from the store? What would be the expected outcome if we enable SQL logging and look at the queries from the fetchEvent? If I understand you right I would expect:
- Once gaps appear in the global event sequence; fetch event queries would include the gaps in the
OR e.globalIndex IN :gapspart of the query. - When the gap gets older than the
gapTimeoutThreshold(default 60 seconds?) it should no longer appear in theOR e.globalIndex IN :gapspart of the query but still be present if we look in theTokenStore
Did I get that right?
Hi Steven, and thanks for the detailed explanation.
Sure thing, Magnus, I am glad to help. :-)
Concerning the SQL, the following steps are performed by the JpaEventStorageEngine#fetchTrackedEvents(TrackingToken lastToken, int batchSize) operation:
- OPTIONAL - The given
lastTokenis cleaned ifgapCleaningThresholdis met. This query retrieves a combination ofglobalIndextotimestamp. This list ofindexToTimestampis then validated against thegapTimeoutThreshold. - The
previousToken(either a copy oflastTokenor a cleaned instance) is used to fetch events. Only if thepreviousTokencontains gaps should you expect the IN-clause.
After step 2, the EventStorageEngine constructs TrackedEventMessage instances based on the (potentially cleaned) TrackingToken and the found events.
For every event, the TrackingToken advances to the globalIndex value of the queried event entry.
While advancing the token, the maxGapOffset (default is 10_000) is used to deduce what the oldest allowed gap may be.
If in this process, gaps are not allowed (deduced by checking whether the timestamp of the event is after the gapCleaningThreshold), then the event's global sequence is used as a hard cut-off for gaps.
The events are then provided to the Event Processor, processing them, and once complete it'll use the TrackingToken of the last event of the event batch as the new position to maintain in the TokenStore.
If i understand you right the gapTimeoutThreshold should not affect the persisted TrackingToken but should clean up gaps in the TrackingToken before it is used to fetch events from the store?
So, not entirely true!
It may impact the persisted token, depending on whether the gapCleaningThreshold took effect on the TrackingToken attached to the TrackedEventMessage (which in turn will dictate the stored TrackingToken at the end of an Event Processor's batch).
I hope the above further clarifies things for you, Magnus!
Hi Steven, thanks for all your help explaining the axon framework details. After further investigation it seems like our original theory about the gaps was not the cause of our performance problems. I will close the issue.
Thanks for sharing this with us, @magnusdurr. If you ever encounter any other potential performance predicaments, be sure to share them with us!