2012-09-13 14:41:50 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'flexnbd/fake_source'
|
|
|
|
require 'socket'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
|
|
|
|
class TestWriteDuringMigration < Test::Unit::TestCase
|
|
|
|
def setup
|
2018-02-02 21:34:14 +00:00
|
|
|
@flexnbd = File.expand_path('../../build/flexnbd')
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
raise 'No binary!' unless File.executable?(@flexnbd)
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
@size = 20 * 1024 * 1024 # 20MB
|
|
|
|
@write_data = 'foo!' * 2048 # 8K write
|
2012-09-13 14:41:50 +01:00
|
|
|
@source_port = 9990
|
|
|
|
@dest_port = 9991
|
2018-02-02 21:34:14 +00:00
|
|
|
@source_sock = 'src.sock'
|
|
|
|
@dest_sock = 'dst.sock'
|
|
|
|
@source_file = 'src.file'
|
|
|
|
@dest_file = 'dst.file'
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
[@dst_proc, @src_proc].each do |pid|
|
2018-02-02 21:34:14 +00:00
|
|
|
next unless pid
|
|
|
|
begin
|
|
|
|
Process.kill('KILL', pid)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-20 16:00:56 +01:00
|
|
|
def debug_arg
|
2018-02-02 21:34:14 +00:00
|
|
|
ENV['DEBUG'] ? '--verbose' : ''
|
2013-09-20 16:00:56 +01:00
|
|
|
end
|
|
|
|
|
2012-09-13 14:41:50 +01:00
|
|
|
def launch_servers
|
2018-02-02 21:34:14 +00:00
|
|
|
@dst_proc = fork do
|
2013-09-20 16:00:56 +01:00
|
|
|
cmd = "#{@flexnbd} listen -l 127.0.0.1 -p #{@dest_port} -f #{@dest_file} -s #{@dest_sock} #{debug_arg}"
|
2012-09-13 14:41:50 +01:00
|
|
|
exec cmd
|
2018-02-02 21:34:14 +00:00
|
|
|
end
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
@src_proc = fork do
|
2013-09-20 16:00:56 +01:00
|
|
|
cmd = "#{@flexnbd} serve -l 127.0.0.1 -p #{@source_port} -f #{@source_file} -s #{@source_sock} #{debug_arg}"
|
2012-09-13 14:41:50 +01:00
|
|
|
exec cmd
|
2018-02-02 21:34:14 +00:00
|
|
|
end
|
2012-09-13 14:41:50 +01:00
|
|
|
begin
|
|
|
|
awaiting = nil
|
|
|
|
Timeout.timeout(10) do
|
|
|
|
awaiting = :source
|
2018-02-02 21:34:14 +00:00
|
|
|
sleep 0.1 until File.exist?(@source_sock)
|
2012-09-13 14:41:50 +01:00
|
|
|
awaiting = :dest
|
2018-02-02 21:34:14 +00:00
|
|
|
sleep 0.1 until File.exist?(@dest_sock)
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
case awaiting
|
|
|
|
when :source
|
2018-02-02 21:34:14 +00:00
|
|
|
raise "Couldn't get a source socket."
|
2012-09-13 14:41:50 +01:00
|
|
|
when :dest
|
2018-02-02 21:34:14 +00:00
|
|
|
raise "Couldn't get a destination socket."
|
2012-09-13 14:41:50 +01:00
|
|
|
else
|
2018-02-02 21:34:14 +00:00
|
|
|
raise "Something went wrong I don't understand."
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def make_files
|
2012-09-13 14:41:50 +01:00
|
|
|
FileUtils.touch(@source_file)
|
|
|
|
File.truncate(@source_file, @size)
|
|
|
|
FileUtils.touch(@dest_file)
|
|
|
|
File.truncate(@dest_file, @size)
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
File.open(@source_file, 'wb') { |f| f.write 'a' * @size }
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_mirror
|
2018-02-02 21:34:14 +00:00
|
|
|
UNIXSocket.open(@source_sock) do |sock|
|
|
|
|
sock.write(['mirror', '127.0.0.1', @dest_port.to_s, 'exit'].join("\x0A") + "\x0A\x0A")
|
2012-09-13 14:41:50 +01:00
|
|
|
sock.flush
|
|
|
|
rsp = sock.readline
|
2018-02-02 21:34:14 +00:00
|
|
|
end
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def wait_for_quit
|
|
|
|
Timeout.timeout(10) do
|
|
|
|
start_time = Time.now
|
|
|
|
dst_result = Process.waitpid2(@dst_proc)
|
|
|
|
src_result = Process.waitpid2(@src_proc)
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
2013-09-24 10:11:40 +01:00
|
|
|
end
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
def source_writer
|
2018-02-02 21:34:14 +00:00
|
|
|
client = FlexNBD::FakeSource.new('127.0.0.1', @source_port, 'Timed out connecting')
|
|
|
|
offsets = Range.new(0, (@size - @write_data.size) / 4096).to_a
|
2013-09-24 10:11:40 +01:00
|
|
|
loop do
|
|
|
|
begin
|
|
|
|
client.write(offsets[rand(offsets.size)] * 4096, @write_data)
|
2018-02-02 21:34:14 +00:00
|
|
|
rescue StandardError => err
|
2013-09-24 10:11:40 +01:00
|
|
|
# We expect a broken write at some point, so ignore it
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
def assert_both_sides_identical
|
|
|
|
# puts `md5sum #{@source_file} #{@dest_file}`
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
# Ensure each block matches
|
2018-02-02 21:34:14 +00:00
|
|
|
File.open(@source_file, 'r') do |source|
|
|
|
|
File.open(@dest_file, 'r') do |dest|
|
|
|
|
0.upto(@size / 4096) do |block_num|
|
|
|
|
s_data = source.read(4096)
|
|
|
|
d_data = dest.read(4096)
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
assert s_data == d_data, "Block #{block_num} mismatch!"
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
source.seek(4096, IO::SEEK_CUR)
|
|
|
|
dest.seek(4096, IO::SEEK_CUR)
|
2013-09-24 10:11:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_during_migration
|
2018-02-02 21:34:14 +00:00
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
|
|
Dir.chdir(tmpdir) do
|
|
|
|
make_files
|
2013-09-20 16:00:56 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
launch_servers
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
src_writer = Thread.new { source_writer }
|
2012-09-13 14:41:50 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
start_mirror
|
|
|
|
wait_for_quit
|
2012-09-13 14:41:50 +01:00
|
|
|
src_writer.join
|
2013-09-24 10:11:40 +01:00
|
|
|
assert_both_sides_identical
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-09-20 16:00:56 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
def test_many_clients_during_migration
|
2018-02-02 21:34:14 +00:00
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
|
|
Dir.chdir(tmpdir) do
|
|
|
|
make_files
|
2013-09-20 16:53:30 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
launch_servers
|
2013-09-20 16:00:56 +01:00
|
|
|
|
2013-09-24 10:11:40 +01:00
|
|
|
src_writers_1 = (1..5).collect { Thread.new { source_writer } }
|
2013-09-20 16:00:56 +01:00
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
start_mirror
|
2013-09-24 10:11:40 +01:00
|
|
|
|
|
|
|
src_writers_2 = (1..5).collect { Thread.new { source_writer } }
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
wait_for_quit
|
|
|
|
(src_writers_1 + src_writers_2).each(&:join)
|
2013-09-24 10:11:40 +01:00
|
|
|
assert_both_sides_identical
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|
2018-02-02 21:34:14 +00:00
|
|
|
end end
|
2012-09-13 14:41:50 +01:00
|
|
|
end
|