Commit 2fb3c041 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Rename shared runner builds table to running builds

parent 7204a481
...@@ -39,7 +39,7 @@ module Ci ...@@ -39,7 +39,7 @@ module Ci
has_one :deployment, as: :deployable, class_name: 'Deployment' has_one :deployment, as: :deployable, class_name: 'Deployment'
has_one :pending_state, class_name: 'Ci::BuildPendingState', inverse_of: :build has_one :pending_state, class_name: 'Ci::BuildPendingState', inverse_of: :build
has_one :queuing_entry, class_name: 'Ci::PendingBuild', foreign_key: :build_id has_one :queuing_entry, class_name: 'Ci::PendingBuild', foreign_key: :build_id
has_one :shared_runner_metadata, class_name: 'Ci::SharedRunnerBuild', foreign_key: :build_id has_one :runtime_metadata, class_name: 'Ci::RunningBuild', foreign_key: :build_id
has_many :trace_sections, class_name: 'Ci::BuildTraceSection' has_many :trace_sections, class_name: 'Ci::BuildTraceSection'
has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id, inverse_of: :build has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id, inverse_of: :build
has_many :report_results, class_name: 'Ci::BuildReportResult', inverse_of: :build has_many :report_results, class_name: 'Ci::BuildReportResult', inverse_of: :build
...@@ -1074,7 +1074,7 @@ module Ci ...@@ -1074,7 +1074,7 @@ module Ci
end end
## ##
# We can have only one queuing entry or shared runner build tracking entry, # We can have only one queuing entry or running build tracking entry,
# because there is a unique index on `build_id` in each table, but we need # because there is a unique index on `build_id` in each table, but we need
# a relation to remove these entries more efficiently in a single statement # a relation to remove these entries more efficiently in a single statement
# without actually loading data. # without actually loading data.
...@@ -1083,8 +1083,8 @@ module Ci ...@@ -1083,8 +1083,8 @@ module Ci
::Ci::PendingBuild.where(build_id: self.id) ::Ci::PendingBuild.where(build_id: self.id)
end end
def all_shared_runner_metadata def all_runtime_metadata
::Ci::SharedRunnerBuild.where(build_id: self.id) ::Ci::RunningBuild.where(build_id: self.id)
end end
protected protected
......
# frozen_string_literal: true # frozen_string_literal: true
module Ci module Ci
class SharedRunnerBuild < ApplicationRecord class RunningBuild < ApplicationRecord
extend Gitlab::Ci::Model extend Gitlab::Ci::Model
belongs_to :project belongs_to :project
belongs_to :build, class_name: 'Ci::Build' belongs_to :build, class_name: 'Ci::Build'
belongs_to :runner, class_name: 'Ci::Runner' belongs_to :runner, class_name: 'Ci::Runner'
def self.upsert_from_build!(build) enum runner_type: ::Ci::Runner.runner_types
def self.upsert_shared_runner_build!(build)
unless build.runner && build.runner.instance_type? unless build.runner && build.runner.instance_type?
raise ArgumentError, 'build has not been picked by a shared runner' raise ArgumentError, 'build has not been picked by a shared runner'
end end
entry = self.new(build: build, project: build.project, runner: build.runner) entry = self.new(build: build,
project: build.project,
runner: build.runner,
runner_type: build.runner.runner_type)
entry.validate! entry.validate!
......
...@@ -49,7 +49,8 @@ module Ci ...@@ -49,7 +49,8 @@ module Ci
end end
## ##
# Add shared runner build tracking entry (used for queuing). # Add shared runner build tracking entry (used for queuing). This will rase
# an exception if a build has not been picked by a shared runner.
# #
def track(build, transition) def track(build, transition)
return unless Feature.enabled?(:ci_track_shared_runner_builds, build.project, default_enabled: :yaml) return unless Feature.enabled?(:ci_track_shared_runner_builds, build.project, default_enabled: :yaml)
...@@ -57,7 +58,7 @@ module Ci ...@@ -57,7 +58,7 @@ module Ci
raise InvalidQueueTransition unless transition.to == 'running' raise InvalidQueueTransition unless transition.to == 'running'
transition.within_transaction do transition.within_transaction do
result = ::Ci::SharedRunnerBuild.upsert_from_build!(build) result = ::Ci::RunningBuild.upsert_shared_runner_build!(build)
unless result.empty? unless result.empty?
metrics.increment_queue_operation(:shared_runner_build_new) metrics.increment_queue_operation(:shared_runner_build_new)
...@@ -68,7 +69,7 @@ module Ci ...@@ -68,7 +69,7 @@ module Ci
end end
## ##
# Remove a shared runner build tracking entry (used for queuing). # Remove a runtime build tracking entry (used for queuing).
# #
def untrack(build, transition) def untrack(build, transition)
return unless Feature.enabled?(:ci_untrack_shared_runner_builds, build.project, default_enabled: :yaml) return unless Feature.enabled?(:ci_untrack_shared_runner_builds, build.project, default_enabled: :yaml)
...@@ -76,7 +77,7 @@ module Ci ...@@ -76,7 +77,7 @@ module Ci
raise InvalidQueueTransition unless transition.from == 'running' raise InvalidQueueTransition unless transition.from == 'running'
transition.within_transaction do transition.within_transaction do
removed = build.all_shared_runner_metadata.delete_all removed = build.all_runtime_metadata.delete_all
if removed > 0 if removed > 0
metrics.increment_queue_operation(:shared_runner_build_done) metrics.increment_queue_operation(:shared_runner_build_done)
......
# frozen_string_literal: true # frozen_string_literal: true
class AddSharedRunnerBuildsTable < ActiveRecord::Migration[6.0] class AddRunningBuildsTable < ActiveRecord::Migration[6.0]
def up def up
create_table :ci_shared_runner_builds do |t| create_table :ci_running_builds do |t|
t.references :build, index: { unique: true }, null: false, foreign_key: { to_table: :ci_builds, on_delete: :cascade } t.references :build, index: { unique: true }, null: false, foreign_key: { to_table: :ci_builds, on_delete: :cascade }
t.references :project, index: true, null: false, foreign_key: { on_delete: :cascade } t.references :project, index: true, null: false, foreign_key: { on_delete: :cascade }
t.references :runner, index: true, null: false, foreign_key: { to_table: :ci_runners, on_delete: :cascade } t.references :runner, index: true, null: false, foreign_key: { to_table: :ci_runners, on_delete: :cascade }
t.integer :runner_type, limit: 2, null: false
t.datetime_with_timezone :created_at, null: false, default: -> { 'NOW()' } t.datetime_with_timezone :created_at, null: false, default: -> { 'NOW()' }
end end
end end
def down def down
drop_table :ci_shared_runner_builds drop_table :ci_running_builds
end end
end end
...@@ -11151,22 +11151,23 @@ CREATE SEQUENCE ci_runners_id_seq ...@@ -11151,22 +11151,23 @@ CREATE SEQUENCE ci_runners_id_seq
ALTER SEQUENCE ci_runners_id_seq OWNED BY ci_runners.id; ALTER SEQUENCE ci_runners_id_seq OWNED BY ci_runners.id;
CREATE TABLE ci_shared_runner_builds ( CREATE TABLE ci_running_builds (
id bigint NOT NULL, id bigint NOT NULL,
build_id bigint NOT NULL, build_id bigint NOT NULL,
project_id bigint NOT NULL, project_id bigint NOT NULL,
runner_id bigint NOT NULL, runner_id bigint NOT NULL,
runner_type smallint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL created_at timestamp with time zone DEFAULT now() NOT NULL
); );
CREATE SEQUENCE ci_shared_runner_builds_id_seq CREATE SEQUENCE ci_running_builds_id_seq
START WITH 1 START WITH 1
INCREMENT BY 1 INCREMENT BY 1
NO MINVALUE NO MINVALUE
NO MAXVALUE NO MAXVALUE
CACHE 1; CACHE 1;
ALTER SEQUENCE ci_shared_runner_builds_id_seq OWNED BY ci_shared_runner_builds.id; ALTER SEQUENCE ci_running_builds_id_seq OWNED BY ci_running_builds.id;
CREATE TABLE ci_sources_pipelines ( CREATE TABLE ci_sources_pipelines (
id integer NOT NULL, id integer NOT NULL,
...@@ -19714,7 +19715,7 @@ ALTER TABLE ONLY ci_runner_projects ALTER COLUMN id SET DEFAULT nextval('ci_runn ...@@ -19714,7 +19715,7 @@ ALTER TABLE ONLY ci_runner_projects ALTER COLUMN id SET DEFAULT nextval('ci_runn
ALTER TABLE ONLY ci_runners ALTER COLUMN id SET DEFAULT nextval('ci_runners_id_seq'::regclass); ALTER TABLE ONLY ci_runners ALTER COLUMN id SET DEFAULT nextval('ci_runners_id_seq'::regclass);
ALTER TABLE ONLY ci_shared_runner_builds ALTER COLUMN id SET DEFAULT nextval('ci_shared_runner_builds_id_seq'::regclass); ALTER TABLE ONLY ci_running_builds ALTER COLUMN id SET DEFAULT nextval('ci_running_builds_id_seq'::regclass);
ALTER TABLE ONLY ci_sources_pipelines ALTER COLUMN id SET DEFAULT nextval('ci_sources_pipelines_id_seq'::regclass); ALTER TABLE ONLY ci_sources_pipelines ALTER COLUMN id SET DEFAULT nextval('ci_sources_pipelines_id_seq'::regclass);
...@@ -20915,8 +20916,8 @@ ALTER TABLE ONLY ci_runner_projects ...@@ -20915,8 +20916,8 @@ ALTER TABLE ONLY ci_runner_projects
ALTER TABLE ONLY ci_runners ALTER TABLE ONLY ci_runners
ADD CONSTRAINT ci_runners_pkey PRIMARY KEY (id); ADD CONSTRAINT ci_runners_pkey PRIMARY KEY (id);
ALTER TABLE ONLY ci_shared_runner_builds ALTER TABLE ONLY ci_running_builds
ADD CONSTRAINT ci_shared_runner_builds_pkey PRIMARY KEY (id); ADD CONSTRAINT ci_running_builds_pkey PRIMARY KEY (id);
ALTER TABLE ONLY ci_sources_pipelines ALTER TABLE ONLY ci_sources_pipelines
ADD CONSTRAINT ci_sources_pipelines_pkey PRIMARY KEY (id); ADD CONSTRAINT ci_sources_pipelines_pkey PRIMARY KEY (id);
...@@ -22904,11 +22905,11 @@ CREATE INDEX index_ci_runners_on_token ON ci_runners USING btree (token); ...@@ -22904,11 +22905,11 @@ CREATE INDEX index_ci_runners_on_token ON ci_runners USING btree (token);
CREATE INDEX index_ci_runners_on_token_encrypted ON ci_runners USING btree (token_encrypted); CREATE INDEX index_ci_runners_on_token_encrypted ON ci_runners USING btree (token_encrypted);
CREATE UNIQUE INDEX index_ci_shared_runner_builds_on_build_id ON ci_shared_runner_builds USING btree (build_id); CREATE UNIQUE INDEX index_ci_running_builds_on_build_id ON ci_running_builds USING btree (build_id);
CREATE INDEX index_ci_shared_runner_builds_on_project_id ON ci_shared_runner_builds USING btree (project_id); CREATE INDEX index_ci_running_builds_on_project_id ON ci_running_builds USING btree (project_id);
CREATE INDEX index_ci_shared_runner_builds_on_runner_id ON ci_shared_runner_builds USING btree (runner_id); CREATE INDEX index_ci_running_builds_on_runner_id ON ci_running_builds USING btree (runner_id);
CREATE INDEX index_ci_sources_pipelines_on_pipeline_id ON ci_sources_pipelines USING btree (pipeline_id); CREATE INDEX index_ci_sources_pipelines_on_pipeline_id ON ci_sources_pipelines USING btree (pipeline_id);
...@@ -26422,9 +26423,6 @@ ALTER TABLE ONLY onboarding_progresses ...@@ -26422,9 +26423,6 @@ ALTER TABLE ONLY onboarding_progresses
ALTER TABLE ONLY protected_branch_unprotect_access_levels ALTER TABLE ONLY protected_branch_unprotect_access_levels
ADD CONSTRAINT fk_rails_2d2aba21ef FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_2d2aba21ef FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_shared_runner_builds
ADD CONSTRAINT fk_rails_2de6fdddcb FOREIGN KEY (runner_id) REFERENCES ci_runners(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_freeze_periods ALTER TABLE ONLY ci_freeze_periods
ADD CONSTRAINT fk_rails_2e02bbd1a6 FOREIGN KEY (project_id) REFERENCES projects(id); ADD CONSTRAINT fk_rails_2e02bbd1a6 FOREIGN KEY (project_id) REFERENCES projects(id);
...@@ -26725,6 +26723,9 @@ ALTER TABLE ONLY vulnerability_scanners ...@@ -26725,6 +26723,9 @@ ALTER TABLE ONLY vulnerability_scanners
ALTER TABLE ONLY reviews ALTER TABLE ONLY reviews
ADD CONSTRAINT fk_rails_5ca11d8c31 FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_5ca11d8c31 FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_running_builds
ADD CONSTRAINT fk_rails_5ca491d360 FOREIGN KEY (runner_id) REFERENCES ci_runners(id) ON DELETE CASCADE;
ALTER TABLE ONLY epic_issues ALTER TABLE ONLY epic_issues
ADD CONSTRAINT fk_rails_5d942936b4 FOREIGN KEY (epic_id) REFERENCES epics(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_5d942936b4 FOREIGN KEY (epic_id) REFERENCES epics(id) ON DELETE CASCADE;
...@@ -26824,9 +26825,6 @@ ALTER TABLE ONLY plan_limits ...@@ -26824,9 +26825,6 @@ ALTER TABLE ONLY plan_limits
ALTER TABLE ONLY operations_feature_flags_issues ALTER TABLE ONLY operations_feature_flags_issues
ADD CONSTRAINT fk_rails_6a8856ca4f FOREIGN KEY (feature_flag_id) REFERENCES operations_feature_flags(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_6a8856ca4f FOREIGN KEY (feature_flag_id) REFERENCES operations_feature_flags(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_shared_runner_builds
ADD CONSTRAINT fk_rails_6b0c3012c9 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE;
ALTER TABLE ONLY prometheus_alerts ALTER TABLE ONLY prometheus_alerts
ADD CONSTRAINT fk_rails_6d9b283465 FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_6d9b283465 FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE;
...@@ -27022,9 +27020,6 @@ ALTER TABLE ONLY alert_management_alert_user_mentions ...@@ -27022,9 +27020,6 @@ ALTER TABLE ONLY alert_management_alert_user_mentions
ALTER TABLE ONLY project_daily_statistics ALTER TABLE ONLY project_daily_statistics
ADD CONSTRAINT fk_rails_8e549b272d FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_8e549b272d FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_shared_runner_builds
ADD CONSTRAINT fk_rails_8f43a1469c FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_pipelines_config ALTER TABLE ONLY ci_pipelines_config
ADD CONSTRAINT fk_rails_906c9a2533 FOREIGN KEY (pipeline_id) REFERENCES ci_pipelines(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_906c9a2533 FOREIGN KEY (pipeline_id) REFERENCES ci_pipelines(id) ON DELETE CASCADE;
...@@ -27436,6 +27431,9 @@ ALTER TABLE ONLY geo_hashed_storage_attachments_events ...@@ -27436,6 +27431,9 @@ ALTER TABLE ONLY geo_hashed_storage_attachments_events
ALTER TABLE ONLY merge_request_reviewers ALTER TABLE ONLY merge_request_reviewers
ADD CONSTRAINT fk_rails_d9fec24b9d FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_d9fec24b9d FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_running_builds
ADD CONSTRAINT fk_rails_da45cfa165 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE;
ALTER TABLE ONLY jira_imports ALTER TABLE ONLY jira_imports
ADD CONSTRAINT fk_rails_da617096ce FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL; ADD CONSTRAINT fk_rails_da617096ce FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
...@@ -27448,6 +27446,9 @@ ALTER TABLE ONLY issues_prometheus_alert_events ...@@ -27448,6 +27446,9 @@ ALTER TABLE ONLY issues_prometheus_alert_events
ALTER TABLE ONLY board_user_preferences ALTER TABLE ONLY board_user_preferences
ADD CONSTRAINT fk_rails_dbebdaa8fe FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_dbebdaa8fe FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_running_builds
ADD CONSTRAINT fk_rails_dc1d0801e8 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY vulnerability_occurrence_pipelines ALTER TABLE ONLY vulnerability_occurrence_pipelines
ADD CONSTRAINT fk_rails_dc3ae04693 FOREIGN KEY (occurrence_id) REFERENCES vulnerability_occurrences(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_dc3ae04693 FOREIGN KEY (occurrence_id) REFERENCES vulnerability_occurrences(id) ON DELETE CASCADE;
...@@ -2,30 +2,33 @@ ...@@ -2,30 +2,33 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::SharedRunnerBuild do RSpec.describe Ci::RunningBuild do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) } let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
let(:runner) { create(:ci_runner, :instance_type) } let(:runner) { create(:ci_runner, :instance_type) }
let(:build) { create(:ci_build, :running, runner: runner, pipeline: pipeline) } let(:build) { create(:ci_build, :running, runner: runner, pipeline: pipeline) }
describe '.upsert_from_build!' do describe '.upsert_shared_runner_build!' do
context 'another pending entry does not exist' do context 'another pending entry does not exist' do
it 'creates a new pending entry' do it 'creates a new pending entry' do
result = described_class.upsert_from_build!(build) result = described_class.upsert_shared_runner_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id expect(result.rows.dig(0, 0)).to eq build.id
expect(build.reload.shared_runner_metadata).to be_present expect(build.reload.runtime_metadata).to be_present
end end
end end
context 'when another queuing entry exists for given build' do context 'when another queuing entry exists for given build' do
before do before do
described_class.create!(build: build, project: project, runner: runner) described_class.create!(build: build,
project: project,
runner: runner,
runner_type: runner.runner_type)
end end
it 'returns a build id as a result' do it 'returns a build id as a result' do
result = described_class.upsert_from_build!(build) result = described_class.upsert_shared_runner_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id expect(result.rows.dig(0, 0)).to eq build.id
end end
...@@ -35,7 +38,7 @@ RSpec.describe Ci::SharedRunnerBuild do ...@@ -35,7 +38,7 @@ RSpec.describe Ci::SharedRunnerBuild do
let(:runner) { create(:ci_runner, :project) } let(:runner) { create(:ci_runner, :project) }
it 'raises an error' do it 'raises an error' do
expect { described_class.upsert_from_build!(build) } expect { described_class.upsert_shared_runner_build!(build) }
.to raise_error(ArgumentError, 'build has not been picked by a shared runner') .to raise_error(ArgumentError, 'build has not been picked by a shared runner')
end end
end end
...@@ -44,7 +47,7 @@ RSpec.describe Ci::SharedRunnerBuild do ...@@ -44,7 +47,7 @@ RSpec.describe Ci::SharedRunnerBuild do
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
it 'raises an error' do it 'raises an error' do
expect { described_class.upsert_from_build!(build) } expect { described_class.upsert_shared_runner_build!(build) }
.to raise_error(ArgumentError, 'build has not been picked by a shared runner') .to raise_error(ArgumentError, 'build has not been picked by a shared runner')
end end
end end
......
...@@ -60,7 +60,7 @@ RSpec.describe Ci::RetryBuildService do ...@@ -60,7 +60,7 @@ RSpec.describe Ci::RetryBuildService do
artifacts_file artifacts_metadata artifacts_size commands artifacts_file artifacts_metadata artifacts_size commands
resource resource_group_id processed security_scans author resource resource_group_id processed security_scans author
pipeline_id report_results pending_state pages_deployments pipeline_id report_results pending_state pages_deployments
queuing_entry shared_runner_metadata].freeze queuing_entry runtime_metadata].freeze
shared_examples 'build duplication' do shared_examples 'build duplication' do
let_it_be(:another_pipeline) { create(:ci_empty_pipeline, project: project) } let_it_be(:another_pipeline) { create(:ci_empty_pipeline, project: project) }
......
...@@ -146,8 +146,9 @@ RSpec.describe Ci::UpdateBuildQueueService do ...@@ -146,8 +146,9 @@ RSpec.describe Ci::UpdateBuildQueueService do
context 'when duplicate entry exists' do context 'when duplicate entry exists' do
before do before do
::Ci::SharedRunnerBuild ::Ci::RunningBuild.create!(
.create!(build: build, project: project, runner: runner) build: build, project: project, runner: runner, runner_type: runner.runner_type
)
end end
it 'does nothing and returns build id' do it 'does nothing and returns build id' do
...@@ -168,8 +169,9 @@ RSpec.describe Ci::UpdateBuildQueueService do ...@@ -168,8 +169,9 @@ RSpec.describe Ci::UpdateBuildQueueService do
context 'when shared runner build tracking entry exists' do context 'when shared runner build tracking entry exists' do
before do before do
Ci::SharedRunnerBuild Ci::RunningBuild.create!(
.create!(build: build, project: project, runner: runner) build: build, project: project, runner: runner, runner_type: runner.runner_type
)
end end
it 'removes shared runner build' do it 'removes shared runner build' do
......
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