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

Add json columns in Postgres #1865

Merged
merged 1 commit into from
Jul 8, 2021
Merged

Add json columns in Postgres #1865

merged 1 commit into from
Jul 8, 2021

Conversation

pm47
Copy link
Member

@pm47 pm47 commented Jul 7, 2021

A json column has been added to the few tables that contains an
opaque serialized blob:

  • local_channels.data
  • nodes.data
  • channels.channel_announcement, channels.channel_update_x

We can now access all the individual data fields from SQL.

For the serialization, we use the same serializers than the one
that were previously used by the API. They have been moved to the
eclair-core module and simplified a bit.

There are two json data types in Postgres: JSON and JSONB. We use
the latter one, which is more recent, and allows indexing.

An alternative to this PR would have been to use columns, but:

  • there would have been a lot of columns for the channel data
  • every modification of our types would have required a db migration

NB: to handle non-backwards compatible changes in the json serializersi,
all the json columns can be recomputed on restart by setting
eclair.db.reset-json-columns=true.

@pm47 pm47 requested a review from t-bast July 7, 2021 09:31
@pm47 pm47 mentioned this pull request Jul 7, 2021
@codecov-commenter
Copy link

Codecov Report

Merging #1865 (e5cb371) into master (85ed433) will decrease coverage by 1.84%.
The diff coverage is 61.81%.

@@            Coverage Diff             @@
##           master    #1865      +/-   ##
==========================================
- Coverage   88.94%   87.09%   -1.85%     
==========================================
  Files         150      156       +6     
  Lines       11239    11666     +427     
  Branches      447      464      +17     
==========================================
+ Hits         9997    10161     +164     
- Misses       1242     1505     +263     
Impacted Files Coverage Δ
...ain/scala/fr/acinq/eclair/balance/Monitoring.scala 0.00% <0.00%> (ø)
.../acinq/eclair/blockchain/bitcoind/ZmqWatcher.scala 95.95% <ø> (-0.92%) ⬇️
.../main/scala/fr/acinq/eclair/channel/Register.scala 81.48% <ø> (ø)
...n/scala/fr/acinq/eclair/balance/BalanceActor.scala 11.32% <11.32%> (ø)
...r-core/src/main/scala/fr/acinq/eclair/Eclair.scala 51.04% <25.00%> (-1.10%) ⬇️
...lockchain/bitcoind/rpc/ExtendedBitcoinClient.scala 91.17% <50.00%> (-2.77%) ⬇️
...n/scala/fr/acinq/eclair/json/JsonSerializers.scala 87.17% <55.55%> (ø)
...n/scala/fr/acinq/eclair/balance/CheckBalance.scala 58.82% <58.82%> (ø)
.../src/main/scala/fr/acinq/eclair/db/Databases.scala 80.95% <60.00%> (-4.77%) ⬇️
...main/scala/fr/acinq/eclair/db/jdbc/JdbcUtils.scala 93.93% <86.66%> (-2.14%) ⬇️
... and 23 more

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.

First review for 5a8d76d

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.

Reviewing e5cb371, could you also fix ChannelCodecsSpec where we copy-pasted the serializers because we couldn't reference them once they were in eclair-node, but now we can directly reference them instead of duplicating.

@pm47
Copy link
Member Author

pm47 commented Jul 7, 2021

Reviewing e5cb371, could you also fix ChannelCodecsSpec where we copy-pasted the serializers because we couldn't reference them once they were in eclair-node, but now we can directly reference them instead of duplicating.

Good catch, done with c5804ad. Note that this test is pretty subtle and brings strong non-reg guarantees, see the commit message.

@t-bast
Copy link
Member

t-bast commented Jul 7, 2021

ACK c5804ad

Very nice to improve these tests, adding json serialization will indeed make that kind of tests more readable in the future, we should leverage it more.

@pm47
Copy link
Member Author

pm47 commented Jul 7, 2021

Rebased on parent PR and squashed.

A json column has been added to the few tables that contains an
opaque serialized blob:
- `local_channels.data`
- `nodes.data`
- `channels.channel_announcement`, `channels.channel_update_x`

We can now access all the individual data fields from SQL.

For the serialization, we use the same serializers than the one
that were previously used by the API. They have been moved to the
`eclair-core` module and simplified a bit.

There are two json data types in Postgres: `JSON` and `JSONB`. We use
the latter one, which is more recent, and allows indexing.

An alternative to this PR would have been to use columns, but:
- there would have been a *lot* of columns for the channel data
- every modification of our types would have required a db migration

NB: to handle non-backwards compatible changes in the json serializersi,
 all the json columns can be recomputed on restart by setting
`eclair.db.reset-json-columns=true`.

Change in in ChannelCodecsSpec:

The goal of this test is to make sure that, in addition to successfully
decoding data that encoded with an older codec, we actually read the
correct data. Just because there is no error doesn't mean that we
interpreted the data properly. For example we could invert a
`payment_hash` and a `payment_preimage`.

We can't compare object to object, because the current version of the
class has probably changed too. That's why we compare using the json
representation of the data, that we amend to ignore new or modified
fields.

After doing a manual comparison, I updated the test to use the current
json serializers, and replaced the test data with the latest json
serialization. This allows us to remove all the tweaks that we added
over time to take into account new and updated fields.
@pm47
Copy link
Member Author

pm47 commented Jul 8, 2021

Parent PR merged, rebased on master.

@pm47 pm47 requested a review from t-bast July 8, 2021 12:07
@pm47 pm47 merged commit 08faf3b into master Jul 8, 2021
@pm47 pm47 deleted the pg-json-columns branch July 8, 2021 13:02
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.

None yet

3 participants