Skip to content

Commit

Permalink
tests: use in-built stats instead of custom helper
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 25, 2022
1 parent 162dbc5 commit ded9d55
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 222 deletions.
10 changes: 10 additions & 0 deletions test/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,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
118 changes: 0 additions & 118 deletions test/helpers/storage_stat.lua

This file was deleted.

37 changes: 11 additions & 26 deletions test/integration/count_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local clock = require('clock')
local t = require('luatest')

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

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

g.cluster:start()

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_for_count()
]])
end)
g.cluster:server('router').net_box:eval([[
require('crud').cfg{ stats = true }
]])
end)

pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
Expand Down Expand Up @@ -583,7 +579,8 @@ pgroup.test_count_no_map_reduce = function(g)
},
})

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.count', {
Expand All @@ -594,15 +591,9 @@ pgroup.test_count_no_map_reduce = function(g)
t.assert_equals(err, nil)
t.assert_equals(result, 1)

local stat_b = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
['s-1'] = {
requests = 1,
},
['s-2'] = {
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, 'Count 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 @@ -614,15 +605,9 @@ pgroup.test_count_no_map_reduce = function(g)
t.assert_equals(err, nil)
t.assert_equals(result, 1)

local stat_c = storage_stat.collect(g.cluster)
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
['s-1'] = {
requests = 0,
},
['s-2'] = {
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, 'Count request was not a map reduce')
end

pgroup.test_count_timeout = function(g)
Expand Down
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_for_select()
]])
end)
g.cluster.main_server.net_box:eval([[
require('crud').cfg{ stats = true }
]])
end)

pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
Expand Down Expand Up @@ -367,45 +363,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_for_select()
]])
end)
g.cluster.main_server.net_box:eval([[
require('crud').cfg{ stats = true }
]])
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'] = {
requests = 1,
},
['s-2'] = {
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'] = {
requests = 0,
},
['s-2'] = {
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
Loading

0 comments on commit ded9d55

Please sign in to comment.