Skip to content

Initial database migration is not atomic in 3.9.1 and can become wedged if interrupted #2074

Closed

Description

@hfinucane

Bug Report

  • Concourse version: 3.9.1
  • Deployment type (BOSH/Docker/binary): binary

This appears to be a regression of #722. As a refresher, my Chef deployment of Concourse is also a torture test of initial migration safety, because it runs 'concourse web', and then pretty rapidly restarts that process. I'm seeing this in my 'integration tests' on fresh systems with a fresh db. On the Concourse side, I get

failed to migrate database: Dirty database version 1510262030. Fix and force version.

With verbose postgres logs, the problem seems like a straightforward repeat of #722- there are two separate transactions, one to update the schema_migrations table, one to apply the schema migrations, and if the second transaction is interrupted, the database is in an indeterminate state.

LOG:  execute <unnamed>: SELECT pg_try_advisory_lock($1)
DETAIL:  parameters: $1 = '6'
LOG:  statement: SELECT version FROM migration_version
ERROR:  relation "migration_version" does not exist at character 21
STATEMENT:  SELECT version FROM migration_version
LOG:  statement: SELECT CURRENT_DATABASE()
LOG:  execute <unnamed>: SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1
DETAIL:  parameters: $1 = 'schema_migrations'
LOG:  statement: CREATE TABLE "schema_migrations" (version bigint not null primary key, dirty boolean not null)
LOG:  execute <unnamed>: SELECT pg_try_advisory_lock($1)
DETAIL:  parameters: $1 = '2019668375'
LOG:  statement: SELECT version, dirty FROM "schema_migrations" LIMIT 1
LOG:  statement: BEGIN
LOG:  statement: TRUNCATE "schema_migrations"
LOG:  execute <unnamed>: INSERT INTO "schema_migrations" (version, dirty) VALUES ($1, $2)
DETAIL:  parameters: $1 = '1510262030', $2 = 't'
LOG:  statement: COMMIT
LOG:  statement: BEGIN;

    CREATE TYPE build_status AS ENUM (
          'pending',
        'started',
        'aborted',

... (omit the concourse schema)

          ALTER TABLE ONLY workers
              ADD CONSTRAINT workers_team_id_fkey FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE;


        COMMIT;

LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer
LOG:  execute <unnamed>: SELECT pg_try_advisory_lock($1)
DETAIL:  parameters: $1 = '6'
LOG:  statement: SELECT version FROM migration_version
ERROR:  relation "migration_version" does not exist at character 21
STATEMENT:  SELECT version FROM migration_version
LOG:  statement: SELECT CURRENT_DATABASE()
LOG:  execute <unnamed>: SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1
DETAIL:  parameters: $1 = 'schema_migrations'
LOG:  execute <unnamed>: SELECT pg_try_advisory_lock($1)
DETAIL:  parameters: $1 = '2019668375'
LOG:  statement: SELECT version, dirty FROM "schema_migrations" LIMIT 1
LOG:  execute <unnamed>: SELECT pg_advisory_unlock($1)
DETAIL:  parameters: $1 = '2019668375'
LOG:  execute <unnamed>: SELECT pg_advisory_unlock($1)
DETAIL:  parameters: $1 = '6'
LOG:  could not receive data from client: Connection reset by peer

Activity

  1. topherbullock commented on Mar 22, 2018

    @topherbullock
    Contributor

    @hfinucane This issue is related to the migration library we're using, and the inability to safely recover from a state where the migrations it is trying to run for us never succeeded. As you observed, there's 3 steps it uses to run each migration. Here's the flow it follows for running migrations:

      • Set the schema_migrations version to <desired-version> and dirty to true
      • Run the contents of the up migration for <desired-version>
      • Set the schema_migrations version to <desired-version> and dirty to false
      • Repeat steps 1-3 for each up migration to run
    1. ???
    2. Profit

    Our initial thought was "Let's just change it to run 1-3 in a transaction", but there are reasons for not wanting every migration to run in a transaction. Since we don't really know whether step 2 (running the migration) or 3 (marking the migration table as clean) failed, the ATC isn't really able to determine what to do about the state being dirty.

    One thing which may help you is the ability to upgrade the db to a desired version once, and exit. concourse web supports a --migrate-db-to-version flag which does just that. You could potentially use that in conjunction with --supported-db-version like so :
    concourse web --migrate-db-to-version $(concourse web --supported-db-version)

    If you run this once before the aggressive stopping and starting of the concourse web service (maybe in the db setup after the initial db is created?), then it will be more resilient to restarts going forward.

  2. nbrys commented on May 7, 2018

    @nbrys

    We are currently having the same issue after trying an upgrade from 3.9.2 to 3.13.0. We get the following errors in the log: failed to migrate database: Dirty database version 1522178770. Fix and force version.

    Any help to get our concourse installation working again is appreciated.

  3. ywei2017 commented on May 15, 2018

    @ywei2017

    Any updates on the 3.9.2 to 3.13.0 upgrade? I just got into the exact same error, and am totally at a loss.

  4. DRuggeri commented on May 15, 2018

    @DRuggeri

    Same. It sounds like @vito is suggesting that this needs to be fixed in the schema somehow? The error Fix and force version doesn't quite tell me, as an administrator, what needs to be done.

  5. DRuggeri commented on May 15, 2018

    @DRuggeri

    FWIW, I was able to work past the issue by...

    • Adding the postgres release to the ATC box
    • Use psql and concourse credentials to log into the DB
    • Manually run the commands in migrations
    • Run UPDATE schema_migrations SET dirty=false

    YMMV, but that's how I recovered from this half state.

  6. ddadlani commented on Aug 20, 2018

    @ddadlani
    Contributor

    We have moved to a new migration library (#2452) which should provide more information when migrations fail, so we are closing this issue.

    If anyone runs into issues with migrations in the future (from Concourse v4.0.1 onward), please look at the migrations_history table for more information on what failed. Please also feel free to open a new issue to discuss the migration failure. Thanks!

  7. vito commented on Aug 21, 2018

    @vito
    Contributor

    Accepting this! I opened a separate issue for extracting it to a separate library (repo) and some odds and ends: #2516

  8. modified the milestone: v4.1.0 on Aug 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions