AxonFramework icon indicating copy to clipboard operation
AxonFramework copied to clipboard

Same event received twice in tracking event processor after database connectivity glitch

Open magnusdurr opened this issue 4 years ago • 8 comments

Basic information

  • Axon Framework 4.5.9
    • Event Sourcing with Hibernate on PostreSQL
    • Tracking event processors, running parallel processing
  • Spring Boot 2.6.7
  • Amazon Corretto 17 on Alpine Linux

We are running two parallel instances of our service in AWS Fargate. We had a database connectivity glitch for a few second on one of our instances. Once the database connectivity was restored one of our tracking event processors was receiving duplicated events. From what we could tell, the token entry table looked normal in the database. The duplicated events were both delivered to our projection on the same server instance, and even the same thread.

A restart of the misbehaving instance solved the problem, which we interpret as it being an issue somewhere in the event processing state of Axon Framework. The service was running in a production-like environment, so we were unable to attach a debugger or dump internal state to get more insights from the misbehaving instance before the restart.

Steps to reproduce

Unfortunately we are unable to reproduce the issue, which we realize complicates things. We felt the issue was severe enough to file a bug report anyway, thinking that maybe this is not the first time a similar issue has occurred?

Expected behaviour

Event processing should resume normally once database connectivity is restored, or event processing should have failed over to the second instance in case of a longer database outage.

Actual behaviour

Event processing resumed once the database glitch was over, but the same event was received twice on one of our tracking event processors.

Application log

These are the logs produced during the glitch that caused the issue. The system was not receiving traffic during this time, so no new events are created or processed. The projection that is receiving duplicated events is named FailingViewProjection in the log

"2022-05-20T00:47:42.005Z","EventProcessor[FailingViewProjection]-4","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[0/3]"
"2022-05-20T00:47:41.987Z","EventProcessor[FailingViewProjection]-1","Dispatching new tracking segment worker: TrackingSegmentWorker{processor=FailingViewProjection, segment=Segment[0/3]}"
"2022-05-20T00:47:41.982Z","EventProcessor[FailingViewProjection]-1","Worker assigned to segment Segment[0/3] for processing"
"2022-05-20T00:47:31.984Z","EventProcessor[FailingViewProjection]-1","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[0/3]"
"2022-05-20T00:47:31.966Z","EventProcessor[FailingViewProjection]-0","Worker for segment Segment[3/3] stopped."
"2022-05-20T00:47:31.966Z","EventProcessor[FailingViewProjection]-0","event processor failure (segment=3 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:31.966Z","EventProcessor[FailingViewProjection]-0","Released claim"
"2022-05-20T00:47:31.962Z","EventProcessor[FailingViewProjection]-0","Segment is owned by another node. Releasing thread to process another segment..."
"2022-05-20T00:47:31.959Z","EventProcessor[SnsNotificationProjection]-2","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[1/3]"
"2022-05-20T00:47:31.958Z","EventProcessor[SnsNotificationProjection]-1","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[0/3]"
"2022-05-20T00:47:31.957Z","EventProcessor[FailingViewProjection]-1","No Worker Launcher active. Using current thread to assign segments."
"2022-05-20T00:47:31.957Z","EventProcessor[FailingViewProjection]-1","Worker for segment Segment[0/3] stopped."
"2022-05-20T00:47:31.957Z","EventProcessor[FailingViewProjection]-1","Released claim"
"2022-05-20T00:47:31.950Z","EventProcessor[FailingViewProjection]-1","Segment is owned by another node. Releasing thread to process another segment..."
"2022-05-20T00:47:31.946Z","EventProcessor[FailingViewProjection]-0","No Worker Launcher active. Using current thread to assign segments."
"2022-05-20T00:47:31.946Z","EventProcessor[FailingViewProjection]-0","Worker for segment Segment[3/3] stopped."
"2022-05-20T00:47:31.946Z","EventProcessor[FailingViewProjection]-0","Released claim"
"2022-05-20T00:47:31.945Z","EventProcessor[SnsCallbackNotificationProjection]-0","No Worker Launcher active. Using current thread to assign segments."
"2022-05-20T00:47:31.945Z","EventProcessor[SnsCallbackNotificationProjection]-0","Worker for segment Segment[3/3] stopped."
"2022-05-20T00:47:31.944Z","EventProcessor[SnsCallbackNotificationProjection]-0","event processor failure (segment=3 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:31.943Z","EventProcessor[SnsCallbackNotificationProjection]-0","Released claim"
"2022-05-20T00:47:31.937Z","EventProcessor[FailingViewProjection]-0","Segment is owned by another node. Releasing thread to process another segment..."
"2022-05-20T00:47:31.935Z","EventProcessor[SnsCallbackNotificationProjection]-0","Segment is owned by another node. Releasing thread to process another segment..."
"2022-05-20T00:47:31.054Z","EventProcessor[FailingViewProjection]-1","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[3/3]"
"2022-05-20T00:47:31.046Z","EventProcessor[SnsCallbackNotificationProjection]-1","Fetched token: GapAwareTrackingToken{index=1855613, gaps=[]} for segment: Segment[3/3]"
"2022-05-20T00:47:31.035Z","EventProcessor[SnsCallbackNotificationProjection]-0","Dispatching new tracking segment worker: TrackingSegmentWorker{processor=SnsCallbackNotificationProjection, segment=Segment[3/3]}"
"2022-05-20T00:47:31.032Z","EventProcessor[FailingViewProjection]-0","Dispatching new tracking segment worker: TrackingSegmentWorker{processor=FailingViewProjection, segment=Segment[3/3]}"
"2022-05-20T00:47:31.029Z","EventProcessor[SnsCallbackNotificationProjection]-0","Worker assigned to segment Segment[3/3] for processing"
"2022-05-20T00:47:31.019Z","EventProcessor[FailingViewProjection]-0","Worker assigned to segment Segment[3/3] for processing"
"2022-05-20T00:47:30.965Z","EventProcessor[FailingViewProjection]-1","Released claim"
"2022-05-20T00:47:30.955Z","EventProcessor[FailingViewProjection]-0","Released claim"
"2022-05-20T00:47:30.927Z","EventProcessor[SnsCallbackNotificationProjection]-0","Released claim"
"2022-05-20T00:47:30.927Z","EventProcessor[SnsNotificationProjection]-2","Released claim"
"2022-05-20T00:47:30.926Z","EventProcessor[SnsNotificationProjection]-1","Released claim"
"2022-05-20T00:47:30.797Z","EventProcessor[SnsNotificationProjection]-2","event processor failure (segment=1 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:30.796Z","EventProcessor[SnsNotificationProjection]-2","Releasing claim on token and preparing for retry in 1s"
"2022-05-20T00:47:30.796Z","EventProcessor[SnsNotificationProjection]-2","Error occurred. Starting retry mode."
"2022-05-20T00:47:30.796Z","EventProcessor[SnsNotificationProjection]-2","HikariPool-1 - Connection is not available, request timed out after 1000ms."
"2022-05-20T00:47:30.796Z","EventProcessor[SnsNotificationProjection]-2","SQL Error: 0, SQLState: null"
"2022-05-20T00:47:30.741Z","EventProcessor[FailingViewProjection]-0","event processor failure (segment=3 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:30.741Z","EventProcessor[FailingViewProjection]-0","Releasing claim on token and preparing for retry in 1s"
"2022-05-20T00:47:30.741Z","EventProcessor[FailingViewProjection]-0","Error occurred. Starting retry mode."
"2022-05-20T00:47:30.741Z","EventProcessor[FailingViewProjection]-0","HikariPool-1 - Connection is not available, request timed out after 1000ms."
"2022-05-20T00:47:30.741Z","EventProcessor[FailingViewProjection]-0","SQL Error: 0, SQLState: null"
"2022-05-20T00:47:30.729Z","EventProcessor[SnsNotificationProjection]-1","event processor failure (segment=0 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:30.729Z","EventProcessor[SnsNotificationProjection]-1","Releasing claim on token and preparing for retry in 1s"
"2022-05-20T00:47:30.729Z","EventProcessor[SnsNotificationProjection]-1","Error occurred. Starting retry mode."
"2022-05-20T00:47:30.729Z","EventProcessor[SnsNotificationProjection]-1","HikariPool-1 - Connection is not available, request timed out after 1000ms."
"2022-05-20T00:47:30.728Z","EventProcessor[SnsNotificationProjection]-1","SQL Error: 0, SQLState: null"
"2022-05-20T00:47:30.695Z","EventProcessor[SnsCallbackNotificationProjection]-0","event processor failure (segment=3 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:30.695Z","EventProcessor[SnsCallbackNotificationProjection]-0","Releasing claim on token and preparing for retry in 1s"
"2022-05-20T00:47:30.695Z","EventProcessor[SnsCallbackNotificationProjection]-0","Error occurred. Starting retry mode."
"2022-05-20T00:47:30.694Z","EventProcessor[SnsCallbackNotificationProjection]-0","HikariPool-1 - Connection is not available, request timed out after 1000ms."
"2022-05-20T00:47:30.694Z","EventProcessor[SnsCallbackNotificationProjection]-0","SQL Error: 0, SQLState: null"
"2022-05-20T00:47:30.609Z","EventProcessor[FailingViewProjection]-1","event processor failure (segment=0 caughtUp): Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"
"2022-05-20T00:47:30.605Z","EventProcessor[FailingViewProjection]-1","Releasing claim on token and preparing for retry in 1s"
"2022-05-20T00:47:30.605Z","EventProcessor[FailingViewProjection]-1","Error occurred. Starting retry mode."
"2022-05-20T00:47:30.599Z","EventProcessor[FailingViewProjection]-1","HikariPool-1 - Connection is not available, request timed out after 1000ms."
"2022-05-20T00:47:30.599Z","EventProcessor[FailingViewProjection]-1","SQL Error: 0, SQLState: null"

magnusdurr avatar May 24 '22 08:05 magnusdurr

I agree with the decision you and the team made, @magnusdurr. This seems serious enough to share, even though you're not capable to replicate the scenario.

I don't see anything obvious from your logs that point me in a solution direction, by the way. What I can deduce, is the following:

  • You have three processing groups (at least), called FailingViewProjection, SnsNotificationProjection, and SnsCallbackNotificationProjection.
  • The segment count configuration is 4 for these three processors.
  • At least for the FailingViewProjection the thread count is four.
  • I think you're using JPA over PostgreSQL.

If anything above is incorrect, please share that with us.

Let me ask a couple of other questions too, to have a bit more context on this problem.

  1. How did you notice events were handled twice? Curious about your statement that "duplicated events were delivered on the same server instance and the same thread" specifically.
  2. Do you have a separate database for your domain_event_entry and your query models with their tokens?
  3. Are there any irregularities in your domain_event_entry, like with the globalIndex?
  4. Have you configured the batchSize of these Event Processors?
  5. Do you happen to have a dump of the token_entry table at the time of the predicament?
  6. How did you perform a restart of the event processor?
  7. What's the task your event processors perform?

Hopefully the above points out something obvious. However, I need to be honest that I can't give you any certainty at this stage.

...thinking that maybe this is not the first time a similar issue has occurred

Luckily for the majority, although a bummer for you, this is relatively new to me. I've checked within AxonIQ if any other scenarios are known.

Apart from the problem at hand I do have a recommendation for you. It's best to make your message handling methods idempotent. Doing so ensures that even though the event is handled twice, the task at hand does not have any impact the second time around.

smcvb avatar May 24 '22 11:05 smcvb

You are correct on the information about our setup. To answer your questions:

  1. The misbehaving event processor was building a read model and we noticed duplicates in the data model, as the event processor was adding items to a list when receiving a ChildAddedEvent. We did not check for duplicates as we did not think it was needed. When investigating we could tell from the our logging that the same event was handled twice by the same instance on the same thread.
  2. In this service the read and write models use the same database, so domain events, tracking tokens and the read model is in the same RDS.
  3. No irregularities, the issue happened in a staging environment where we only have traffic periodically. We had no traffic anywhere close to this time period, so no events were created.
  4. The batch size of the misbehaving event processor is set to 100, the other two are set to 1.
  5. Unfortunately not.
  6. We killed the entire failing instance, and let our auto-scaling start a new one. Event processing was taken over by the remaining instance without any duplicated events.
  7. For the misbehaving one, see my first answer. The two others (which were unaffected) sends messages over SNS.

In general, we are using event processors to trigger communication to external systems in many of our services. Fixing idempotency for updates to internal read models should be doable but having to account for duplicated events in event processors talking to external systems is more challenging, and something we would have hoped to get for free from the framework.

magnusdurr avatar May 24 '22 13:05 magnusdurr

Maybe I should also clarify that it was not a few duplicated events that were received. After the database connectivity glitch, all new events that were created was delivered twice to one of the event processors (not all) until we killed the server instance where the misbehaving event processor was running.

magnusdurr avatar May 24 '22 13:05 magnusdurr

Thanks for the update above, @magnusdurr. That provides some guidance, although not necessarily a conclusion to how it happened.

We have a hunch right now, by the way. But, let me first reply to one of the points you've added:

In general, we are using event processors to trigger communication to external systems in many of our services. Fixing idempotency for updates to internal read models should be doable but having to account for duplicated events in event processors talking to external systems is more challenging, and something we would have hoped to get for free from the framework.

Understood and reasonable. Services that introduce side effects are harder to solve. Axon Framework definitely does its best to eliminate occurrences like these. Sadly enough, you and your team found a scenario that imposed this issue regardless.

It proves that distributed/asynchronous systems react in unknown scenarios...allowing me a bridge to the hunch we have.

After checking the logs in a bit more detail, something odd struck us:

# left out the rest for brevity
"2022-05-20T00:47:31.957Z","EventProcessor[FailingViewProjection]-1","No Worker Launcher active. Using current thread to assign segments."
# ...
"2022-05-20T00:47:31.946Z","EventProcessor[FailingViewProjection]-0","No Worker Launcher active. Using current thread to assign segments."
# ...

The TrackingEventProcessor should construct a single Worker Launcher thread. The job of that thread is to claim tokens and start processing them within the assigned number of threads (through your configuration). However, your logs show that FailingViewProjection thread 1 (so thread 2) "assumed" there was no Worker Launcher active at that moment in time, starting another one. Although the implementation of the (private) TrackingEventProcessor.WorkerLauncher has additional checks to ensure it's not handling the same segment with several threads, if anything could trigger it that's the WorkerLauncher.

So, we can impose a fix on the TrackingEventProcessor when a worker launcher is started, checking whether a worker launcher is already active.

How your application ended up in this hunch situation is, well, not clear to us at this stage. Nonetheless, I've committed a fix to ensure the TrackingSegmentWorker does not start a WorkerLauncher whenever a WorkerLauncher is active.

smcvb avatar May 25 '22 15:05 smcvb

With that said, I do have another question for you. Did you notice duplicate event handling for all the events or for a subset of the events?

smcvb avatar May 25 '22 15:05 smcvb

It looked to be all received events, but the affected processing group was not listening to every event produced from the aggregate.

magnusdurr avatar Jun 02 '22 09:06 magnusdurr

I hope the answer is no, but I'm going to ask it regardless: have you seen the described predicament again, @magnusdurr? And if so, with which version of AF? Is your team still using 4.5.9?

Note that we've done numerous fixes in the threading logic of the TrackingEventProcessor and its WorkerLauncher (which we assumed might cause the predicament). The most recent adjustments have been introduced in 4.5.15, which we've released just today actually.

smcvb avatar Aug 03 '22 14:08 smcvb

I am closing this issue due to inactivity. If you have any updates on the matter, @magnusdurr (or others encountering this predicament), be sure to share those with us.

smcvb avatar Oct 03 '22 15:10 smcvb