diff --git a/crud/common/sharding/router_metadata_cache.lua b/crud/common/sharding/router_metadata_cache.lua index a7c1e099..a6496f00 100644 --- a/crud/common/sharding/router_metadata_cache.lua +++ b/crud/common/sharding/router_metadata_cache.lua @@ -4,14 +4,17 @@ local router_metadata_cache = {} router_metadata_cache.SHARDING_KEY_MAP_NAME = "sharding_key_as_index_obj_map" router_metadata_cache.SHARDING_FUNC_MAP_NAME = "sharding_func_map" +router_metadata_cache.META_HASH_MAP_NAME = "sharding_meta_hash_map" router_metadata_cache[router_metadata_cache.SHARDING_KEY_MAP_NAME] = nil router_metadata_cache[router_metadata_cache.SHARDING_FUNC_MAP_NAME] = nil +router_metadata_cache[router_metadata_cache.META_HASH_MAP_NAME] = {} router_metadata_cache.fetch_lock = fiber.channel(1) router_metadata_cache.is_part_of_pk = {} function router_metadata_cache.drop_caches() router_metadata_cache[router_metadata_cache.SHARDING_KEY_MAP_NAME] = nil router_metadata_cache[router_metadata_cache.SHARDING_FUNC_MAP_NAME] = nil + router_metadata_cache[router_metadata_cache.META_HASH_MAP_NAME] = {} if router_metadata_cache.fetch_lock ~= nil then router_metadata_cache.fetch_lock:close() end diff --git a/crud/common/sharding/sharding_func.lua b/crud/common/sharding/sharding_func.lua index 3ac9d561..6a503b45 100644 --- a/crud/common/sharding/sharding_func.lua +++ b/crud/common/sharding/sharding_func.lua @@ -82,7 +82,12 @@ function sharding_func_module.construct_as_callable_obj_cache(metadata_map, spec local result_err - cache.sharding_func_map = {} + cache[cache.SHARDING_FUNC_MAP_NAME] = {} + local func_cache = cache[cache.SHARDING_FUNC_MAP_NAME] + + cache[cache.META_HASH_MAP_NAME][cache.SHARDING_FUNC_MAP_NAME] = {} + local func_hash_cache = cache[cache.META_HASH_MAP_NAME][cache.SHARDING_FUNC_MAP_NAME] + for space_name, metadata in pairs(metadata_map) do if metadata.sharding_func_def ~= nil then local sharding_func, err = as_callable_object(metadata.sharding_func_def, @@ -96,7 +101,8 @@ function sharding_func_module.construct_as_callable_obj_cache(metadata_map, spec end end - cache.sharding_func_map[space_name] = sharding_func + func_cache[space_name] = sharding_func + func_hash_cache[space_name] = metadata.sharding_func_hash end end diff --git a/crud/common/sharding/sharding_key.lua b/crud/common/sharding/sharding_key.lua index cc5cc7e0..635c3e34 100644 --- a/crud/common/sharding/sharding_key.lua +++ b/crud/common/sharding/sharding_key.lua @@ -102,7 +102,12 @@ function sharding_key_module.construct_as_index_obj_cache(metadata_map, specifie local result_err - cache.sharding_key_as_index_obj_map = {} + cache[cache.SHARDING_KEY_MAP_NAME] = {} + local key_cache = cache[cache.SHARDING_KEY_MAP_NAME] + + cache[cache.META_HASH_MAP_NAME][cache.SHARDING_KEY_MAP_NAME] = {} + local key_hash_cache = cache[cache.META_HASH_MAP_NAME][cache.SHARDING_KEY_MAP_NAME] + 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, @@ -117,7 +122,8 @@ function sharding_key_module.construct_as_index_obj_cache(metadata_map, specifie end end - cache.sharding_key_as_index_obj_map[space_name] = sharding_key_as_index_obj + key_cache[space_name] = sharding_key_as_index_obj + key_hash_cache[space_name] = metadata.sharding_key_hash end end diff --git a/crud/common/sharding/sharding_metadata.lua b/crud/common/sharding/sharding_metadata.lua index 8589f67d..f9a9a4f6 100644 --- a/crud/common/sharding/sharding_metadata.lua +++ b/crud/common/sharding/sharding_metadata.lua @@ -60,6 +60,7 @@ function sharding_metadata_module.fetch_on_storage() local space_format = box.space[space_name]:format() metadata_map[space_name] = { sharding_key_def = sharding_key_def, + sharding_key_hash = storage_cache.get_sharding_key_hash(space_name), space_format = space_format, } end @@ -71,6 +72,7 @@ function sharding_metadata_module.fetch_on_storage() local sharding_func_def = sharding_utils.extract_sharding_func_def(tuple) metadata_map[space_name] = metadata_map[space_name] or {} metadata_map[space_name].sharding_func_def = sharding_func_def + metadata_map[space_name].sharding_func_hash = storage_cache.get_sharding_func_hash(space_name) end end @@ -99,6 +101,10 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name) if metadata_map == nil then cache[cache.SHARDING_KEY_MAP_NAME] = {} cache[cache.SHARDING_FUNC_MAP_NAME] = {} + cache[cache.META_HASH_MAP_NAME] = { + [cache.SHARDING_KEY_MAP_NAME] = {}, + [cache.SHARDING_FUNC_MAP_NAME] = {}, + } return end diff --git a/test/unit/sharding_metadata_test.lua b/test/unit/sharding_metadata_test.lua index bb14d438..e049c28e 100644 --- a/test/unit/sharding_metadata_test.lua +++ b/test/unit/sharding_metadata_test.lua @@ -3,7 +3,9 @@ local ffi = require('ffi') local sharding_metadata_module = require('crud.common.sharding.sharding_metadata') local sharding_key_module = require('crud.common.sharding.sharding_key') local sharding_func_module = require('crud.common.sharding.sharding_func') +local sharding_utils = require('crud.common.sharding.utils') local cache = require('crud.common.sharding.router_metadata_cache') +local storage_cache = require('crud.common.sharding.storage_metadata_cache') local utils = require('crud.common.utils') local helpers = require('test.helper') @@ -55,6 +57,7 @@ g.after_each(function() box.space.fetch_on_storage:drop() cache.drop_caches() + storage_cache.drop_caches() end) g.test_as_index_object_positive = function() @@ -119,7 +122,9 @@ g.test_fetch_sharding_metadata_on_storage_positive = function() t.assert_equals(metadata_map, { [space_name] = { sharding_key_def = sharding_key_def, + sharding_key_hash = sharding_utils.compute_hash(sharding_key_def), sharding_func_def = sharding_func_def, + sharding_func_hash = sharding_utils.compute_hash(sharding_func_def), space_format = {} }, }) @@ -137,6 +142,7 @@ g.test_fetch_sharding_key_on_storage_positive = function() t.assert_equals(metadata_map, { [space_name] = { sharding_key_def = sharding_key_def, + sharding_key_hash = sharding_utils.compute_hash(sharding_key_def), space_format = {} }, }) @@ -154,6 +160,7 @@ g.test_fetch_sharding_func_name_on_storage_positive = function() t.assert_equals(metadata_map, { [space_name] = { sharding_func_def = sharding_func_def, + sharding_func_hash = sharding_utils.compute_hash(sharding_func_def), }, }) end @@ -170,6 +177,7 @@ g.test_fetch_sharding_func_body_on_storage_positive = function() t.assert_equals(metadata_map, { [space_name] = { sharding_func_def = {body = sharding_func_def}, + sharding_func_hash = sharding_utils.compute_hash({body = sharding_func_def}), }, }) end