Neo4j 頻繁導入數據導致DeadLock的問題

2018-01-04:補充說明

如下的解決方案還是會偶爾蹦出幾個DeadLock的問題,在運維查看了neo4j的內存占用時發現一個問題,neo4j十分吃swap,導致執行效率十分低下,再次仔細查閱官網發現缺少了一個很重要的內存配置參數,

dbms.memory.pagecache.size=1g

The amount of memory to use for mapping the store files, in bytes (or
kilobytes with the 'k' suffix, megabytes with 'm' and gigabytes with 'g').
If Neo4j is running on a dedicated server, then it is generally recommended
to leave about 2-4 gigabytes for the operating system, give the JVM enough
heap to hold all your transaction state and query context, and then leave the
rest for the page cache.
The default page cache memory assumes the machine is dedicated to running
Neo4j, and is heuristically set to 50% of RAM minus the max Java heap size.


 {"results":[],"errors":[{"code":"Neo.TransientError.Transaction.DeadlockDetected","message":"LockClient[6092] can't wait on resource RWLock[NODE(133321815), hash=1215661247] since => LockClient[6092] <-[:HELD_BY]- RWLock[NODE(38473669), hash=1470152465] <-[:WAITING_FOR]- LockClient[6091] <-[:HELD_BY]- RWLock[NODE(133321815), hash=1215661247]","stackTrace":"org.neo4j.kernel.DeadlockDetectedException: LockClient[6092] can't wait on resource RWLock[NODE(133321815), hash=1215661247] since => LockClient[6092] <-[:HELD_BY]- RWLock[NODE(38473669), hash=1470152465] <-[:WAITING_FOR]- LockClient[6091] <-[:HELD_BY]- RWLock[NODE(133321815), hash=1215661247]\n\tat org.neo4j.kernel.impl.locking.community.RagManager.checkWaitOnRecursive(RagManager.java:198)\n\tat org.neo4j.kernel.impl.locking.community.RagManager.checkWaitOnRecursive(RagManager.java:225)\n\tat org.neo4j.kernel.impl.locking.community.RagManager.checkWaitOn(RagManager.java:165)\n\tat org.neo4j.kernel.impl.locking.community.RWLock.acquireWriteLock(RWLock.java:396)\n\tat org.neo4j.kernel.impl.locking.community.LockManagerImpl.getWriteLock(LockManagerImpl.java:67)\n\tat org.neo4j.kernel.impl.locking.community.CommunityLockClient.acquireExclusive(CommunityLockClient.java:147)\n\tat org.neo4j.kernel.impl.api.LockingStatementOperations.exclusiveOptimisticLock(LockingStatementOperations.java:539)\n\tat org.neo4j.kernel.impl.api.LockingStatementOperations.acquireExclusiveNodeLock(LockingStatementOperations.java:500)\n\tat org.neo4j.kernel.impl.api.LockingStatementOperations.lockRelationshipNodes(LockingStatementOperations.java:307)\n\tat org.neo4j.kernel.impl.api.LockingStatementOperations.relationshipCreate(LockingStatementOperations.java:286)\n\tat org.neo4j.kernel.impl.api.OperationsFacade.relationshipCreate(OperationsFacade.java:854)\n\tat org.neo4j.cypher.internal.spi.v3_3.TransactionBoundQueryContext.createRelationship(TransactionBoundQueryContext.scala:109)\n\tat org.neo4j.cypher.internal.spi.v3_3.TransactionBoundQueryContext.createRelationship(TransactionBoundQueryContext.scala:70)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.ExceptionTranslatingQueryContext$$anonfun$createRelationship$2.apply(ExceptionTranslatingQueryContext.scala:200)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.ExceptionTranslatingQueryContext$$anonfun$createRelationship$2.apply(ExceptionTranslatingQueryContext.scala:200)\n\tat org.neo4j.cypher.internal.spi.v3_3.ExceptionTranslationSupport$class.translateException(ExceptionTranslationSupport.scala:32)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.ExceptionTranslatingQueryContext.translateException(ExceptionTranslatingQueryContext.scala:39)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.ExceptionTranslatingQueryContext.createRelationship(ExceptionTranslatingQueryContext.scala:200)\n\tat org.neo4j.cypher.internal.spi.v3_3.UpdateCountingQueryContext.createRelationship(UpdateCountingQueryContext.scala:92)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.BaseRelationshipPipe.org$neo4j$cypher$internal$compatibility$v3_3$runtime$pipes$BaseRelationshipPipe$$createRelationship(CreateRelationshipPipe.scala:47)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.BaseRelationshipPipe$$anonfun$internalCreateResults$1.apply(CreateRelationshipPipe.scala:41)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.BaseRelationshipPipe$$anonfun$internalCreateResults$1.apply(CreateRelationshipPipe.scala:41)\n\tat scala.collection.Iterator$$anon$11.next(Iterator.scala:410)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.EmptyResultPipe.internalCreateResults(EmptyResultPipe.scala:29)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.PipeWithSource.createResults(Pipe.scala:62)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.PipeWithSource.createResults(Pipe.scala:59)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.executionplan.DefaultExecutionResultBuilderFactory$ExecutionWorkflowBuilder.createResults(DefaultExecutionResultBuilderFactory.scala:102)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.executionplan.DefaultExecutionResultBuilderFactory$ExecutionWorkflowBuilder.build(DefaultExecutionResultBuilderFactory.scala:74)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.BuildInterpretedExecutionPlan$$anonfun$getExecutionPlanFunction$1.apply(BuildInterpretedExecutionPlan.scala:111)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.BuildInterpretedExecutionPlan$$anonfun$getExecutionPlanFunction$1.apply(BuildInterpretedExecutionPlan.scala:94)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.runtime.BuildInterpretedExecutionPlan$$anon$1.run(BuildInterpretedExecutionPlan.scala:62)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.Compatibility$ExecutionPlanWrapper$$anonfun$run$1.apply(Compatibility.scala:182)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.Compatibility$ExecutionPlanWrapper$$anonfun$run$1.apply(Compatibility.scala:178)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.exceptionHandler$runSafely$.apply(exceptionHandler.scala:90)\n\tat org.neo4j.cypher.internal.compatibility.v3_3.Compatibility$ExecutionPlanWrapper.run(Compatibility.scala:178)\n\tat org.neo4j.cypher.internal.PreparedPlanExecution.execute(PreparedPlanExecution.scala:29)\n\tat org.neo4j.cypher.internal.ExecutionEngine.execute(ExecutionEngine.scala:116)\n\tat org.neo4j.cypher.internal.ExecutionEngine.execute(ExecutionEngine.scala:111)\n\tat org.neo4j.cypher.internal.javacompat.ExecutionEngine.executeQuery(ExecutionEngine.java:76)\n\tat org.neo4j.server.rest.transactional.TransactionHandle.safelyExecute(TransactionHandle.java:371)\n\tat org.neo4j.server.rest.transactional.TransactionHandle.executeStatements(TransactionHandle.java:323)\n\tat org.neo4j.server.rest.transactional.TransactionHandle.commit(TransactionHandle.java:157)\n\tat org.neo4j.server.rest.web.TransactionalService.lambda$executeStatementsAndCommit$1(TransactionalService.java:218)\n\tat com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider.writeTo(StreamingOutputProvider.java:71)\n\tat com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider.writeTo(StreamingOutputProvider.java:57)\n\tat com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)\n\tat com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)\n\tat com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)\n\tat com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)\n\tat com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)\n\tat com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)\n\tat com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:790)\n\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)\n\tat org.neo4j.server.rest.dbms.AuthorizationDisabledFilter.doFilter(AuthorizationDisabledFilter.java:49)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)\n\tat org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\n\tat org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:499)\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)\n\tat org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)\n\tat java.lang.Thread.run(Thread.java:748)\n"}]}

并發導入數據到neo4j時,時常給出這個LockDead的錯誤,官方沒有明確的給出解決方案,但是有相關介紹【官方說deadlock】 ,上線前,想使用錯誤重試方案來補救并減少這中LockDead的出現幾率,沒有時間測試和實施(跟上頭說,上頭也是含糊其辭……不爽),只好強制將規則矩陣由并發改為串行,大量的體力活(因為商戶數量眾多,大概十家,這樣的改動導致上線步驟繁瑣,各種體力活)。
今天抽出空,大體跑了一下使用錯誤恢復補救方案,效果還可以,編碼思路很簡單,前期測試階段給出三次重新錯誤重試次數,間隔步長為7、8、11秒,但是仍然會有極小的幾率出現,是不是要改成無限重試的?還得繼續觀察。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容