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

SQL Driver for KV interface #36

Closed
wants to merge 7 commits into from

Conversation

griffin
Copy link
Contributor

@griffin griffin commented Feb 4, 2023

Enables using a SQL database as a registry through the KV interface.

Todo:

  • Fix prepared statements to use Postgres syntax
  • Add postgres container to CI

Fixes #15

@griffin griffin mentioned this pull request Feb 4, 2023
Copy link
Owner

@richardartoul richardartoul left a comment

Choose a reason for hiding this comment

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

This looks really good, just left a few mostly nit commments with one legit one about versionstamp.

also thanks for the CI PR, I really appreciate that!

virtual/registry/sql_kv.go Outdated Show resolved Hide resolved
virtual/registry/sql_kv.go Outdated Show resolved Hide resolved
virtual/registry/sql_kv.go Outdated Show resolved Hide resolved
return nil, err
}

// TODO Improve indexing
Copy link
Owner

Choose a reason for hiding this comment

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

What can be improved here? Just curious if you have any ideas

Copy link
Contributor Author

@griffin griffin Feb 7, 2023

Choose a reason for hiding this comment

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

I added a comment in here suggesting that we could add an index on the first few bytes of the byte array to improve the performance of iterPrefix.

return nil, fmt.Errorf("failed to prepare delete statement: %w", err)
}

rangeStmt, err := db.Prepare("SELECT k, v FROM nola_kv WHERE k LIKE ?;")
Copy link
Owner

Choose a reason for hiding this comment

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

Can you use any of these functions (sub string maybe?) to make this faster: https://www.postgresql.org/docs/9.0/functions-binarystring.html

virtual/registry/sql_kv.go Outdated Show resolved Hide resolved
virtual/registry/sql_kv.go Outdated Show resolved Hide resolved
}

func (sqltransactionkv *sqlTransactionKV) getVersionStamp() (int64, error) {
return time.Since(sqltransactionkv.vst).Microseconds(), nil
Copy link
Owner

Choose a reason for hiding this comment

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

I did a bit of googling and I think we can do something that is actually correct using Postgres using this function: https://pgpedia.info/p/pg_current_xact_id.html

https://www.postgresql.org/docs/current/functions-info.html#FUNCTIONS-PG-SNAPSHOT

double check me but it seems like calling that function in a new transaction is guaranteed to give us a monotonically increasing number like FDBs version stamp.

of course it won’t increase at the right “rate” which is a bit annoying…

At the very least instead of using local timestamp, can you run a transaction that calls a function in Postgres to get the time? This makes it so we only depend on a single nodes clock (Postgres) being consistent and increasing monotonically instead of every node in the clusters clock being accurate

Copy link
Owner

Choose a reason for hiding this comment

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

There is probably some smart way to combiners these two ideas but I’m not sure what it is. I’ll take calling the get time function in a Postgres transaction for now though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is probably some smart way to combiners these two ideas but I’m not sure what it is. I’ll take calling the get time function in a Postgres transaction for now though

Yeah I'll do this for now.

From what I saw in a comment, this should be increasing at a rate of 1mil/sec, from what I understand FDB does dummy transactions under the hood which keeps this value increasing. In this case I'd need to see when PG updates this value if no changes are made.

virtual/registry/sql_registry.go Outdated Show resolved Hide resolved
virtual/registry/sql_registry.go Outdated Show resolved Hide resolved
@griffin
Copy link
Contributor Author

griffin commented Feb 7, 2023

Resolved most of the comments, need to finish testing, no need to re-review yet.

@richardartoul richardartoul mentioned this pull request Feb 20, 2023
@griffin griffin closed this Jun 20, 2023
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.

Implement SQL Registry
2 participants