Skip to content

Commit

Permalink
test: introduce infrastructure for named config
Browse files Browse the repository at this point in the history
This commit allows vtest to create named config, wich will be introduced
in the following commits. If identification_mode is 'name_as_key', then
names are used instead of UUIDs as keys, replica/replicaset.name is not
specified, as it's forbidden, UUIDs are set as replica/replicaset.uuid.

The commit also introduces new server API: instance/replicaset_name(),
which is meant to be used in tests for Tarantool >= 3.0.0.

Part of tarantool#426

NO_DOC=internal
  • Loading branch information
Serpentian committed Dec 12, 2023
1 parent 22ecef4 commit 937306d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 19 deletions.
8 changes: 8 additions & 0 deletions test/instances/storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ local function wal_sync()
local_meta:replace{'wal_sync'}
end

local function box_vstorage_id(cfg)
if cfg.identification_mode == 'name_as_key' then
return box.info.name
end
return box.info.uuid
end

_G.box_error = box_error
_G.echo = echo
_G.get_uuid = get_uuid
Expand All @@ -170,5 +177,6 @@ _G.bucket_recovery_wait = bucket_recovery_wait
_G.bucket_recovery_pause = bucket_recovery_pause
_G.bucket_recovery_continue = bucket_recovery_continue
_G.wal_sync = wal_sync
_G.box_vstorage_id = box_vstorage_id

_G.ready = true
26 changes: 26 additions & 0 deletions test/luatest_helpers/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ function Server:replicaset_uuid()
return uuid
end

-- Return instance name. Only for Tarantool >= 3.0.0. instance_name may be
-- changed, so opts.update_cache is accepted for updating cached name.
function Server:instance_name(opts)
opts = opts or {}
if self.instance_name_value and not opts.update_cache then
return self.instance_name_value
end
local instance_name = self:exec(function() return box.info.name end)
self.instance_name_value = instance_name
return instance_name
end

-- Return replicaset name. Only for Tarantool >= 3.0.0. replicaset_name may be
-- changed, so opts.update_cache is accepted for updating cached name.
function Server:replicaset_name(opts)
opts = opts or {}
if self.replicaset_name_value and not opts.update_cache then
return self.replicaset_name_value
end
local rs_name = self:exec(function() return box.info.replicaset.name end)
self.replicaset_name_value = rs_name
return rs_name
end

function Server:election_term()
return self:exec(function() return box.info.election.term end)
end
Expand Down Expand Up @@ -245,6 +269,8 @@ function Server:cleanup()
self.instance_id_value = nil
self.instance_uuid_value = nil
self.replicaset_uuid_value = nil
self.instance_name_value = nil
self.replicaset_name_value = nil
end

function Server:drop()
Expand Down
58 changes: 39 additions & 19 deletions test/luatest_helpers/vtest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local fio = require('fio')
local fiber = require('fiber')
local uuid = require('uuid')
local yaml = require('yaml')
local vcfg = require('vshard.cfg')
local vrepset = require('vshard.replicaset')
local log = require('log')

Expand Down Expand Up @@ -87,19 +88,26 @@ local function config_new(templ)
local res = table.deepcopy(templ)
local sharding = {}
res.sharding = sharding
for i, replicaset_templ in pairs(templ.sharding) do
local replicaset_uuid = replicaset_name_to_uuid(i)
local is_named = res.identification_mode == 'name_as_key'
for replicaset_name, replicaset_templ in pairs(templ.sharding) do
local replicas = {}
local replicaset = table.deepcopy(replicaset_templ)
replicaset.replicas = replicas
replicaset.is_ssl = nil
if is_named then
-- Config template must be not index-value table.
assert(type(replicaset_name) ~= 'number')
end
local is_ssl = replicaset_templ.is_ssl
for replica_name, replica_templ in pairs(replicaset_templ.replicas) do
local replica_uuid = replica_name_to_uuid(replica_name)
local replica = table.deepcopy(replica_templ)
replica.port_uri = nil
replica.port_count = nil
replica.name = replica_name
if not is_named then
replica.name = replica_name
-- Note, that autogenerated UUIDs are not set in case of
-- named config. Though, UUIDs can be specified in template.
end

local port_count = replica_templ.port_count
local creds = 'storage:storage@'
Expand Down Expand Up @@ -135,9 +143,13 @@ local function config_new(templ)
}
}
end
replicas[replica_uuid] = replica
local replica_id = is_named and replica_name or
replica_name_to_uuid(replica_name)
replicas[replica_id] = replica
end
sharding[replicaset_uuid] = replicaset
local replicaset_id = is_named and replicaset_name or
replicaset_name_to_uuid(replicaset_name)
sharding[replicaset_id] = replicaset
end
return res
end
Expand All @@ -153,7 +165,10 @@ local function cluster_new(g, cfg)
local masters = {}
local replicas = {}
local master_map = {}
for replicaset_uuid, replicaset in pairs(cfg.sharding) do
local is_named = cfg.identification_mode == 'name_as_key'
for replicaset_id, replicaset in pairs(cfg.sharding) do
local replicaset_uuid, replicaset_name = vcfg.extract_identifiers(
replicaset_id, replicaset, is_named)
-- Luatest depends on box.cfg being ready and listening. Need to
-- configure it before vshard.storage.cfg().
local box_repl = {}
Expand All @@ -165,11 +180,12 @@ local function cluster_new(g, cfg)
-- Speed retries up.
replication_timeout = 0.1,
}
for replica_uuid, replica in pairs(replicaset.replicas) do
local name = replica.name
for replica_id, replica in pairs(replicaset.replicas) do
local replica_uuid, replica_name = vcfg.extract_identifiers(
replica_id, replica, is_named)
box_cfg.instance_uuid = replica_uuid
box_cfg.replicaset_uuid = replicaset_uuid
box_cfg.listen = helpers.instance_uri(replica.name)
box_cfg.listen = helpers.instance_uri(replica_name)
-- Need to specify read-only explicitly to know how is master.
local is_master
if replica.read_only ~= nil then
Expand All @@ -178,23 +194,27 @@ local function cluster_new(g, cfg)
is_master = replica.master
end
if is_master then
local prev_uuid = master_map[replicaset_uuid]
local prev_uuid = master_map[replicaset_id]
if prev_uuid then
error('On bootstrap each replicaset has to have exactly '..
'one master')
end
master_map[replicaset_uuid] = replica_uuid
master_map[replicaset_id] = replica_uuid
end
box_cfg.read_only = not is_master
box_cfg.memtx_use_mvcc_engine = cfg.memtx_use_mvcc_engine
if is_named then
box_cfg.instance_name = replica_name
box_cfg.replicaset_name = replicaset_name
end
local server = g.cluster:build_server({
alias = name,
alias = replica_name,
box_cfg = box_cfg,
}, 'storage.lua')
g[name] = server
g[replica_name] = server
-- VShard specific details to use in various helper functions.
server.vtest = {
name = name,
name = replica_name,
is_storage = true,
}
g.cluster:add_server(server)
Expand Down Expand Up @@ -237,7 +257,7 @@ local function cluster_new(g, cfg)
vexports.deploy_privs(exports, username)
box.schema.user.grant(username, 'replication')
end
ivshard.storage.cfg(cfg, box.info.uuid)
ivshard.storage.cfg(cfg, _G.box_vstorage_id(cfg))

if grant_range ~= nil then
box.schema.user.grant('storage', grant_range, nil, nil,
Expand All @@ -251,7 +271,7 @@ local function cluster_new(g, cfg)
replica:wait_for_readiness()
replica:exec(function(cfg)
ivtest.clear_test_cfg_options(cfg)
ivshard.storage.cfg(cfg, box.info.uuid)
ivshard.storage.cfg(cfg, _G.box_vstorage_id(cfg))
end, {cfg})
end
end
Expand Down Expand Up @@ -438,7 +458,7 @@ local function cluster_cfg(g, cfg)
-- No support yet for dynamic node addition and removal. Only reconfig.
local _, err = cluster_exec_each(g, function(cfg)
ivtest.clear_test_cfg_options(cfg)
return ivshard.storage.cfg(cfg, box.info.uuid)
return ivshard.storage.cfg(cfg, _G.box_vstorage_id(cfg))
end, {cfg})
t.assert_equals(err, nil, 'storage reconfig')
end
Expand Down Expand Up @@ -613,7 +633,7 @@ local function storage_start(storage, cfg)
storage:start()
local _, err = storage:exec(function(cfg)
ivtest.clear_test_cfg_options(cfg)
return ivshard.storage.cfg(cfg, box.info.uuid)
return ivshard.storage.cfg(cfg, _G.box_vstorage_id(cfg))
end, {cfg})
t.assert_equals(err, nil, 'storage cfg on start')
end
Expand Down
21 changes: 21 additions & 0 deletions vshard/cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,29 @@ local function cfg_check(shard_cfg, old_cfg)
return shard_cfg
end

--
-- Extract uuid and name from the replicaset/replica config. If is_named = true,
-- then identification_mode is 'name_as_key', new type of config is used:
-- names becomes identifiers instead of UUIDs, replica/replicaset.name
-- is not specified, UUIDs are specified as replica/replicaset.uuid.
--
local function cfg_extract_identifiers(cfg_key, cfg_value, is_named)
local uuid, name
if is_named then
uuid = cfg_value.uuid
name = cfg_key
assert(cfg_value.name == nil)
else
uuid = cfg_key
name = cfg_value.name
assert(cfg_value.uuid == nil)
end
return uuid, name
end

return {
check = cfg_check,
extract_vshard = cfg_extract_vshard,
extract_box = cfg_extract_box,
extract_identifiers = cfg_extract_identifiers,
}

0 comments on commit 937306d

Please sign in to comment.