Commit 672545b3 authored by Mike Kozono's avatar Mike Kozono

Move update_verification_state! method

...into the Geo::VerificationState concern.
parent 5dbbc40c
......@@ -52,10 +52,10 @@ module Geo
def verify
checksum = model_record.calculate_checksum
update_verification_state!(checksum: checksum)
model_record.update_verification_state!(checksum: checksum)
rescue => e
log_error('Error calculating the checksum', e)
update_verification_state!(failure: e.message)
model_record.update_verification_state!(failure: e.message)
end
# Check if given checksum matches known one
......@@ -83,28 +83,5 @@ module Geo
def secondary_checksum
registry.verification_checksum
end
private
# Update checksum on Geo primary database
#
# @param [String] checksum value generated by the checksum routine
# @param [String] failure (optional) stacktrace from failed execution
def update_verification_state!(checksum: nil, failure: nil)
retry_at, retry_count = calculate_next_retry_attempt if failure.present?
model_record.update!(
verification_checksum: checksum,
verified_at: Time.current,
verification_failure: failure,
verification_retry_at: retry_at,
verification_retry_count: retry_count
)
end
def calculate_next_retry_attempt
retry_count = model_record.verification_retry_count.to_i + 1
[next_retry_time(retry_count), retry_count]
end
end
end
......@@ -92,6 +92,27 @@ module Gitlab
end
end
# Update checksum on Geo primary database
#
# @param [String] checksum value generated by the checksum routine
# @param [String] failure (optional) stacktrace from failed execution
def update_verification_state!(checksum: nil, failure: nil)
retry_at, retry_count = calculate_next_retry_attempt if failure.present?
update!(
verification_checksum: checksum,
verified_at: Time.current,
verification_failure: failure,
verification_retry_at: retry_at,
verification_retry_count: retry_count
)
end
def calculate_next_retry_attempt
retry_count = model_record.verification_retry_count.to_i + 1
[next_retry_time(retry_count), retry_count]
end
# @param [String] message error information
# @param [StandardError] error exception
def set_verification_failure(message, error = nil)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Geo::VerificationState do
include ::EE::GeoHelpers
let_it_be(:primary_node) { create(:geo_node, :primary) }
let_it_be(:secondary_node) { create(:geo_node) }
before(:all) do
create_dummy_model_table
end
after(:all) do
drop_dummy_model_table
end
before do
stub_dummy_replicator_class
stub_dummy_model_class
end
subject { DummyModel.new }
describe '#update_verification_state!' do
before do
subject.save!
end
it 'saves the checksum' do
subject.update_verification_state!(checksum: 'abc123')
expect(subject.reload.verification_checksum).to eq('abc123')
expect(subject.verified_at).not_to be_nil
end
it 'saves the error message and increments retry counter' do
subject.update_verification_state!(failure: 'Failure to calculate checksum')
expect(subject.reload.verification_failure).to eq 'Failure to calculate checksum'
expect(subject.verification_retry_count).to be 1
end
end
end
......@@ -127,24 +127,25 @@ RSpec.shared_examples 'a verifiable replicator' do
model_record.save!
end
it 'calculates the checksum' do
expect(model_record).to receive(:calculate_checksum).and_return('abc123')
context 'when a Geo primary' do
context 'when an error is raised during calculate_checksum!' do
it 'delegates checksum calculation and the state change to model_record' do
expect(model_record).to receive(:calculate_checksum).and_return('abc123')
expect(model_record).to receive(:update_verification_state!).with(checksum: 'abc123')
replicator.verify
expect(model_record.reload.verification_checksum).to eq('abc123')
expect(model_record.verified_at).not_to be_nil
end
replicator.verify
end
it 'saves the error message and increments retry counter' do
allow(model_record).to receive(:calculate_checksum) do
raise StandardError.new('Failure to calculate checksum')
end
it 'passes the error message' do
allow(model_record).to receive(:calculate_checksum) do
raise StandardError.new('Failure to calculate checksum')
end
replicator.verify
expect(model_record).to receive(:update_verification_state!).with(failure: 'Failure to calculate checksum')
expect(model_record.reload.verification_failure).to eq 'Failure to calculate checksum'
expect(model_record.verification_retry_count).to be 1
replicator.verify
end
end
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