Skip to content

Commit

Permalink
feature: remove disable/enable host before rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
isqad committed Jul 5, 2021
1 parent 037a420 commit 7f294eb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
--tty
--format progress
--order random
--fail-fast
3 changes: 3 additions & 0 deletions lib/sphinx/integration/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Helper
end
end

DEFAULT_ROTATION_TIME = 10.seconds

def initialize(options = {})
::ThinkingSphinx.context.define_indexes

Expand Down Expand Up @@ -63,6 +65,7 @@ def index
index.switch_rt if rotate_index

@sphinx.index(index)
sleep(Integer(index.local_options[:rotation_time] || DEFAULT_ROTATION_TIME)) if rotate_index

index.last_indexing_time.write
end
Expand Down
26 changes: 4 additions & 22 deletions lib/sphinx/integration/helper_adapters/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ def logger
end

class Remote < Base
AVG_ROTATION_TIME = 10
AVG_CLOSE_CONNECTIONS_TIME = 10
private_constant :AVG_ROTATION_TIME, :AVG_CLOSE_CONNECTIONS_TIME
AVG_CLOSE_CONNECTIONS_TIME = 10.seconds
private_constant :AVG_CLOSE_CONNECTIONS_TIME

def initialize(*)
super
Expand Down Expand Up @@ -143,32 +142,15 @@ def index(idx)
# Returns nil
def reload(idx)
logger.info "Rotation #{idx.core_name}"
local_rotatition_time = idx.local_options[:rotation_time]

# behaviour by default
return sighup if local_rotatition_time.blank? || hosts.size == 1

hosts.each do |host|
begin
disable_host(host)

@ssh.within(host) { sighup }

sleep(local_rotatition_time)
ensure
enable_host(host)
end
end
hosts.each { |host| @ssh.within(host) { sighup } }
end

private

def sighup
logger.info 'Sending SIGHUP to process. Waiting rotation...'

@ssh.execute('kill', "-SIGHUP `cat #{config.configuration.searchd.pid_file}`")

sleep(AVG_ROTATION_TIME)
logger.info 'SIGHUP sent'
end

def indexer_args
Expand Down
13 changes: 5 additions & 8 deletions spec/sphinx/integration/helper_adapters/remote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:adapter) { described_class.new }

before do
allow_any_instance_of(Sphinx::Integration::HelperAdapters::Remote).to receive(:sleep)
allow_any_instance_of(described_class).to receive(:sleep)
class_double("Sphinx::Integration::HelperAdapters::SshProxy", new: ssh).as_stubbed_const
end

Expand Down Expand Up @@ -39,6 +39,7 @@

describe "#suspend" do
it do
expect(adapter).to receive(:sleep)
adapter.suspend
server_status = Sphinx::Integration::ServerStatus.new(ThinkingSphinx::Configuration.instance.address)
expect(server_status.available?).to be false
Expand Down Expand Up @@ -92,12 +93,8 @@

expect(ssh).to receive(:execute).with("kill", /SIGHUP/).twice

expect(adapter).to receive(:disable_host) do |args|
expect(args).to match(/s\d\.dev/)
end.at_least(:twice)
expect(adapter).to receive(:enable_host) do |args|
expect(args).to match(/s\d\.dev/)
end.at_least(:twice)
expect(adapter).not_to receive(:disable_host)
expect(adapter).not_to receive(:enable_host)

adapter.index(double('Index', core_name: 'index_name', local_options: {rotation_time: 5 * 60}))
end
Expand All @@ -111,7 +108,7 @@
end

it do
expect(ssh).to receive(:within).with('s1.dev').and_yield
expect(ssh).to receive(:within).with('s1.dev').twice.and_yield
expect(ssh).to receive(:execute).
with("indexer", "--config /path/sphinx.conf", "--rotate", "--nohup", 'index_name', exit_status: [0, 2])
expect(ssh).to receive(:execute).
Expand Down
3 changes: 3 additions & 0 deletions spec/sphinx/integration/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let(:adapter) { instance_double("Sphinx::Integration::HelperAdapters::Local") }
let(:default_options) { {sphinx_adapter: adapter} }

before { allow_any_instance_of(described_class).to receive(:sleep) }

describe "#configure" do
it do
expect(ThinkingSphinx::Configuration.instance).to receive(:build).with(/test\.sphinx\.conf/)
Expand All @@ -17,6 +19,7 @@
helper = described_class.new(default_options.merge(rotate: true, indexes: 'model_with_rt'))
expect_any_instance_of(Sphinx::Integration::Mysql::Replayer).to receive(:reset)
expect_any_instance_of(RedisMutex).to receive(:with_lock).and_yield
expect(helper).to receive(:sleep).with(60)
expect(adapter).to receive(:index) do |args|
expect(args.core_name).to eq('model_with_rt_core')
end
Expand Down

0 comments on commit 7f294eb

Please sign in to comment.