Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions test/luatest_helpers/vtest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,18 @@ local function cluster_exec_each_master(g, func, args)
end)
end

local function storage_wait_bucket_sync(storage)
storage:exec(function()
ilt.helpers.retrying({timeout = iwait_timeout}, function()
ivshard.storage.master_sync_wakeup()
local storage = ivshard.storage.internal
ilt.assert(storage.is_master and storage.is_bucket_in_sync)
end)
end)
end

local function storage_boot_one_f(first, count)
return ivshard.storage.bucket_force_create(first, count)
return ivshard.storage.bucket_create(first, count)
end

--
Expand Down Expand Up @@ -419,6 +429,8 @@ local function cluster_bootstrap(g, cfg)
replicaset_count = replicaset_count + 1
end
t.assert_not_equals(masters, {}, 'have masters')
local _, sync_err = cluster_for_each_in(masters, storage_wait_bucket_sync)
t.assert_equals(sync_err, nil, 'storage master synchronization')
vrepset.calculate_etalon_balance(etalon_balance, cfg.bucket_count)
local fibers = table.new(0, replicaset_count)
local bid = 1
Expand Down Expand Up @@ -484,19 +496,6 @@ local function storage_get_n_buckets(storage, n)
end, {n})
end

--
-- Wait for _bucket space being synchronized on master. Without synchronization
-- the buckets cannot be sent or received, rebalancer and recovery doesn't work.
--
local function storage_wait_bucket_sync(storage)
storage:exec(function()
ilt.helpers.retrying({timeout = iwait_timeout}, function()
ivshard.storage.master_sync_wakeup()
ilt.assert(ivshard.storage.internal.is_bucket_in_sync)
end)
end)
end

--
-- Disable rebalancer on all storages.
--
Expand Down
118 changes: 112 additions & 6 deletions test/replicaset-luatest/replicaset_2_2_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local vreplicaset = require('vshard.replicaset')
local vtest = require('test.luatest_helpers.vtest')
local test_group = t.group('replicaset-2-2')
local vcfg = require('vshard.cfg')
local verror = require('vshard.error')

local timeout_opts = {timeout = vtest.wait_timeout}

Expand Down Expand Up @@ -43,6 +44,49 @@ test_group.after_all(function(g)
g.cluster:drop()
end)

local function active_connection(call)
return {
state = 'active',
is_connected = function() return true end,
call = call,
}
end

test_group.test_map_call_accounts_ready_replies = function()
for _, fail_on_send in ipairs({false, true}) do
local sets = vreplicaset.buildall(global_cfg)
local sent, failed_id = 0, nil
for id, rs in pairs(sets) do
local future = {
wait_result = function()
failed_id = id
return nil, verror.timeout()
end,
is_ready = function() return id ~= failed_id end,
result = function() return {} end,
discard = function() end,
}
rs.master.conn = active_connection(function()
sent = sent + 1
if fail_on_send and sent == 2 then
failed_id = id
return nil, verror.timeout()
end
return future
end)
end
local res, err, id = vreplicaset.masters_map_call(
sets, 'get_uuid', nil, timeout_opts)
t.assert_equals(res, nil)
t.assert(verror.is_timeout(err))
t.assert_equals(id, failed_id)
for rs_id, rs in pairs(sets) do
t.assert_equals(rs.master.net_sequential_ok,
rs_id == failed_id and 0 or 1)
end
end
end

test_group.test_wait_masters_connect = function()
local replicasets = vreplicaset.buildall(global_cfg)
for _, rs in pairs(replicasets) do
Expand All @@ -66,13 +110,75 @@ test_group.test_masters_map_call = function()
local replicasets = vreplicaset.buildall(global_cfg)
local res = vreplicaset.masters_map_call(
replicasets, 'get_uuid', nil, timeout_opts)
for _, rs in pairs(replicasets) do
local expected = {}
for id, rs in pairs(replicasets) do
t.assert(rs.master:is_connected())
expected[id] = {rs.master.id}
end
t.assert_equals(res, expected)
end

test_group.test_masters_map_call_no_replies = function()
local replicasets = vreplicaset.buildall(global_cfg)
for _, rs in pairs(replicasets) do
rs.master.uri = rs.master.uri .. '.offline'
end
local count = 0
for rs_id, uuid in pairs(res) do
t.assert(replicasets[rs_id].master.id, uuid)
count = count + 1
local res, err, err_id = vreplicaset.masters_map_call(
replicasets, 'get_uuid', nil, {coverage = 'accidental', timeout = 0.1})
for _, rs in pairs(replicasets) do
rs.master.conn:close()
end
t.assert_equals(res, {})
t.assert(err and verror.is_timeout(err))
t.assert(replicasets[err_id])
end

test_group.test_masters_map_call_default_timeout = function()
local replicasets = vreplicaset.buildall(global_cfg)
local future = {wait_result = function(_, timeout) return {timeout} end}
for _, rs in pairs(replicasets) do
rs.master.conn = active_connection(function() return future end)
end
local a = next(replicasets)
local b = next(replicasets, a)
replicasets[a].master.net_timeout = 0.5
replicasets[b].master.net_timeout = 2
local res = vreplicaset.masters_map_call(
replicasets, 'echo_timeout', nil, {})
t.assert_almost_equals(res[a][1], 2, 1e-3)
t.assert_almost_equals(res[b][1], 2, 1e-3)
res = vreplicaset.masters_map_call(replicasets, 'echo_timeout', nil,
{coverage = 'accidental'})
t.assert_almost_equals(res[a][1], 0.5, 1e-3)
t.assert_almost_equals(res[b][1], 2, 1e-3)
end

test_group.test_masters_map_call_reply_timeout = function()
local id, rs = next(vreplicaset.buildall(global_cfg))
local future = {
wait_result = function() return nil, verror.timeout() end,
is_ready = function() return false end,
discard = function() end,
}
rs.master.conn = active_connection(function() return future end)
local timeout = rs.master.net_timeout
for _ = 1, 2 do
local _, err = vreplicaset.masters_map_call(
{[id] = rs}, 'get_uuid', nil, {timeout = timeout})
t.assert(err and verror.is_timeout(err))
end
t.assert_equals(rs.master.net_timeout, timeout * 2)
end

test_group.test_masters_map_call_missing_master = function()
local id, rs = next(vreplicaset.buildall(global_cfg))
rs.wait_connected = function(self, timeout)
self.master = nil
return timeout
end
t.assert_equals(count, 2)
local res, err, err_id = vreplicaset.masters_map_call(
{[id] = rs}, 'get_uuid', nil, timeout_opts)
t.assert_equals(res, nil)
t.assert_equals(err.code, verror.code.MISSING_MASTER)
t.assert_equals(err_id, id)
end
17 changes: 17 additions & 0 deletions test/replicaset-luatest/vconnect_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,20 @@ test_group.test_wait_connected_waits_for_vconnect = function()
rs:wait_connected(vtest.wait_timeout)
t.assert(rs.master.conn.vconnect.future:is_ready())
end

test_group.test_wait_connected_on_active_connection = function()
local _, rs = next(vreplicaset.buildall(global_cfg))
rs.master.conn = {
state = 'active',
is_connected = function() return true end,
vconnect = {future_cond = fiber.cond()},
}
local remaining, err = rs:wait_connected(0)
t.assert_equals(remaining, nil)
t.assert(verror.is_timeout(err))
local id
remaining, err, id = rs:wait_connected_all({timeout = 0})
t.assert_equals(remaining, nil)
t.assert(verror.is_timeout(err))
t.assert_equals(id, rs.master.id)
end
166 changes: 166 additions & 0 deletions test/router-luatest/bootstrap_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
local t = require('luatest')
local verror = require('vshard.error')
local vtest = require('test.luatest_helpers.vtest')

local g = t.group('bootstrap')

g.before_each(function(g)
g.cfg = vtest.config_new({
sharding = {
{replicas = {a = {master = true}, a2 = {}}},
{replicas = {b = {master = true}}},
},
bucket_count = 2,
rebalancer_mode = 'off',
})
vtest.cluster_new(g, g.cfg)
vtest.router_new(g, 'router', g.cfg)
vtest.storage_wait_bucket_sync(g.a)
vtest.storage_wait_bucket_sync(g.b)
end)

g.after_each(function(g)
g.cluster:drop()
g.cluster = nil
end)

g.test_bootstrap_preserves_count_error = function(g)
-- Fail the first count; the other replicaset is empty.
local first = g.router:exec(function()
return (next(ivshard.router.routeall()))
end)
local storage = first == g.a:replicaset_uuid() and g.a or g.b
storage:exec(function() ivshard.storage.disable() end)
local res, err = g.router:exec(function()
return ivshard.router.bootstrap({if_not_bootstrapped = true,
timeout = iwait_timeout})
end)
t.assert_equals(res, nil)
t.assert_equals(err and err.code, verror.code.STORAGE_IS_DISABLED)
end

local function check_reconfigured_during_count(g, populated)
if populated then
g.b:exec(function() ilt.assert(ivshard.storage.bucket_create(1)) end)
end
local cfg = table.deepcopy(g.cfg)
cfg.sharding[g.b:replicaset_uuid()] = nil
cfg.connection_outdate_delay = 60
g.router:exec(function(cfg, populated)
ivtest.clear_test_cfg_options(cfg)
local rs = require('vshard.replicaset')
local call = rs.masters_map_call
rs.masters_map_call = function(...)
local results, err = call(...)
ivshard.router.cfg(cfg)
return results, err
end
local ok, res, err = pcall(ivshard.router.bootstrap,
{if_not_bootstrapped = populated})
rs.masters_map_call = call
ilt.assert(ok, res)
ilt.assert_equals(res, nil)
ilt.assert_equals(err.code, iverror.code.OBJECT_IS_OUTDATED)
end, {cfg, populated})
t.assert_equals(g.a:eval('return box.space._bucket:count()'), 0)
t.assert_equals(g.b:eval('return box.space._bucket:count()'),
populated and 1 or 0)
end

g.test_bootstrap_reconfigured = function(g)
check_reconfigured_during_count(g, false)
end

g.test_idempotent_bootstrap_reconfigured = function(g)
check_reconfigured_during_count(g, true)
end

local function check_reconfigured_during_create(g, last_write)
local last_id = g.router:exec(function(cfg, last_write)
ivtest.clear_test_cfg_options(cfg)
local sets = ivshard.router.routeall()
local first = next(sets)
local last = next(sets, first)
cfg.sharding[last] = nil
cfg.connection_outdate_delay = 60
local rs = sets[last_write and last or first]
local callrw = rs.callrw
rs.callrw = function(...)
local ok, err = callrw(...)
ivshard.router.cfg(cfg)
return ok, err
end
local res, err = ivshard.router.bootstrap()
ilt.assert_equals(res, nil)
ilt.assert_equals(err.code, iverror.code.OBJECT_IS_OUTDATED)
return last
end, {g.cfg, last_write})
for _, storage in ipairs({g.a, g.b}) do
local skipped = not last_write and storage:replicaset_uuid() == last_id
t.assert_equals(storage:eval('return box.space._bucket:count()'),
skipped and 0 or 1)
end
end

g.test_reconfigured_during_first_create = function(g)
check_reconfigured_during_create(g, false)
end

g.test_reconfigured_during_last_create = function(g)
check_reconfigured_during_create(g, true)
end

g.test_bootstrap_unsynced_master = function(g)
-- The promoted replica must not see the bucket until replication resumes.
g.a2:wait_vclock_of(g.a)
g.a2:update_box_cfg({replication = {}})
g.a:exec(function() ilt.assert(ivshard.storage.bucket_create(1)) end)
local replicas = g.cfg.sharding[g.a:replicaset_uuid()].replicas
replicas[g.a:instance_uuid()].master = nil
replicas[g.a2:instance_uuid()].master = true
local isolated_cfg = table.copy(g.cfg)
isolated_cfg.replication = {}
vtest.storage_cfg(g.a2, isolated_cfg)
vtest.router_cfg(g.router, g.cfg)
t.assert_equals(g.a2:eval('return box.space._bucket:count()'), 0)

local function bootstrap()
return ivshard.router.bootstrap({timeout = 1})
end
local res, err = g.router:exec(bootstrap)
t.assert_equals(res, nil)
t.assert_equals(err.code, verror.code.MASTER_NOT_SYNCED)
t.assert_equals(g.b:eval('return box.space._bucket:count()'), 0)

vtest.storage_cfg(g.a2, g.cfg)
vtest.storage_cfg(g.a, g.cfg)
vtest.storage_wait_bucket_sync(g.a2)
res, err = g.router:exec(bootstrap)
t.assert_equals(res, nil)
t.assert_equals(err.code, verror.code.NON_EMPTY)
end

g.test_bootstrap_if_not_bootstrapped_with_unavailable_master = function(g)
g.a:exec(function() ilt.assert(ivshard.storage.bucket_create(1)) end)
g.router:exec(function(id)
local rs = ivshard.router.routeall()[id]
rs.wait_master = function() return nil, iverror.timeout() end
local res, err = ivshard.router.bootstrap({timeout = iwait_timeout})
ilt.assert_equals(res, nil)
ilt.assert(iverror.is_timeout(err))

local cancelled = false
rs.wait_master = function(_, timeout)
local ok = pcall(ifiber.sleep, timeout)
cancelled = not ok
return nil, iverror.timeout()
end
local ok, result = pcall(ivshard.router.bootstrap, {
if_not_bootstrapped = true, timeout = iwait_timeout,
})
rs.wait_master = nil
ilt.assert(ok, result)
ilt.assert_equals(result, true)
ilt.assert(cancelled)
end, {g.b:replicaset_uuid()})
end
8 changes: 8 additions & 0 deletions test/router/boot_replica_first.result
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ vshard.storage.cfg(cfg, instance_uuid)
| ---
| ...

test_run:switch('box_1_c')
| ---
| - true
| ...
vshard.storage.cfg(cfg, instance_uuid)
| ---
| ...

test_run:switch('box_1_b')
| ---
| - true
Expand Down
Loading
Loading