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
Sharding key cache contains sharding key structures
separated by space name. Before this fix if sharding key
was incorrect for some space an error was returned and
sharding keys that were after the incorrect one
did not get into the cache. The solution is to
create a variable for an error and write a
message to it if an error occurred for the space
we are working with, output a warning for other spaces.

Part of #237
  • Loading branch information
AnaNek committed Jan 27, 2022
1 parent 92c3fbd commit 83413a5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crud/common/sharding/sharding_key.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local errors = require('errors')
local log = require('log')

local dev_checks = require('crud.common.dev_checks')
local cache = require('crud.common.sharding.sharding_metadata_cache')
Expand Down Expand Up @@ -96,8 +97,10 @@ function sharding_key_module.extract_from_pk(space_name, sharding_key_as_index_o
return extract_from_index(primary_key, primary_index_parts, sharding_key_as_index_obj)
end

function sharding_key_module.construct_as_index_obj_cache(metadata_map)
dev_checks('table')
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
Expand All @@ -106,12 +109,18 @@ function sharding_key_module.construct_as_index_obj_cache(metadata_map)
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
67 changes: 67 additions & 0 deletions test/integration/ddl_sharding_key_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,70 @@ pgroup.test_update_cache = function(g)
t.assert_equals(err, nil)
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_age_key = {parts = {{fieldno = 4}}},
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_age_key = {parts = {{fieldno = 4}}},
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_age_key = {parts = {{fieldno = 4}}},
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 83413a5

Please sign in to comment.