Skip to content

Commit

Permalink
Fix filling sharding key cache when sharding key data is incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaNek committed Dec 22, 2021
1 parent 34a9229 commit 0d37a6e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crud/common/sharding/sharding_key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,27 @@ end
function sharding_key_module.construct_as_index_obj_cache(metadata_map, specified_space_name)
dev_checks('table', 'string')

local result_err

cache.sharding_key_as_index_obj_map = {}
for space_name, metadata in pairs(metadata_map) do
if metadata.sharding_key_def ~= nil then
local sharding_key_as_index_obj, err = as_index_object(space_name,
metadata.space_format,
metadata.sharding_key_def)
if err ~= nil then
return err
if specified_space_name == space_name then
result_err = err
else
log.warn(err)
end
end

cache.sharding_key_as_index_obj_map[space_name] = sharding_key_as_index_obj
end
end

return result_err
end

sharding_key_module.internal = {
Expand Down
8 changes: 8 additions & 0 deletions test/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,12 @@ function helpers.update_sharding_key_cache(cluster, space_name)
]], {space_name})
end

function helpers.get_sharding_key_cache(cluster)
return cluster.main_server.net_box:eval([[
local sharding_metadata_cache = require('crud.common.sharding.sharding_metadata_cache')
return sharding_metadata_cache[sharding_metadata_cache.SHARDING_KEY_MAP_NAME]
]])
end

return helpers
73 changes: 73 additions & 0 deletions test/integration/ddl_sharding_key_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,76 @@ pgroup.test_update_cache = function(g)
sharding_key_as_index_obj = helpers.update_sharding_key_cache(g.cluster, space_name)
t.assert_equals(sharding_key_as_index_obj, {parts = {{fieldno = 3}}})
end

pgroup.test_update_cache_with_incorrect_key = function(g)
-- get data from cache for space with correct sharding key
local space_name = 'customers_name_key'

local sharding_key_as_index_obj, err = helpers.update_sharding_key_cache(g.cluster, space_name)
t.assert_equals(err, nil)
t.assert_equals(sharding_key_as_index_obj, {parts = {{fieldno = 3}}})

-- records for all spaces exist
sharding_key_as_index_obj = helpers.get_sharding_key_cache(g.cluster)
t.assert_equals(sharding_key_as_index_obj, {
customers_G_func = {parts = {{fieldno = 1}}},
customers_age_key = {parts = {{fieldno = 4}}},
customers_body_func = {parts = {{fieldno = 1}}},
customers_name_age_key_different_indexes = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_age_key_three_fields_index = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_key = {parts = {{fieldno = 3}}},
customers_name_key_non_uniq_index = {parts = {{fieldno = 3}}},
customers_name_key_uniq_index = {parts = {{fieldno = 3}}},
customers_secondary_idx_name_key = {parts = {{fieldno = 3}}},
})

-- no error just warning
local space_name = 'customers_name_key'
helpers.call_on_servers(g.cluster, {'s1-master', 's2-master'}, function(server)
server.net_box:call('set_sharding_key', {space_name, {'non_existent_field'}})
end)

-- we get no error because we sent request for correct space
local sharding_key_as_index_obj, err = helpers.update_sharding_key_cache(g.cluster, 'customers_age_key')
t.assert_equals(err, nil)
t.assert_equals(sharding_key_as_index_obj, {parts = {{fieldno = 4}}})

-- cache['customers_name_key'] == nil (space with incorrect key)
-- other records for correct spaces exist in cache
sharding_key_as_index_obj = helpers.get_sharding_key_cache(g.cluster)
t.assert_equals(sharding_key_as_index_obj, {
customers_G_func = {parts = {{fieldno = 1}}},
customers_age_key = {parts = {{fieldno = 4}}},
customers_body_func = {parts = {{fieldno = 1}}},
customers_name_age_key_different_indexes = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_age_key_three_fields_index = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_key_non_uniq_index = {parts = {{fieldno = 3}}},
customers_name_key_uniq_index = {parts = {{fieldno = 3}}},
customers_secondary_idx_name_key = {parts = {{fieldno = 3}}},
})

-- get data from cache for space with incorrect sharding key
local space_name = 'customers_name_key'
helpers.call_on_servers(g.cluster, {'s1-master', 's2-master'}, function(server)
server.net_box:call('set_sharding_key', {space_name, {'non_existent_field'}})
end)

-- we get an error because we sent request for incorrect space
local sharding_key_as_index_obj, err = helpers.update_sharding_key_cache(g.cluster, space_name)
t.assert_equals(sharding_key_as_index_obj, nil)
t.assert_str_contains(err.err, "No such field (non_existent_field) in a space format (customers_name_key)")

-- cache['customers_name_key'] == nil (space with incorrect key)
-- other records for correct spaces exist in cache
sharding_key_as_index_obj = helpers.get_sharding_key_cache(g.cluster)
t.assert_equals(sharding_key_as_index_obj, {
customers_G_func = {parts = {{fieldno = 1}}},
customers_age_key = {parts = {{fieldno = 4}}},
customers_body_func = {parts = {{fieldno = 1}}},
customers_name_age_key_different_indexes = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_age_key_three_fields_index = {parts = {{fieldno = 3}, {fieldno = 4}}},
customers_name_key_non_uniq_index = {parts = {{fieldno = 3}}},
customers_name_key_uniq_index = {parts = {{fieldno = 3}}},
customers_secondary_idx_name_key = {parts = {{fieldno = 3}}},
})
end

0 comments on commit 0d37a6e

Please sign in to comment.