-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
feat(cli) disable automatic migrations #2421
Conversation
74306b5
to
0d25e12
Compare
0d25e12
to
dff20ea
Compare
Thoughts on inverting this logic, and providing a flag to not run migrations automatically? That makes the change less drastic, and doesn't require simpler/new users to take an extra step to keep up to date, while still allowing user with complex deployment/data store environments an avenue to avoid problems. |
I disagree - once there is a migration because of a major version update, the upgrade process needs to be explicit and not manual. Then once it's migrated, you will never have to worry about this again because Kong will start normally. We want that extra step. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a consistently failing test, not sure how its related, perhaps it just needs adjustment.
5560a1c
to
093eb1f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change LGTM -- thanks for adding this safety check 👍
093eb1f
to
859ad7b
Compare
Tests now passing. |
kong/cmd/start.lua
Outdated
if args.run_migrations then | ||
assert(dao:run_migrations()) | ||
end | ||
assert(dao:are_migrations_uptodate()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The are_migrations_uptodate
will log a message with warn
level. Still it seems like here it is a fatal. I would probably move that log statement out of that are_migrations_uptodate
and do the logging where it has a context (e.g. here).
If we want this to prevent Kong from starting, it should be logged with possibly > ngx.ERR
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also do we want this to be a fatal thing? Or should we just start the Kong and wait for the results of what happened to fan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is indeed fatal, since the assert
will fail and return the error. The error is that migrations are not up to date, the warn
just tells which migration is missing (it's a more verbose information). I could log it as verbose
or error
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for explanation. I don't think anything needs to be changed for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of throwing the error, I think it would be nice if it would retry for a specified timeout (default 60 seconds?). That would allow orchestration tools to run a single kong migrations up
and then schedule Kong containers/nodes using kong start
. The containers will then wait before actually starting until migrations are complete, instead of failing.
whups, didnt mean to approve this, just cancel my requested changes since it was fixed
Something it'd be great to clarify, especially for new users, is when migrations are supposed to be executed. As far as I understand, Kong starts with an empty schema, creates it and then runs migrations. That sounds a little counterintuitive to me because when you think about "migrating" something, that is typically related to updating to the next major version, not installing. I agree with @thefosk that running migrations must be explicit when upgrading but I don't think that should be the case when installing Kong from scratch. I think those are two different test cases that should be treated differently from a user perspective. |
I will share our case to see if it matches anyone else's case. The way we deploy stuff is by creating recipes of what has to be deployed. As a result, deciding whether migrations must be run or not based on a parameter doesn't really work because we need to create one recipe for running migrations, wait until migrations are finished, then create the permanent recipe (without the migrations). |
kong/dao/factory.lua
Outdated
local log = require "kong.cmd.utils.log" | ||
log.warn(string.format("database is missing migration: (%s) %s", | ||
module, migration.name)) | ||
return false, "migrations are not up to date" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the error message should probably contain a better user instruction telling them to start kong with the --run-migrations
option (on one node only)
CHANGELOG.md
Outdated
`kong restart`. Now they need to be **explicitly** executed **before** Kong is | ||
started with `kong migrations up`. You can still use the old behavior by | ||
adding the optional `--run-migrations` argument to `kong start` or | ||
`kong restart`, which will automatically runs the migrations before starting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runs
==> run
kong/cmd/start.lua
Outdated
if args.run_migrations then | ||
assert(dao:run_migrations()) | ||
end | ||
assert(dao:are_migrations_uptodate()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of throwing the error, I think it would be nice if it would retry for a specified timeout (default 60 seconds?). That would allow orchestration tools to run a single kong migrations up
and then schedule Kong containers/nodes using kong start
. The containers will then wait before actually starting until migrations are complete, instead of failing.
@mgiorgio I understand your point, but either we execute migrations by default, or we disable them by default, but we shouldn't create edge-cases so that "they are always disabled, but.." The first time you start Kong, you would run it with
or, another way would be to fix the semantics of the command and call the flag |
@thefosk, thanks for your answer. While it's arguable whether mine is an edge-case or not, I see the value on implementing this solution. The concern I still have is that relying on manual intervention for installing and upgrading Kong, will prevent developers from fully automating their deployments. |
I have updated the PR:
|
Regarding this item:
IMHO it will make a first installation of Kong harder on an empty schema. I agree that multiple nodes could still create conflicts, but since it's a first run those conflicts will basically have no side effect and no data will be involved. At the same time it creates an "exception" in how we are handling migrations, so there's that. Thoughts? |
16f6e24
to
ae2af81
Compare
@marco No more thoughts, we had a lengthy debate, and came to this consensus: remove the magic and add a very explicit/instructive error message. (even the error message got a lengthy debate) I fixed the first 2 items on the list, I'll add the schema consistency check tomorrow, then it will be up for review and merge. |
034bf3e
to
1ba9177
Compare
c1a1b43
to
64bccf1
Compare
ce9b10b
to
29a6de4
Compare
29a6de4
to
b3e05b3
Compare
Co-authors who participated to this decision: - Marco Palladino <[email protected]> - Thijs Schreijer <[email protected]> Migrations now must be run via `kong migrations up`. This is done to start spreading good practises regarding migrations, especially for Cassandra clusters, and considering our current migrations can be harmful is run incorrectly. Context ------- When using Cassandra, migrations **must** be run from a single node, whether the keyspace is fresh (new install) or not (upgrade). This is because of the eventual nature of Cassandra itself, and the lack of "lock" or "mutex" mechanism without the use of consistency=ALL flag, which we want to avoid for now. For this reason and because we want to enforce a manual process for major upgrades, we wish to disable automatic migrations until further rewrite of said migrations. Current implementation ---------------------- When Kong starts, we don't run the migrations automatically. - If the migrations are not up-to-date with the version of Kong being started: we error with a helpful message instructing the user on how to run the migrations manually, *from a single node* - When using Cassandra, if the schema consensus is not yet reached, we error out with a helpful error message as well, instructing the user to wait or investigate the cluster (schema consensus should have been waited for during the migrations already, so something could have went wrong if consensus is still not reached) - A `--run-migrations` flag was left, to allow for running migrations from `kong start`. While this is **the pattern we want to get away from**, it is left over as a "backwards-compatible" behavior, but should be considered deprecated. Long term --------- In the future, postgres and Cassandra instances should behave differently: - PostgreSQL: we can run the _initial_ migrations automatically, and via many nodes at the same time, especially if we use some mutex mechanism - Cassandra: we should enforce _initial_ manual migrations, from a *single node*, and eventually consider a mutex mechanism via a consistency=ALL flag. In both cases, we wish _upgrade_ migrations to be safer and manual, to provide no-loss-of-data and no-downtime upgrade paths. This process requires manual steps, or semi-manual. Fix: #1692 From: #2421
b3e05b3
to
0771335
Compare
There are two steps to improve the current migrations:
This PR is only trying to fix the (2) second point, by disabling automatic database migrations on
kong start
orkong restart
, unless it's the first time Kong runs and in that case Kong will execute the migrations to initialize the empty database/keyspace.Migrations now need to be explicitly executed with
kong migrations up
. The old behavior can be still replicated by adding--run-migrations
onkong (re)start
.If
--run-migrations
is missing, this PR also checks that Kong can effectively run with the existing migrations or not.Full changelog
kong start
orkong restart
.--run-migrations
flag onkong start
andkong restart
.Issues resolved
Fix #1692.
TODO
Update website and documentation.