Description
Overview:
We should follow the example set by LND and migrate our kvdb stores to SQL. Luckily, LiTs set of tables is quite small and so this is quite a contained task. We also have never supported the kv schema in other backend options like sql or postgres and don't currently have any tables in any sql backend. This is nice because it keeps set of possible starting points to a size of 1: Migrate bbolt kvstore data to native sql stores (either postgres or sqlite). I think that since currently we only store LNC data in LiT's DB, nothing is critical and so I think we should force the migration at the end of the day and not necessarily let uses opt to not run the migration.
Getting started:
We can get started by:
- defining the new SQL tables (dont actually trigger them in anything other than tests)
- defining the SQL queries & putting them behind an interface such that all current unit tests can be run against both the kvdb backends and new sql backends.
The above can be done for the accounts
, actions
, session
and firewalldb
stores.
We can get started on the work for the above and merge things well before we need to worry about the final switch to sql.
End goal:
By the end of the project, we want a single SQL backend with the following relations.
Action Plan/Progress tracking
Phase 1: Defining Schemas
By the end of phase 1, we want to be able to spin up LiT in dev
mode with a full SQL backend.
To do this, we will follow the following conversion strategy for each DB store:
- massage the given Store interface into one that is more SQL compatible
- Make any changes to the unit tests and KVDB impl that will make that unit tests runnable against all DB backends.
- Define the SQL schemas & queries
- Implement the SQL version of the Store interface (CRUD layer)
- Get the CI to run all that DB's unit tests against all possible backends.
- get the itests running against the new backends
We will do the above steps for each of the following and in the following order:
A) The Accounts store
B) Sessions store
C) rules kvstore
D) rules privacy mapper store
E) actions store.
Phase 2: Migration code
Here we will write the migration functions for each of the tables plus thorough tests for them. At the end of this phase, we are still not exposing any new functionality to a prod release.
For each database store, we will:
-
Implement the migration function.
When invoked with a specific KVDB store, this function will copy all data from that KVDB store into the corresponding SQLB store. -
Create unit tests.
- Populate a KVDB instance with every possible combination of data relevant to that store, as well as randomised data.
- Run the migration function against that KVDB instance and verify that the data is successfully migrated.
-
Ensure that the unit tests is executed successfully against
postgres
&sqllite
backends. -
Manually verify the migration locally.
- Use a modified
litd
instance with thedev
build flag enabled to expose the migration. - Confirm that the migration runs without errors.
- Use a modified
We will repeat these four steps for each of the following stores, in the order listed:
A) The Accounts store
B) Sessions store
C) rules kvstore
D) rules privacy mapper store
E) actions store.
Phase 3: Calling the migration code & handling user config
Finally, we will invoke the migration logic and add configuration options to switch to a new backend. This process will be carried out in the following steps:
-
Update
litd
to usesqldb-v2
. -
Use
sqldb-v2
to run the standard SQL migrations inlitd
, and add support that ensures that the exact set of SQLCmodels
andqueries
that existed when each migration was introduced can be used. -
Add a migration step to the SQL migration list that transfers data from KVDB to SQL. Initially, this step will only run if the
dev
migrate-to-sql
flag is set. -
Add an integration test that:
- Starts a
litd
instance using abbolt
databasebackend
. - Populates all relevant stores.
- Restarts
litd
with an SQL backend and themigrate-to-sql
flag enabled. - Verifies that the migration completes successfully.
- Starts a
-
Remove the
migrate-to-sql
flag and make KVDB-to-SQL migrations run automatically whenever an SQL backend is selected. Note: at this stage, thesqlite
&postgres
backends will only be supported in development builds. -
Introduce a
disable-kvdb-to-sql-migration
configuration flag. When set, this flag will prevent the KVDB→SQL migration from running. -
Perform extensive manual testing of KVDB→SQL migrations, especially on large
litd
nodes with substantial data volumes. -
Enable the
sqlite
andpostgres
databasebackend
options in the production configuration, and designate one of them as the default. -
Once a node has successfully migrated from KVDB to SQL:
- Delete the data stored in the Bbolt DB.
- Prevent
litd
from starting ifbbolt
is still configured as thedatabasebackend
.
Phase 4: Removing support for bbolt
database backends
After sufficient time has passed and we are confident that KVDB→SQL migrations will succeed for all users, we will remove BoltDB support by:
- Removing support for the the
disable-kvdb-to-sql-migration
configuration flag. - Removing
bbolt
from the list of supporteddatabasebackend
options.
Progress Checklists
Phase 1 (Schemas/Queries/CRUD)
### Accounts: - [x] Prep/Cleanup - [x] Prepare the accounts `Store` interface to be ready for a SQL impl - [x] Prep unit tests to be ready to pass against a different backend - [x] #956 - [x] implement the SQL CRUD (ie, the SQL version of the Store interface). - [x] Run the above impl against the existing unit tests (part of CI) - [x] CI step for getting itests to run against LiT with SQL store for accounts
Sessions:
- Prep/Cleanup
- Prepare the sessions
Store
interface to be ready for a SQL impl #966 - Prep unit tests to be ready to pass against a different backend
- Define the SQL schemas and queries
- implement the SQL CRUD (ie, the SQL version of the Store interface).
- Run the above impl against the existing unit tests (part of CI)
- CI step for getting itests to run against LiT with SQL store for sessions
KV stores
- Prep/Cleanup
- Prepare the kvstores interface to be ready for a SQL impl
- Prep unit tests to be ready to pass against a different backend
- Define the SQL schemas and queries
- implement the SQL CRUD (ie, the SQL version of the Store interface).
- Run the above impl against the existing unit tests (part of CI)
- CI step for getting itests to run against LiT with SQL store for kvstores
Privacy Pairs
- Prep/Cleanup
- Prepare the privacy map store interface to be ready for a SQL impl
- Prep unit tests to be ready to pass against a different backend
- Define the SQL schemas and queries
- implement the SQL CRUD (ie, the SQL version of the Store interface).
- Run the above impl against the existing unit tests (part of CI)
- CI step for getting itests to run against LiT with SQL store for privacy pairs
Actions
- Prep/Cleanup
- Prepare the actions store interface to be ready for a SQL impl
- Prep unit tests to be ready to pass against a different backend
- Define the SQL schemas and queries
- implement the SQL CRUD (ie, the SQL version of the Store interface).
- Run the above impl against the existing unit tests (part of CI)
- CI step for getting itests to run against LiT with SQL store for actions
Phase 2 (Migration)
- Accounts store migration implementation
- Sessions store migration implementation
- rules kvstore migration implementation
- rules privacy migration implementation
- actions store migration implementation
Phase 3 (Integration)
- Use
sqldb-v2
inlitd
. - Use
sqldb-v2
to run sql migrations with the correct models & queries - Add the kvdb to sql migration step
- Add itest coverage
- Automatically trigger the kvdb to sql migration from SQL backends
- Introduce a
disable-kvdb-to-sql-migration
configuration flag - Extensive manual testing of KVDB→SQL migrations
- Enable the
sqlite
andpostgres
databasebackend
for the prod conf - Ensure successfully migrated nodes can't use
bbolt
backends