Commit 7da6b2df authored by Alex Ives's avatar Alex Ives

Ensure secondary is enabled on failover

- Updated `geo:set_secondary_as_primary` to enabled
  the secondary node when run
- Migrated logic from task to helper so it could be
  tested

Relates to https://gitlab.com/gitlab-org/gitlab/issues/237896
parent 9f5fe5d5
---
title: Ensure secondary is enabled on failover
merge_request: 39615
author:
type: fixed
...@@ -17,6 +17,21 @@ module Gitlab ...@@ -17,6 +17,21 @@ module Gitlab
end end
end end
def set_secondary_as_primary
ActiveRecord::Base.transaction do
primary_node = GeoNode.primary_node
current_node = GeoNode.current_node
abort 'The primary is not set' unless primary_node
abort 'This is not a secondary node' unless current_node.secondary?
primary_node.destroy
current_node.update!(primary: true, enabled: true)
$stdout.puts "#{current_node.url} is now the primary Geo node".color(:green)
end
end
def update_primary_geo_node_url def update_primary_geo_node_url
node = Gitlab::Geo.primary_node node = Gitlab::Geo.primary_node
......
...@@ -190,23 +190,7 @@ namespace :geo do ...@@ -190,23 +190,7 @@ namespace :geo do
task set_secondary_as_primary: :environment do task set_secondary_as_primary: :environment do
abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows? abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows?
ActiveRecord::Base.transaction do Gitlab::Geo::GeoTasks.set_secondary_as_primary
primary_node = GeoNode.primary_node
unless primary_node
abort 'The primary is not set'
end
primary_node.destroy
current_node = GeoNode.current_node
unless current_node.secondary?
abort 'This is not a secondary node'
end
current_node.update!(primary: true)
end
end end
desc 'GitLab | Geo | Update Geo primary node URL' desc 'GitLab | Geo | Update Geo primary node URL'
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Geo::GeoTasks do RSpec.describe Gitlab::Geo::GeoTasks do
include ::EE::GeoHelpers
describe '.set_primary_geo_node' do describe '.set_primary_geo_node' do
before do before do
allow(GeoNode).to receive(:current_node_name).and_return('https://primary.geo.example.com') allow(GeoNode).to receive(:current_node_name).and_return('https://primary.geo.example.com')
...@@ -19,4 +21,48 @@ RSpec.describe Gitlab::Geo::GeoTasks do ...@@ -19,4 +21,48 @@ RSpec.describe Gitlab::Geo::GeoTasks do
expect { subject.set_primary_geo_node }.to output(/Error saving Geo node:/).to_stdout expect { subject.set_primary_geo_node }.to output(/Error saving Geo node:/).to_stdout
end end
end end
describe '.set_secondary_as_primary' do
let_it_be(:primary) { create(:geo_node, :primary) }
let(:secondary) { create(:geo_node) }
before do
stub_current_geo_node(secondary)
stub_current_node_name(secondary.name)
end
it 'aborts if the primary node is not set' do
primary.update_column(:primary, false)
expect(subject).to receive(:abort).with('The primary is not set').and_raise('aborted')
expect { subject.set_secondary_as_primary }.to raise_error('aborted')
end
it 'aborts if run on a node that is not a secondary' do
primary.update_column(:primary, false)
secondary.update!(primary: true)
expect(subject).to receive(:abort).with('This is not a secondary node').and_raise('aborted')
expect { subject.set_secondary_as_primary }.to raise_error('aborted')
end
it 'sets the secondary as the primary node' do
expect(subject).not_to receive(:abort)
expect { subject.set_secondary_as_primary }.to output(/#{secondary.url} is now the primary Geo node/).to_stdout
expect(secondary.reload.primary).to be true
end
it 'sets the secondary as the primary node, even if the secondary is disabled' do
secondary.update_column(:enabled, true)
expect(subject).not_to receive(:abort)
expect { subject.set_secondary_as_primary }.to output(/#{secondary.url} is now the primary Geo node/).to_stdout
expect(secondary.reload.primary).to be true
expect(secondary.reload.enabled?).to be true
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