Skip to content

Commit ee5586b

Browse files
Add schema reload counter to stats
Add counter of schema reloads in calls to module statistics. Schema reloads are counted over all spaces and stored in top level, like `space_not_found` counter. ``` crud.stats().schema_reloads --- - 2 ... ``` In metrics, they are stored in `tnt_crud_schema_reloads` counter. Follows up #224
1 parent 36dc9bb commit ee5586b

14 files changed

+249
-34
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ crud.stats()
638638
count: 4
639639
time: 0.000004
640640
space_not_found: 0
641+
schema_reloads: 0
641642
...
642643
crud.stats('my_space')
643644
---
@@ -658,7 +659,9 @@ field will be empty. If no operations has been called for a
658659
space, it will not be represented. Operations called for
659660
spaces not found both on storages and in statistics registry
660661
(including cases when crud can't verify space existence)
661-
will increment `space_not_found` counter.
662+
will increment `space_not_found` counter. Operation calls
663+
that reloaded a schema will increment `schema_reloads`
664+
counter.
662665

663666
Possible statistics operation labels are
664667
`insert` (for `insert` and `insert_object` calls),
@@ -678,7 +681,8 @@ otherwise `latency` is total average.
678681
In [`metrics`](https://www.tarantool.io/en/doc/latest/book/monitoring/)
679682
registry statistics are stored as `tnt_crud_stats` metrics
680683
with `operation`, `status` and `name` labels. Collector
681-
`tnt_crud_space_not_found` stores count of calls to unknown spaces.
684+
`tnt_crud_space_not_found` stores count of calls to unknown spaces,
685+
`tnt_crud_schema_reloads` stores count of schema reloads in calls.
682686
```
683687
metrics:collect()
684688
---

crud/common/fiber_context.lua

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
local fiber = require('fiber')
2+
3+
local fiber_context = {}
4+
5+
function fiber_context.init_section(section)
6+
local storage = fiber.self().storage
7+
if storage[section] == nil then
8+
storage[section] = {}
9+
end
10+
11+
return storage[section]
12+
end
13+
14+
function fiber_context.get_section(section)
15+
return fiber.self().storage[section]
16+
end
17+
18+
function fiber_context.drop_section(section)
19+
fiber.self().storage[section] = nil
20+
end
21+
22+
return fiber_context

crud/common/schema.lua

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local ReloadSchemaError = errors.new_class('ReloadSchemaError', {capture_stack =
99

1010
local const = require('crud.common.const')
1111
local dev_checks = require('crud.common.dev_checks')
12+
local fiber_context = require('crud.common.fiber_context')
1213

1314
local schema = {}
1415

@@ -103,6 +104,13 @@ function schema.wrap_func_reload(func, ...)
103104
end
104105
end
105106

107+
local context_stats = fiber_context.init_section('router_stats')
108+
if context_stats.schema_reloads == nil then
109+
context_stats.schema_reloads = i
110+
else
111+
context_stats.schema_reloads = context_stats.schema_reloads + i
112+
end
113+
106114
return res, err
107115
end
108116

crud/common/utils.lua

-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local errors = require('errors')
22
local ffi = require('ffi')
3-
local fiber = require('fiber')
43
local vshard = require('vshard')
54
local fun = require('fun')
65

@@ -614,21 +613,4 @@ end
614613
-- Do nothing.
615614
function utils.pass() end
616615

617-
function utils.init_context_section(section)
618-
local storage = fiber.self().storage
619-
if storage[section] == nil then
620-
storage[section] = {}
621-
end
622-
623-
return storage[section]
624-
end
625-
626-
function utils.get_context_section(section)
627-
return fiber.self().storage[section]
628-
end
629-
630-
function utils.drop_context_section(section)
631-
fiber.self().storage[section] = nil
632-
end
633-
634616
return utils

crud/select.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local errors = require('errors')
22

3+
local fiber_context = require('crud.common.fiber_context')
34
local utils = require('crud.common.utils')
45
local select_executor = require('crud.select.executor')
56
local select_filters = require('crud.select.filters')
@@ -76,8 +77,8 @@ local function select_on_storage(space_name, index_id, conditions, opts)
7677
cursor = make_cursor(tuples)
7778
end
7879

79-
cursor.stats = utils.get_context_section('storage_stats')
80-
utils.drop_context_section('storage_stats')
80+
cursor.stats = fiber_context.get_section('storage_stats')
81+
fiber_context.drop_section('storage_stats')
8182

8283
-- getting tuples with user defined fields (if `fields` option is specified)
8384
-- and fields that are needed for comparison on router (primary key + scan key)

crud/select/compat/select.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local checks = require('checks')
22
local errors = require('errors')
33
local vshard = require('vshard')
44

5+
local fiber_context = require('crud.common.fiber_context')
56
local utils = require('crud.common.utils')
67
local sharding = require('crud.common.sharding')
78
local dev_checks = require('crud.common.dev_checks')
@@ -113,7 +114,7 @@ local function build_select_iterator(space_name, user_conditions, opts)
113114
return nil, err, true
114115
end
115116
else
116-
local context_stats = utils.init_context_section('router_stats')
117+
local context_stats = fiber_context.init_section('router_stats')
117118
context_stats.map_reduces = 1
118119
end
119120

crud/select/compat/select_old.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local vshard = require('vshard')
44
local fun = require('fun')
55

66
local call = require('crud.common.call')
7+
local fiber_context = require('crud.common.fiber_context')
78
local utils = require('crud.common.utils')
89
local sharding = require('crud.common.sharding')
910
local dev_checks = require('crud.common.dev_checks')
@@ -148,7 +149,7 @@ local function build_select_iterator(space_name, user_conditions, opts)
148149
return nil, err, true
149150
end
150151
else
151-
local context_stats = utils.init_context_section('router_stats')
152+
local context_stats = fiber_context.init_section('router_stats')
152153
context_stats.map_reduces = 1
153154
end
154155

crud/select/executor.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local errors = require('errors')
22

33
local dev_checks = require('crud.common.dev_checks')
4+
local fiber_context = require('crud.common.fiber_context')
45
local select_comparators = require('crud.compare.comparators')
56
local compat = require('crud.common.compat')
67
local has_keydef = compat.exists('tuple.keydef', 'key_def')
@@ -68,7 +69,7 @@ function executor.execute(space, index, filter_func, opts)
6869

6970
opts = opts or {}
7071

71-
local stats = utils.init_context_section('storage_stats')
72+
local stats = fiber_context.init_section('storage_stats')
7273
stats.tuples_lookup = 0
7374
stats.tuples_fetched = 0
7475

crud/stats/local_registry.lua

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function registry.init(opts)
3131
internal.registry = {}
3232
internal.registry.spaces = {}
3333
internal.registry.space_not_found = 0
34+
internal.registry.schema_reloads = 0
3435

3536
return true
3637
end
@@ -188,4 +189,21 @@ function registry.observe_map_reduces(count, space_name)
188189
return true
189190
end
190191

192+
--- Increase statistics of schema reloads
193+
--
194+
-- @function observe_schema_reloads
195+
--
196+
-- @tparam number count
197+
-- Schema reloads performed.
198+
--
199+
-- @treturn boolean Returns true.
200+
--
201+
function registry.observe_schema_reloads(count)
202+
dev_checks('number')
203+
204+
internal.registry.schema_reloads = internal.registry.schema_reloads + count
205+
206+
return true
207+
end
208+
191209
return registry

crud/stats/metrics_registry.lua

+33-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ local metric_name = {
2020
-- Counter collector for spaces not found.
2121
space_not_found = 'tnt_crud_space_not_found',
2222

23+
-- Counter collector for schema reloads.
24+
schema_reloads = 'tnt_crud_schema_reloads',
25+
2326
-- Counter collectors for select/pairs details.
2427
details = {
2528
tuples_fetched = 'tnt_crud_tuples_fetched',
@@ -110,6 +113,10 @@ function registry.init(opts)
110113
metric_name.space_not_found,
111114
'Spaces not found during CRUD calls')
112115

116+
internal.registry[metric_name.schema_reloads] = metrics.counter(
117+
metric_name.schema_reloads,
118+
'Schema reloads performed in operation calls')
119+
113120
internal.registry[metric_name.details.tuples_fetched] = metrics.counter(
114121
metric_name.details.tuples_fetched,
115122
'Tuples fetched from CRUD storages during select/pairs')
@@ -210,6 +217,7 @@ function registry.get(space_name)
210217
local stats = {
211218
spaces = {},
212219
space_not_found = 0,
220+
schema_reloads = 0,
213221
}
214222

215223
-- Fill operation basic statistics values.
@@ -264,9 +272,14 @@ function registry.get(space_name)
264272
return stats.spaces[space_name] or {}
265273
end
266274

267-
local _, obs = next(internal.registry[metric_name.space_not_found]:collect())
268-
if obs ~= nil then
269-
stats.space_not_found = obs.value
275+
local _, not_found_obs = next(internal.registry[metric_name.space_not_found]:collect())
276+
if not_found_obs ~= nil then
277+
stats.space_not_found = not_found_obs.value
278+
end
279+
280+
local _, reload_obs = next(internal.registry[metric_name.schema_reloads]:collect())
281+
if reload_obs ~= nil then
282+
stats.schema_reloads = reload_obs.value
270283
end
271284

272285
return stats
@@ -396,6 +409,23 @@ function registry.observe_map_reduces(count, space_name)
396409
return true
397410
end
398411

412+
--- Increase statistics of schema reloads
413+
--
414+
-- @function observe_schema_reloads
415+
--
416+
-- @tparam number count
417+
-- Schema reloads performed.
418+
--
419+
-- @treturn boolean Returns true.
420+
--
421+
function registry.observe_schema_reloads(count)
422+
dev_checks('number')
423+
424+
internal.registry[metric_name.schema_reloads]:inc(count)
425+
426+
return true
427+
end
428+
399429
-- Workaround for https://github.com/tarantool/metrics/issues/334 .
400430
-- This workaround does not prevent observations reset between role reloads,
401431
-- but it fixes collector unlink from registry. Without this workaround,

crud/stats/module.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local errors = require('errors')
44
local vshard = require('vshard')
55

66
local dev_checks = require('crud.common.dev_checks')
7+
local fiber_context = require('crud.common.fiber_context')
78
local utils = require('crud.common.utils')
89
local op_module = require('crud.stats.operation')
910
local stash = require('crud.stats.stash')
@@ -181,7 +182,7 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...)
181182
err = second_return_val
182183
end
183184

184-
local context_stats = utils.get_context_section('router_stats')
185+
local context_stats = fiber_context.get_section('router_stats')
185186
-- Describe local variables to use `goto`.
186187
local space_not_found_msg, space
187188

@@ -226,7 +227,13 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...)
226227
internal.registry.observe_map_reduces(
227228
context_stats.map_reduces, space_name)
228229
end
229-
utils.drop_context_section('router_stats')
230+
231+
if context_stats.schema_reloads ~= nil then
232+
internal.registry.observe_schema_reloads(
233+
context_stats.schema_reloads)
234+
end
235+
236+
fiber_context.drop_section('router_stats')
230237
end
231238

232239
:: return_values ::

test/entrypoint/srv_stats.lua

+39
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,45 @@ package.preload['customers-storage'] = function()
4040
unique = false,
4141
if_not_exists = true,
4242
})
43+
44+
rawset(_G, 'create_space', function()
45+
local change_customers_space = box.schema.space.create('change_customers', {
46+
format = {
47+
{name = 'id', type = 'unsigned'},
48+
{name = 'bucket_id', type = 'unsigned'},
49+
{name = 'value', type = 'string'},
50+
},
51+
if_not_exists = true,
52+
engine = engine,
53+
})
54+
55+
-- primary index
56+
change_customers_space:create_index('id_index', {
57+
parts = { {field = 'id'} },
58+
if_not_exists = true,
59+
})
60+
end)
61+
62+
rawset(_G, 'create_bucket_id_index', function()
63+
box.space.change_customers:create_index('bucket_id', {
64+
parts = { {field = 'bucket_id'} },
65+
if_not_exists = true,
66+
unique = false,
67+
})
68+
end)
69+
70+
rawset(_G, 'set_value_type_to_unsigned', function()
71+
local new_format = {}
72+
73+
for _, field_format in ipairs(box.space.change_customers:format()) do
74+
if field_format.name == 'value' then
75+
field_format.type = 'unsigned'
76+
end
77+
table.insert(new_format, field_format)
78+
end
79+
80+
box.space.change_customers:format(new_format)
81+
end)
4382
end,
4483
}
4584
end

0 commit comments

Comments
 (0)