From f165d33146fc4d023492e11e20b5280e823cd570 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Tue, 14 Dec 2021 09:56:17 +0300 Subject: [PATCH] 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 --- crud/stats/init.lua | 33 ++++++++++++++++++++++++++++++++- test/entrypoint/srv_stats.lua | 1 + test/integration/stats_test.lua | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crud/stats/init.lua b/crud/stats/init.lua index 6208d278d..e415c4bc5 100644 --- a/crud/stats/init.lua +++ b/crud/stats/init.lua @@ -6,6 +6,8 @@ local clock = require('clock') local checks = require('checks') local fiber = require('fiber') 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') @@ -109,6 +111,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/ @@ -179,11 +197,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) diff --git a/test/entrypoint/srv_stats.lua b/test/entrypoint/srv_stats.lua index d9649b702..b8bd813fb 100755 --- a/test/entrypoint/srv_stats.lua +++ b/test/entrypoint/srv_stats.lua @@ -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', { diff --git a/test/integration/stats_test.lua b/test/integration/stats_test.lua index cf937ca83..9da069771 100644 --- a/test/integration/stats_test.lua +++ b/test/integration/stats_test.lua @@ -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' @@ -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)