From 107f92a1036c5c7930a630304581c4f47b68fa2c Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Tue, 14 Dec 2021 09:56:17 +0300 Subject: [PATCH] Resolve space name in statistics `crud.len` supports using space id instead of name. After this patch, stats wrapper support mapping id to name. Part of #224 --- crud/stats/module.lua | 15 +++++++++++++-- test/entrypoint/srv_select.lua | 1 + test/entrypoint/srv_simple_operations.lua | 1 + test/integration/stats_test.lua | 9 +++++++++ test/unit/stats_test.lua | 9 +++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crud/stats/module.lua b/crud/stats/module.lua index dceee8ba..b0716018 100644 --- a/crud/stats/module.lua +++ b/crud/stats/module.lua @@ -118,6 +118,8 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...) end local context_stats = utils.get_context_section('router_stats') + -- Describe local variables to use `goto`. + local space_not_found_msg, space -- If space not exists, do not build a separate collector for it. -- Call request for non-existing space will always result in error. @@ -127,7 +129,7 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...) -- it is treated as unknown as well. if status == 'error' and registry.is_unknown_space(space_name) then if type(err) == 'table' and type(err.err) == 'string' then - local space_not_found_msg = utils.space_doesnt_exist_msg(space_name) + space_not_found_msg = utils.space_doesnt_exist_msg(space_name) if string.find(err.err, space_not_found_msg) ~= nil then registry.observe_space_not_found() goto return_values @@ -137,13 +139,22 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...) -- We can't rely only on parsing error value because space existence -- is not always the first check in request validation. -- Check explicitly if space do not exist. - local space = utils.get_space(space_name, vshard.router.routeall()) + space = utils.get_space(space_name, vshard.router.routeall()) if space == nil then registry.observe_space_not_found() goto return_values end end + -- If space id is provided instead of name, resolve name. + 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 + registry.observe(latency, space_name, op, status) if context_stats ~= nil then diff --git a/test/entrypoint/srv_select.lua b/test/entrypoint/srv_select.lua index 0a1fce68..40b2b815 100755 --- a/test/entrypoint/srv_select.lua +++ b/test/entrypoint/srv_select.lua @@ -24,6 +24,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/entrypoint/srv_simple_operations.lua b/test/entrypoint/srv_simple_operations.lua index 3ea1705f..2748d52d 100755 --- a/test/entrypoint/srv_simple_operations.lua +++ b/test/entrypoint/srv_simple_operations.lua @@ -21,6 +21,7 @@ package.preload['customers-storage'] = function() }, if_not_exists = true, engine = engine, + id = 542, }) customers_space:create_index('id', { parts = { {field = 'id'} }, diff --git a/test/integration/stats_test.lua b/test/integration/stats_test.lua index 7cc5c2ee..9acac603 100644 --- a/test/integration/stats_test.lua +++ b/test/integration/stats_test.lua @@ -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' @@ -560,3 +561,11 @@ for name, case in pairs(select_cases) do 'Expected count of map reduces planned') end 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 diff --git a/test/unit/stats_test.lua b/test/unit/stats_test.lua index 385f1566..3daa77e6 100644 --- a/test/unit/stats_test.lua +++ b/test/unit/stats_test.lua @@ -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' @@ -496,3 +497,11 @@ g.test_disable_stats_after_fetch_callback_get_do_not_break_call = function(g) t.success('No unexpected errors') 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