Commit 1e2b82eb authored by Adam Hegyi's avatar Adam Hegyi Committed by Andreas Brandl

Remove code hotspots tables

- Using `with_lock_retries` helper method.
- Fix duration conversion within `with_lock_retries`.
parent 91fee199
---
title: Remove unused Code Hotspots database tables
merge_request: 23590
author:
type: other
# frozen_string_literal: true
class RemoveAnalyticsRepositoryTableFksOnProjects < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
# Requires ExclusiveLock on all tables. analytics_* tables are empty
remove_foreign_key :analytics_repository_files, :projects
remove_foreign_key :analytics_repository_file_edits, :projects
remove_foreign_key :analytics_repository_file_commits, :projects
end
end
def down
with_lock_retries do
# rubocop:disable Migration/AddConcurrentForeignKey
add_foreign_key :analytics_repository_files, :projects, on_delete: :cascade
add_foreign_key :analytics_repository_file_edits, :projects, on_delete: :cascade
add_foreign_key :analytics_repository_file_commits, :projects, on_delete: :cascade
# rubocop:enable Migration/AddConcurrentForeignKey
end
end
end
# frozen_string_literal: true
class RemoveAnalyticsRepositoryFilesFkOnOtherAnalyticsTables < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
# Requires ExclusiveLock on all tables. analytics_* tables are empty
remove_foreign_key :analytics_repository_file_edits, :analytics_repository_files
remove_foreign_key :analytics_repository_file_commits, :analytics_repository_files
end
end
def down
with_lock_retries do
# rubocop:disable Migration/AddConcurrentForeignKey
add_foreign_key :analytics_repository_file_edits, :analytics_repository_files, on_delete: :cascade
add_foreign_key :analytics_repository_file_commits, :analytics_repository_files, on_delete: :cascade
# rubocop:enable Migration/AddConcurrentForeignKey
end
end
end
# frozen_string_literal: true
class DropAnalyticsRepositoryFilesTable < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
# Requires ExclusiveLock on the table. Not in use, no records, no FKs.
drop_table :analytics_repository_files
end
def down
create_table :analytics_repository_files do |t|
t.bigint :project_id, null: false
t.string :file_path, limit: 4096, null: false
end
add_index :analytics_repository_files, [:project_id, :file_path], unique: true
end
end
# frozen_string_literal: true
class DropAnalyticsRepositoryFileCommitsTable < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
# Requires ExclusiveLock on the table. Not in use, no records, no FKs.
drop_table :analytics_repository_file_commits
end
def down
create_table :analytics_repository_file_commits do |t|
t.bigint :analytics_repository_file_id, null: false
t.index :analytics_repository_file_id, name: 'index_analytics_repository_file_commits_file_id'
t.bigint :project_id, null: false
t.date :committed_date, null: false
t.integer :commit_count, limit: 2, null: false
end
add_index :analytics_repository_file_commits,
[:project_id, :committed_date, :analytics_repository_file_id],
name: 'index_file_commits_on_committed_date_file_id_and_project_id',
unique: true
end
end
# frozen_string_literal: true
class DropAnalyticsRepositoryFileEditsTable < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
# Requires ExclusiveLock on the table. Not in use, no records, no FKs.
drop_table :analytics_repository_file_edits
end
def down
create_table :analytics_repository_file_edits do |t|
t.bigint :project_id, null: false
t.index :project_id
t.bigint :analytics_repository_file_id, null: false
t.date :committed_date, null: false
t.integer :num_edits, null: false, default: 0
end
add_index :analytics_repository_file_edits,
[:analytics_repository_file_id, :committed_date, :project_id],
name: 'index_file_edits_on_committed_date_file_id_and_project_id',
unique: true
end
end
......@@ -94,30 +94,6 @@ ActiveRecord::Schema.define(version: 2020_01_27_090233) do
t.index ["project_id"], name: "analytics_repository_languages_on_project_id"
end
create_table "analytics_repository_file_commits", force: :cascade do |t|
t.bigint "analytics_repository_file_id", null: false
t.bigint "project_id", null: false
t.date "committed_date", null: false
t.integer "commit_count", limit: 2, null: false
t.index ["analytics_repository_file_id"], name: "index_analytics_repository_file_commits_file_id"
t.index ["project_id", "committed_date", "analytics_repository_file_id"], name: "index_file_commits_on_committed_date_file_id_and_project_id", unique: true
end
create_table "analytics_repository_file_edits", force: :cascade do |t|
t.bigint "project_id", null: false
t.bigint "analytics_repository_file_id", null: false
t.date "committed_date", null: false
t.integer "num_edits", default: 0, null: false
t.index ["analytics_repository_file_id", "committed_date", "project_id"], name: "index_file_edits_on_committed_date_file_id_and_project_id", unique: true
t.index ["project_id"], name: "index_analytics_repository_file_edits_on_project_id"
end
create_table "analytics_repository_files", force: :cascade do |t|
t.bigint "project_id", null: false
t.string "file_path", limit: 4096, null: false
t.index ["project_id", "file_path"], name: "index_analytics_repository_files_on_project_id_and_file_path", unique: true
end
create_table "appearances", id: :serial, force: :cascade do |t|
t.string "title", null: false
t.text "description", null: false
......@@ -4476,11 +4452,6 @@ ActiveRecord::Schema.define(version: 2020_01_27_090233) do
add_foreign_key "analytics_cycle_analytics_project_stages", "projects", on_delete: :cascade
add_foreign_key "analytics_language_trend_repository_languages", "programming_languages", on_delete: :cascade
add_foreign_key "analytics_language_trend_repository_languages", "projects", on_delete: :cascade
add_foreign_key "analytics_repository_file_commits", "analytics_repository_files", on_delete: :cascade
add_foreign_key "analytics_repository_file_commits", "projects", on_delete: :cascade
add_foreign_key "analytics_repository_file_edits", "analytics_repository_files", on_delete: :cascade
add_foreign_key "analytics_repository_file_edits", "projects", on_delete: :cascade
add_foreign_key "analytics_repository_files", "projects", on_delete: :cascade
add_foreign_key "application_settings", "namespaces", column: "custom_project_templates_group_id", on_delete: :nullify
add_foreign_key "application_settings", "namespaces", column: "instance_administrators_group_id", name: "fk_e8a145f3a7", on_delete: :nullify
add_foreign_key "application_settings", "projects", column: "file_template_project_id", name: "fk_ec757bd087", on_delete: :nullify
......
......@@ -147,11 +147,11 @@ module Gitlab
end
def current_lock_timeout_in_ms
timing_configuration[current_iteration - 1][0].in_milliseconds
Integer(timing_configuration[current_iteration - 1][0].in_milliseconds)
end
def current_sleep_time_in_seconds
timing_configuration[current_iteration - 1][1].to_i
timing_configuration[current_iteration - 1][1].to_f
end
end
end
......
......@@ -128,4 +128,23 @@ describe Gitlab::Database::WithLockRetries do
end
end
end
context 'casting durations correctly' do
let(:timing_configuration) { [[0.015.seconds, 0.025.seconds], [0.015.seconds, 0.025.seconds]] } # 15ms, 25ms
it 'executes `SET LOCAL lock_timeout` using the configured timeout value in milliseconds' do
expect(ActiveRecord::Base.connection).to receive(:execute).with("SAVEPOINT active_record_1").and_call_original
expect(ActiveRecord::Base.connection).to receive(:execute).with("SET LOCAL lock_timeout TO '15ms'").and_call_original
expect(ActiveRecord::Base.connection).to receive(:execute).with("RELEASE SAVEPOINT active_record_1").and_call_original
subject.run { }
end
it 'calls `sleep` after the first iteration fails, using the configured sleep time' do
expect(subject).to receive(:run_block_with_transaction).and_raise(ActiveRecord::LockWaitTimeout).twice
expect(subject).to receive(:sleep).with(0.025)
subject.run { }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment