From ce0cfc6d240893aa5033c425596972fc876759ee Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Mon, 22 Jan 2024 15:34:57 +0300 Subject: [PATCH] init: move storage code to a separate file Part of #412 Part of #415 --- crud.lua | 58 +++---------- crud/borders.lua | 4 +- crud/common/sharding/sharding_metadata.lua | 4 +- crud/common/utils.lua | 24 ------ crud/count.lua | 4 +- crud/delete.lua | 4 +- crud/get.lua | 4 +- crud/insert.lua | 4 +- crud/insert_many.lua | 4 +- crud/len.lua | 4 +- crud/readview.lua | 18 ++-- crud/replace.lua | 4 +- crud/replace_many.lua | 4 +- crud/select.lua | 4 +- crud/storage.lua | 98 ++++++++++++++++++++++ crud/storage_info.lua | 4 +- crud/truncate.lua | 4 +- crud/update.lua | 4 +- crud/upsert.lua | 4 +- crud/upsert_many.lua | 4 +- 20 files changed, 135 insertions(+), 127 deletions(-) create mode 100644 crud/storage.lua diff --git a/crud.lua b/crud.lua index 1efa46b6..60301163 100644 --- a/crud.lua +++ b/crud.lua @@ -17,12 +17,12 @@ local truncate = require('crud.truncate') local len = require('crud.len') local count = require('crud.count') local borders = require('crud.borders') -local sharding_metadata = require('crud.common.sharding.sharding_metadata') local utils = require('crud.common.utils') local stats = require('crud.stats') local readview = require('crud.readview') local schema = require('crud.schema') local storage_info = require('crud.storage_info') +local storage = require('crud.storage') local crud = {} @@ -158,49 +158,6 @@ crud.readview = readview.new -- @function schema crud.schema = schema.call ---- Initializes crud on node --- --- Exports all functions that are used for calls --- and CRUD operations. --- --- @function init --- -function crud.init_storage() - if type(box.cfg) ~= 'table' then - error('box.cfg() must be called first') - end - - local user = nil - if not box.info.ro then - user = utils.get_this_replica_user() or 'guest' - end - - if rawget(_G, utils.STORAGE_NAMESPACE) == nil then - rawset(_G, utils.STORAGE_NAMESPACE, {}) - end - - insert.init(user) - insert_many.init(user) - get.init(user) - replace.init(user) - replace_many.init(user) - update.init(user) - upsert.init(user) - upsert_many.init(user) - delete.init(user) - select.init(user) - truncate.init(user) - len.init(user) - count.init(user) - borders.init(user) - sharding_metadata.init(user) - readview.init(user) - - -- Must be initialized last: properly working storage info is the flag - -- of initialization success. - storage_info.init(user) -end - function crud.init_router() rawset(_G, 'crud', crud) end @@ -209,8 +166,15 @@ function crud.stop_router() rawset(_G, 'crud', nil) end -function crud.stop_storage() - rawset(_G, utils.STORAGE_NAMESPACE, nil) -end +--- Initializes crud on node +-- +-- Exports all functions that are used for calls +-- and CRUD operations. +-- +-- @function init +-- +crud.init_storage = storage.init + +crud.stop_storage = storage.stop return crud diff --git a/crud/borders.lua b/crud/borders.lua index 6e94d8a7..c78780f8 100644 --- a/crud/borders.lua +++ b/crud/borders.lua @@ -43,9 +43,7 @@ local function get_border_on_storage(border_name, space_name, index_id, field_na }) end -function borders.init(user) - utils.init_storage_call(user, STAT_FUNC_NAME, get_border_on_storage) -end +borders.storage_api = {[STAT_FUNC_NAME] = get_border_on_storage} local is_closer diff --git a/crud/common/sharding/sharding_metadata.lua b/crud/common/sharding/sharding_metadata.lua index 10e77e09..8fe5c858 100644 --- a/crud/common/sharding/sharding_metadata.lua +++ b/crud/common/sharding/sharding_metadata.lua @@ -214,8 +214,6 @@ function sharding_metadata_module.reload_sharding_cache(vshard_router, space_nam end end -function sharding_metadata_module.init(user) - utils.init_storage_call(user, FETCH_FUNC_NAME, sharding_metadata_module.fetch_on_storage) -end +sharding_metadata_module.storage_api = {[FETCH_FUNC_NAME] = sharding_metadata_module.fetch_on_storage} return sharding_metadata_module diff --git a/crud/common/utils.lua b/crud/common/utils.lua index aec3377f..8eee319c 100644 --- a/crud/common/utils.lua +++ b/crud/common/utils.lua @@ -1130,30 +1130,6 @@ function utils.list_slice(list, start_index, end_index) return slice end ---- Initializes a storage function by its name. --- --- It adds the function into the global scope by its name and required --- access to a vshard storage user. --- --- @function init_storage_call --- --- @param string name of a user or nil if there is no need to setup access. --- @param string name a name of the function. --- @param function func the function. --- --- @return nil -function utils.init_storage_call(user, name, func) - dev_checks('?string', 'string', 'function') - - rawset(_G[utils.STORAGE_NAMESPACE], name, func) - - if user ~= nil then - name = utils.get_storage_call(name) - box.schema.func.create(name, {setuid = true, if_not_exists = true}) - box.schema.user.grant(user, 'execute', 'function', name, {if_not_exists=true}) - end -end - local expected_vshard_api = { 'routeall', 'route', 'bucket_id_strcrc32', 'callrw', 'callro', 'callbro', 'callre', diff --git a/crud/count.lua b/crud/count.lua index 470bcce5..be6cd589 100644 --- a/crud/count.lua +++ b/crud/count.lua @@ -86,9 +86,7 @@ local function count_on_storage(space_name, index_id, conditions, opts) return tuples_count end -function count.init(user) - utils.init_storage_call(user, COUNT_FUNC_NAME, count_on_storage) -end +count.storage_api = {[COUNT_FUNC_NAME] = count_on_storage} local check_count_safety_rl = ratelimit.new() local function check_count_safety(space_name, plan, opts) diff --git a/crud/delete.lua b/crud/delete.lua index 1916c14e..cb4d5cee 100644 --- a/crud/delete.lua +++ b/crud/delete.lua @@ -52,9 +52,7 @@ local function delete_on_storage(space_name, key, field_names, opts) }) end -function delete.init(user) - utils.init_storage_call(user, DELETE_FUNC_NAME, delete_on_storage) -end +delete.storage_api = {[DELETE_FUNC_NAME] = delete_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/get.lua b/crud/get.lua index 2c154ddf..4d4ffc96 100644 --- a/crud/get.lua +++ b/crud/get.lua @@ -50,9 +50,7 @@ local function get_on_storage(space_name, key, field_names, opts) }) end -function get.init(user) - utils.init_storage_call(user, GET_FUNC_NAME, get_on_storage) -end +get.storage_api = {[GET_FUNC_NAME] = get_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/insert.lua b/crud/insert.lua index c04cca24..d8b848a0 100644 --- a/crud/insert.lua +++ b/crud/insert.lua @@ -53,9 +53,7 @@ local function insert_on_storage(space_name, tuple, opts) }) end -function insert.init(user) - utils.init_storage_call(user, INSERT_FUNC_NAME, insert_on_storage) -end +insert.storage_api = {[INSERT_FUNC_NAME] = insert_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/insert_many.lua b/crud/insert_many.lua index 87c89f86..f4299fd6 100644 --- a/crud/insert_many.lua +++ b/crud/insert_many.lua @@ -123,9 +123,7 @@ local function insert_many_on_storage(space_name, tuples, opts) return inserted_tuples, nil, replica_schema_version end -function insert_many.init(user) - utils.init_storage_call(user, INSERT_MANY_FUNC_NAME, insert_many_on_storage) -end +insert_many.storage_api = {[INSERT_MANY_FUNC_NAME] = insert_many_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/len.lua b/crud/len.lua index 6fcc7b2b..a5dbe5d1 100644 --- a/crud/len.lua +++ b/crud/len.lua @@ -17,9 +17,7 @@ local function len_on_storage(space_name) return box.space[space_name]:len() end -function len.init(user) - utils.init_storage_call(user, LEN_FUNC_NAME, len_on_storage) -end +len.storage_api = {[LEN_FUNC_NAME] = len_on_storage} --- Calculates the number of tuples in the space for memtx engine --- Calculates the maximum approximate number of tuples in the space for vinyl engine diff --git a/crud/readview.lua b/crud/readview.lua index d5ff6dd5..3046384d 100644 --- a/crud/readview.lua +++ b/crud/readview.lua @@ -27,9 +27,11 @@ local CRUD_CLOSE_FUNC_NAME = utils.get_storage_call(CLOSE_FUNC_NAME) if (not utils.tarantool_version_at_least(2, 11, 0)) or (tarantool.package ~= 'Tarantool Enterprise') or (not has_merger) then return { - new = function() return nil, - ReadviewError:new("Tarantool does not support readview") end, - init = function() return nil end} + new = function() + return nil, ReadviewError:new("Tarantool does not support readview") + end, + storage_api = {}, + } end local select = require('crud.select.compat.select') @@ -241,11 +243,11 @@ function Readview_obj:pairs(space_name, user_conditions, opts) return pairs_call(space_name, user_conditions, opts) end -function readview.init(user) - utils.init_storage_call(user, OPEN_FUNC_NAME, readview_open_on_storage) - utils.init_storage_call(user, CLOSE_FUNC_NAME, readview_close_on_storage) - utils.init_storage_call(user, SELECT_FUNC_NAME, select_readview_on_storage) - end +readview.storage_api = { + [OPEN_FUNC_NAME] = readview_open_on_storage, + [CLOSE_FUNC_NAME] = readview_close_on_storage, + [SELECT_FUNC_NAME] = select_readview_on_storage, +} function Readview_obj:close(opts) checks('table', { diff --git a/crud/replace.lua b/crud/replace.lua index 0533641e..73c7b8a1 100644 --- a/crud/replace.lua +++ b/crud/replace.lua @@ -53,9 +53,7 @@ local function replace_on_storage(space_name, tuple, opts) }) end -function replace.init(user) - utils.init_storage_call(user, REPLACE_FUNC_NAME, replace_on_storage) -end +replace.storage_api = {[REPLACE_FUNC_NAME] = replace_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/replace_many.lua b/crud/replace_many.lua index e9dcb5e0..d047a3b0 100644 --- a/crud/replace_many.lua +++ b/crud/replace_many.lua @@ -126,9 +126,7 @@ local function replace_many_on_storage(space_name, tuples, opts) return inserted_tuples, nil, replica_schema_version end -function replace_many.init(user) - utils.init_storage_call(user, REPLACE_MANY_FUNC_NAME, replace_many_on_storage) -end +replace_many.storage_api = {[REPLACE_MANY_FUNC_NAME] = replace_many_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/select.lua b/crud/select.lua index a095b589..2ade76b7 100644 --- a/crud/select.lua +++ b/crud/select.lua @@ -126,8 +126,6 @@ local function select_on_storage(space_name, index_id, conditions, opts) return unpack(result) end -function select_module.init(user) - utils.init_storage_call(user, SELECT_FUNC_NAME, select_on_storage) -end +select_module.storage_api = {[SELECT_FUNC_NAME] = select_on_storage} return select_module diff --git a/crud/storage.lua b/crud/storage.lua new file mode 100644 index 00000000..de46ee8e --- /dev/null +++ b/crud/storage.lua @@ -0,0 +1,98 @@ +local dev_checks = require('crud.common.dev_checks') +local utils = require('crud.common.utils') + +local sharding_metadata = require('crud.common.sharding.sharding_metadata') +local insert = require('crud.insert') +local insert_many = require('crud.insert_many') +local replace = require('crud.replace') +local replace_many = require('crud.replace_many') +local get = require('crud.get') +local update = require('crud.update') +local upsert = require('crud.upsert') +local upsert_many = require('crud.upsert_many') +local delete = require('crud.delete') +local select = require('crud.select') +local truncate = require('crud.truncate') +local len = require('crud.len') +local count = require('crud.count') +local borders = require('crud.borders') +local readview = require('crud.readview') +local storage_info = require('crud.storage_info') + +local storage = {} + +local function init_local_part(_, name, func) + rawset(_G[utils.STORAGE_NAMESPACE], name, func) +end + +local function init_persistent_part(user, name, _) + name = utils.get_storage_call(name) + box.schema.func.create(name, {setuid = true, if_not_exists = true}) + box.schema.user.grant(user, 'execute', 'function', name, {if_not_exists = true}) +end + +--- Initializes a storage function by its name. +-- +-- It adds the function into the global scope by its name and required +-- access to a vshard storage user. +-- +-- @function init_storage_call +-- +-- @param string name of a user. +-- @param string name a name of the function. +-- @param function func the function. +-- +-- @return nil +local function init_storage_call(user, storage_api) + dev_checks('string', 'table') + + for name, func in pairs(storage_api) do + init_local_part(user, name, func) + + if not box.info.ro then + init_persistent_part(user, name, func) + end + end +end + +local modules_with_storage_api = { + sharding_metadata, + insert, + insert_many, + get, + replace, + replace_many, + update, + upsert, + upsert_many, + delete, + select, + truncate, + len, + count, + borders, + readview, + -- Must be initialized last: properly working storage info is the flag + -- of initialization success. + storage_info, +} + +function storage.init() + if type(box.cfg) ~= 'table' then + error('box.cfg() must be called first') + end + + rawset(_G, utils.STORAGE_NAMESPACE, {}) + + local user = utils.get_this_replica_user() or 'guest' + + for _, module in ipairs(modules_with_storage_api) do + init_storage_call(user, module.storage_api) + end +end + +function storage.stop() + rawset(_G, utils.STORAGE_NAMESPACE, nil) +end + +return storage diff --git a/crud/storage_info.lua b/crud/storage_info.lua index 839487a1..84c6a3f4 100644 --- a/crud/storage_info.lua +++ b/crud/storage_info.lua @@ -22,9 +22,7 @@ local function storage_info_on_storage() return {status = "running"} end -function storage_info.init(user) - utils.init_storage_call(user, STORAGE_INFO_FUNC_NAME, storage_info_on_storage) -end +storage_info.storage_api = {[STORAGE_INFO_FUNC_NAME] = storage_info_on_storage} --- Polls replicas for storage state -- diff --git a/crud/truncate.lua b/crud/truncate.lua index fa9f0f31..d54053a8 100644 --- a/crud/truncate.lua +++ b/crud/truncate.lua @@ -23,9 +23,7 @@ local function truncate_on_storage(space_name) return space:truncate() end -function truncate.init(user) - utils.init_storage_call(user, TRUNCATE_FUNC_NAME, truncate_on_storage) -end +truncate.storage_api = {[TRUNCATE_FUNC_NAME] = truncate_on_storage} --- Truncates specified space -- diff --git a/crud/update.lua b/crud/update.lua index 00441d82..6e88e3c4 100644 --- a/crud/update.lua +++ b/crud/update.lua @@ -77,9 +77,7 @@ local function update_on_storage(space_name, key, operations, field_names, opts) return res, err end -function update.init(user) - utils.init_storage_call(user, UPDATE_FUNC_NAME, update_on_storage) -end +update.storage_api = {[UPDATE_FUNC_NAME] = update_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/upsert.lua b/crud/upsert.lua index 71da9da1..3ddc94e5 100644 --- a/crud/upsert.lua +++ b/crud/upsert.lua @@ -50,9 +50,7 @@ local function upsert_on_storage(space_name, tuple, operations, opts) }) end -function upsert.init(user) - utils.init_storage_call(user, UPSERT_FUNC_NAME, upsert_on_storage) -end +upsert.storage_api = {[UPSERT_FUNC_NAME] = upsert_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help diff --git a/crud/upsert_many.lua b/crud/upsert_many.lua index c287bfd9..f030e778 100644 --- a/crud/upsert_many.lua +++ b/crud/upsert_many.lua @@ -120,9 +120,7 @@ local function upsert_many_on_storage(space_name, tuples, operations, opts) return nil, nil, replica_schema_version end -function upsert_many.init(user) - utils.init_storage_call(user, UPSERT_MANY_FUNC_NAME, upsert_many_on_storage) -end +upsert_many.storage_api = {[UPSERT_MANY_FUNC_NAME] = upsert_many_on_storage} -- returns result, err, need_reload -- need_reload indicates if reloading schema could help