Observing some strange results while doing Performance testing with Netflix Condcutor
I constantly see this error message in the logs. My test is 100 concurrent threads starting a sample workflow for 10 mins and at the same time a java worker client polling for the tasks.
Can some one explain what pop() method do and why do I see this error?
116194 [qtp1870234754-176] ERROR com.netflix.conductor.server.resources.ApplicationExceptionMapper - BACKEND_ERROR - could not pop all messages for given ids: [dc8da953-9365-4d5d-aa7b-e69d08eee55b] (0 messages were popped) com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - could not pop all messages for given ids: [dc8da953-9365-4d5d-aa7b-e69d08eee55b] (0 messages were popped) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:103) at com.netflix.conductor.dao.mysql.MySQLQueueDAO.pop(MySQLQueueDAO.java:66) at com.netflix.conductor.service.ExecutionService.poll(ExecutionService.java:111) at com.netflix.conductor.server.resources.TaskResource.batchPoll(TaskResource.java:115) at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:286) at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:276) at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:181) at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85) at com.netflix.conductor.server.JerseyModule$1.doFilter(JerseyModule.java:97) at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82) at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:120) at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:135) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1174) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1106) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) at org.eclipse.jetty.server.Server.handle(Server.java:524) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) at java.lang.Thread.run(Thread.java:748) Caused by: com.netflix.conductor.core.execution.ApplicationException: could not pop all messages for given ids: [dc8da953-9365-4d5d-aa7b-e69d08eee55b] (0 messages were popped) at com.netflix.conductor.dao.mysql.MySQLQueueDAO.popMessages(MySQLQueueDAO.java:241) at com.netflix.conductor.dao.mysql.MySQLQueueDAO.lambda$4(MySQLQueueDAO.java:66) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:98) ... 50 more
@moustaki @cyzhao - Can you help me understanding the flow?
@picaron Hi Picaron, I think you are the one who wrote the original MYSQL module. There are several deadlocks and race conditions I have identified while doing performance testing. I have an idea on what needs to be done. Can we have a discussion and I will create a PR to fix the issues
Hi @jvemugunta
Yes I wrote the original MySQL module. It was rewritten entirely by @mashurex to remove the dependency to a 3rd party library.
We should include him as well in the discussion.
Please share your idea on what needs to be done.
Hey @picaron, long time no chat :) I recently chatted with @jvemugunta on a different PR, and he think he’s doing some really good testing of the MySQL module.
The workload I have been using this module for is obviously not nearly as concurrent as his usage and I think it is showing a few spots where the optimistic update then insert patterns are causing issues.
Some of this may be MySQL configuration or tuning, but the entire module may see a performance boost if we identified the various queries that could be optimized into single calls like upserts, replace into, etc.
I wanted to add a couple of ideas if we’re going to be re-evaluating various queries...
My original PR was to remove the dependency on Sql2o because it wasn’t playing well in our environment, seems to be dying or rarely maintained, and it has a couple of big bugs out there. It also seemed like the MySQL module could get by with lower level JDBC API calls and no extra SQL libraries.
So my PR had the ‘simple’ goal of removing the need for Sql2o but leaving the original methods, flow, and queries in place to make it easier to do a like for like validation of the changes.
Besides identifying methods where we make two calls back to back, I wonder if there is extra caching that can be performed to reduce overall calls to the database.
Do we need think about transactions more carefully? Should we disable transactions altogether? Opposite of that, should we be using more transactions?
Connection pool tuning, are there different sizes and configurations that could be put in place for higher concurrency workloads?
Like I said, I did not want to change too many things at once for fear of creating regressions or making it difficult for anyone already using the module to upgrade, now it seems like there’s more of us using it and we’ll be able to get some help on the trickier issues.
Hi @mashurex , long time no chat indeed 😄
Yeah I think your refactoring was great and necessary. Also, like you said, both our workloads didn't make these concurrency issue come out.
I agree its a great opportunity to fix those issues and improve the MySQL support now that we are more using it and that can contribute to it.
Hi @mashurex https://github.com/mashurex and @picaron https://github.com/picaron,
Thanks for the response. I am doing simple perf test case with 5 threads and am seeing many deadlocks and race conditions in the code. I think the first thing is to fix the code ASAP.
Once the fixes are complete, we can think about caching, improving on connection pooling and transaction management as a whole.
Look at the attached file and run this sample test case, i see a race condition as below
MySQLExecutionDAOCreateWorkflowTest com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest testWith5THreads(com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest) java.util.concurrent.ExecutionException: com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - Duplicate entry 'workflow0' for key 'unique_workflow_id' at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest.testcreateTasksWithParallelThreads(MySQLExecutionDAOCreateWorkflowTest.java:134) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest.testWith5THreads(MySQLExecutionDAOCreateWorkflowTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110) at org.junit.rules.RunRules.evaluate(RunRules.java:18) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - Duplicate entry 'workflow0' for key 'unique_workflow_id' at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:103) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.withTransaction(MySQLBaseDAO.java:125) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.insertOrUpdateWorkflow(MySQLExecutionDAO.java:479) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.createWorkflow(MySQLExecutionDAO.java:235) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:119) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: com.netflix.conductor.core.execution.ApplicationException: Duplicate entry 'workflow0' for key 'unique_workflow_id' at com.netflix.conductor.dao.mysql.Query.executeUpdate(Query.java:274) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.lambda$33(MySQLExecutionDAO.java:543) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.execute(MySQLBaseDAO.java:170) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.addWorkflow(MySQLExecutionDAO.java:542) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.lambda$31(MySQLExecutionDAO.java:481) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.lambda$4(MySQLBaseDAO.java:126) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:98) ... 9 more
On Thu, Jun 28, 2018 at 12:10 PM, Pascal Chouinard <[email protected]
wrote:
Hi @mashurex https://github.com/mashurex
Yeah I think your refactoring was great and necessary. Also, like you said, both our workloads didn't make these concurrency issue come out.
I agree its a great opportunity to fix those issues and improve the MySQL support now that we are more using it and that can contribute to it.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Netflix/conductor/issues/630#issuecomment-401088674, or mute the thread https://github.com/notifications/unsubscribe-auth/AHJ_Q0GRIafHE2_aUEfqTWIysUQK81eCks5uBP_igaJpZM4U6QbA .
This is now a true deadlock
@Override
public List<Task> createTasks(List<Task> tasks) {
List<Task> created = Lists.newArrayListWithCapacity(tasks.size());
withTransaction(connection -> {
for (Task task : tasks) {
validate(task);
task.setScheduledTime(System.currentTimeMillis());
String taskKey = task.getReferenceTaskName() + "" + task
.getRetryCount();
boolean scheduledTaskAdded = addScheduledTask(connection,
task, taskKey);
if (!scheduledTaskAdded) {
logger.info("Task already scheduled, skipping the run "
-
task.getTaskId() + ", ref="
+ task.getReferenceTaskName() + ", key=" +
taskKey);
continue;
}
*insertOrUpdateTaskData(connection, task); // This is the
buggy code *
addWorkflowToTaskMapping(connection, task);
addTaskInProgress(connection, task);
updateTask(connection, task); // This method also
calls insertOrUpdateTaskData code
created.add(task);
}
});
return created;
}
private void updateTask(Connection connection, Task task) {
task.setUpdateTime(System.currentTimeMillis());
if (task.getStatus() != null && task.getStatus().isTerminal()) {
task.setEndTime(System.currentTimeMillis());
}
TaskDef taskDef = metadata.getTaskDef(task.getTaskDefName());
if (taskDef != null && taskDef.concurrencyLimit() > 0) {
boolean inProgress = task.getStatus() != null && task
.getStatus().equals(Task.Status.IN_PROGRESS);
updateInProgressStatus(connection, task, inProgress);
}
insertOrUpdateTaskData(connection, task); // again a call is made
in this code
if (task.getStatus() != null && task.getStatus().isTerminal()) {
removeTaskInProgress(connection, task);
}
addWorkflowToTaskMapping(connection, task);
indexer.indexTask(task);
}
MySQLExecutionDAOCreateWorkflowTest com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest testWith5THreads(com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest) java.util.concurrent.ExecutionException: com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - Deadlock found when trying to get lock; try restarting transaction at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest.testcreateTasksWithParallelThreads(MySQLExecutionDAOCreateWorkflowTest.java:137) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest.testWith5THreads(MySQLExecutionDAOCreateWorkflowTest.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110) at org.junit.rules.RunRules.evaluate(RunRules.java:18) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - Deadlock found when trying to get lock; try restarting transaction at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:103) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.withTransaction(MySQLBaseDAO.java:125) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.createTasks(MySQLExecutionDAO.java:98) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:123) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: com.netflix.conductor.core.execution.ApplicationException: Deadlock found when trying to get lock; try restarting transaction at com.netflix.conductor.dao.mysql.Query.executeUpdate(Query.java:274) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.lambda$39(MySQLExecutionDAO.java:583) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.execute(MySQLBaseDAO.java:170) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.insertOrUpdateTaskData(MySQLExecutionDAO.java:583) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.lambda$1(MySQLExecutionDAO.java:114) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.lambda$4(MySQLBaseDAO.java:126) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction(MySQLBaseDAO.java:98) ... 8 more
On Thu, Jun 28, 2018 at 6:00 PM, Jagadish Babu Vemugunta < [email protected]> wrote:
Hi @mashurex https://github.com/mashurex and @picaron https://github.com/picaron,
Thanks for the response. I am doing simple perf test case with 5 threads and am seeing many deadlocks and race conditions in the code. I think the first thing is to fix the code ASAP.
Once the fixes are complete, we can think about caching, improving on connection pooling and transaction management as a whole.
Look at the attached file and run this sample test case, i see a race condition as below
MySQLExecutionDAOCreateWorkflowTest com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest testWith5THreads(com.netflix.conductor.dao.mysql. MySQLExecutionDAOCreateWorkflowTest) java.util.concurrent.ExecutionException: com.netflix.conductor.core. execution.ApplicationException: BACKEND_ERROR - Duplicate entry 'workflow0' for key 'unique_workflow_id' at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflowTest. testcreateTasksWithParallelThreads(MySQLExecutionDAOCreateWorkflo wTest.java:134) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflo wTest.testWith5THreads(MySQLExecutionDAOCreateWorkflowTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall( FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run( ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively( FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod. evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores. evaluate(RunBefores.java:28) at org.junit.rules.ExpectedException$ExpectedExceptionStatement. evaluate(ExpectedException.java:110) at org.junit.rules.RunRules.evaluate(RunRules.java:18) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution. run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. main(RemoteTestRunner.java:206) Caused by: com.netflix.conductor.core.execution.ApplicationException: BACKEND_ERROR - Duplicate entry 'workflow0' for key 'unique_workflow_id' at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction( MySQLBaseDAO.java:103) at com.netflix.conductor.dao.mysql.MySQLBaseDAO. withTransaction(MySQLBaseDAO.java:125) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO. insertOrUpdateWorkflow(MySQLExecutionDAO.java:479) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO.createWorkflow( MySQLExecutionDAO.java:235) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflo wTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:119) at com.netflix.conductor.dao.mysql.MySQLExecutionDAOCreateWorkflo wTest$1.call(MySQLExecutionDAOCreateWorkflowTest.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run( ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: com.netflix.conductor.core.execution.ApplicationException: Duplicate entry 'workflow0' for key 'unique_workflow_id' at com.netflix.conductor.dao.mysql.Query.executeUpdate(Query.java:274) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO. lambda$33(MySQLExecutionDAO.java:543) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.execute( MySQLBaseDAO.java:170) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO. addWorkflow(MySQLExecutionDAO.java:542) at com.netflix.conductor.dao.mysql.MySQLExecutionDAO. lambda$31(MySQLExecutionDAO.java:481) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.lambda$4( MySQLBaseDAO.java:126) at com.netflix.conductor.dao.mysql.MySQLBaseDAO.getWithTransaction( MySQLBaseDAO.java:98) ... 9 more
On Thu, Jun 28, 2018 at 12:10 PM, Pascal Chouinard < [email protected]> wrote:
Hi @mashurex https://github.com/mashurex
Yeah I think your refactoring was great and necessary. Also, like you said, both our workloads didn't make these concurrency issue come out.
I agree its a great opportunity to fix those issues and improve the MySQL support now that we are more using it and that can contribute to it.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Netflix/conductor/issues/630#issuecomment-401088674, or mute the thread https://github.com/notifications/unsubscribe-auth/AHJ_Q0GRIafHE2_aUEfqTWIysUQK81eCks5uBP_igaJpZM4U6QbA .
Hey @picaron and @mashurex ,
I have compared MYSQL DAO code with REDIS DAOs written by Netflix Team
While I like the overall approach you took in writing the SQL code, As mentioned by @mashurex we suggest we should reconsider the below MYSQL recommondations
1) Do we need to run the SQL statements as “one unit of work”?
I think the answer is no. Even the REDIS DAOs implemented by Netflix team are not transactional. In other words, I enabled a break point and started a simple workflow. After making few inserts into REDIS, I shutdown REDIS database server to make workflow fail. The client gets an workflow error message.
Now, I have restarted the REDIS database and found the workflow in a limbo state with incomplete data. Why can't we apply the same rule of thumb to MYSQL DAOs. Additionally, Even if we make MYSQL DAOs to run with setAutoCommit(false), the system is still not transactional because Netflix Conductor integrates with Elastic Search external system for Indexing the data. If the ES system is down, your data is out of sync.
I recommend disabling the transactions in DAOs and rewrite the code as
MySQLWorkflowModule
@Provides
@Singleton
public DataSource getDataSource(Configuration config) {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(config.getProperty("jdbc.url", "jdbc:mysql://localhost:3306/conductor"));
dataSource.setUsername(config.getProperty("jdbc.username", "conductor"));
dataSource.setPassword(config.getProperty("jdbc.password", "password"));
dataSource.setMaximumPoolSize(100);
dataSource.setAutoCommit(true); // This line requires chnage
flywayMigrate(config, dataSource);
return dataSource;
}
MySQLBaseDAO
protected <R> R getWithTransaction(TransactionalFunction<R> function) {
Instant start = Instant.now();
LazyToString callingMethod = getCallingMethod();
logger.trace("{} : starting transaction", callingMethod);
try(Connection tx = dataSource.getConnection()) {
tx.setAutoCommit(true);
try {
R result = function.apply(tx);
return result;
} catch (Throwable th) {
throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, th.getMessage(), th);
}
} catch (SQLException ex) {
throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, ex.getMessage(), ex);
} finally {
logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis());
}
}
2) Caching As workers poll data continuously and for an infinite amount of time, there is lot of overhead on tables like poll_data and queue_message. When the no active scheduled tasks , the workers can still poll for tasks every second. If concurrent worker threads are polling for tasks that are empty, we should make attempts to not put load on database servers. The current REDIS DAO’s also has the same limitation as REDIS is in memory database. We can’t afford to take the same technique on Relational databases like MYSQL as overhead would be more on infinite selects. We need to find a simplified caching solution. What do you think?
3) Rewriting queries If we make setAutoCommit(true), all of the transactions will be short lived and we can avoid all of the deadlocks. While I am not MYSQL expert, I found most of locks happens because of setAutoCommit(false) . It starts with Share and Exclusive locks, but will be elevated as Intention Locks resulting in locking the whole table. If we change setAutoCommit(true), this problem will go away and we can relook at fixing the queries at later point of time
4) Race conditions I see race condition in the MySQLQueueDAO code as
pop() method makes two calls
- PeekMessage
- popMessages
private List<String> peekMessages(Connection connection, String queueName, int count) {
if (count < 1) return Collections.emptyList();
final long peekTime = System.currentTimeMillis() + 1;
String PEEK_MESSAGES = "SELECT message_id FROM queue_message WHERE queue_name = ? AND popped = false AND deliver_on <= TIMESTAMP(?) ORDER BY deliver_on, created_on LIMIT ?";
return query(connection, PEEK_MESSAGES, q -> q.addParameter(queueName).addTimestampParameter(peekTime).addParameter(count).executeScalarList(String.class));
}
private List<String> popMessages(Connection connection, String queueName, List<String> messageIds) {
if (messageIds.isEmpty()) {
return messageIds;
}
final String POP_MESSAGES = "UPDATE queue_message SET popped = true WHERE queue_name = ? AND message_id IN (%s) AND popped = false";
final String query = String.format(POP_MESSAGES, Query.generateInBindings(messageIds.size()));
int result = query(connection, query, q -> q.addParameter(queueName).addParameters(messageIds).executeUpdate());
if (result != messageIds.size()) {
String message = String.format("could not pop all messages for given ids: %s (%d messages were popped)", messageIds, result);
throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, message);
}
return messageIds;
}
If two concurrent connections tries to pop from the same queue, there can be a race condition with this check
if (result != messageIds.size()) { // Can we remove this check
String message = String.format("could not pop all messages for given ids: %s (%d messages were popped)", messageIds, result);
}
5) Connection Pooling: Please refer to the thread https://github.com/brettwooldridge/HikariCP/issues/256 We need to expose three properties for applications to change the DataSources maximumPoolSize minimumIdle idleTimeout
@v1r3n @apanicker-nflx This issue can be closed as @jvemugunta addressed it in PR #771