flowable-engine icon indicating copy to clipboard operation
flowable-engine copied to clipboard

Timer job never unlocked after an exception

Open maxime066 opened this issue 6 years ago • 15 comments

I have a simple process, very fast to execute, with this timer start event running each minute:

<startEvent id="StartEvent_17bxin3" name="Each minute"> <outgoing>SequenceFlow_17asw5l</outgoing> <timerEventDefinition> <timeCycle><![CDATA["0 0/1 * 1/1 * ? *"]]></timeCycle> </timerEventDefinition> </startEvent>

Then, I run that bpmn, everything goes fine but sometimes I can see that this timer doesn’t work anymore, until I restart the engine (I clean the flowable DBs at startup). It’s not the related to the retries getting to 0, because I use a value very very high.

To reproduce, I go in AcquireTimerJobsRunnable in the run() method and after I have one value in the acquiredJobs array, I just need to make the code throw an exception before the job is effectively launched, to make the job in the acquiredJobs array to not work anymore. So I breakpoint on the commandExecutor.execute method. At this point I can see in the database, for the collection ACT_RU_TIMER_JOB, that there’s an entry with the field LOCK_EXP_TIME_ that is not null; it’s a date a couple of minutes in the future. LOCK_OWNER_ is also not null and REV_ is 2. Then in Eclipse, I generate an exception by stepping into the code until I see a logger where the code do “config.getTransactionPropagation()” and config is a local variable, so I just need to set its value to null to cause a NullPointerException. After that, if I let that run, the entry in ACT_RU_TIMER_JOB will never disappears and the timer will not start again. Its field values will not change. Nothing appears in ACT_RU_DEADLETTER_JOB nor ACT_RU_JOB for this timer. Other timers continue to work normally.

Here I simulated an Exception with a NullPointerException, but on production I saw a MySQL Timeout in the logs.

I’ve found that there’s a flowable-reset-expired-jobs thread, and debugging it I’ve found that it search for expired jobs, correctly I think, but in the ACT_RU_JOB collection, not the ACT_RU_TIMER_JOB collection. I didn’t found a class in the package org.flowable.job.service.impl.asyncexecutor that run a job to watch for expired TIMER_JOB.

I think something is missing, help would be appreciated. Thank you.

I'm using Flowable 6.4.1, but I saw the same behavior with Flowable 6.5.0. I use it with MySQL and Spring Boot.

maxime066 avatar May 21 '20 15:05 maxime066

Hi, Timer jobs are moved to the ACT_RU_JOB table when the due date is reached:

https://github.com/flowable/flowable-engine/blob/master/modules/flowable-job-service/src/main/java/org/flowable/job/service/impl/asyncexecutor/AcquireTimerJobsRunnable.java

From your description I understand that some timer jobs are not moved to the ACT_RU_JOB table anymore. Did you look at the due date values for the timer jobs? When DEBUG logging is turned on you can see what the acquire timer job query returns.

Best regards

tijsrademakers avatar May 21 '20 19:05 tijsrademakers

I did it with a breakpoint, and yes, I the acquire timer job query returns (line 58 on your link).

The problem is when I have a job in acquiredJobs and there's an exception at the line 60 (I mean, in the code executed at line 60), but before the line 64 get executed.

Then the exception is catched on line 87, but the locked timer job is never unlocked.

Thanks for the help.

maxime066 avatar May 21 '20 19:05 maxime066

After a day of work, I'm able to make it work correctly by having my own implementation of AcquireTimerJobsRunnable that, when there's an exception in the block of lines 60 to 70, register the acquiredJobs, that are still locked, in my own implementation of ResetExpiredJobsRunnable that will also reset the locked timer jobs expired.

I'm not sure what I do is totally correct but my first tests work.

maxime066 avatar May 21 '20 20:05 maxime066

If there is an exception on the lines you are mentioning then there should be handled between lines 78 and 89. Do you see those messages in the logs?

  • Under debug logging "Optimistic locking exception during timer job acquisition"
  • Under error logging "exception during timer job acquisition"

filiphr avatar May 22 '20 06:05 filiphr

"Under error logging "exception during timer job acquisition""

Yes I see that message. The problem is not catching the exception, it's to handle it correctly. Line 58 will lock the timer job but on the next run, line 58 won't fetch locked timer job, even the one expired. That's ok, because a locked job should not be started again. What should happens however, is that every job in acquiredJobs that were not moveTimerJobToExecutableJob, should be unlocked. Unlocking those jobs should also be done periodically, until it succeed, so if we still have an exception to unlocking it (ex a MySQL timeout) we'll retry.

May be you'll better understand with my implementation of a fix. See https://pastebin.com/XSNtjiHc . resetExpiredJobsAndTimerJobsRunnable.addTimerJobToUnlock(timerJobEntity); will register the timer job to be unlocked periodically until it succeeds.

Thanks for the help.

maxime066 avatar May 22 '20 14:05 maxime066

Here's the exception I had on production if it can help you:

ERROR exception during timer job acquisition: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection. java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection. at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:233) at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183) at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93) at o.s.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:246) ... 8 common frames omitted Wrapped by: o.s.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection. at o.s.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:289) at o.s.t.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377) at o.s.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130) at o.f.common.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:46) at o.f.common.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30) at o.f.common.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:56) at o.f.common.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:51) at o.f.j.s.i.asyncexecutor.AcquireTimerJobsRunnable.run(AcquireTimerJobsRunnable.java:60) at java.lang.Thread.run(Thread.java:748)

maxime066 avatar May 22 '20 14:05 maxime066

Thanks a lot for the exception stacktrace. It looks like there is some connection timeout. This shouldn't really happen but it seems your connection pool is too small. Can you please share your connection pool settings?

We understand the problem and we are looking into it so we can fix it.

btw your solution is not correct, because everything which happens within one command execution is in one transaction. You either unlock all or you unlock none of the jobs

filiphr avatar May 22 '20 14:05 filiphr

Thanks a lot for the exception stacktrace. It looks like there is some connection timeout. This shouldn't really happen but it seems your connection pool is too small. Can you please share your connection pool settings?

processEngineConfiguration.setDataSource(dataSource);

where dataSource comes from

HikariConfig config = new HikariConfig(); //... then set url, username, etc. then config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.setMinimumIdle(1); config.setMaximumPoolSize(2); DataSource dataSource = new HikariDataSource(config); and I also have

processEngineConfiguration.setJdbcMaxActiveConnections(10); processEngineConfiguration.setJdbcMaxIdleConnections(5);

We understand the problem and we are looking into it so we can fix it.

Thank you :+1:

btw your solution is not correct, because everything which happens within one command execution is in one transaction. You either unlock all or you unlock none of the jobs

Ok so that means, in my solution, I don't need the jobLocked variable, I can just use acquiredJobs right?

maxime066 avatar May 22 '20 16:05 maxime066

Your configuration is way too small. You have a connection pool with max 2 connections. You'll have a lot of problems, there are some async threads which need DB connections.

Just FYI when setting a custom DataSource the following is ignored

processEngineConfiguration.setJdbcMaxActiveConnections(10); processEngineConfiguration.setJdbcMaxIdleConnections(5);

I would strongly advice increasing to connection pool to at least 20. Keep in mind if you are invoking the engine a lot from your user threads you'll need to make it even bigger in order to cope with user requests.

Ok so that means, in my solution, I don't need the jobLocked variable, I can just use acquiredJobs right?

Yes

filiphr avatar May 22 '20 18:05 filiphr

What's a good value for the pool then? If you have a documentation for that I'll gladly take it. Is it only directly linked to the number of bpmn running or it's more complex than that? We run from 1 to 20 bpmns per engine instance. However, the number of process instance can vary. We start them with timers and also with signals.

maxime066 avatar May 22 '20 18:05 maxime066

As I said at least 20. The Flowable REST Application has it set to 100. It is not linked to the number of running bpmn, nor the running instances. It is a mix of things and it is different for everyone. It is dependent on how you use it.

filiphr avatar May 23 '20 07:05 filiphr

This was fixed in https://github.com/flowable/flowable-engine/commit/1d7f9357a187546db333bfda812f2c7160f04187

filiphr avatar May 26 '20 07:05 filiphr

I commented the commit. I think there's still a possibility to have a infinite lock. https://github.com/flowable/flowable-engine/commit/1d7f9357a187546db333bfda812f2c7160f04187

maxime066 avatar May 26 '20 14:05 maxime066

You are right. Thanks for spotting it. We've added https://github.com/flowable/flowable-engine/commit/8d7c0fff6a0be060958fc20dd43074d004c5002c to solve the potential lock. We also expanded the reset expired runnable so that timer jobs are reseted as well.

filiphr avatar May 26 '20 22:05 filiphr

I think there's still a possibility to have a lock, now UnlockTimerJobsCmd is run the catch code block, but there is no chance to execute this code block when the app process is closed by exception.

So i think that UnlockTimerJobsCmd should run on an independent Thread just like ResetExpiredJobsRunnable, the lock happens in any exception will be unlocked.

silentjesse126 avatar Jun 21 '24 07:06 silentjesse126