Skip to content

Commit

Permalink
Resolve space name from id in statistics
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 4, 2022
1 parent a64392b commit dd19759
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crud/stats/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function stats.get(space_name)
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
Expand Down Expand Up @@ -168,6 +168,17 @@ local function wrap_tail(space_name, op, pairs, start_time, call_status, ...)
end
end

-- If space id is provided instead of name, resolve name.
-- 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
if space == nil then
space = utils.get_space(space_name, vshard.router.routeall())
end

space_name = space.name
end

if context_stats ~= nil then
if context_stats.map_reduces ~= nil then
registry.observe_map_reduces(context_stats.map_reduces, space_name)
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
10 changes: 10 additions & 0 deletions test/integration/stats_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local stats_registry_common = require('crud.stats.registry_common')
local g = t.group('stats_integration')
local helpers = require('test.helper')

local space_id = 542
local space_name = 'customers'
local unknown_space_name = 'non_existing_space'
local new_space_name = 'newspace'
Expand Down Expand Up @@ -637,6 +638,15 @@ 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.before_test(
'test_role_reload_do_not_reset_observations',
generate_stats)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/stats_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local utils = require('crud.common.utils')
local g = t.group('stats_unit')
local helpers = require('test.helper')

local space_id = 542
local space_name = 'customers'
local unknown_space_name = 'non_existing_space'

Expand Down Expand Up @@ -528,3 +529,11 @@ g.test_map_reduce_increment = function(g)
t.assert_equals(stats.spaces[space_name][op].details.map_reduces, inc,
"Counter of map reduces incremented")
end

g.test_resolve_name_from_id = function(g)
local op = stats_module.op.LEN
g.router:eval(call_wrapped, { 'return_true', stats_module.op.LEN, {}, space_id })

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

0 comments on commit dd19759

Please sign in to comment.