Skip to content

Commit

Permalink
ddl: fetch sharding info hashes to router
Browse files Browse the repository at this point in the history
Fetch sharding info hashes to router on ddl schema load. Hashes are
stored in router metadata cache together with sharding info.

Part of #212
  • Loading branch information
DifferentialOrange committed Apr 20, 2022
1 parent 1d23e92 commit 64fa2f9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions crud/common/sharding/router_metadata_cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions crud/common/sharding/sharding_func.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
10 changes: 8 additions & 2 deletions crud/common/sharding/sharding_key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions crud/common/sharding/sharding_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions test/unit/sharding_metadata_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = {}
},
})
Expand All @@ -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 = {}
},
})
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 64fa2f9

Please sign in to comment.