Skip to content

Initial database setup is not atomic and can become wedged if interrupted #722

Closed

Description

@hfinucane

Bug Report

I have a Chef cookbook that deploys Concourse. This is fine, but one of the things it makes difficult is controlling the timing of delayed actions. As an overview, the actions taken by an actual installation look something like this:

  • Do a bunch of postgres init stuff- initdb, create the 'atc' database, write out conf files
  • Register delayed restarts because we changed config files
  • Install concourse, kickstart concourse-web
  • Chef run is done! Now do the delayed restarts of postgres and concourse

Everything Works(tm) in Vagrant, but in EC2, with 2.3.1, this fails pretty reliably. The atc database appears to be partly set up:

atc=> \dt
             List of relations
 Schema |       Name        | Type  | Owner
--------+-------------------+-------+-------
 public | migration_version | table | atc
(1 row)

atc=> select * from migration_version;
 version
---------
(0 rows)

The concourse logs look like

Oct 25 22:23:27 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434207.567716599","source":"atc","message":"atc.db.migrations.failed-to-acquire-lock-retrying","log_level":2,"data":{"error":"pq: the database system is shutting down","session":"2"}}
Oct 25 22:23:32 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434212.579482079","source":"atc","message":"atc.db.migrations.failed-to-acquire-lock-retrying","log_level":2,"data":{"error":"pq: the database system is shutting down","session":"2"}}
Oct 25 22:23:37 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434217.592168808","source":"atc","message":"atc.db.migrations.failed-to-acquire-lock-retrying","log_level":2,"data":{"error":"pq: the database system is shutting down","session":"2"}}
Oct 25 22:23:42 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434222.599715471","source":"atc","message":"atc.db.migrations.failed-to-acquire-lock-retrying","log_level":2,"data":{"error":"pq: the database system is shutting down","session":"2"}}
Oct 25 22:23:47 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434227.771785975","source":"atc","message":"atc.db.migrations.failed-to-acquire-lock-retrying","log_level":2,"data":{"error":"pq: the database system is starting up","session":"2"}}
Oct 25 22:23:52 ip-172-29-1-168 concourse[10032]: {"timestamp":"1477434232.796749353","source":"atc","message":"atc.db.migrations.migration-lock-acquired","log_level":1,"data":{"session":"2"}}
Oct 25 22:23:53 ip-172-29-1-168 systemd[1]: Stopping Concourse Web...
Oct 25 22:23:53 ip-172-29-1-168 systemd[1]: Started Concourse Web.
Oct 25 22:23:53 ip-172-29-1-168 systemd[1]: Starting Concourse Web...
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: {"timestamp":"1477434247.272916317","source":"atc","message":"atc.db.migrations.migration-lock-acquired","log_level":1,"data":{"session":"2"}}
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: {"timestamp":"1477434247.307472944","source":"atc","message":"atc.db.migrations.failed-to-run-migrations","log_level":3,"data":{"error":"Could not get DB version: sql: no rows in result set","session":"2","trace":"goroutine 1 [running]:\ncode.cloudfoundry.org/lager.(*logger).Fatal(0xc42004e2a0, 0xe35cad, 0x18, 0x
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: mp/build/9674af12/gopath/src/github.com/concourse/bin/vendor/github.com/jessevdk/go-flags/parser.go:185 +0x74\nmain.main()\n\t/tmp/build/9674af12/gopath/src/github.com/concourse/bin/cmd/concourse/main.go:29 +0x113\n"}}
Oct 25 22:24:07 ip-172-29-1-168 systemd[1]: concourse-web.service: main process exited, code=exited, status=2/INVALIDARGUMENT
Oct 25 22:24:07 ip-172-29-1-168 systemd[1]: Unit concourse-web.service entered failed state.
Oct 25 22:24:07 ip-172-29-1-168 systemd[1]: concourse-web.service failed.
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: panic: Could not get DB version: sql: no rows in result set
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: goroutine 1 [running]:
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: panic(0xce9560, 0xc420148d60)
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: /usr/local/go/src/runtime/panic.go:500 +0x1a1
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: code.cloudfoundry.org/lager.(*logger).Fatal(0xc42004e2a0, 0xe35cad, 0x18, 0xef9cc60, 0xc420148d60, 0x0, 0x0, 0x0)
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: /tmp/build/9674af12/concourse/src/code.cloudfoundry.org/lager/logger.go:152 +0x41c
Oct 25 22:24:07 ip-172-29-1-168 concourse[10431]: github.com/concourse/atc/db/migrations.LockDBAndMigrate(0xefaf220, 0xc42004e2a0, 0xe2fd36, 0x13, 0x7ffcf9e1ff69, 0x1c, 0xefaf220, 0xc42004e2a0, 0xc4201c6ce8, 0x599f23)

There is a certain degree of "doctor, doctor, it hurts when I do this" here, but I don't really want to have to litigate a move away from Chef, and it also seems like the "set up the database" step shouldn't fail like this.

Activity

  1. concourse-bot commented on Oct 25, 2016

    @concourse-bot

    Hi there!

    We use Pivotal Tracker to provide visibility into what our team is working on. A story for this issue has been automatically created.

    The current status is as follows:

    • #133148779 Initial database setup is not atomic and can become wedged if interrupted

    This comment, as well as the labels on the issue, will be automatically updated as the status in Tracker changes.

  2. vito commented on Oct 26, 2016

    @vito
    Contributor

    Sounds like it maybe died midway through setting up the migrations table? That's a little odd as it happens in one transaction: https://github.com/BurntSushi/migration/blob/master/migration.go#L232-L235

    I'd be surprised if any of this was Chef related, but it sounds like your database just straight up got corrupt. Don't know if there's much we can do here.

  3. ahelal commented on Oct 26, 2016

    @ahelal
    Contributor

    @hfinucane I have an ansible role, but don't experience anything smiler. Not sure if that helps https://github.com/ahelal/ansible-concourse, But i had smiler issues with Chef before

  4. hfinucane commented on Oct 26, 2016

    @hfinucane
    Author

    I mean, it's Chef related only in the sense that it's super hard to get Chef to not do this while also preserving the ability to have it "do the right thing" if it ever changes any config files later. I can replicate this failure pretty reliably, if that makes you feel more like digging in to it. Happy to produce pg configs, os configs, etc.

  5. hfinucane commented on Oct 26, 2016

    @hfinucane
    Author

    Alright, environment summary- AWS, CentOS 7, EBS, no LVM, XFS root partition mounted with 'defaults', which mount says resolves to (rw,relatime,seclabel,attr2,inode64,noquota). Using Postgres 9.5 from Software Collections, postgresql.conf is pretty risible, and doesn't do much of anything.

    The postgres logs agree with everything else:

    LOG:  database system was shut down at 2016-10-26 20:54:25 UTC
    LOG:  MultiXact member wraparound protections are now enabled
    LOG:  database system is ready to accept connections
    LOG:  autovacuum launcher started
    ERROR:  relation "migration_version" does not exist at character 21
    STATEMENT:  SELECT version FROM migration_version
    LOG:  could not receive data from client: Connection reset by peer
    LOG:  could not receive data from client: Connection reset by peer
    ERROR:  relation "migration_version" already exists
    STATEMENT:
                            CREATE TABLE migration_version (
                                    version INTEGER
                            )
    

    Although 'character 21' seems a little odd.

    I'm willing to close this if this 'has to be' a pg-side data corruption issue, and you're sure it's transactional on the Concourse side :/.

  6. hfinucane commented on Oct 26, 2016

    @hfinucane
    Author

    OK, with pg_log_statement=all:

    LOG:  database system was shut down at 2016-10-26 22:57:47 UTC
    LOG:  MultiXact member wraparound protections are now enabled
    LOG:  database system is ready to accept connections
    LOG:  autovacuum launcher started
    LOG:  statement: SELECT d.datname as "Name",
               pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
               pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
               d.datcollate as "Collate",
               d.datctype as "Ctype",
               pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
        FROM pg_catalog.pg_database d
        ORDER BY 1;
    LOG:  statement: CREATE DATABASE atc ENCODING 'UTF-8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
    LOG:  statement: SELECT r.rolname, r.rolsuper, r.rolinherit,
          r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
          r.rolconnlimit, r.rolvaliduntil,
          ARRAY(SELECT b.rolname
                FROM pg_catalog.pg_auth_members m
                JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
                WHERE m.member = r.oid) as memberof
        , r.rolreplication
        , r.rolbypassrls
        FROM pg_catalog.pg_roles r
        ORDER BY 1;
    LOG:  statement: CREATE USER atc WITH PASSWORD '<redacted>';
    LOG:  statement: GRANT ALL PRIVILEGES ON DATABASE atc to atc;
    LOG:  received fast shutdown request
    LOG:  aborting any active transactions
    LOG:  autovacuum launcher shutting down
    LOG:  shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    FATAL:  the database system is shutting down
    LOG:  database system is shut down
    LOG:  database system was shut down at 2016-10-26 22:59:56 UTC
    LOG:  MultiXact member wraparound protections are now enabled
    LOG:  database system is ready to accept connections
    LOG:  autovacuum launcher started
    LOG:  execute 1: select pg_advisory_lock($1)
    DETAIL:  parameters: $1 = '2367292471'
    ERROR:  relation "migration_version" does not exist at character 21
    STATEMENT:  SELECT version FROM migration_version
    LOG:  execute 2:
                CREATE TABLE migration_version (
                    version INTEGER
                )
    
    LOG:  could not receive data from client: Connection reset by peer
    LOG:  could not receive data from client: Connection reset by peer
    LOG:  execute 1: select pg_advisory_lock($1)
    DETAIL:  parameters: $1 = '2367292471'
    LOG:  execute 1: SELECT version FROM migration_version
    LOG:  execute 2:
                CREATE TABLE migration_version (
                    version INTEGER
                )
    
    ERROR:  relation "migration_version" already exists
    STATEMENT:
                CREATE TABLE migration_version (
                    version INTEGER
                )
    
    LOG:  execute 3: SELECT version FROM migration_version
    LOG:  could not receive data from client: Connection reset by peer
    LOG:  could not receive data from client: Connection reset by peer
    

    So the problem isn't bouncing postgres, or that I've somehow managed to turn on libeatmydata, but the problem is (probably?) bouncing concourse while it's getting set up.

  7. hfinucane commented on Oct 26, 2016

    @hfinucane
    Author

    Are you sure this is wrapped in a transaction? If I type 'BEGIN;' at the prompt I can get LOG: statement: BEGIN; in the log.

  8. hfinucane commented on Oct 27, 2016

    @hfinucane
    Author

    Yeah, so migrate() in BurntSushi/migration looks like this:

    func (m migration) migrate() error {
        libVersion := len(m.migrations)
        dbVersion, err := m.getVersion(m) // should be calling safeGetVersion in concourse/atc. Still, there's no transaction yet.
        if err != nil {
            return ef("Could not get DB version: %s", err)
        }
        if dbVersion > libVersion {
            return ef("Database version (%d) is greater than library version (%d).",
                dbVersion, libVersion)
        }
        if dbVersion == libVersion {
            return nil
        }
    
        tx, err := m.Begin() // Create a transaction, and then do all the fun real migrations
    

    This is more obvious with a super-verbose log from a successful build:

    LOG:  statement: GRANT ALL PRIVILEGES ON DATABASE atc to atc;
    LOG:  duration: 0.757 ms
    LOG:  disconnection: session time: 0:00:00.002 user=postgres database=postgres host=[local]
    LOG:  connection received: host=::1 port=40938
    LOG:  connection authorized: user=atc database=atc SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES128-GCM-SHA256, compression=off)
    LOG:  duration: 28.806 ms
    LOG:  duration: 0.185 ms
    LOG:  execute 1: select pg_advisory_lock($1)
    DETAIL:  parameters: $1 = '2367292471'
    LOG:  duration: 12.195 ms
    LOG:  connection received: host=::1 port=40939
    LOG:  received fast shutdown request
    LOG:  aborting any active transactions
    FATAL:  terminating connection due to administrator command
    LOG:  disconnection: session time: 0:00:00.796 user=atc database=atc host=::1 port=40938
    LOG:  autovacuum launcher shutting down
    LOG:  shutting down
    LOG:  connection received: host=::1 port=40940
    FATAL:  the database system is shutting down
    LOG:  connection received: host=::1 port=40941
    FATAL:  the database system is shutting down
    LOG:  database system is shut down
    LOG:  database system was shut down at 2016-10-27 00:10:51 UTC
    LOG:  MultiXact member wraparound protections are now enabled
    LOG:  database system is ready to accept connections
    LOG:  autovacuum launcher started
    LOG:  connection received: host=[local]
    LOG:  connection authorized: user=postgres database=postgres
    LOG:  disconnection: session time: 0:00:00.099 user=postgres database=postgres host=[local]
    LOG:  connection received: host=::1 port=40942
    LOG:  connection authorized: user=atc database=atc SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES128-GCM-SHA256, compression=off)
    LOG:  duration: 89.001 ms
    LOG:  duration: 0.390 ms
    LOG:  execute 1: select pg_advisory_lock($1)
    DETAIL:  parameters: $1 = '2367292471'
    LOG:  duration: 0.036 ms
    LOG:  connection received: host=::1 port=40943
    LOG:  connection authorized: user=atc database=atc SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES128-GCM-SHA256, compression=off)
    ERROR:  relation "migration_version" does not exist at character 21
    STATEMENT:  SELECT version FROM migration_version
    LOG:  duration: 200.154 ms
    LOG:  duration: 0.829 ms
    LOG:  execute 2:
                            CREATE TABLE migration_version (
                                    version INTEGER
                            )
    
    LOG:  duration: 810.983 ms
    LOG:  duration: 1162.397 ms
    LOG:  duration: 145.834 ms
    LOG:  execute 3:
                            INSERT INTO migration_version (version) VALUES (0)
    
    LOG:  duration: 0.159 ms
    LOG:  duration: 0.180 ms
    LOG:  duration: 71.687 ms
    LOG:  execute 4: SELECT version FROM migration_version
    LOG:  duration: 0.030 ms
    LOG:  statement: BEGIN
    LOG:  duration: 0.046 ms
    LOG:  duration: 0.116 ms
    LOG:  duration: 0.009 ms
    LOG:  execute 5:
                            CREATE TYPE build_status AS ENUM (
                                    'pending',
                                    'started',
                                    'aborted',
                                    'succeeded',
    
  9. vito commented on Dec 30, 2016

    @vito
    Contributor

    oh gosh. yeah, despite the code saying func createVersionTable(tx LimitedTx) error { it looks like that tx isn't actually a transaction. maybe that's what they mean by Limited. :P bummer.

    i'll mark this as a bug and maybe we'll submit a PR upstream (though it'd happen quicker if someone else did it cough). thanks for the sleuthing!

  10. changed the title [-]Problems with database setup in 2.3.1[/-] [+]Initial database setup is not atomic and can become wedged if interrupted[/+] on Dec 30, 2016
  11. concourse-bot commented on Feb 8, 2017

    @concourse-bot

    Hello again!

    All stories related to this issue have been accepted, so I'm going to automatically close this issue.

    At the time of writing, the following stories have been accepted:

    • #133148779 Initial database setup is not atomic and can become wedged if interrupted

    If you feel there is still more to be done, or if you have any questions, leave a comment and we'll reopen if necessary!

  12. hfinucane commented on Feb 15, 2017

    @hfinucane
    Author

    Thanks everyone, I'm excited to try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions