From 6f89bd3628322d0c17f97163d060e6ef7238de3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Wed, 19 Apr 2017 11:06:20 +0200 Subject: [PATCH 1/3] Use Gitaly for getting Branch/Tag counts - Backup-rake-spec fixed. Storage config broken - Use rugged to compare branch/tag-count in specs - upgrade gitaly --- GITALY_SERVER_VERSION | 2 +- app/models/repository.rb | 8 ++------ lib/gitlab/git/repository.rb | 29 +++++++++++++++++++++------ lib/gitlab/gitaly_client/ref.rb | 8 ++++++++ spec/models/repository_spec.rb | 5 +++++ spec/tasks/gitlab/backup_rake_spec.rb | 6 ++++-- 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/GITALY_SERVER_VERSION b/GITALY_SERVER_VERSION index a918a2aa18d..a3df0a6959e 100644 --- a/GITALY_SERVER_VERSION +++ b/GITALY_SERVER_VERSION @@ -1 +1 @@ -0.6.0 +0.8.0 diff --git a/app/models/repository.rb b/app/models/repository.rb index d02aea49689..ce1238e66d2 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -505,14 +505,10 @@ class Repository delegate :tag_names, to: :raw_repository cache_method :tag_names, fallback: [] - def branch_count - branches.size - end + delegate :branch_count, to: :raw_repository cache_method :branch_count, fallback: 0 - def tag_count - raw_repository.rugged.tags.count - end + delegate :tag_count, to: :raw_repository cache_method :tag_count, fallback: 0 def avatar diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index 452dba7971d..4af7f1396c7 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -122,13 +122,30 @@ module Gitlab # Returns the number of valid branches def branch_count - rugged.branches.count do |ref| - begin - ref.name && ref.target # ensures the branch is valid + Gitlab::GitalyClient.migrate(:branch_names) do |is_enabled| + if is_enabled + gitaly_ref_client.count_branch_names + else + rugged.branches.count do |ref| + begin + ref.name && ref.target # ensures the branch is valid - true - rescue Rugged::ReferenceError - false + true + rescue Rugged::ReferenceError + false + end + end + end + end + end + + # Returns the number of valid tags + def tag_count + Gitlab::GitalyClient.migrate(:tag_names) do |is_enabled| + if is_enabled + gitaly_ref_client.count_tag_names + else + rugged.tags.count end end end diff --git a/lib/gitlab/gitaly_client/ref.rb b/lib/gitlab/gitaly_client/ref.rb index d3c0743db4e..2a5e8f73e55 100644 --- a/lib/gitlab/gitaly_client/ref.rb +++ b/lib/gitlab/gitaly_client/ref.rb @@ -34,6 +34,14 @@ module Gitlab stub.find_ref_name(request).name end + def count_tag_names + tag_names.count + end + + def count_branch_names + branch_names.count + end + private def consume_refs_response(response, prefix:) diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 98d0641443e..4526c3194b6 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1379,12 +1379,17 @@ describe Repository, models: true do describe '#branch_count' do it 'returns the number of branches' do expect(repository.branch_count).to be_an(Integer) + rugged_count = repository.raw_repository.rugged.branches.count + expect(repository.branch_count).to eq(rugged_count) end end describe '#tag_count' do it 'returns the number of tags' do expect(repository.tag_count).to be_an(Integer) + # NOTE: Until rugged goes away, make sure rugged and gitaly are in sync + rugged_count = repository.raw_repository.rugged.tags.count + expect(repository.tag_count).to eq(rugged_count) end end diff --git a/spec/tasks/gitlab/backup_rake_spec.rb b/spec/tasks/gitlab/backup_rake_spec.rb index 0a4a6ed8145..df2f2ce95e6 100644 --- a/spec/tasks/gitlab/backup_rake_spec.rb +++ b/spec/tasks/gitlab/backup_rake_spec.rb @@ -230,11 +230,13 @@ describe 'gitlab:app namespace rake task' do before do FileUtils.mkdir('tmp/tests/default_storage') FileUtils.mkdir('tmp/tests/custom_storage') + gitaly_address = Gitlab.config.repositories.storages.default.gitaly_address storages = { - 'default' => { 'path' => Settings.absolute('tmp/tests/default_storage') }, - 'custom' => { 'path' => Settings.absolute('tmp/tests/custom_storage') } + 'default' => { 'path' => Settings.absolute('tmp/tests/default_storage'), 'gitaly_address' => gitaly_address }, + 'custom' => { 'path' => Settings.absolute('tmp/tests/custom_storage'), 'gitaly_address' => gitaly_address } } allow(Gitlab.config.repositories).to receive(:storages).and_return(storages) + Gitlab::GitalyClient.configure_channels # Create the projects now, after mocking the settings but before doing the backup project_a From a998710a3b2d4d2f5fa7dbdaf4ae468cd304730c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Wed, 26 Apr 2017 18:33:48 +0200 Subject: [PATCH 2/3] Fix RSpec --- spec/lib/gitlab/git/repository_spec.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb index f88653cb1fe..1b78910fa3c 100644 --- a/spec/lib/gitlab/git/repository_spec.rb +++ b/spec/lib/gitlab/git/repository_spec.rb @@ -1074,20 +1074,8 @@ describe Gitlab::Git::Repository, seed_helper: true do end describe '#branch_count' do - before(:each) do - valid_ref = double(:ref) - invalid_ref = double(:ref) - - allow(valid_ref).to receive_messages(name: 'master', target: double(:target)) - - allow(invalid_ref).to receive_messages(name: 'bad-branch') - allow(invalid_ref).to receive(:target) { raise Rugged::ReferenceError } - - allow(repository.rugged).to receive_messages(branches: [valid_ref, invalid_ref]) - end - it 'returns the number of branches' do - expect(repository.branch_count).to eq(1) + expect(repository.branch_count).to eq(9) end end From 573d0989110ccb0630481f416d1a4f8fd05ee608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Fri, 28 Apr 2017 14:12:29 +0200 Subject: [PATCH 3/3] Cleanup --- app/models/repository.rb | 4 +--- spec/models/repository_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index ce1238e66d2..2ffd9115372 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -505,10 +505,8 @@ class Repository delegate :tag_names, to: :raw_repository cache_method :tag_names, fallback: [] - delegate :branch_count, to: :raw_repository + delegate :branch_count, :tag_count, to: :raw_repository cache_method :branch_count, fallback: 0 - - delegate :tag_count, to: :raw_repository cache_method :tag_count, fallback: 0 def avatar diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 4526c3194b6..dfa211ec6cd 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1379,7 +1379,10 @@ describe Repository, models: true do describe '#branch_count' do it 'returns the number of branches' do expect(repository.branch_count).to be_an(Integer) + + # NOTE: Until rugged goes away, make sure rugged and gitaly are in sync rugged_count = repository.raw_repository.rugged.branches.count + expect(repository.branch_count).to eq(rugged_count) end end @@ -1387,8 +1390,10 @@ describe Repository, models: true do describe '#tag_count' do it 'returns the number of tags' do expect(repository.tag_count).to be_an(Integer) + # NOTE: Until rugged goes away, make sure rugged and gitaly are in sync rugged_count = repository.raw_repository.rugged.tags.count + expect(repository.tag_count).to eq(rugged_count) end end