Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posgres: fix concurrency in channels db #1762

Merged
merged 6 commits into from
Apr 12, 2021
Merged

Posgres: fix concurrency in channels db #1762

merged 6 commits into from
Apr 12, 2021

Conversation

pm47
Copy link
Member

@pm47 pm47 commented Apr 12, 2021

The upsert pattern in channelsDb.addOrUpdateChannel is apparently not compatible with concurrent transactions when they are in SERIALIZABLE mode.

A simple test shows the issue and the following exception is thrown:

Canceled on identification as a pivot, during conflict out checking

I tried reducing the isolation level, but 1) I'm not sure what the consequences are in terms of locking the db and 2) it didn't entirely fix the issue.

As per the postgres doc, the recommended construct for the upsert pattern is with the ON CONFLICT clause:

It is recommended that applications use INSERT with ON CONFLICT DO
UPDATE rather than actually using this pattern.

I'm not sure about the performance of this (we mostly do updates, so we will run into the ON CONFLICT a lot), but let's first make it work.

This test unveils a concurrency issue in the upsert logic of the local
channels db, with the following error being thrown when we update many
channels concurrently:

```
Canceled on identification as a pivot, during conflict out checking
```
This is the recommended pattern according to postgres doc
(https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE):

> It is recommended that applications use INSERT with ON CONFLICT DO
UPDATE rather than actually using this pattern.
@pm47 pm47 requested a review from t-bast April 12, 2021 08:12
Copy link
Member

@t-bast t-bast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we have the same kind of issue with addOrUpdatePeer?

@pm47
Copy link
Member Author

pm47 commented Apr 12, 2021

Why don't we have the same kind of issue with addOrUpdatePeer?

We probably do! Will do a pass on all other db calls.

@pm47
Copy link
Member Author

pm47 commented Apr 12, 2021

Why don't we have the same kind of issue with addOrUpdatePeer?

We probably do! Will do a pass on all other db calls.

Done with bb93ee5.

@codecov-io
Copy link

Codecov Report

Merging #1762 (bb93ee5) into master (1e2abae) will increase coverage by 0.07%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1762      +/-   ##
==========================================
+ Coverage   89.17%   89.24%   +0.07%     
==========================================
  Files         143      143              
  Lines       10772    10768       -4     
  Branches      477      464      -13     
==========================================
+ Hits         9606     9610       +4     
+ Misses       1166     1158       -8     
Impacted Files Coverage Δ
...src/main/scala/fr/acinq/eclair/db/pg/PgUtils.scala 91.17% <ø> (ø)
...ain/scala/fr/acinq/eclair/db/pg/PgChannelsDb.scala 95.83% <100.00%> (-0.12%) ⬇️
...c/main/scala/fr/acinq/eclair/db/pg/PgPeersDb.scala 97.22% <100.00%> (-0.15%) ⬇️
...c/main/scala/fr/acinq/eclair/channel/Channel.scala 86.68% <0.00%> (+0.08%) ⬆️
...main/scala/fr/acinq/eclair/io/PeerConnection.scala 83.40% <0.00%> (+0.42%) ⬆️
...cala/fr/acinq/eclair/payment/relay/NodeRelay.scala 95.52% <0.00%> (+1.49%) ⬆️
...main/scala/fr/acinq/eclair/router/Validation.scala 92.30% <0.00%> (+1.53%) ⬆️

@pm47 pm47 merged commit 6518bb4 into master Apr 12, 2021
@pm47 pm47 deleted the pg-concurrency branch April 12, 2021 10:18
@pm47 pm47 mentioned this pull request May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants