Skip to content

Commit

Permalink
Use in-built stats instead on helper in tests
Browse files Browse the repository at this point in the history
Use in-built `crud.stats()` info instead on `storage_stat` helper
in tests to track map reduce calls.

Part of #224
  • Loading branch information
DifferentialOrange committed Feb 4, 2022
1 parent e1811b8 commit 1a99f0d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 188 deletions.
10 changes: 10 additions & 0 deletions test/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,14 @@ function helpers.reload_roles(srv)
t.assert_equals({ok, err}, {true, nil})
end

function helpers.get_map_reduces_stat(router, space_name)
return router:eval([[
local stats = require('crud').stats(...)
if stats.select == nil then
return 0
end
return stats.select.details.map_reduces
]], { space_name })
end

return helpers
110 changes: 0 additions & 110 deletions test/helpers/storage_stat.lua

This file was deleted.

40 changes: 15 additions & 25 deletions test/integration/ddl_sharding_key_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local crud = require('crud')
local t = require('luatest')

local helpers = require('test.helper')
local storage_stat = require('test.helpers.storage_stat')

local ok = pcall(require, 'ddl')
if not ok then
Expand Down Expand Up @@ -35,12 +34,9 @@ pgroup.before_all(function(g)
t.assert_equals(type(result), 'table')
t.assert_equals(err, nil)

helpers.call_on_storages(g.cluster, function(server)
server.net_box:eval([[
local storage_stat = require('test.helpers.storage_stat')
storage_stat.init_on_storage()
]])
end)
g.cluster.main_server.net_box:eval([[
crud.enable_stats()
]])
end)

pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
Expand Down Expand Up @@ -349,45 +345,39 @@ for name, case in pairs(cases) do
pgroup[('test_%s_wont_lead_to_map_reduce'):format(name)] = function(g)
case.prepare_data(g, case.space_name)

local stat_a = storage_stat.collect(g.cluster)
local router = g.cluster:server('router').net_box
local map_reduces_before = helpers.get_map_reduces_stat(router, case.space_name)

local result, err = g.cluster.main_server.net_box:call('crud.select', {
local result, err = router:call('crud.select', {
case.space_name, case.conditions
})
t.assert_equals(err, nil)
t.assert_not_equals(result, nil)
t.assert_equals(#result.rows, 1)

local stat_b = storage_stat.collect(g.cluster)

-- Check a number of select() requests made by CRUD on cluster's storages
-- after calling select() on a router. Make sure only a single storage has
-- a single select() request. Otherwise we lead to map-reduce.
local stats = storage_stat.diff(stat_b, stat_a)
t.assert_equals(storage_stat.total(stats), 1, 'Select request was not a map reduce')
local map_reduces_after = helpers.get_map_reduces_stat(router, case.space_name)
local diff = map_reduces_after - map_reduces_before
t.assert_equals(diff, 0, 'Select request was not a map reduce')
end
end

pgroup.test_select_for_part_of_sharding_key_will_lead_to_map_reduce = function(g)
local space_name = 'customers_name_age_key_different_indexes'
prepare_data_name_age_sharding_key(g, space_name)

local stat_a = storage_stat.collect(g.cluster)
local router = g.cluster:server('router').net_box
local map_reduces_before = helpers.get_map_reduces_stat(router, space_name)

local result, err = g.cluster.main_server.net_box:call('crud.select', {
local result, err = router:call('crud.select', {
space_name, {{'==', 'age', 58}},
})
t.assert_equals(err, nil)
t.assert_not_equals(result, nil)
t.assert_equals(#result.rows, 1)

local stat_b = storage_stat.collect(g.cluster)

-- Check a number of select() requests made by CRUD on cluster's storages
-- after calling select() on a router. Make sure it was a map-reduce
-- since we do not have sharding key values in conditions.
local stats = storage_stat.diff(stat_b, stat_a)
t.assert_equals(storage_stat.total(stats), 2, 'Select request was a map reduce')
local map_reduces_after = helpers.get_map_reduces_stat(router, space_name)
local diff = map_reduces_after - map_reduces_before
t.assert_equals(diff, 1, 'Select request was a map reduce')
end

pgroup.test_select_secondary_idx = function(g)
Expand Down
39 changes: 12 additions & 27 deletions test/integration/pairs_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local t = require('luatest')
local crud_utils = require('crud.common.utils')

local helpers = require('test.helper')
local storage_stat = require('test.helpers.storage_stat')

local pgroup = t.group('pairs', {
{engine = 'memtx'},
Expand All @@ -27,12 +26,9 @@ pgroup.before_all(function(g)

g.space_format = g.cluster.servers[2].net_box.space.customers:format()

helpers.call_on_storages(g.cluster, function(server)
server.net_box:eval([[
local storage_stat = require('test.helpers.storage_stat')
storage_stat.init_on_storage()
]])
end)
g.cluster.main_server.net_box:eval([[
crud.enable_stats()
]])
end)

pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
Expand Down Expand Up @@ -842,10 +838,11 @@ pgroup.test_pairs_no_map_reduce = function(g)

table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)

local stat_a = storage_stat.collect(g.cluster)
local router = g.cluster:server('router').net_box
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')

-- Case: no conditions, just bucket id.
local rows = g.cluster.main_server.net_box:eval([[
local rows = router:eval([[
local crud = require('crud')
return crud.pairs(...):totable()
Expand All @@ -858,15 +855,9 @@ pgroup.test_pairs_no_map_reduce = function(g)
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
})

local stat_b = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
['s-1'] = {
select_requests = 1,
},
['s-2'] = {
select_requests = 0,
},
})
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
local diff_1 = map_reduces_after_1 - map_reduces_before
t.assert_equals(diff_1, 0, 'Select request was not a map reduce')

-- Case: EQ on secondary index, which is not in the sharding
-- index (primary index in the case).
Expand All @@ -883,13 +874,7 @@ pgroup.test_pairs_no_map_reduce = function(g)
{4, 1161, 'William', 'White', 81, 'Chicago'},
})

local stat_c = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
['s-1'] = {
select_requests = 0,
},
['s-2'] = {
select_requests = 1,
},
})
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
local diff_2 = map_reduces_after_2 - map_reduces_after_1
t.assert_equals(diff_2, 0, 'Select request was not a map reduce')
end
37 changes: 11 additions & 26 deletions test/integration/select_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local crud = require('crud')
local crud_utils = require('crud.common.utils')

local helpers = require('test.helper')
local storage_stat = require('test.helpers.storage_stat')

local pgroup = t.group('select', {
{engine = 'memtx'},
Expand All @@ -28,12 +27,9 @@ pgroup.before_all(function(g)

g.space_format = g.cluster.servers[2].net_box.space.customers:format()

helpers.call_on_storages(g.cluster, function(server)
server.net_box:eval([[
local storage_stat = require('test.helpers.storage_stat')
storage_stat.init_on_storage()
]])
end)
g.cluster.main_server.net_box:eval([[
crud.enable_stats()
]])
end)

pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
Expand Down Expand Up @@ -1615,7 +1611,8 @@ pgroup.test_select_no_map_reduce = function(g)

table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)

local stat_a = storage_stat.collect(g.cluster)
local router = g.cluster:server('router').net_box
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')

-- Case: no conditions, just bucket id.
local result, err = g.cluster.main_server.net_box:call('crud.select', {
Expand All @@ -1628,15 +1625,9 @@ pgroup.test_select_no_map_reduce = function(g)
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
})

local stat_b = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
['s-1'] = {
select_requests = 1,
},
['s-2'] = {
select_requests = 0,
},
})
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
local diff_1 = map_reduces_after_1 - map_reduces_before
t.assert_equals(diff_1, 0, 'Select request was not a map reduce')

-- Case: EQ on secondary index, which is not in the sharding
-- index (primary index in the case).
Expand All @@ -1650,13 +1641,7 @@ pgroup.test_select_no_map_reduce = function(g)
{4, 1161, 'William', 'White', 81, 'Chicago'},
})

local stat_c = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
['s-1'] = {
select_requests = 0,
},
['s-2'] = {
select_requests = 1,
},
})
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
local diff_2 = map_reduces_after_2 - map_reduces_after_1
t.assert_equals(diff_2, 0, 'Select request was not a map reduce')
end

0 comments on commit 1a99f0d

Please sign in to comment.