Skip to content

Migrate build_id references to bigint - #6203

Merged
aoldershaw merged 3 commits into
concourse:masterfrom
vito:build-fkey-bigints
Oct 26, 2020
Merged

aoldershaw merged 3 commits into
concourse:masterfrom
vito:build-fkey-bigints

Conversation

@vito

@vito vito commented Oct 26, 2020 •

Copy link
Copy Markdown
Contributor

What does this PR accomplish?

fixes #6199

follow-up to #6022

This introduces a handful of migrations which convert build_id foreign key references to bigint in-place. For build_events that approach would grind everything to a halt because it can have millions of rows, so this PR adds a batch migration component which will migrate build events in batches of 100,000 (by default) every 10 seconds (by default). Flags are also added for both settings just in case operators feel that it's too aggressive or too slow depending on their situation.

The migration first renames build_events.build_id to build_events.build_id_old, and then adds a "new" build_id bigint column and updates the indexes. This rename dance ensures other web nodes which may be running mid-upgrade will write their events as bigints, so that the batch migrator doesn't have to play "catch-up." Build events are migrated from newest to oldest. Older builds will just not have any logs until the migrator gets to them. When all events are migrated, a final schema cleanup is performed and the primary key is updated.

Release Note

Contributor Checklist

Reviewer Checklist

  • Code reviewed
  • Tests reviewed
  • Documentation reviewed
  • Release notes reviewed
  • PR acceptance performed
  • New config flags added? Ensure that they are added to the
    BOSH and
    Helm packaging; otherwise, ignored for
    the integration
    tests

    (for example, if they are Garden configs that are not displayed in the
    --help text).

some of these tables can be quite large, but none are near the magnitude
of build_events, which we'll have to do in batches - this will be
handled in subsequent commits.

Signed-off-by: Alex Suraci <[email protected]>
@vito vito added the misc label Oct 26, 2020
@vito vito added this to the v6.7.0 milestone Oct 26, 2020
* runs as a component, migrating one batch every interval (default 10
  seconds). when all events are migrated, it cleans up the schema.
* default batch size is 100,000 build events. in totally empirical
  testing on my local machine that seemed to be a good balance.

this should be safe with concurrent build event writes even by older
ATCs, because the migration starts by renaming the current column to
build_id_old and creates a new build_id bigint column. all new writes
will be automatically saved as bigint, and the batch migrator will
slowly convert batches of rows at a time, starting from the newest and
working backwards.

this adds a test suite which runs against a local postgres server
instead of running one itself - the postgresrunner is a bit too
automagical to be used for tests at the migration level. the suite only
runs if you set $BATCH_POSTGRES_HOST. there's also a benchmark test
which only runs with $SLOW configured.

i don't plan to run these tests in CI - they're a bit slow and won't be
covering a hot code path.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the build-fkey-bigints branch from 06d22c8 to 97f0f2c Compare October 26, 2020 03:04
also: during down migration, update newly written bigint-only events.
just do this in-place, assuming it won't be *too* many rows.

it's not really safe to run this because it makes it impossible to run
the 'down' migration. let's just leave this extra data around for now
and address it in a future release. it kind of seems like this could be
turned into a feature integrated into the migration library so it's less
awkward to handle, but that's a voyage for another day. (we should
probably also integrate voyager btw.)

Signed-off-by: Alex Suraci <[email protected]>

@aoldershaw aoldershaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Getting the following error when trying to migrate up from some other dev version:

web_1     | error: failed to migrate database: 1 error occurred:
web_1     | 	* Migration '1603401316_alter_build_integers_to_bigints.up.sql' failed: 2 errors occurred:
web_1     | 	* Transaction ALTER TABLE jobs
web_1     |       ALTER COLUMN next_build_id TYPE bigint,
web_1     |       ALTER COLUMN latest_completed_build_id TYPE bigint,
web_1     |       ALTER COLUMN transition_build_id TYPE bigint failed, rolled back the migration
web_1     | 	* pq: cannot alter type of a column used in a trigger definition

Not sure if my DB got in a weird state for some reason as a result of jumping around between dev versions. Not sure what column/trigger is causing issues, will try to investigate

@vito

vito commented Oct 26, 2020

Copy link
Copy Markdown
Contributor Author

@aoldershaw hmm maybe it's the dashboard event polling stuff?

@aoldershaw aoldershaw left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The batch migration seems to work well - I happened to have ~200,000 build events just lying around, so was able to see it work in batches, and it appeared to handle the 100,000 row updates without a sweat. I can see this taking a fairly long time on some massive deployments with the default batch settings, but that's not the end of the world, especially considering it starts with the most recent builds. I migrated down and back up several times, and that also worked well.

As a side note - it does seem concerning that I have 200,000 build events, given that I have only 9 resources and haven't been running any "builds" (they're pretty much all check builds). To be fair, my local deployment has been up for a while, checks were erroring due to a misconfigured var, and #6192 wasn't merged yet.

I see you have a comment about this in the linked issue:

Which led me to discovering that build events don't have a foreign key reference for the build_id column. 🤔 This probably means that resource checking builds are leaking their build events forever. This is probably so we can just truncate/drop the whole table partition when a pipeline is deleted - but maybe we could just truncate it first so the cascade doesn't fire? Maybe we need a new garbage collector to remove build events for deleted builds? ...might be challenging to even find which ones to remove efficiently though. Hmm....

Is that something we should look into before 6.7? Rather than adding a GC component, could we just delete the events when we delete the previous resource check build? I recognize this won't work for builds that are deleted elsewhere, but at least the build events won't pile up as resource checking occurs

EDIT: Opened #6211

@aoldershaw
aoldershaw merged commit 40bd776 into concourse:master Oct 26, 2020
@vito

vito commented Oct 26, 2020

Copy link
Copy Markdown
Contributor Author

@aoldershaw Woops I forgot to post the results of the benchmark tests - my finding was that 100k was a good balance of update throughput and not taking too long for each batch. On my machine 100k row batches took ~1 second. Smaller batch sizes were faster but it took longer to migrate everything. I don't think you want to set it too high since it might affect performance of writing new events. Haven't tested it though.

@chenbh chenbh added release/documented Documentation and release notes have been updated. release/undocumented This didn't warrant being documented or put in release notes. and removed release/documented Documentation and release notes have been updated. labels Oct 26, 2020
@vito
vito deleted the build-fkey-bigints branch October 29, 2020 04:21
@chenbh chenbh modified the milestones: v6.7.0, v6.8.0 Oct 29, 2020
@taylorsilva taylorsilva modified the milestones: v6.7.2, v7.0.0 Nov 9, 2020
@aoldershaw aoldershaw changed the title Migrate build_id references to bigint, batch event migration Migrate build_id references to bigint Jan 25, 2021
@xtremerui xtremerui mentioned this pull request Nov 16, 2022
1 of 2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

misc release/undocumented This didn't warrant being documented or put in release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate remaining foreign keys pointing to builds (id) from integer to bigint

6 participants