gitlab-org--gitlab-foss/spec/models/lfs_object_spec.rb
Toon Claes d5f290e417 Enhance performance of counting local LFS objects
Add an index to the `file_store` column on `lfs_objects`. This makes
counting local objects faster.

Also, there is no longer need to check for objects with `file_store`
being `NULL`. See
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18557

---

### Query plans

#### Before & with `NULL`

```
Aggregate  (cost=113495.96..113495.97 rows=1 width=8) (actual time=1691.394..1691.394 rows=1 loops=1)
  ->  Seq Scan on lfs_objects  (cost=0.00..106415.50 rows=2832186 width=0) (actual time=0.012..1312.488 rows=2852607 loops=1)
        Filter: ((file_store = 1) OR (file_store IS NULL))
        Rows Removed by Filter: 131
Planning time: 0.077 ms
Execution time: 1691.433 ms
```

#### Before, without `NULL`

```
Aggregate  (cost=113495.96..113495.97 rows=1 width=8) (actual time=856.423..856.424 rows=1 loops=1)
  ->  Seq Scan on lfs_objects  (cost=0.00..106415.50 rows=2832186 width=0) (actual time=0.012..672.181 rows=2852607 loops=1)
        Filter: (file_store = 1)
        Rows Removed by Filter: 131
Planning time: 0.128 ms
Execution time: 856.470 ms
```

#### After & with `NULL`

```
Aggregate  (cost=68819.95..68819.96 rows=1 width=8) (actual time=583.355..583.355 rows=1 loops=1)
  ->  Index Only Scan using index_lfs_objects_on_file_store on lfs_objects  (cost=0.43..61688.35 rows=2852643 width=0) (actual time=0.028..399.177 rows=2852607 loops=1)
        Filter: ((file_store = 1) OR (file_store IS NULL))
        Rows Removed by Filter: 131
        Heap Fetches: 867
Planning time: 0.096 ms
Execution time: 583.404 ms
```

#### After, without `NULL`

```
Aggregate  (cost=68817.29..68817.30 rows=1 width=8) (actual time=490.550..490.551 rows=1 loops=1)
  ->  Index Only Scan using index_lfs_objects_on_file_store on lfs_objects  (cost=0.43..61685.68 rows=2852643 width=0) (actual time=0.040..311.760 rows=2852607 loops=1)
        Index Cond: (file_store = 1)
        Heap Fetches: 831
Planning time: 0.294 ms
Execution time: 490.590 ms
```

Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/6067
2018-10-30 08:42:11 +01:00

103 lines
3 KiB
Ruby

require 'spec_helper'
describe LfsObject do
describe '#local_store?' do
it 'returns true when file_store is equal to LfsObjectUploader::Store::LOCAL' do
subject.file_store = LfsObjectUploader::Store::LOCAL
expect(subject.local_store?).to eq true
end
it 'returns false whe file_store is equal to LfsObjectUploader::Store::REMOTE' do
subject.file_store = LfsObjectUploader::Store::REMOTE
expect(subject.local_store?).to eq false
end
end
describe '#schedule_background_upload' do
before do
stub_lfs_setting(enabled: true)
end
subject { create(:lfs_object, :with_file) }
context 'when object storage is disabled' do
before do
stub_lfs_object_storage(enabled: false)
end
it 'does not schedule the migration' do
expect(ObjectStorage::BackgroundMoveWorker).not_to receive(:perform_async)
subject
end
end
context 'when object storage is enabled' do
context 'when background upload is enabled' do
context 'when is licensed' do
before do
stub_lfs_object_storage(background_upload: true)
end
it 'schedules the model for migration' do
expect(ObjectStorage::BackgroundMoveWorker)
.to receive(:perform_async)
.with('LfsObjectUploader', described_class.name, :file, kind_of(Numeric))
.once
subject
end
it 'schedules the model for migration once' do
expect(ObjectStorage::BackgroundMoveWorker)
.to receive(:perform_async)
.with('LfsObjectUploader', described_class.name, :file, kind_of(Numeric))
.once
create(:lfs_object, :with_file)
end
end
end
context 'when background upload is disabled' do
before do
stub_lfs_object_storage(background_upload: false)
end
it 'schedules the model for migration' do
expect(ObjectStorage::BackgroundMoveWorker).not_to receive(:perform_async)
subject
end
end
end
describe 'file is being stored' do
let(:lfs_object) { create(:lfs_object, :with_file) }
context 'when existing object has local store' do
it 'is stored locally' do
expect(lfs_object.file_store).to be(ObjectStorage::Store::LOCAL)
expect(lfs_object.file).to be_file_storage
expect(lfs_object.file.object_store).to eq(ObjectStorage::Store::LOCAL)
end
end
context 'when direct upload is enabled' do
before do
stub_lfs_object_storage(direct_upload: true)
end
context 'when file is stored' do
it 'is stored remotely' do
expect(lfs_object.file_store).to eq(ObjectStorage::Store::REMOTE)
expect(lfs_object.file).not_to be_file_storage
expect(lfs_object.file.object_store).to eq(ObjectStorage::Store::REMOTE)
end
end
end
end
end
end