Skip to content

Commit

Permalink
stats: resolve space name from id
Browse files Browse the repository at this point in the history
`crud.len` supports using space id instead of name. After this patch,
stats wrapper support mapping id to name.

Since using space id is a questionable pattern (see #255), this commit
may be reverted later.

Part of #224
  • Loading branch information
DifferentialOrange committed Feb 25, 2022
1 parent 5d42d61 commit 5282464
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
34 changes: 33 additions & 1 deletion crud/stats/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
local clock = require('clock')
local checks = require('checks')
local fun = require('fun')
local log = require('log')
local vshard = require('vshard')

local dev_checks = require('crud.common.dev_checks')
local stash = require('crud.common.stash')
local utils = require('crud.common.utils')
local op_module = require('crud.stats.operation')
local registry = require('crud.stats.local_registry')

Expand Down Expand Up @@ -107,6 +110,22 @@ function stats.get(space_name)
return registry.get(space_name)
end

local function resolve_space_name(space_id)
local replicasets = vshard.router.routeall()
if next(replicasets) == nil then
log.warn('Failed to resolve space name for stats: no replicasets found')
return nil
end

local space = utils.get_space(space_id, replicasets)
if space == nil then
log.warn('Failed to resolve space name for stats: no space found for id %d', space_id)
return nil
end

return space.name
end

-- Hack to set __gc for a table in Lua 5.1
-- See https://stackoverflow.com/questions/27426704/lua-5-1-workaround-for-gc-metamethod-for-tables
-- or https://habr.com/ru/post/346892/
Expand Down Expand Up @@ -175,11 +194,24 @@ local function wrap_pairs_gen(build_latency, space_name, op, gen, param, state)
end

local function wrap_tail(space_name, op, pairs, start_time, call_status, ...)
dev_checks('string', 'string', 'boolean', 'number', 'boolean')
dev_checks('string|number', 'string', 'boolean', 'number', 'boolean')

local finish_time = clock.monotonic()
local latency = finish_time - start_time

-- If space id is provided instead of name, try to resolve name.
-- If resolve have failed, use id as string to observe space.
-- If using space id will be deprecated, remove this code as well,
-- see https://github.com/tarantool/crud/issues/255
if type(space_name) ~= 'string' then
local name = resolve_space_name(space_name)
if name ~= nil then
space_name = name
else
space_name = tostring(space_name)
end
end

if call_status == false then
registry.observe(latency, space_name, op, 'error')
error((...), 2)
Expand Down
1 change: 1 addition & 0 deletions test/entrypoint/srv_stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package.preload['customers-storage'] = function()
},
if_not_exists = true,
engine = engine,
id = 542,
})
-- primary index
customers_space:create_index('id_index', {
Expand Down
20 changes: 20 additions & 0 deletions test/integration/stats_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ local stats_registry_utils = require('crud.stats.registry_utils')
local g = t.group('stats_integration')
local helpers = require('test.helper')

local space_id = 542
local space_name = 'customers'
local non_existing_space_id = 100500
local non_existing_space_name = 'non_existing_space'
local new_space_name = 'newspace'

Expand Down Expand Up @@ -567,6 +569,24 @@ for name, case in pairs(select_cases) do
end


g.test_resolve_name_from_id = function(g)
local op = 'len'
g.router:call('crud.len', { space_id })

local stats = g:get_stats(space_name)
t.assert_not_equals(stats[op], nil, "Statistics is filled by name")
end


g.test_resolve_nonexisting_space_from_id = function(g)
local op = 'len'
g.router:call('crud.len', { non_existing_space_id })

local stats = g:get_stats(tostring(non_existing_space_id))
t.assert_not_equals(stats[op], nil, "Statistics is filled by id as string")
end


g.before_test(
'test_role_reload_do_not_reset_observations',
generate_stats)
Expand Down

0 comments on commit 5282464

Please sign in to comment.