Commit f5b96fb2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'mk/refactor-request-service' into 'master'

Refactor usages of Geo RequestService

See merge request gitlab-org/gitlab!48420
parents 2fdd4ce2 e28eff4f
...@@ -29,7 +29,7 @@ module Geo ...@@ -29,7 +29,7 @@ module Geo
end end
def send_status_to_primary(node, status) def send_status_to_primary(node, status)
if !NodeStatusRequestService.new.execute(status) && prometheus_enabled? if !NodeStatusRequestService.new(status).execute && prometheus_enabled?
increment_failed_status_counter(node) increment_failed_status_counter(node)
end end
end end
......
...@@ -4,7 +4,13 @@ module Geo ...@@ -4,7 +4,13 @@ module Geo
class NodeStatusRequestService < RequestService class NodeStatusRequestService < RequestService
include Gitlab::Geo::LogHelpers include Gitlab::Geo::LogHelpers
def execute(status) attr_reader :status
def initialize(status)
@status = status
end
def execute
return false unless primary_node.present? return false unless primary_node.present?
super(primary_status_url, payload(status)) super(primary_status_url, payload(status))
......
...@@ -4,7 +4,13 @@ module Geo ...@@ -4,7 +4,13 @@ module Geo
class ReplicationToggleRequestService < RequestService class ReplicationToggleRequestService < RequestService
include Gitlab::Geo::LogHelpers include Gitlab::Geo::LogHelpers
def execute(enabled:) attr_reader :enabled
def initialize(enabled:)
@enabled = enabled
end
def execute
return false unless primary_node.present? return false unless primary_node.present?
success = super(primary_node_api_url, payload(enabled), method: Net::HTTP::Put) success = super(primary_node_api_url, payload(enabled), method: Net::HTTP::Put)
......
namespace :geo do namespace :geo do
namespace :replication do namespace :replication do
task pause: :gitlab_environment do task pause: :gitlab_environment do
Geo::ReplicationToggleRequestService.new.execute(enabled: false) Geo::ReplicationToggleRequestService.new(enabled: false).execute
end end
task resume: :gitlab_environment do task resume: :gitlab_environment do
Geo::ReplicationToggleRequestService.new.execute(enabled: true) Geo::ReplicationToggleRequestService.new(enabled: true).execute
end end
end end
end end
...@@ -14,7 +14,7 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do ...@@ -14,7 +14,7 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do
end end
it_behaves_like 'a geo RequestService' do it_behaves_like 'a geo RequestService' do
let(:args) { secondary.find_or_build_status } subject { described_class.new(secondary.find_or_build_status) }
end end
describe '#execute' do describe '#execute' do
...@@ -23,6 +23,13 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do ...@@ -23,6 +23,13 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do
end end
it 'does not include id in the payload' do it 'does not include id in the payload' do
args = GeoNodeStatus.new({
geo_node_id: secondary.id,
status_message: nil,
db_replication_lag_seconds: 0,
projects_count: 10
})
expect(Gitlab::HTTP).to receive(:perform_request) expect(Gitlab::HTTP).to receive(:perform_request)
.with( .with(
Net::HTTP::Post, Net::HTTP::Post,
...@@ -30,15 +37,17 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do ...@@ -30,15 +37,17 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do
hash_including(body: hash_not_including('id'))) hash_including(body: hash_not_including('id')))
.and_return(double(success?: true)) .and_return(double(success?: true))
subject.execute(GeoNodeStatus.new({ described_class.new(args).execute
end
it 'sends geo_node_id in the request' do
args = GeoNodeStatus.new({
geo_node_id: secondary.id, geo_node_id: secondary.id,
status_message: nil, status_message: nil,
db_replication_lag_seconds: 0, db_replication_lag_seconds: 0,
projects_count: 10 projects_count: 10
})) })
end
it 'sends geo_node_id in the request' do
expect(Gitlab::HTTP).to receive(:perform_request) expect(Gitlab::HTTP).to receive(:perform_request)
.with( .with(
Net::HTTP::Post, Net::HTTP::Post,
...@@ -46,15 +55,12 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do ...@@ -46,15 +55,12 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do
hash_including(body: hash_including('geo_node_id' => secondary.id))) hash_including(body: hash_including('geo_node_id' => secondary.id)))
.and_return(double(success?: true)) .and_return(double(success?: true))
subject.execute(GeoNodeStatus.new({ described_class.new(args).execute
geo_node_id: secondary.id,
status_message: nil,
db_replication_lag_seconds: 0,
projects_count: 10
}))
end end
it 'sends all of the data in the status JSONB field in the request' do it 'sends all of the data in the status JSONB field in the request' do
args = create(:geo_node_status, :healthy)
expect(Gitlab::HTTP).to receive(:perform_request) expect(Gitlab::HTTP).to receive(:perform_request)
.with( .with(
Net::HTTP::Post, Net::HTTP::Post,
...@@ -65,7 +71,7 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do ...@@ -65,7 +71,7 @@ RSpec.describe Geo::NodeStatusRequestService, :geo do
*GeoNodeStatus::RESOURCE_STATUS_FIELDS)))) *GeoNodeStatus::RESOURCE_STATUS_FIELDS))))
.and_return(double(success?: true)) .and_return(double(success?: true))
subject.execute(create(:geo_node_status, :healthy)) described_class.new(args).execute
end end
end end
end end
...@@ -8,7 +8,8 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do ...@@ -8,7 +8,8 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do
let_it_be(:secondary) { create(:geo_node) } let_it_be(:secondary) { create(:geo_node) }
let_it_be(:primary) { create(:geo_node, :primary) } let_it_be(:primary) { create(:geo_node, :primary) }
let(:args) { { enabled: false } }
subject { described_class.new(enabled: false) }
before do before do
stub_current_geo_node(secondary) stub_current_geo_node(secondary)
...@@ -22,7 +23,7 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do ...@@ -22,7 +23,7 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do
allow(Gitlab::HTTP).to receive(:perform_request).and_return(response) allow(Gitlab::HTTP).to receive(:perform_request).and_return(response)
expect(Gitlab::Geo).to receive(:expire_cache!) expect(Gitlab::Geo).to receive(:expire_cache!)
expect(subject.execute(**args)).to be_truthy expect(subject.execute).to be_truthy
end end
it 'does not expire the geo cache on failure' do it 'does not expire the geo cache on failure' do
...@@ -34,6 +35,6 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do ...@@ -34,6 +35,6 @@ RSpec.describe Geo::ReplicationToggleRequestService, :geo do
allow(Gitlab::HTTP).to receive(:perform_request).and_return(response) allow(Gitlab::HTTP).to receive(:perform_request).and_return(response)
expect(Gitlab::Geo).not_to receive(:expire_cache!) expect(Gitlab::Geo).not_to receive(:expire_cache!)
expect(subject.execute(**args)).to be_falsey expect(subject.execute).to be_falsey
end end
end end
...@@ -6,8 +6,6 @@ RSpec.shared_examples 'a geo RequestService' do ...@@ -6,8 +6,6 @@ RSpec.shared_examples 'a geo RequestService' do
let_it_be(:primary) { create(:geo_node, :primary) } unless method_defined?(:primary) let_it_be(:primary) { create(:geo_node, :primary) } unless method_defined?(:primary)
let(:args) { raise 'args must be supplied in a let variable in order to execute the request' } unless method_defined?(:args)
describe '#execute' do describe '#execute' do
it 'parses a 401 response' do it 'parses a 401 response' do
response = double(success?: false, response = double(success?: false,
...@@ -17,14 +15,14 @@ RSpec.shared_examples 'a geo RequestService' do ...@@ -17,14 +15,14 @@ RSpec.shared_examples 'a geo RequestService' do
allow(Gitlab::HTTP).to receive(:perform_request).and_return(response) allow(Gitlab::HTTP).to receive(:perform_request).and_return(response)
expect(subject).to receive(:log_error).with("Could not connect to Geo primary node - HTTP Status Code: 401 Unauthorized\nTest") expect(subject).to receive(:log_error).with("Could not connect to Geo primary node - HTTP Status Code: 401 Unauthorized\nTest")
expect(subject.execute(args)).to be_falsey expect(subject.execute).to be_falsey
end end
it 'alerts on bad SSL certficate' do it 'alerts on bad SSL certficate' do
allow(Gitlab::HTTP).to receive(:perform_request).and_raise(OpenSSL::SSL::SSLError.new('bad certificate')) allow(Gitlab::HTTP).to receive(:perform_request).and_raise(OpenSSL::SSL::SSLError.new('bad certificate'))
expect(subject).to receive(:log_error).with(/Failed to Net::HTTP::(Put|Post) to primary url: /, kind_of(OpenSSL::SSL::SSLError)) expect(subject).to receive(:log_error).with(/Failed to Net::HTTP::(Put|Post) to primary url: /, kind_of(OpenSSL::SSL::SSLError))
expect(subject.execute(args)).to be_falsey expect(subject.execute).to be_falsey
end end
it 'handles connection refused' do it 'handles connection refused' do
...@@ -32,7 +30,7 @@ RSpec.shared_examples 'a geo RequestService' do ...@@ -32,7 +30,7 @@ RSpec.shared_examples 'a geo RequestService' do
expect(subject).to receive(:log_error).with(/Failed to Net::HTTP::(Put|Post) to primary url: /, kind_of(Errno::ECONNREFUSED)) expect(subject).to receive(:log_error).with(/Failed to Net::HTTP::(Put|Post) to primary url: /, kind_of(Errno::ECONNREFUSED))
expect(subject.execute(args)).to be_falsey expect(subject.execute).to be_falsey
end end
it 'returns meaningful error message when primary uses incorrect db key' do it 'returns meaningful error message when primary uses incorrect db key' do
...@@ -43,13 +41,13 @@ RSpec.shared_examples 'a geo RequestService' do ...@@ -43,13 +41,13 @@ RSpec.shared_examples 'a geo RequestService' do
kind_of(OpenSSL::Cipher::CipherError) kind_of(OpenSSL::Cipher::CipherError)
) )
expect(subject.execute(args)).to be_falsey expect(subject.execute).to be_falsey
end end
it 'gracefully handles case when primary is deleted' do it 'gracefully handles case when primary is deleted' do
primary.destroy! primary.destroy!
expect(subject.execute(args)).to be_falsey expect(subject.execute).to be_falsey
end 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