Hello,
I tried multiple thread to persist a list of Edges. While persist edges, each node will be first check existed,
if not existed , then persist the node and later the edge .
I encountered the error:
io.dgraph.TxnConflictException: Transaction has been aborted. Please retry.
at io.dgraph.DgraphClient$Transaction.checkAndThrowException(DgraphClient.java:315)
at io.dgraph.DgraphClient$Transaction.commit(DgraphClient.java:256)
at com.wacai.dgraph.PhoneEdgeDgraphPersister.run(PhoneEdgeDgraphPersister.java:113)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Is there any good solution to avoid transaction aborted error?
This error is thrown if you are trying to modify the same indexes or data in transactions executing concurrently.
If you are not doing upserts, you could use the IgnoreIndexConflict flag.
Check your schema and remove unnecessary indexes to avoid conflict.
If you are doing upserts, execute the mutation to create the node if it doesn’t with IgnoreIndexConflict as false, then add other edges with IgnoreIndexConflict as true in another mutation.
For example. - You might check if a person exists by checking their email. If it doesn’t, then create a person with the email edge. Then you can get back their uid and add other edges like name, age etc with IgnoreIndexConflict as true.
The schema is : phone_number: string @index(exact), only one index
And my logic to insert an edge is : my code will first check point existed, if not existed try to upset node in transaction. And then query the edge and also upset the edge in transaction.
I could not understand why transaction was aborted while multiple threads are used for upset?
Don’t use IgnoreIndexConflict while doing upserts. If multiple transactions are trying to say set the same phone_number at the same time then one of them will abort. How many phone numbers are you trying to upsert in one mutation? How often are you committing?
For upserts, I would suggest having smaller batch size and committing more frequently to minimize conflicts and hence aborts.
Actually the abort is raised during upsets.
Still I could not find any better solution but catch the exception and re-try several times. It seems works after a lot tests.
I would like to work out any better solution.