Skip to content

Commit

Permalink
init: move storage code to a separate file
Browse files Browse the repository at this point in the history
Part of #412
Part of #415
  • Loading branch information
DifferentialOrange committed Jan 22, 2024
1 parent cb041bb commit ce0cfc6
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 127 deletions.
58 changes: 11 additions & 47 deletions crud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions crud/borders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions crud/common/sharding/sharding_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 0 additions & 24 deletions crud/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 1 addition & 3 deletions crud/count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions crud/delete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/get.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/insert_many.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/len.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions crud/readview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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', {
Expand Down
4 changes: 1 addition & 3 deletions crud/replace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/replace_many.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crud/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
98 changes: 98 additions & 0 deletions crud/storage.lua
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions crud/storage_info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
4 changes: 1 addition & 3 deletions crud/truncate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
4 changes: 1 addition & 3 deletions crud/update.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ce0cfc6

Please sign in to comment.