Skip to content

Commit be9cb46

Browse files
stats: resolve space name from id
`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
1 parent 5ec87c5 commit be9cb46

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

crud/stats/init.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local clock = require('clock')
66
local checks = require('checks')
77
local fiber = require('fiber')
88
local fun = require('fun')
9+
local log = require('log')
10+
local vshard = require('vshard')
911

1012
local dev_checks = require('crud.common.dev_checks')
1113
local stash = require('crud.common.stash')
@@ -109,6 +111,22 @@ function stats.get(space_name)
109111
return registry.get(space_name)
110112
end
111113

114+
local function resolve_space_name(space_id)
115+
local replicasets = vshard.router.routeall()
116+
if next(replicasets) == nil then
117+
log.warn('Failed to resolve space name for stats: no replicasets found')
118+
return nil
119+
end
120+
121+
local space = utils.get_space(space_id, replicasets)
122+
if space == nil then
123+
log.warn('Failed to resolve space name for stats: no space found for id %d', space_id)
124+
return nil
125+
end
126+
127+
return space.name
128+
end
129+
112130
-- Hack to set __gc for a table in Lua 5.1
113131
-- See https://stackoverflow.com/questions/27426704/lua-5-1-workaround-for-gc-metamethod-for-tables
114132
-- or https://habr.com/ru/post/346892/
@@ -179,11 +197,24 @@ local function wrap_pairs_gen(build_latency, space_name, op, gen, param, state)
179197
end
180198

181199
local function wrap_tail(space_name, op, pairs, start_time, call_status, ...)
182-
dev_checks('string', 'string', 'boolean', 'number', 'boolean')
200+
dev_checks('string|number', 'string', 'boolean', 'number', 'boolean')
183201

184202
local finish_time = clock.monotonic()
185203
local latency = finish_time - start_time
186204

205+
-- If space id is provided instead of name, try to resolve name.
206+
-- If resolve have failed, use id as string to observe space.
207+
-- If using space id will be deprecated, remove this code as well,
208+
-- see https://github.com/tarantool/crud/issues/255
209+
if type(space_name) ~= 'string' then
210+
local name = resolve_space_name(space_name)
211+
if name ~= nil then
212+
space_name = name
213+
else
214+
space_name = tostring(space_name)
215+
end
216+
end
217+
187218
if call_status == false then
188219
registry.observe(latency, space_name, op, 'error')
189220
error((...), 2)

test/entrypoint/srv_stats.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package.preload['customers-storage'] = function()
2323
},
2424
if_not_exists = true,
2525
engine = engine,
26+
id = 542,
2627
})
2728
-- primary index
2829
customers_space:create_index('id_index', {

test/integration/stats_test.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ local stats_registry_utils = require('crud.stats.registry_utils')
77
local g = t.group('stats_integration')
88
local helpers = require('test.helper')
99

10+
local space_id = 542
1011
local space_name = 'customers'
12+
local non_existing_space_id = 100500
1113
local non_existing_space_name = 'non_existing_space'
1214
local new_space_name = 'newspace'
1315

@@ -567,6 +569,24 @@ for name, case in pairs(select_cases) do
567569
end
568570

569571

572+
g.test_resolve_name_from_id = function(g)
573+
local op = 'len'
574+
g.router:call('crud.len', { space_id })
575+
576+
local stats = g:get_stats(space_name)
577+
t.assert_not_equals(stats[op], nil, "Statistics is filled by name")
578+
end
579+
580+
581+
g.test_resolve_nonexisting_space_from_id = function(g)
582+
local op = 'len'
583+
g.router:call('crud.len', { non_existing_space_id })
584+
585+
local stats = g:get_stats(tostring(non_existing_space_id))
586+
t.assert_not_equals(stats[op], nil, "Statistics is filled by id as string")
587+
end
588+
589+
570590
g.before_test(
571591
'test_role_reload_do_not_reset_observations',
572592
generate_stats)

0 commit comments

Comments
 (0)