|
| 1 | +local checks = require('checks') |
| 2 | +local helpers = require('test.helper') |
| 3 | + |
| 4 | +local storage_stat = {} |
| 5 | + |
| 6 | +-- Wrap crud's select_on_storage() function to count selects |
| 7 | +-- and add storage_stat() function that returns resulting |
| 8 | +-- statistics. |
| 9 | +-- |
| 10 | +-- Call it after crud's initialization. |
| 11 | +function storage_stat.init_on_storage() |
| 12 | + assert(_G._crud.select_on_storage ~= nil) |
| 13 | + |
| 14 | + -- Here we count requests. |
| 15 | + local storage_stat_table = { |
| 16 | + select_requests = 0, |
| 17 | + } |
| 18 | + |
| 19 | + -- Wrap select_on_storage() function. |
| 20 | + local select_on_storage_saved = _G._crud.select_on_storage |
| 21 | + _G._crud.select_on_storage = function(...) |
| 22 | + local requests = storage_stat_table.select_requests |
| 23 | + storage_stat_table.select_requests = requests + 1 |
| 24 | + return select_on_storage_saved(...) |
| 25 | + end |
| 26 | + |
| 27 | + -- Accessor for the statistics. |
| 28 | + rawset(_G, 'storage_stat', function() |
| 29 | + return storage_stat_table |
| 30 | + end) |
| 31 | +end |
| 32 | + |
| 33 | +-- Accumulate statistics from storages. |
| 34 | +-- |
| 35 | +-- The statistics is grouped by replicasets. |
| 36 | +-- |
| 37 | +-- Example of a return value: |
| 38 | +-- |
| 39 | +-- | { |
| 40 | +-- | ['s-1'] = { |
| 41 | +-- | select_requests = 1, |
| 42 | +-- | }, |
| 43 | +-- | ['s-2'] = { |
| 44 | +-- | select_requests = 0, |
| 45 | +-- | }, |
| 46 | +-- | } |
| 47 | +function storage_stat.collect(cluster) |
| 48 | + checks('table') |
| 49 | + |
| 50 | + local res = {} |
| 51 | + |
| 52 | + helpers.call_on_storages(cluster, function(server, replicaset) |
| 53 | + checks('table', 'table') |
| 54 | + |
| 55 | + -- Collect the statistics. |
| 56 | + local storage_stat = server.net_box:call('storage_stat') |
| 57 | + |
| 58 | + -- Initialize if needed. |
| 59 | + if res[replicaset.alias] == nil then |
| 60 | + res[replicaset.alias] = {} |
| 61 | + end |
| 62 | + |
| 63 | + -- Accumulate the collected statistics. |
| 64 | + for key, val in pairs(storage_stat) do |
| 65 | + local old = res[replicaset.alias][key] or 0 |
| 66 | + res[replicaset.alias][key] = old + val |
| 67 | + end |
| 68 | + end) |
| 69 | + |
| 70 | + return res |
| 71 | +end |
| 72 | + |
| 73 | +-- Difference between 'a' and 'b' storage statistics. |
| 74 | +-- |
| 75 | +-- The return value structure is the same as for |
| 76 | +-- storage_stat.collect(). |
| 77 | +function storage_stat.diff(a, b) |
| 78 | + checks('table', 'table') |
| 79 | + |
| 80 | + local diff = table.deepcopy(a) |
| 81 | + |
| 82 | + for replicaset_alias, stat_b in pairs(b) do |
| 83 | + -- Initialize if needed. |
| 84 | + if diff[replicaset_alias] == nil then |
| 85 | + diff[replicaset_alias] = {} |
| 86 | + end |
| 87 | + |
| 88 | + -- Substract 'b' statistics from 'a'. |
| 89 | + for key, val in pairs(stat_b) do |
| 90 | + local old = diff[replicaset_alias][key] or 0 |
| 91 | + diff[replicaset_alias][key] = old - val |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + return diff |
| 96 | +end |
| 97 | + |
| 98 | +return storage_stat |
0 commit comments