Commit 1271fdf1 authored by Mike Kozono's avatar Mike Kozono Committed by Gabriel Mazetto

Verify only synced registries

And add tests for Geo::VerifiableRegistry.
parent 3c40b659
......@@ -15,6 +15,21 @@ module Geo::VerifiableRegistry
def verification_state_model_key
self::MODEL_FOREIGN_KEY
end
override :verification_pending_batch_relation
def verification_pending_batch_relation(batch_size:)
super.synced
end
override :verification_failed_batch_relation
def verification_failed_batch_relation(batch_size:)
super.synced
end
override :needs_verification_relation
def needs_verification_relation
super.synced
end
end
included do
......
......@@ -101,25 +101,40 @@ module Gitlab
# query.
#
def verification_pending_batch(batch_size:)
relation = verification_pending.order(Gitlab::Database.nulls_first_order(:verified_at)).limit(batch_size) # rubocop:disable CodeReuse/ActiveRecord
relation = verification_pending_batch_relation(batch_size: batch_size)
start_verification_batch(relation)
end
# Overridden by Geo::VerifiableRegistry
def verification_pending_batch_relation(batch_size:)
verification_pending.order(Gitlab::Database.nulls_first_order(:verified_at)).limit(batch_size) # rubocop:disable CodeReuse/ActiveRecord
end
# Returns IDs of records that failed to verify (calculate and save checksum).
#
# Atomically marks those records "verification_started" in the same DB
# query.
#
def verification_failed_batch(batch_size:)
relation = verification_failed.order(Gitlab::Database.nulls_first_order(:verification_retry_at)).limit(batch_size) # rubocop:disable CodeReuse/ActiveRecord
relation = verification_failed_batch_relation(batch_size: batch_size)
start_verification_batch(relation)
end
# Overridden by Geo::VerifiableRegistry
def verification_failed_batch_relation(batch_size:)
verification_failed.order(Gitlab::Database.nulls_first_order(:verification_retry_at)).limit(batch_size) # rubocop:disable CodeReuse/ActiveRecord
end
# @return [Integer] number of records that need verification
def needs_verification_count(limit:)
needs_verification.limit(limit).count # rubocop:disable CodeReuse/ActiveRecord
needs_verification_relation.limit(limit).count # rubocop:disable CodeReuse/ActiveRecord
end
# Overridden by Geo::VerifiableRegistry
def needs_verification_relation
needs_verification
end
# Atomically marks the records as verification_started, with a
......
......@@ -7,6 +7,90 @@ RSpec.shared_examples 'a Geo verifiable registry' do
subject(:registry_record) { create(registry_class_factory, :synced) }
describe '.verification_pending_batch' do
before do
subject.save!
end
it 'returns IDs of rows which are synced and pending verification' do
expect(described_class.verification_pending_batch(batch_size: 4)).to match_array([subject.model_record_id])
end
it 'excludes rows which are not synced or are not pending verification' do
# rubocop:disable Rails/SaveBang
create(registry_class_factory, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :started, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :failed, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_failed), verification_failure: 'foo')
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_started))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_succeeded), verification_checksum: 'abc123')
# rubocop:enable Rails/SaveBang
expect(described_class.verification_pending_batch(batch_size: 4)).to match_array([subject.model_record_id])
end
it 'marks verification as started' do
described_class.verification_pending_batch(batch_size: 4)
expect(subject.reload.verification_started?).to be_truthy
expect(subject.verification_started_at).to be_present
end
end
describe '.verification_failed_batch' do
before do
subject.verification_failed_with_message!('foo')
end
it 'returns IDs of rows which are synced and failed verification' do
expect(described_class.verification_failed_batch(batch_size: 4)).to match_array([subject.model_record_id])
end
it 'excludes rows which are not synced or have not failed verification' do
# rubocop:disable Rails/SaveBang
create(registry_class_factory, verification_state: verification_state_value(:verification_failed), verification_failure: 'foo')
create(registry_class_factory, :started, verification_state: verification_state_value(:verification_failed), verification_failure: 'foo')
create(registry_class_factory, :failed, verification_state: verification_state_value(:verification_failed), verification_failure: 'foo')
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_started))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_succeeded), verification_checksum: 'abc123')
# rubocop:enable Rails/SaveBang
expect(described_class.verification_failed_batch(batch_size: 4)).to match_array([subject.model_record_id])
end
it 'marks verification as started' do
described_class.verification_failed_batch(batch_size: 4)
expect(subject.reload.verification_started?).to be_truthy
expect(subject.verification_started_at).to be_present
end
end
describe '.needs_verification_count' do
before do
subject.save!
end
it 'returns the number of rows which are synced and (pending or failed) verification' do
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_failed), verification_failure: 'foo') # rubocop:disable Rails/SaveBang
expect(described_class.needs_verification_count(limit: 3)).to eq(2)
end
it 'excludes rows which are not synced or are not (pending or failed) verification' do
# rubocop:disable Rails/SaveBang
create(registry_class_factory, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :started, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :failed, verification_state: verification_state_value(:verification_pending))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_started))
create(registry_class_factory, :synced, verification_state: verification_state_value(:verification_succeeded), verification_checksum: 'abc123')
# rubocop:enable Rails/SaveBang
expect(described_class.needs_verification_count(limit: 3)).to eq(1)
end
end
describe '#verification_succeeded!', :aggregate_failures do
before do
subject.verification_started!
......@@ -108,4 +192,8 @@ RSpec.shared_examples 'a Geo verifiable registry' do
end
end
end
def verification_state_value(key)
described_class.verification_state_value(key)
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