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

migrate token pooling to postgres #8922

Merged
merged 4 commits into from
Feb 23, 2023
Merged

Conversation

chris48s
Copy link
Member

@chris48s chris48s commented Feb 18, 2023

This PR implements the suggestion from issue #8921 allowing us to migrate the token pool from redis to fly.io postgres.

@chris48s chris48s added the operations Hosting, monitoring, and reliability for the production badge servers label Feb 18, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2023

Warnings
⚠️ This PR modified the server but none of its tests.
That's okay so long as it's refactoring existing code.
Messages
📖 ✨ Thanks for your contribution to Shields, @chris48s!

Generated by 🚫 dangerJS against dbf5c67

@socket-security
Copy link

socket-security bot commented Feb 18, 2023

Socket Security Pull Request Report

👍 No new dependency issues detected in pull request

Pull request report summary
Issue Status
Install scripts ✅ 0 issues
Native code ✅ 0 issues
Bin script shell injection ✅ 0 issues
Unresolved require ✅ 0 issues
Invalid package.json ✅ 0 issues
HTTP dependency ✅ 0 issues
Git dependency ✅ 0 issues
Potential typo squat ✅ 0 issues
Known Malware ✅ 0 issues
Telemetry ✅ 0 issues
Protestware/Troll package ✅ 0 issues
Bot Commands

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore [email protected] [email protected]

Powered by socket.dev

@@ -54,8 +54,10 @@
"lodash.groupby": "^4.6.0",
"lodash.times": "^4.3.2",
"node-env-flag": "^0.1.0",
"node-pg-migrate": "^6.2.2",
Copy link
Member Author

Choose a reason for hiding this comment

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

Even though we don't expect to change our DB schema super often, I think its worth having a migration package to make it easier to evolve this over time. Ideally we should also look at running npm run migrate up on deploy. For the first deploy I think it can live with doing it manually.

Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be a prod dep or can it still be utilized as a dev dep?

Copy link
Member Author

Choose a reason for hiding this comment

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

In order to create the DB (in the first instance) or modify it (later) we need to be able to run npm run migrate up in the production environment so it has to be a prod dependency.

migrations/1676731511125_initialize.cjs Show resolved Hide resolved
core/token-pooling/sql-token-persistence.js Show resolved Hide resolved
package-lock.json Show resolved Hide resolved
Comment on lines +37 to +44
beforeEach('Create temporary table', async function () {
await pool.query(
`CREATE TEMPORARY TABLE ${tableName} (LIKE github_user_tokens INCLUDING ALL);`
)
})
afterEach('Drop temporary table', async function () {
await pool.query(`DROP TABLE IF EXISTS pg_temp.${tableName};`)
})
Copy link
Member Author

Choose a reason for hiding this comment

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

This copies our table to a temp schema for the purposes of running the tests then clears it up again after. This means we can run the tests against a local DB with real data in it and not break our local data. We don't need a separate test DB. It also means the tests will keep step with schema changes from the migrations. We do need to migrate up before running the tests though.

Copy link
Member

Choose a reason for hiding this comment

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

This means we can run the tests against a local DB with real data in it and not break our local data. We don't need a separate test DB. It also means the tests will keep step with schema changes from the migrations. We do need to migrate up before running the tests though

No objections to this in the local/inner dev loop (nor CI) context, and agree the benefits outweigh the tradeoffs. Would be nervous abut this in the context of the real DB though (whether connected to deliberately or accidentally)

Copy link
Member Author

Choose a reason for hiding this comment

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

In principle it should be safe but I agree - I wouldn't want to run the tests while connected to the prod DB. Tbh that would be fairly unusual thing to do for any application. I think the fact that the dev dependencies (e.g: mocha, etc) aren't installed in the prod container is enough of a guard rail to stop this from happening.

Personally I'd tend to avoid connecting my dev environment up to a prod database for any application.

package.json Show resolved Hide resolved
Comment on lines +45 to +50
if (config.private.redis_url != null) {
console.warning(
'RedisTokenPersistence is deprecated for token pooling and will be removed in a future release. Migrate to SqlTokenPersistence'
)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I decided that it is useful to keep both backends around for some length of time because it allows us to switch between them just by changing an env var - no code changes needed. Doing this means we can:

  • deploy this code with REDIS_URL set (so we're still using the redis backend)
  • migrate our data to postgres with scripts/migrate-token-pool.js
  • set POSTGRES_URL to switch to the postgres backend
  • if it all goes wrong, we can easily just unset POSTGRES_URL to go back to redis
  • unset REDIS_URL once we're happy everything is fine
  • delete the redis code at a later date

This both makes the migration super low-risk/easy to back out of and means we can provide a migration path for self-hosting users with basically no effort.

Copy link
Member

@calebcartwright calebcartwright Feb 19, 2023

Choose a reason for hiding this comment

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

Might be worth extending the warning message to include a link to #8921 (or some pertinent issue/discussion)

Copy link
Member Author

@chris48s chris48s Feb 19, 2023

Choose a reason for hiding this comment

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

Yes. I'll call this out at the top of the changelog for the next server release and write it with a bit more of a focus on self-hosting users than the discussion in #8921 (which is quite shields.io-specific). I'm going to set myself a reminder to ping me so I remember to do this when I do the changelog.

scripts/migrate-token-pool.js Show resolved Hide resolved
@chris48s chris48s marked this pull request as ready for review February 18, 2023 20:36
@chris48s chris48s changed the title WIP migrate token pooling to postgres migrate token pooling to postgres Feb 18, 2023
@@ -0,0 +1,14 @@
/* eslint-disable camelcase */

exports.shorthands = undefined
Copy link
Member

Choose a reason for hiding this comment

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

What's this?

Copy link
Member Author

Choose a reason for hiding this comment

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

@calebcartwright
Copy link
Member

LGTM, happy to 👍 once merge conflicts are sorted

@chris48s
Copy link
Member Author

chris48s commented Feb 19, 2023

@SnoozeThis wait until Feb 28 2023

TODO: update console.warn() with a link to changelog when you write changelog for next server release

@SnoozeThis
Copy link

(https://snoozeth.is/0ky67I-_i6g) I will wait until Tue, 28 Feb 2023 00:00:00 UTC and then add a comment.

@chris48s
Copy link
Member Author

Should be good to go, I think.

I can look at setting up a postgres instance on fly and getting the actual data migration done in the week.

@chris48s chris48s merged commit 091ccfd into badges:master Feb 23, 2023
@SnoozeThis
Copy link

Resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
operations Hosting, monitoring, and reliability for the production badge servers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants