diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-07-10 17:43:57 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-08-10 17:45:44 +0200 |
commit | 0395c47193b3bbf6b4f060f28c9f632580313a35 (patch) | |
tree | d7cf216392120385186c36d18bd38443ae5b1642 /db | |
parent | afd6e517e59aa07ab377fc29da45be08918a4f8e (diff) | |
download | gitlab-ce-0395c47193b3bbf6b4f060f28c9f632580313a35.tar.gz |
Migrate events into a new format
This commit migrates events data in such a way that push events are
stored much more efficiently. This is done by creating a shadow table
called "events_for_migration", and a table called "push_event_payloads"
which is used for storing push data of push events. The background
migration in this commit will copy events from the "events" table into
the "events_for_migration" table, push events in will also have a row
created in "push_event_payloads".
This approach allows us to reclaim space in the next release by simply
swapping the "events" and "events_for_migration" tables, then dropping
the old events (now "events_for_migration") table.
The new table structure is also optimised for storage space, and does
not include the unused "title" column nor the "data" column (since this
data is moved to "push_event_payloads").
== Newly Created Events
Newly created events are inserted into both "events" and
"events_for_migration", both using the exact same primary key value. The
table "push_event_payloads" in turn has a foreign key to the _shadow_
table. This removes the need for recreating and validating the foreign
key after swapping the tables. Since the shadow table also has a foreign
key to "projects.id" we also don't have to worry about orphaned rows.
This approach however does require some additional storage as we're
duplicating a portion of the events data for at least 1 release. The
exact amount is hard to estimate, but for GitLab.com this is expected to
be between 10 and 20 GB at most. The background migration in this commit
deliberately does _not_ update the "events" table as doing so would put
a lot of pressure on PostgreSQL's auto vacuuming system.
== Supporting Both Old And New Events
Application code has also been adjusted to support push events using
both the old and new data formats. This is done by creating a PushEvent
class which extends the regular Event class. Using Rails' Single Table
Inheritance system we can ensure the right class is used for the right
data, which in this case is based on the value of `events.action`. To
support displaying old and new data at the same time the PushEvent class
re-defines a few methods of the Event class, falling back to their
original implementations for push events in the old format.
Once all existing events have been migrated the various push event
related methods can be removed from the Event model, and the calls to
`super` can be removed from the methods in the PushEvent model.
The UI and event atom feed have also been slightly changed to better
handle this new setup, fortunately only a few changes were necessary to
make this work.
== API Changes
The API only displays push data of events in the new format. Supporting
both formats in the API is a bit more difficult compared to the UI.
Since the old push data was not really well documented (apart from one
example that used an incorrect "action" nmae) I decided that supporting
both was not worth the effort, especially since events will be migrated
in a few days _and_ new events are created in the correct format.
Diffstat (limited to 'db')
4 files changed, 168 insertions, 0 deletions
diff --git a/db/migrate/20170608152747_prepare_events_table_for_push_events_migration.rb b/db/migrate/20170608152747_prepare_events_table_for_push_events_migration.rb new file mode 100644 index 00000000000..f4f03bbabaf --- /dev/null +++ b/db/migrate/20170608152747_prepare_events_table_for_push_events_migration.rb @@ -0,0 +1,51 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class PrepareEventsTableForPushEventsMigration < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + disable_ddl_transaction! + + def up + # The order of these columns is deliberate and results in the following + # columns and sizes: + # + # * id (4 bytes) + # * project_id (4 bytes) + # * author_id (4 bytes) + # * target_id (4 bytes) + # * created_at (8 bytes) + # * updated_at (8 bytes) + # * action (2 bytes) + # * target_type (variable) + # + # Unfortunately we can't make the "id" column a bigint/bigserial as Rails 4 + # does not support this properly. + create_table :events_for_migration do |t| + t.references :project, + index: true, + foreign_key: { on_delete: :cascade } + + t.integer :author_id, index: true, null: false + t.integer :target_id + + t.timestamps_with_timezone null: false + + t.integer :action, null: false, limit: 2, index: true + t.string :target_type + + t.index %i[target_type target_id] + end + + # t.references doesn't like it when the column name doesn't make the table + # name so we have to add the foreign key separately. + add_concurrent_foreign_key(:events_for_migration, :users, column: :author_id) + end + + def down + drop_table :events_for_migration + end +end diff --git a/db/migrate/20170608152748_create_push_event_payloads_tables.rb b/db/migrate/20170608152748_create_push_event_payloads_tables.rb new file mode 100644 index 00000000000..6c55ad1f2f7 --- /dev/null +++ b/db/migrate/20170608152748_create_push_event_payloads_tables.rb @@ -0,0 +1,46 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class CreatePushEventPayloadsTables < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + disable_ddl_transaction! + + def up + create_table :push_event_payloads, id: false do |t| + t.bigint :commit_count, null: false + + t.integer :event_id, null: false + t.integer :action, null: false, limit: 2 + t.integer :ref_type, null: false, limit: 2 + + t.binary :commit_from + t.binary :commit_to + + t.text :ref + t.string :commit_title, limit: 70 + + t.index :event_id, unique: true + end + + # We're adding a foreign key to the _shadow_ table, and this is deliberate. + # By using the shadow table we don't have to recreate/revalidate this + # foreign key after swapping the "events_for_migration" and "events" tables. + # + # The "events_for_migration" table has a foreign key to "projects.id" + # ensuring that project removals also remove events from the shadow table + # (and thus also from this table). + add_concurrent_foreign_key( + :push_event_payloads, + :events_for_migration, + column: :event_id + ) + end + + def down + drop_table :push_event_payloads + end +end diff --git a/db/post_migrate/20170627101016_schedule_event_migrations.rb b/db/post_migrate/20170627101016_schedule_event_migrations.rb new file mode 100644 index 00000000000..1f34375ff0d --- /dev/null +++ b/db/post_migrate/20170627101016_schedule_event_migrations.rb @@ -0,0 +1,40 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class ScheduleEventMigrations < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + BUFFER_SIZE = 1000 + + disable_ddl_transaction! + + class Event < ActiveRecord::Base + include EachBatch + + self.table_name = 'events' + end + + def up + jobs = [] + + Event.each_batch(of: 1000) do |relation| + min, max = relation.pluck('MIN(id), MAX(id)').first + + if jobs.length == BUFFER_SIZE + # We push multiple jobs at a time to reduce the time spent in + # Sidekiq/Redis operations. We're using this buffer based approach so we + # don't need to run additional queries for every range. + BackgroundMigrationWorker.perform_bulk(jobs) + jobs.clear + end + + jobs << ['MigrateEventsToPushEventPayloads', [min, max]] + end + + BackgroundMigrationWorker.perform_bulk(jobs) unless jobs.empty? + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index ed3cf70bcdd..8a2df991f0d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -534,6 +534,21 @@ ActiveRecord::Schema.define(version: 20170807160457) do add_index "events", ["target_id"], name: "index_events_on_target_id", using: :btree add_index "events", ["target_type"], name: "index_events_on_target_type", using: :btree + create_table "events_for_migration", force: :cascade do |t| + t.integer "project_id" + t.integer "author_id", null: false + t.integer "target_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "action", limit: 2, null: false + t.string "target_type" + end + + add_index "events_for_migration", ["action"], name: "index_events_for_migration_on_action", using: :btree + add_index "events_for_migration", ["author_id"], name: "index_events_for_migration_on_author_id", using: :btree + add_index "events_for_migration", ["project_id"], name: "index_events_for_migration_on_project_id", using: :btree + add_index "events_for_migration", ["target_type", "target_id"], name: "index_events_for_migration_on_target_type_and_target_id", using: :btree + create_table "feature_gates", force: :cascade do |t| t.string "feature_key", null: false t.string "key", null: false @@ -1254,6 +1269,19 @@ ActiveRecord::Schema.define(version: 20170807160457) do add_index "protected_tags", ["project_id"], name: "index_protected_tags_on_project_id", using: :btree + create_table "push_event_payloads", id: false, force: :cascade do |t| + t.integer "commit_count", limit: 8, null: false + t.integer "event_id", null: false + t.integer "action", limit: 2, null: false + t.integer "ref_type", limit: 2, null: false + t.binary "commit_from" + t.binary "commit_to" + t.text "ref" + t.string "commit_title", limit: 70 + end + + add_index "push_event_payloads", ["event_id"], name: "index_push_event_payloads_on_event_id", unique: true, using: :btree + create_table "redirect_routes", force: :cascade do |t| t.integer "source_id", null: false t.string "source_type", null: false @@ -1654,6 +1682,8 @@ ActiveRecord::Schema.define(version: 20170807160457) do add_foreign_key "deployments", "projects", name: "fk_b9a3851b82", on_delete: :cascade add_foreign_key "environments", "projects", name: "fk_d1c8c1da6a", on_delete: :cascade add_foreign_key "events", "projects", name: "fk_0434b48643", on_delete: :cascade + add_foreign_key "events_for_migration", "projects", on_delete: :cascade + add_foreign_key "events_for_migration", "users", column: "author_id", name: "fk_edfd187b6f", on_delete: :cascade add_foreign_key "forked_project_links", "projects", column: "forked_to_project_id", name: "fk_434510edb0", on_delete: :cascade add_foreign_key "gpg_keys", "users", on_delete: :cascade add_foreign_key "gpg_signatures", "gpg_keys", on_delete: :nullify @@ -1696,6 +1726,7 @@ ActiveRecord::Schema.define(version: 20170807160457) do add_foreign_key "protected_tag_create_access_levels", "protected_tags" add_foreign_key "protected_tag_create_access_levels", "users" add_foreign_key "protected_tags", "projects", name: "fk_8e4af87648", on_delete: :cascade + add_foreign_key "push_event_payloads", "events_for_migration", column: "event_id", name: "fk_36c74129da", on_delete: :cascade add_foreign_key "releases", "projects", name: "fk_47fe2a0596", on_delete: :cascade add_foreign_key "services", "projects", name: "fk_71cce407f9", on_delete: :cascade add_foreign_key "snippets", "projects", name: "fk_be41fd4bb7", on_delete: :cascade |